@langchain/core 0.2.20 → 0.2.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -89,6 +89,14 @@ export interface FunctionDefinition {
89
89
  * how to call the function.
90
90
  */
91
91
  description?: string;
92
+ /**
93
+ * Whether to enable strict schema adherence when generating the function call. If
94
+ * set to true, the model will follow the exact schema defined in the `parameters`
95
+ * field. Only a subset of JSON Schema is supported when `strict` is `true`. Learn
96
+ * more about Structured Outputs in the
97
+ * [function calling guide](https://platform.openai.com/docs/guides/function-calling).
98
+ */
99
+ strict?: boolean;
92
100
  }
93
101
  export interface ToolDefinition {
94
102
  type: "function";
@@ -6,7 +6,7 @@ import { BaseLanguageModel, StructuredOutputMethodOptions, ToolDefinition, type
6
6
  import { type CallbackManagerForLLMRun, type Callbacks } from "../callbacks/manager.js";
7
7
  import type { RunnableConfig } from "../runnables/config.js";
8
8
  import type { BaseCache } from "../caches/base.js";
9
- import { StructuredToolInterface } from "../tools/index.js";
9
+ import { StructuredToolInterface, StructuredToolParams } from "../tools/index.js";
10
10
  import { Runnable, RunnableToolLike } from "../runnables/base.js";
11
11
  type ToolChoice = string | Record<string, any> | "auto" | "any";
12
12
  /**
@@ -61,6 +61,7 @@ export type LangSmithParams = {
61
61
  ls_max_tokens?: number;
62
62
  ls_stop?: Array<string>;
63
63
  };
64
+ export type BindToolsInput = StructuredToolInterface | Record<string, any> | ToolDefinition | RunnableToolLike | StructuredToolParams;
64
65
  /**
65
66
  * Base class for chat models. It extends the BaseLanguageModel class and
66
67
  * provides methods for generating chat based on input messages.
@@ -79,7 +80,7 @@ export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCal
79
80
  * matching the provider's specific tool schema.
80
81
  * @param kwargs Any additional parameters to bind.
81
82
  */
82
- bindTools?(tools: (StructuredToolInterface | Record<string, unknown> | ToolDefinition | RunnableToolLike)[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, OutputMessageType, CallOptions>;
83
+ bindTools?(tools: BindToolsInput[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, OutputMessageType, CallOptions>;
83
84
  /**
84
85
  * Invokes the chat model with a single input.
85
86
  * @param input The input for the language model.
@@ -26,8 +26,17 @@ export interface ToolParams extends BaseLangChainParams {
26
26
  */
27
27
  responseFormat?: ResponseFormat;
28
28
  }
29
+ export interface StructuredToolParams extends Pick<StructuredToolInterface, "name" | "schema"> {
30
+ /**
31
+ * An optional description of the tool to pass to the model.
32
+ */
33
+ description?: string;
34
+ }
29
35
  export interface StructuredToolInterface<T extends ZodObjectAny = ZodObjectAny> extends RunnableInterface<(z.output<T> extends string ? string : never) | z.input<T> | ToolCall, ToolReturnType> {
30
36
  lc_namespace: string[];
37
+ /**
38
+ * A Zod schema representing the parameters of the tool.
39
+ */
31
40
  schema: T | z.ZodEffects<T>;
32
41
  /**
33
42
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
@@ -43,7 +52,13 @@ export interface StructuredToolInterface<T extends ZodObjectAny = ZodObjectAny>
43
52
  call(arg: (z.output<T> extends string ? string : never) | z.input<T> | ToolCall, configArg?: Callbacks | RunnableConfig,
44
53
  /** @deprecated */
45
54
  tags?: string[]): Promise<ToolReturnType>;
55
+ /**
56
+ * The name of the tool.
57
+ */
46
58
  name: string;
59
+ /**
60
+ * A description of the tool.
61
+ */
47
62
  description: string;
48
63
  returnDirect: boolean;
49
64
  }
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isRunnableToolLike = exports.isStructuredTool = exports.convertToOpenAITool = exports.convertToOpenAIFunction = void 0;
3
+ exports.isLangChainTool = exports.isStructuredToolParams = exports.isRunnableToolLike = exports.isStructuredTool = exports.convertToOpenAITool = exports.convertToOpenAIFunction = void 0;
4
4
  const zod_to_json_schema_1 = require("zod-to-json-schema");
5
5
  const base_js_1 = require("../runnables/base.cjs");
6
+ const is_zod_schema_js_1 = require("./types/is_zod_schema.cjs");
6
7
  /**
7
8
  * Formats a `StructuredTool` or `RunnableToolLike` instance into a format
8
9
  * that is compatible with OpenAI function calling. It uses the `zodToJsonSchema`
@@ -12,11 +13,15 @@ const base_js_1 = require("../runnables/base.cjs");
12
13
  * @param {StructuredToolInterface | RunnableToolLike} tool The tool to convert to an OpenAI function.
13
14
  * @returns {FunctionDefinition} The inputted tool in OpenAI function format.
14
15
  */
15
- function convertToOpenAIFunction(tool) {
16
+ function convertToOpenAIFunction(tool, fields) {
17
+ // @TODO 0.3.0 Remove the `number` typing
18
+ const fieldsCopy = typeof fields === "number" ? undefined : fields;
16
19
  return {
17
20
  name: tool.name,
18
21
  description: tool.description,
19
22
  parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema),
23
+ // Do not include the `strict` field if it is `undefined`.
24
+ ...(fieldsCopy?.strict !== undefined ? { strict: fieldsCopy.strict } : {}),
20
25
  };
21
26
  }
22
27
  exports.convertToOpenAIFunction = convertToOpenAIFunction;
@@ -32,14 +37,23 @@ exports.convertToOpenAIFunction = convertToOpenAIFunction;
32
37
  */
33
38
  function convertToOpenAITool(
34
39
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
- tool) {
36
- if (isStructuredTool(tool) || isRunnableToolLike(tool)) {
37
- return {
40
+ tool, fields) {
41
+ // @TODO 0.3.0 Remove the `number` typing
42
+ const fieldsCopy = typeof fields === "number" ? undefined : fields;
43
+ let toolDef;
44
+ if (isLangChainTool(tool)) {
45
+ toolDef = {
38
46
  type: "function",
39
47
  function: convertToOpenAIFunction(tool),
40
48
  };
41
49
  }
42
- return tool;
50
+ else {
51
+ toolDef = tool;
52
+ }
53
+ if (fieldsCopy?.strict !== undefined) {
54
+ toolDef.function.strict = fieldsCopy.strict;
55
+ }
56
+ return toolDef;
43
57
  }
44
58
  exports.convertToOpenAITool = convertToOpenAITool;
45
59
  /**
@@ -69,3 +83,33 @@ function isRunnableToolLike(tool) {
69
83
  tool.constructor.lc_name() === "RunnableToolLike");
70
84
  }
71
85
  exports.isRunnableToolLike = isRunnableToolLike;
86
+ /**
87
+ * Confirm whether or not the tool contains the necessary properties to be considered a `StructuredToolParams`.
88
+ *
89
+ * @param {unknown | undefined} tool The object to check if it is a `StructuredToolParams`.
90
+ * @returns {tool is StructuredToolParams} Whether the inputted object is a `StructuredToolParams`.
91
+ */
92
+ function isStructuredToolParams(tool) {
93
+ return (!!tool &&
94
+ typeof tool === "object" &&
95
+ "name" in tool &&
96
+ "schema" in tool &&
97
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
+ (0, is_zod_schema_js_1.isZodSchema)(tool.schema));
99
+ }
100
+ exports.isStructuredToolParams = isStructuredToolParams;
101
+ /**
102
+ * Whether or not the tool is one of StructuredTool, RunnableTool or StructuredToolParams.
103
+ * It returns `is StructuredToolParams` since that is the most minimal interface of the three,
104
+ * while still containing the necessary properties to be passed to a LLM for tool calling.
105
+ *
106
+ * @param {unknown | undefined} tool The tool to check if it is a LangChain tool.
107
+ * @returns {tool is StructuredToolParams} Whether the inputted tool is a LangChain tool.
108
+ */
109
+ function isLangChainTool(tool) {
110
+ return (isStructuredToolParams(tool) ||
111
+ isRunnableToolLike(tool) ||
112
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
113
+ isStructuredTool(tool));
114
+ }
115
+ exports.isLangChainTool = isLangChainTool;
@@ -1,4 +1,4 @@
1
- import { StructuredToolInterface } from "../tools/index.js";
1
+ import { StructuredToolInterface, StructuredToolParams } from "../tools/index.js";
2
2
  import { FunctionDefinition, ToolDefinition } from "../language_models/base.js";
3
3
  import { RunnableToolLike } from "../runnables/base.js";
4
4
  /**
@@ -10,7 +10,13 @@ import { RunnableToolLike } from "../runnables/base.js";
10
10
  * @param {StructuredToolInterface | RunnableToolLike} tool The tool to convert to an OpenAI function.
11
11
  * @returns {FunctionDefinition} The inputted tool in OpenAI function format.
12
12
  */
13
- export declare function convertToOpenAIFunction(tool: StructuredToolInterface | RunnableToolLike): FunctionDefinition;
13
+ export declare function convertToOpenAIFunction(tool: StructuredToolInterface | RunnableToolLike | StructuredToolParams, fields?: {
14
+ /**
15
+ * If `true`, model output is guaranteed to exactly match the JSON Schema
16
+ * provided in the function definition.
17
+ */
18
+ strict?: boolean;
19
+ } | number): FunctionDefinition;
14
20
  /**
15
21
  * Formats a `StructuredTool` or `RunnableToolLike` instance into a
16
22
  * format that is compatible with OpenAI tool calling. It uses the
@@ -21,7 +27,13 @@ export declare function convertToOpenAIFunction(tool: StructuredToolInterface |
21
27
  * @param {StructuredToolInterface | Record<string, any> | RunnableToolLike} tool The tool to convert to an OpenAI tool.
22
28
  * @returns {ToolDefinition} The inputted tool in OpenAI tool format.
23
29
  */
24
- export declare function convertToOpenAITool(tool: StructuredToolInterface | Record<string, any> | RunnableToolLike): ToolDefinition;
30
+ export declare function convertToOpenAITool(tool: StructuredToolInterface | Record<string, any> | RunnableToolLike, fields?: {
31
+ /**
32
+ * If `true`, model output is guaranteed to exactly match the JSON Schema
33
+ * provided in the function definition.
34
+ */
35
+ strict?: boolean;
36
+ } | number): ToolDefinition;
25
37
  /**
26
38
  * Confirm whether the inputted tool is an instance of `StructuredToolInterface`.
27
39
  *
@@ -36,3 +48,19 @@ export declare function isStructuredTool(tool?: StructuredToolInterface | Record
36
48
  * @returns {tool is RunnableToolLike} Whether the inputted tool is an instance of `RunnableToolLike`.
37
49
  */
38
50
  export declare function isRunnableToolLike(tool?: unknown): tool is RunnableToolLike;
51
+ /**
52
+ * Confirm whether or not the tool contains the necessary properties to be considered a `StructuredToolParams`.
53
+ *
54
+ * @param {unknown | undefined} tool The object to check if it is a `StructuredToolParams`.
55
+ * @returns {tool is StructuredToolParams} Whether the inputted object is a `StructuredToolParams`.
56
+ */
57
+ export declare function isStructuredToolParams(tool?: unknown): tool is StructuredToolParams;
58
+ /**
59
+ * Whether or not the tool is one of StructuredTool, RunnableTool or StructuredToolParams.
60
+ * It returns `is StructuredToolParams` since that is the most minimal interface of the three,
61
+ * while still containing the necessary properties to be passed to a LLM for tool calling.
62
+ *
63
+ * @param {unknown | undefined} tool The tool to check if it is a LangChain tool.
64
+ * @returns {tool is StructuredToolParams} Whether the inputted tool is a LangChain tool.
65
+ */
66
+ export declare function isLangChainTool(tool?: unknown): tool is StructuredToolParams;
@@ -1,5 +1,6 @@
1
1
  import { zodToJsonSchema } from "zod-to-json-schema";
2
2
  import { Runnable } from "../runnables/base.js";
3
+ import { isZodSchema } from "./types/is_zod_schema.js";
3
4
  /**
4
5
  * Formats a `StructuredTool` or `RunnableToolLike` instance into a format
5
6
  * that is compatible with OpenAI function calling. It uses the `zodToJsonSchema`
@@ -9,11 +10,15 @@ import { Runnable } from "../runnables/base.js";
9
10
  * @param {StructuredToolInterface | RunnableToolLike} tool The tool to convert to an OpenAI function.
10
11
  * @returns {FunctionDefinition} The inputted tool in OpenAI function format.
11
12
  */
12
- export function convertToOpenAIFunction(tool) {
13
+ export function convertToOpenAIFunction(tool, fields) {
14
+ // @TODO 0.3.0 Remove the `number` typing
15
+ const fieldsCopy = typeof fields === "number" ? undefined : fields;
13
16
  return {
14
17
  name: tool.name,
15
18
  description: tool.description,
16
19
  parameters: zodToJsonSchema(tool.schema),
20
+ // Do not include the `strict` field if it is `undefined`.
21
+ ...(fieldsCopy?.strict !== undefined ? { strict: fieldsCopy.strict } : {}),
17
22
  };
18
23
  }
19
24
  /**
@@ -28,14 +33,23 @@ export function convertToOpenAIFunction(tool) {
28
33
  */
29
34
  export function convertToOpenAITool(
30
35
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
31
- tool) {
32
- if (isStructuredTool(tool) || isRunnableToolLike(tool)) {
33
- return {
36
+ tool, fields) {
37
+ // @TODO 0.3.0 Remove the `number` typing
38
+ const fieldsCopy = typeof fields === "number" ? undefined : fields;
39
+ let toolDef;
40
+ if (isLangChainTool(tool)) {
41
+ toolDef = {
34
42
  type: "function",
35
43
  function: convertToOpenAIFunction(tool),
36
44
  };
37
45
  }
38
- return tool;
46
+ else {
47
+ toolDef = tool;
48
+ }
49
+ if (fieldsCopy?.strict !== undefined) {
50
+ toolDef.function.strict = fieldsCopy.strict;
51
+ }
52
+ return toolDef;
39
53
  }
40
54
  /**
41
55
  * Confirm whether the inputted tool is an instance of `StructuredToolInterface`.
@@ -62,3 +76,31 @@ export function isRunnableToolLike(tool) {
62
76
  typeof tool.constructor.lc_name === "function" &&
63
77
  tool.constructor.lc_name() === "RunnableToolLike");
64
78
  }
79
+ /**
80
+ * Confirm whether or not the tool contains the necessary properties to be considered a `StructuredToolParams`.
81
+ *
82
+ * @param {unknown | undefined} tool The object to check if it is a `StructuredToolParams`.
83
+ * @returns {tool is StructuredToolParams} Whether the inputted object is a `StructuredToolParams`.
84
+ */
85
+ export function isStructuredToolParams(tool) {
86
+ return (!!tool &&
87
+ typeof tool === "object" &&
88
+ "name" in tool &&
89
+ "schema" in tool &&
90
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
91
+ isZodSchema(tool.schema));
92
+ }
93
+ /**
94
+ * Whether or not the tool is one of StructuredTool, RunnableTool or StructuredToolParams.
95
+ * It returns `is StructuredToolParams` since that is the most minimal interface of the three,
96
+ * while still containing the necessary properties to be passed to a LLM for tool calling.
97
+ *
98
+ * @param {unknown | undefined} tool The tool to check if it is a LangChain tool.
99
+ * @returns {tool is StructuredToolParams} Whether the inputted tool is a LangChain tool.
100
+ */
101
+ export function isLangChainTool(tool) {
102
+ return (isStructuredToolParams(tool) ||
103
+ isRunnableToolLike(tool) ||
104
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
105
+ isStructuredTool(tool));
106
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.2.20",
3
+ "version": "0.2.21",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -72,7 +72,7 @@
72
72
  "jest-environment-node": "^29.6.4",
73
73
  "ml-matrix": "^6.10.4",
74
74
  "prettier": "^2.8.3",
75
- "release-it": "^15.10.1",
75
+ "release-it": "^17.6.0",
76
76
  "rimraf": "^5.0.1",
77
77
  "ts-jest": "^29.1.0",
78
78
  "typescript": "~5.1.6",