UNPKG

@langchain/openai

Version:
1 lines 4.78 kB
{"version":3,"file":"webSearch.cjs","names":["options?: WebSearchOptions"],"sources":["../../src/tools/webSearch.ts"],"sourcesContent":["import { OpenAI as OpenAIClient } from \"openai\";\nimport type { ServerTool } from \"@langchain/core/tools\";\n\n/**\n * User location configuration for geographic search refinement.\n */\nexport interface WebSearchUserLocation {\n /**\n * The type of location. Currently only \"approximate\" is supported.\n */\n type: \"approximate\";\n /**\n * Two-letter ISO country code (e.g., \"US\", \"GB\").\n * @see https://en.wikipedia.org/wiki/ISO_3166-1\n */\n country?: string;\n /**\n * City name (e.g., \"San Francisco\", \"London\").\n */\n city?: string;\n /**\n * Region or state name (e.g., \"California\", \"London\").\n */\n region?: string;\n /**\n * IANA timezone (e.g., \"America/Los_Angeles\", \"Europe/London\").\n * @see https://timeapi.io/documentation/iana-timezones\n */\n timezone?: string;\n}\n\n/**\n * Domain filtering configuration for web search.\n */\nexport interface WebSearchFilters {\n /**\n * Allow-list of up to 100 domains to limit search results.\n * Omit the HTTP/HTTPS prefix (e.g., \"openai.com\" instead of \"https://openai.com/\").\n * Includes subdomains automatically.\n */\n allowedDomains?: string[];\n}\n\n/**\n * Options for the OpenAI web search tool.\n */\nexport interface WebSearchOptions {\n /**\n * Domain filtering configuration.\n * Limit results to a specific set of up to 100 domains.\n */\n filters?: WebSearchFilters;\n /**\n * Approximate user location for geographic search refinement.\n * Not supported for deep research models.\n */\n userLocation?: WebSearchUserLocation;\n /**\n * High level guidance for the amount of context window space to use for the\n * search. One of `low`, `medium`, or `high`. `medium` is the default.\n */\n search_context_size?: \"low\" | \"medium\" | \"high\";\n}\n\n/**\n * OpenAI web search tool type for the Responses API.\n */\nexport type WebSearchTool = OpenAIClient.Responses.WebSearchTool;\n\n/**\n * Creates a web search tool that allows OpenAI models to search the web\n * for up-to-date information before generating a response.\n *\n * Web search supports three main types:\n * 1. **Non-reasoning web search**: Quick lookups where the model passes queries\n * directly to the search tool.\n * 2. **Agentic search with reasoning models**: The model actively manages the\n * search process, analyzing results and deciding whether to keep searching.\n * 3. **Deep research**: Extended investigations using models like `o3-deep-research`\n * or `gpt-5` with high reasoning effort.\n *\n * @see {@link https://platform.openai.com/docs/guides/tools-web-search | OpenAI Web Search Documentation}\n * @param options - Configuration options for the web search tool\n * @returns A web search tool definition to be passed to the OpenAI Responses API\n *\n * @example\n * ```typescript\n * import { ChatOpenAI, tools } from \"@langchain/openai\";\n *\n * const model = new ChatOpenAI({\n * model: \"gpt-4o\",\n * });\n *\n * // Basic usage\n * const response = await model.invoke(\"What was a positive news story from today?\", {\n * tools: [tools.webSearch()],\n * });\n *\n * // With domain filtering\n * const filteredResponse = await model.invoke(\"Latest AI research news\", {\n * tools: [tools.webSearch({\n * filters: {\n * allowedDomains: [\"arxiv.org\", \"nature.com\", \"science.org\"],\n * },\n * })],\n * });\n *\n * // With user location for geographic relevance\n * const localResponse = await model.invoke(\"What are the best restaurants near me?\", {\n * tools: [tools.webSearch({\n * userLocation: {\n * type: \"approximate\",\n * country: \"US\",\n * city: \"San Francisco\",\n * region: \"California\",\n * timezone: \"America/Los_Angeles\",\n * },\n * })],\n * });\n *\n * // Cache-only mode (no live internet access)\n * const cachedResponse = await model.invoke(\"Find information about OpenAI\", {\n * tools: [tools.webSearch({\n * externalWebAccess: false,\n * })],\n * });\n * ```\n */\nexport function webSearch(options?: WebSearchOptions): ServerTool {\n return {\n type: \"web_search\",\n filters: options?.filters?.allowedDomains\n ? { allowed_domains: options.filters.allowedDomains }\n : undefined,\n user_location: options?.userLocation,\n search_context_size: options?.search_context_size,\n } satisfies WebSearchTool;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgIA,SAAgB,UAAUA,SAAwC;AAChE,QAAO;EACL,MAAM;EACN,SAAS,SAAS,SAAS,iBACvB,EAAE,iBAAiB,QAAQ,QAAQ,eAAgB,IACnD;EACJ,eAAe,SAAS;EACxB,qBAAqB,SAAS;CAC/B;AACF"}