UNPKG

@langchain/openai

Version:
1 lines 3.51 kB
{"version":3,"file":"toolSearch.cjs","names":[],"sources":["../../src/tools/toolSearch.ts"],"sourcesContent":["import { OpenAI as OpenAIClient } from \"openai\";\nimport type { ServerTool } from \"@langchain/core/tools\";\n\nexport interface ToolSearchOptions {\n /**\n * Whether tool search is executed by the server or by the client.\n * - `\"server\"` (default): OpenAI's servers handle the search internally.\n * - `\"client\"`: The client provides results via agent middleware.\n */\n execution?: \"server\" | \"client\";\n /**\n * Description shown to the model for a client-executed tool search tool.\n */\n description?: string;\n /**\n * Parameter schema for a client-executed tool search tool.\n */\n parameters?: unknown;\n}\n\nexport type ToolSearchTool = OpenAIClient.Responses.ToolSearchTool;\n\n/**\n * Creates a tool search tool that enables OpenAI models to dynamically discover\n * and load tools on-demand from a large pool, rather than requiring all tool\n * definitions to be sent in every request.\n *\n * Tool search works with tools that have `defer_loading: true` set (either\n * directly in the tool definition or via `extras: { defer_loading: true }` on\n * LangChain tools). When the model needs a deferred tool, it issues a tool\n * search call, discovers matching tools, and then makes the actual function call.\n *\n * @see {@link https://platform.openai.com/docs/guides/tools-tool-search | OpenAI Tool Search Documentation}\n * @param options - Configuration options for the tool search tool\n * @returns A tool search tool definition to be passed to the OpenAI Responses API\n *\n * @example\n * ```typescript\n * import { ChatOpenAI, tools } from \"@langchain/openai\";\n * import { tool } from \"@langchain/core/tools\";\n * import { z } from \"zod\";\n *\n * const model = new ChatOpenAI({ model: \"gpt-5.3\" });\n *\n * const getWeather = tool(\n * async (input) => `Weather in ${input.location}: sunny, 72°F`,\n * {\n * name: \"get_weather\",\n * description: \"Get the current weather for a location\",\n * schema: z.object({ location: z.string() }),\n * extras: { defer_loading: true },\n * }\n * );\n *\n * // Server-executed tool search (default)\n * const response = await model.invoke(\"What is the weather in SF?\", {\n * tools: [tools.toolSearch(), getWeather],\n * });\n *\n * // Client-executed tool search\n * const clientResponse = await model.invoke(\"What is the weather in SF?\", {\n * tools: [\n * tools.toolSearch({\n * execution: \"client\",\n * description: \"Search for available tools\",\n * parameters: {\n * type: \"object\",\n * properties: { goal: { type: \"string\" } },\n * required: [\"goal\"],\n * },\n * }),\n * getWeather,\n * ],\n * });\n * ```\n */\nexport function toolSearch(options?: ToolSearchOptions): ServerTool {\n return {\n type: \"tool_search\",\n ...(options?.execution ? { execution: options.execution } : {}),\n ...(options?.description ? { description: options.description } : {}),\n ...(options?.parameters !== undefined\n ? { parameters: options.parameters }\n : {}),\n } satisfies ToolSearchTool;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4EA,SAAgB,WAAW,SAAyC;AAClE,QAAO;EACL,MAAM;EACN,GAAI,SAAS,YAAY,EAAE,WAAW,QAAQ,WAAW,GAAG,EAAE;EAC9D,GAAI,SAAS,cAAc,EAAE,aAAa,QAAQ,aAAa,GAAG,EAAE;EACpE,GAAI,SAAS,eAAe,KAAA,IACxB,EAAE,YAAY,QAAQ,YAAY,GAClC,EAAE;EACP"}