@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 4.37 kB
Source Map (JSON)
{"version":3,"file":"function_calling.cjs","names":["tool: StructuredToolInterface | RunnableToolLike | StructuredToolParams","fields?:\n | {\n /**\n * If `true`, model output is guaranteed to exactly match the JSON Schema\n * provided in the function definition.\n */\n strict?: boolean;\n }\n | number","toJsonSchema","tool: StructuredToolInterface | Record<string, any> | RunnableToolLike","toolDef: ToolDefinition | undefined","isLangChainTool"],"sources":["../../src/utils/function_calling.ts"],"sourcesContent":["import {\n StructuredToolInterface,\n StructuredToolParams,\n isLangChainTool,\n} from \"../tools/types.js\";\nimport { FunctionDefinition, ToolDefinition } from \"../language_models/base.js\";\nimport { RunnableToolLike } from \"../runnables/base.js\";\nimport { toJsonSchema } from \"./json_schema.js\";\n\n// These utility functions were moved to a more appropriate location,\n// but we still export them here for backwards compatibility.\nexport {\n isStructuredTool,\n isStructuredToolParams,\n isRunnableToolLike,\n isLangChainTool,\n} from \"../tools/types.js\";\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a format\n * that is compatible with OpenAI function calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | RunnableToolLike} tool The tool to convert to an OpenAI function.\n * @returns {FunctionDefinition} The inputted tool in OpenAI function format.\n */\nexport function convertToOpenAIFunction(\n tool: StructuredToolInterface | RunnableToolLike | StructuredToolParams,\n fields?:\n | {\n /**\n * If `true`, model output is guaranteed to exactly match the JSON Schema\n * provided in the function definition.\n */\n strict?: boolean;\n }\n | number\n): FunctionDefinition {\n // @TODO 0.3.0 Remove the `number` typing\n const fieldsCopy = typeof fields === \"number\" ? undefined : fields;\n\n return {\n name: tool.name,\n description: tool.description,\n parameters: toJsonSchema(tool.schema),\n // Do not include the `strict` field if it is `undefined`.\n ...(fieldsCopy?.strict !== undefined ? { strict: fieldsCopy.strict } : {}),\n };\n}\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a\n * format that is compatible with OpenAI tool calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | Record<string, any> | RunnableToolLike} tool The tool to convert to an OpenAI tool.\n * @returns {ToolDefinition} The inputted tool in OpenAI tool format.\n */\nexport function convertToOpenAITool(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n tool: StructuredToolInterface | Record<string, any> | RunnableToolLike,\n fields?:\n | {\n /**\n * If `true`, model output is guaranteed to exactly match the JSON Schema\n * provided in the function definition.\n */\n strict?: boolean;\n }\n | number\n): ToolDefinition {\n // @TODO 0.3.0 Remove the `number` typing\n const fieldsCopy = typeof fields === \"number\" ? undefined : fields;\n\n let toolDef: ToolDefinition | undefined;\n if (isLangChainTool(tool)) {\n toolDef = {\n type: \"function\",\n function: convertToOpenAIFunction(tool),\n };\n } else {\n toolDef = tool as ToolDefinition;\n }\n\n if (fieldsCopy?.strict !== undefined) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (toolDef.function as any).strict = fieldsCopy.strict;\n }\n\n return toolDef;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,wBACdA,MACAC,QASoB;CAEpB,MAAM,aAAa,OAAO,WAAW,WAAW,SAAY;AAE5D,QAAO;EACL,MAAM,KAAK;EACX,aAAa,KAAK;EAClB,YAAYC,uCAAa,KAAK,OAAO;EAErC,GAAI,YAAY,WAAW,SAAY,EAAE,QAAQ,WAAW,OAAQ,IAAG,CAAE;CAC1E;AACF;;;;;;;;;;AAWD,SAAgB,oBAEdC,MACAF,QASgB;CAEhB,MAAM,aAAa,OAAO,WAAW,WAAW,SAAY;CAE5D,IAAIG;AACJ,KAAIC,8BAAgB,KAAK,EACvB,UAAU;EACR,MAAM;EACN,UAAU,wBAAwB,KAAK;CACxC;MAED,UAAU;AAGZ,KAAI,YAAY,WAAW,QAExB,QAAQ,SAAiB,SAAS,WAAW;AAGhD,QAAO;AACR"}