@langchain/core 0.3.43 → 0.3.45
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.
- package/dist/language_models/base.d.ts +2 -1
- package/dist/output_parsers/structured.cjs +1 -1
- package/dist/output_parsers/structured.js +1 -1
- package/dist/tools/index.cjs +36 -22
- package/dist/tools/index.d.ts +30 -138
- package/dist/tools/index.js +37 -23
- package/dist/tools/types.cjs +60 -0
- package/dist/tools/types.d.ts +215 -0
- package/dist/tools/types.js +53 -0
- package/dist/tracers/tracer_langchain.cjs +1 -0
- package/dist/tracers/tracer_langchain.js +1 -0
- package/dist/utils/function_calling.cjs +18 -68
- package/dist/utils/function_calling.d.ts +8 -36
- package/dist/utils/function_calling.js +12 -62
- package/dist/utils/json_schema.cjs +85 -4
- package/dist/utils/json_schema.d.ts +17 -0
- package/dist/utils/json_schema.js +79 -0
- package/dist/utils/types/is_zod_schema.cjs +28 -6
- package/dist/utils/types/is_zod_schema.d.ts +3 -3
- package/dist/utils/types/is_zod_schema.js +28 -6
- package/package.json +3 -3
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { CallbackManagerForToolRun, type Callbacks } from "../callbacks/manager.js";
|
|
3
|
+
import type { BaseLangChainParams, ToolDefinition } from "../language_models/base.js";
|
|
4
|
+
import type { RunnableConfig } from "../runnables/config.js";
|
|
5
|
+
import { RunnableToolLike, type RunnableInterface } from "../runnables/base.js";
|
|
6
|
+
import type { ToolCall } from "../messages/tool.js";
|
|
7
|
+
import type { MessageContent } from "../messages/base.js";
|
|
8
|
+
import { JSONSchema } from "../utils/json_schema.js";
|
|
9
|
+
export type ResponseFormat = "content" | "content_and_artifact" | string;
|
|
10
|
+
export type ToolReturnType = any;
|
|
11
|
+
export type ContentAndArtifact = [MessageContent, any];
|
|
12
|
+
export type ZodObjectAny = z.ZodObject<any, any, any, any>;
|
|
13
|
+
/**
|
|
14
|
+
* Base type that establishes the types of input schemas that can be used for LangChain tool
|
|
15
|
+
* definitions.
|
|
16
|
+
*/
|
|
17
|
+
export type ToolInputSchemaBase = ZodObjectAny | z.ZodEffects<ZodObjectAny> | JSONSchema;
|
|
18
|
+
/**
|
|
19
|
+
* Parameters for the Tool classes.
|
|
20
|
+
*/
|
|
21
|
+
export interface ToolParams extends BaseLangChainParams {
|
|
22
|
+
/**
|
|
23
|
+
* The tool response format.
|
|
24
|
+
*
|
|
25
|
+
* If "content" then the output of the tool is interpreted as the contents of a
|
|
26
|
+
* ToolMessage. If "content_and_artifact" then the output is expected to be a
|
|
27
|
+
* two-tuple corresponding to the (content, artifact) of a ToolMessage.
|
|
28
|
+
*
|
|
29
|
+
* @default "content"
|
|
30
|
+
*/
|
|
31
|
+
responseFormat?: ResponseFormat;
|
|
32
|
+
/**
|
|
33
|
+
* Whether to show full details in the thrown parsing errors.
|
|
34
|
+
*
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
37
|
+
verboseParsingErrors?: boolean;
|
|
38
|
+
}
|
|
39
|
+
export type ToolRunnableConfig<ConfigurableFieldType extends Record<string, any> = Record<string, any>> = RunnableConfig<ConfigurableFieldType> & {
|
|
40
|
+
toolCall?: ToolCall;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Schema for defining tools.
|
|
44
|
+
*
|
|
45
|
+
* @version 0.2.19
|
|
46
|
+
*/
|
|
47
|
+
export interface StructuredToolParams extends Pick<StructuredToolInterface, "name" | "schema"> {
|
|
48
|
+
/**
|
|
49
|
+
* An optional description of the tool to pass to the model.
|
|
50
|
+
*/
|
|
51
|
+
description?: string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Utility type that resolves the output type of a tool input schema.
|
|
55
|
+
*
|
|
56
|
+
* Input & Output types are a concept used with Zod schema, as Zod allows for transforms to occur
|
|
57
|
+
* during parsing. When using JSONSchema, input and output types are the same.
|
|
58
|
+
*
|
|
59
|
+
* The input type for a given schema should match the structure of the arguments that the LLM
|
|
60
|
+
* generates as part of its {@link ToolCall}. The output type will be the type that results from
|
|
61
|
+
* applying any transforms defined in your schema. If there are no transforms, the input and output
|
|
62
|
+
* types will be the same.
|
|
63
|
+
*/
|
|
64
|
+
export type ToolInputSchemaOutputType<T extends ToolInputSchemaBase> = T extends z.ZodSchema ? z.output<T> : T extends JSONSchema ? unknown : never;
|
|
65
|
+
/**
|
|
66
|
+
* Utility type that resolves the input type of a tool input schema.
|
|
67
|
+
*
|
|
68
|
+
* Input & Output types are a concept used with Zod schema, as Zod allows for transforms to occur
|
|
69
|
+
* during parsing. When using JSONSchema, input and output types are the same.
|
|
70
|
+
*
|
|
71
|
+
* The input type for a given schema should match the structure of the arguments that the LLM
|
|
72
|
+
* generates as part of its {@link ToolCall}. The output type will be the type that results from
|
|
73
|
+
* applying any transforms defined in your schema. If there are no transforms, the input and output
|
|
74
|
+
* types will be the same.
|
|
75
|
+
*/
|
|
76
|
+
export type ToolInputSchemaInputType<T extends ToolInputSchemaBase> = T extends z.ZodSchema ? z.input<T> : T extends JSONSchema ? unknown : never;
|
|
77
|
+
/**
|
|
78
|
+
* Defines the type that will be passed into a tool handler function as a result of a tool call.
|
|
79
|
+
*
|
|
80
|
+
* @param SchemaT - The type of the tool input schema. Usually you don't need to specify this.
|
|
81
|
+
* @param SchemaInputT - The TypeScript type representing the structure of the tool arguments generated by the LLM. Useful for type checking tool handler functions when using JSONSchema.
|
|
82
|
+
*/
|
|
83
|
+
export type StructuredToolCallInput<SchemaT extends ToolInputSchemaBase = ZodObjectAny, SchemaInputT = ToolInputSchemaInputType<SchemaT>> = (ToolInputSchemaOutputType<SchemaT> extends string ? string : never) | SchemaInputT | ToolCall;
|
|
84
|
+
/**
|
|
85
|
+
* Interface that defines the shape of a LangChain structured tool.
|
|
86
|
+
*
|
|
87
|
+
* A structured tool is a tool that uses a schema to define the structure of the arguments that the
|
|
88
|
+
* LLM generates as part of its {@link ToolCall}.
|
|
89
|
+
*
|
|
90
|
+
* @param SchemaT - The type of the tool input schema. Usually you don't need to specify this.
|
|
91
|
+
* @param SchemaInputT - The TypeScript type representing the structure of the tool arguments generated by the LLM. Useful for type checking tool handler functions when using JSONSchema.
|
|
92
|
+
*/
|
|
93
|
+
export interface StructuredToolInterface<SchemaT extends ToolInputSchemaBase = ZodObjectAny | z.ZodEffects<ZodObjectAny> | JSONSchema, SchemaInputT = ToolInputSchemaInputType<SchemaT>> extends RunnableInterface<StructuredToolCallInput<SchemaT, SchemaInputT>, ToolReturnType> {
|
|
94
|
+
lc_namespace: string[];
|
|
95
|
+
/**
|
|
96
|
+
* A Zod schema representing the parameters of the tool.
|
|
97
|
+
*/
|
|
98
|
+
schema: SchemaT;
|
|
99
|
+
/**
|
|
100
|
+
* @deprecated Use .invoke() instead. Will be removed in 0.3.0.
|
|
101
|
+
*
|
|
102
|
+
* Calls the tool with the provided argument, configuration, and tags. It
|
|
103
|
+
* parses the input according to the schema, handles any errors, and
|
|
104
|
+
* manages callbacks.
|
|
105
|
+
* @param arg The input argument for the tool.
|
|
106
|
+
* @param configArg Optional configuration or callbacks for the tool.
|
|
107
|
+
* @param tags Optional tags for the tool.
|
|
108
|
+
* @returns A Promise that resolves with a string.
|
|
109
|
+
*/
|
|
110
|
+
call(arg: StructuredToolCallInput<SchemaT, SchemaInputT>, configArg?: Callbacks | RunnableConfig,
|
|
111
|
+
/** @deprecated */
|
|
112
|
+
tags?: string[]): Promise<ToolReturnType>;
|
|
113
|
+
/**
|
|
114
|
+
* The name of the tool.
|
|
115
|
+
*/
|
|
116
|
+
name: string;
|
|
117
|
+
/**
|
|
118
|
+
* A description of the tool.
|
|
119
|
+
*/
|
|
120
|
+
description: string;
|
|
121
|
+
/**
|
|
122
|
+
* Whether to return the tool's output directly.
|
|
123
|
+
*
|
|
124
|
+
* Setting this to true means that after the tool is called,
|
|
125
|
+
* an agent should stop looping.
|
|
126
|
+
*/
|
|
127
|
+
returnDirect: boolean;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* A special interface for tools that accept a string input, usually defined with the {@link Tool} class.
|
|
131
|
+
*
|
|
132
|
+
* @param SchemaT - The type of the tool input schema. Usually you don't need to specify this.
|
|
133
|
+
* @param SchemaInputT - The TypeScript type representing the structure of the tool arguments generated by the LLM. Useful for type checking tool handler functions when using JSONSchema.
|
|
134
|
+
*/
|
|
135
|
+
export interface ToolInterface<SchemaT extends ToolInputSchemaBase = ZodObjectAny | z.ZodEffects<ZodObjectAny> | JSONSchema, SchemaInputT = ToolInputSchemaInputType<SchemaT>> extends StructuredToolInterface<SchemaT, SchemaInputT> {
|
|
136
|
+
/**
|
|
137
|
+
* @deprecated Use .invoke() instead. Will be removed in 0.3.0.
|
|
138
|
+
*
|
|
139
|
+
* Calls the tool with the provided argument and callbacks. It handles
|
|
140
|
+
* string inputs specifically.
|
|
141
|
+
* @param arg The input argument for the tool, which can be a string, undefined, or an input of the tool's schema.
|
|
142
|
+
* @param callbacks Optional callbacks for the tool.
|
|
143
|
+
* @returns A Promise that resolves with a string.
|
|
144
|
+
*/
|
|
145
|
+
call(arg: string | undefined | SchemaInputT | ToolCall, callbacks?: Callbacks | RunnableConfig): Promise<ToolReturnType>;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Base interface for the input parameters of the {@link DynamicTool} and
|
|
149
|
+
* {@link DynamicStructuredTool} classes.
|
|
150
|
+
*/
|
|
151
|
+
export interface BaseDynamicToolInput extends ToolParams {
|
|
152
|
+
name: string;
|
|
153
|
+
description: string;
|
|
154
|
+
/**
|
|
155
|
+
* Whether to return the tool's output directly.
|
|
156
|
+
*
|
|
157
|
+
* Setting this to true means that after the tool is called,
|
|
158
|
+
* an agent should stop looping.
|
|
159
|
+
*/
|
|
160
|
+
returnDirect?: boolean;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Interface for the input parameters of the DynamicTool class.
|
|
164
|
+
*/
|
|
165
|
+
export interface DynamicToolInput extends BaseDynamicToolInput {
|
|
166
|
+
func: (input: string, runManager?: CallbackManagerForToolRun, config?: ToolRunnableConfig) => Promise<ToolReturnType>;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Interface for the input parameters of the DynamicStructuredTool class.
|
|
170
|
+
*
|
|
171
|
+
* @param SchemaT - The type of the tool input schema. Usually you don't need to specify this.
|
|
172
|
+
* @param SchemaOutputT - The TypeScript type representing the result of applying the schema to the tool arguments. Useful for type checking tool handler functions when using JSONSchema.
|
|
173
|
+
*/
|
|
174
|
+
export interface DynamicStructuredToolInput<SchemaT extends ToolInputSchemaBase = ZodObjectAny, SchemaOutputT = ToolInputSchemaOutputType<SchemaT>> extends BaseDynamicToolInput {
|
|
175
|
+
/**
|
|
176
|
+
* Tool handler function - the function that will be called when the tool is invoked.
|
|
177
|
+
*
|
|
178
|
+
* @param input - The input to the tool.
|
|
179
|
+
* @param runManager - The run manager for the tool.
|
|
180
|
+
* @param config - The configuration for the tool.
|
|
181
|
+
* @returns The result of the tool.
|
|
182
|
+
*/
|
|
183
|
+
func: (input: BaseDynamicToolInput["responseFormat"] extends "content_and_artifact" ? ToolCall : SchemaOutputT, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<ToolReturnType>;
|
|
184
|
+
schema: SchemaT extends ZodObjectAny ? SchemaT : SchemaT;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Confirm whether the inputted tool is an instance of `StructuredToolInterface`.
|
|
188
|
+
*
|
|
189
|
+
* @param {StructuredToolInterface | JSONSchema | undefined} tool The tool to check if it is an instance of `StructuredToolInterface`.
|
|
190
|
+
* @returns {tool is StructuredToolInterface} Whether the inputted tool is an instance of `StructuredToolInterface`.
|
|
191
|
+
*/
|
|
192
|
+
export declare function isStructuredTool(tool?: StructuredToolInterface | ToolDefinition | JSONSchema): tool is StructuredToolInterface;
|
|
193
|
+
/**
|
|
194
|
+
* Confirm whether the inputted tool is an instance of `RunnableToolLike`.
|
|
195
|
+
*
|
|
196
|
+
* @param {unknown | undefined} tool The tool to check if it is an instance of `RunnableToolLike`.
|
|
197
|
+
* @returns {tool is RunnableToolLike} Whether the inputted tool is an instance of `RunnableToolLike`.
|
|
198
|
+
*/
|
|
199
|
+
export declare function isRunnableToolLike(tool?: unknown): tool is RunnableToolLike;
|
|
200
|
+
/**
|
|
201
|
+
* Confirm whether or not the tool contains the necessary properties to be considered a `StructuredToolParams`.
|
|
202
|
+
*
|
|
203
|
+
* @param {unknown | undefined} tool The object to check if it is a `StructuredToolParams`.
|
|
204
|
+
* @returns {tool is StructuredToolParams} Whether the inputted object is a `StructuredToolParams`.
|
|
205
|
+
*/
|
|
206
|
+
export declare function isStructuredToolParams(tool?: unknown): tool is StructuredToolParams;
|
|
207
|
+
/**
|
|
208
|
+
* Whether or not the tool is one of StructuredTool, RunnableTool or StructuredToolParams.
|
|
209
|
+
* It returns `is StructuredToolParams` since that is the most minimal interface of the three,
|
|
210
|
+
* while still containing the necessary properties to be passed to a LLM for tool calling.
|
|
211
|
+
*
|
|
212
|
+
* @param {unknown | undefined} tool The tool to check if it is a LangChain tool.
|
|
213
|
+
* @returns {tool is StructuredToolParams} Whether the inputted tool is a LangChain tool.
|
|
214
|
+
*/
|
|
215
|
+
export declare function isLangChainTool(tool?: unknown): tool is StructuredToolParams;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Runnable, } from "../runnables/base.js";
|
|
2
|
+
import { isZodSchema } from "../utils/types/is_zod_schema.js";
|
|
3
|
+
/**
|
|
4
|
+
* Confirm whether the inputted tool is an instance of `StructuredToolInterface`.
|
|
5
|
+
*
|
|
6
|
+
* @param {StructuredToolInterface | JSONSchema | undefined} tool The tool to check if it is an instance of `StructuredToolInterface`.
|
|
7
|
+
* @returns {tool is StructuredToolInterface} Whether the inputted tool is an instance of `StructuredToolInterface`.
|
|
8
|
+
*/
|
|
9
|
+
export function isStructuredTool(tool) {
|
|
10
|
+
return (tool !== undefined &&
|
|
11
|
+
Array.isArray(tool.lc_namespace));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Confirm whether the inputted tool is an instance of `RunnableToolLike`.
|
|
15
|
+
*
|
|
16
|
+
* @param {unknown | undefined} tool The tool to check if it is an instance of `RunnableToolLike`.
|
|
17
|
+
* @returns {tool is RunnableToolLike} Whether the inputted tool is an instance of `RunnableToolLike`.
|
|
18
|
+
*/
|
|
19
|
+
export function isRunnableToolLike(tool) {
|
|
20
|
+
return (tool !== undefined &&
|
|
21
|
+
Runnable.isRunnable(tool) &&
|
|
22
|
+
"lc_name" in tool.constructor &&
|
|
23
|
+
typeof tool.constructor.lc_name === "function" &&
|
|
24
|
+
tool.constructor.lc_name() === "RunnableToolLike");
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Confirm whether or not the tool contains the necessary properties to be considered a `StructuredToolParams`.
|
|
28
|
+
*
|
|
29
|
+
* @param {unknown | undefined} tool The object to check if it is a `StructuredToolParams`.
|
|
30
|
+
* @returns {tool is StructuredToolParams} Whether the inputted object is a `StructuredToolParams`.
|
|
31
|
+
*/
|
|
32
|
+
export function isStructuredToolParams(tool) {
|
|
33
|
+
return (!!tool &&
|
|
34
|
+
typeof tool === "object" &&
|
|
35
|
+
"name" in tool &&
|
|
36
|
+
"schema" in tool &&
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
+
isZodSchema(tool.schema));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether or not the tool is one of StructuredTool, RunnableTool or StructuredToolParams.
|
|
42
|
+
* It returns `is StructuredToolParams` since that is the most minimal interface of the three,
|
|
43
|
+
* while still containing the necessary properties to be passed to a LLM for tool calling.
|
|
44
|
+
*
|
|
45
|
+
* @param {unknown | undefined} tool The tool to check if it is a LangChain tool.
|
|
46
|
+
* @returns {tool is StructuredToolParams} Whether the inputted tool is a LangChain tool.
|
|
47
|
+
*/
|
|
48
|
+
export function isLangChainTool(tool) {
|
|
49
|
+
return (isStructuredToolParams(tool) ||
|
|
50
|
+
isRunnableToolLike(tool) ||
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
|
+
isStructuredTool(tool));
|
|
53
|
+
}
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
3
|
+
exports.convertToOpenAITool = exports.convertToOpenAIFunction = exports.isLangChainTool = exports.isRunnableToolLike = exports.isStructuredToolParams = exports.isStructuredTool = void 0;
|
|
4
|
+
const types_js_1 = require("../tools/types.cjs");
|
|
5
|
+
const json_schema_js_1 = require("./json_schema.cjs");
|
|
6
|
+
// These utility functions were moved to a more appropriate location,
|
|
7
|
+
// but we still export them here for backwards compatibility.
|
|
8
|
+
var types_js_2 = require("../tools/types.cjs");
|
|
9
|
+
Object.defineProperty(exports, "isStructuredTool", { enumerable: true, get: function () { return types_js_2.isStructuredTool; } });
|
|
10
|
+
Object.defineProperty(exports, "isStructuredToolParams", { enumerable: true, get: function () { return types_js_2.isStructuredToolParams; } });
|
|
11
|
+
Object.defineProperty(exports, "isRunnableToolLike", { enumerable: true, get: function () { return types_js_2.isRunnableToolLike; } });
|
|
12
|
+
Object.defineProperty(exports, "isLangChainTool", { enumerable: true, get: function () { return types_js_2.isLangChainTool; } });
|
|
7
13
|
/**
|
|
8
14
|
* Formats a `StructuredTool` or `RunnableToolLike` instance into a format
|
|
9
|
-
* that is compatible with OpenAI function calling.
|
|
15
|
+
* that is compatible with OpenAI function calling. If `StructuredTool` or
|
|
16
|
+
* `RunnableToolLike` has a zod schema, it uses the `zodToJsonSchema`
|
|
10
17
|
* function to convert the schema of the `StructuredTool` or `RunnableToolLike`
|
|
11
18
|
* into a JSON schema, which is then used as the parameters for the OpenAI function.
|
|
12
19
|
*
|
|
@@ -19,7 +26,7 @@ function convertToOpenAIFunction(tool, fields) {
|
|
|
19
26
|
return {
|
|
20
27
|
name: tool.name,
|
|
21
28
|
description: tool.description,
|
|
22
|
-
parameters: (0,
|
|
29
|
+
parameters: (0, json_schema_js_1.toJsonSchema)(tool.schema),
|
|
23
30
|
// Do not include the `strict` field if it is `undefined`.
|
|
24
31
|
...(fieldsCopy?.strict !== undefined ? { strict: fieldsCopy.strict } : {}),
|
|
25
32
|
};
|
|
@@ -27,10 +34,10 @@ function convertToOpenAIFunction(tool, fields) {
|
|
|
27
34
|
exports.convertToOpenAIFunction = convertToOpenAIFunction;
|
|
28
35
|
/**
|
|
29
36
|
* Formats a `StructuredTool` or `RunnableToolLike` instance into a
|
|
30
|
-
* format that is compatible with OpenAI tool calling.
|
|
31
|
-
* `
|
|
32
|
-
*
|
|
33
|
-
* parameters for the OpenAI tool.
|
|
37
|
+
* format that is compatible with OpenAI tool calling. If `StructuredTool` or
|
|
38
|
+
* `RunnableToolLike` has a zod schema, it uses the `zodToJsonSchema`
|
|
39
|
+
* function to convert the schema of the `StructuredTool` or `RunnableToolLike`
|
|
40
|
+
* into a JSON schema, which is then used as the parameters for the OpenAI tool.
|
|
34
41
|
*
|
|
35
42
|
* @param {StructuredToolInterface | Record<string, any> | RunnableToolLike} tool The tool to convert to an OpenAI tool.
|
|
36
43
|
* @returns {ToolDefinition} The inputted tool in OpenAI tool format.
|
|
@@ -41,7 +48,7 @@ tool, fields) {
|
|
|
41
48
|
// @TODO 0.3.0 Remove the `number` typing
|
|
42
49
|
const fieldsCopy = typeof fields === "number" ? undefined : fields;
|
|
43
50
|
let toolDef;
|
|
44
|
-
if (isLangChainTool(tool)) {
|
|
51
|
+
if ((0, types_js_1.isLangChainTool)(tool)) {
|
|
45
52
|
toolDef = {
|
|
46
53
|
type: "function",
|
|
47
54
|
function: convertToOpenAIFunction(tool),
|
|
@@ -57,60 +64,3 @@ tool, fields) {
|
|
|
57
64
|
return toolDef;
|
|
58
65
|
}
|
|
59
66
|
exports.convertToOpenAITool = convertToOpenAITool;
|
|
60
|
-
/**
|
|
61
|
-
* Confirm whether the inputted tool is an instance of `StructuredToolInterface`.
|
|
62
|
-
*
|
|
63
|
-
* @param {StructuredToolInterface | Record<string, any> | undefined} tool The tool to check if it is an instance of `StructuredToolInterface`.
|
|
64
|
-
* @returns {tool is StructuredToolInterface} Whether the inputted tool is an instance of `StructuredToolInterface`.
|
|
65
|
-
*/
|
|
66
|
-
function isStructuredTool(
|
|
67
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
68
|
-
tool) {
|
|
69
|
-
return (tool !== undefined &&
|
|
70
|
-
Array.isArray(tool.lc_namespace));
|
|
71
|
-
}
|
|
72
|
-
exports.isStructuredTool = isStructuredTool;
|
|
73
|
-
/**
|
|
74
|
-
* Confirm whether the inputted tool is an instance of `RunnableToolLike`.
|
|
75
|
-
*
|
|
76
|
-
* @param {unknown | undefined} tool The tool to check if it is an instance of `RunnableToolLike`.
|
|
77
|
-
* @returns {tool is RunnableToolLike} Whether the inputted tool is an instance of `RunnableToolLike`.
|
|
78
|
-
*/
|
|
79
|
-
function isRunnableToolLike(tool) {
|
|
80
|
-
return (tool !== undefined &&
|
|
81
|
-
base_js_1.Runnable.isRunnable(tool) &&
|
|
82
|
-
"lc_name" in tool.constructor &&
|
|
83
|
-
typeof tool.constructor.lc_name === "function" &&
|
|
84
|
-
tool.constructor.lc_name() === "RunnableToolLike");
|
|
85
|
-
}
|
|
86
|
-
exports.isRunnableToolLike = isRunnableToolLike;
|
|
87
|
-
/**
|
|
88
|
-
* Confirm whether or not the tool contains the necessary properties to be considered a `StructuredToolParams`.
|
|
89
|
-
*
|
|
90
|
-
* @param {unknown | undefined} tool The object to check if it is a `StructuredToolParams`.
|
|
91
|
-
* @returns {tool is StructuredToolParams} Whether the inputted object is a `StructuredToolParams`.
|
|
92
|
-
*/
|
|
93
|
-
function isStructuredToolParams(tool) {
|
|
94
|
-
return (!!tool &&
|
|
95
|
-
typeof tool === "object" &&
|
|
96
|
-
"name" in tool &&
|
|
97
|
-
"schema" in tool &&
|
|
98
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
99
|
-
(0, is_zod_schema_js_1.isZodSchema)(tool.schema));
|
|
100
|
-
}
|
|
101
|
-
exports.isStructuredToolParams = isStructuredToolParams;
|
|
102
|
-
/**
|
|
103
|
-
* Whether or not the tool is one of StructuredTool, RunnableTool or StructuredToolParams.
|
|
104
|
-
* It returns `is StructuredToolParams` since that is the most minimal interface of the three,
|
|
105
|
-
* while still containing the necessary properties to be passed to a LLM for tool calling.
|
|
106
|
-
*
|
|
107
|
-
* @param {unknown | undefined} tool The tool to check if it is a LangChain tool.
|
|
108
|
-
* @returns {tool is StructuredToolParams} Whether the inputted tool is a LangChain tool.
|
|
109
|
-
*/
|
|
110
|
-
function isLangChainTool(tool) {
|
|
111
|
-
return (isStructuredToolParams(tool) ||
|
|
112
|
-
isRunnableToolLike(tool) ||
|
|
113
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
114
|
-
isStructuredTool(tool));
|
|
115
|
-
}
|
|
116
|
-
exports.isLangChainTool = isLangChainTool;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { StructuredToolInterface, StructuredToolParams } from "../tools/
|
|
1
|
+
import { StructuredToolInterface, StructuredToolParams } from "../tools/types.js";
|
|
2
2
|
import { FunctionDefinition, ToolDefinition } from "../language_models/base.js";
|
|
3
3
|
import { RunnableToolLike } from "../runnables/base.js";
|
|
4
|
+
export { isStructuredTool, isStructuredToolParams, isRunnableToolLike, isLangChainTool, } from "../tools/types.js";
|
|
4
5
|
/**
|
|
5
6
|
* Formats a `StructuredTool` or `RunnableToolLike` instance into a format
|
|
6
|
-
* that is compatible with OpenAI function calling.
|
|
7
|
+
* that is compatible with OpenAI function calling. If `StructuredTool` or
|
|
8
|
+
* `RunnableToolLike` has a zod schema, it uses the `zodToJsonSchema`
|
|
7
9
|
* function to convert the schema of the `StructuredTool` or `RunnableToolLike`
|
|
8
10
|
* into a JSON schema, which is then used as the parameters for the OpenAI function.
|
|
9
11
|
*
|
|
@@ -19,10 +21,10 @@ export declare function convertToOpenAIFunction(tool: StructuredToolInterface |
|
|
|
19
21
|
} | number): FunctionDefinition;
|
|
20
22
|
/**
|
|
21
23
|
* Formats a `StructuredTool` or `RunnableToolLike` instance into a
|
|
22
|
-
* format that is compatible with OpenAI tool calling.
|
|
23
|
-
* `
|
|
24
|
-
*
|
|
25
|
-
* parameters for the OpenAI tool.
|
|
24
|
+
* format that is compatible with OpenAI tool calling. If `StructuredTool` or
|
|
25
|
+
* `RunnableToolLike` has a zod schema, it uses the `zodToJsonSchema`
|
|
26
|
+
* function to convert the schema of the `StructuredTool` or `RunnableToolLike`
|
|
27
|
+
* into a JSON schema, which is then used as the parameters for the OpenAI tool.
|
|
26
28
|
*
|
|
27
29
|
* @param {StructuredToolInterface | Record<string, any> | RunnableToolLike} tool The tool to convert to an OpenAI tool.
|
|
28
30
|
* @returns {ToolDefinition} The inputted tool in OpenAI tool format.
|
|
@@ -34,33 +36,3 @@ export declare function convertToOpenAITool(tool: StructuredToolInterface | Reco
|
|
|
34
36
|
*/
|
|
35
37
|
strict?: boolean;
|
|
36
38
|
} | number): ToolDefinition;
|
|
37
|
-
/**
|
|
38
|
-
* Confirm whether the inputted tool is an instance of `StructuredToolInterface`.
|
|
39
|
-
*
|
|
40
|
-
* @param {StructuredToolInterface | Record<string, any> | undefined} tool The tool to check if it is an instance of `StructuredToolInterface`.
|
|
41
|
-
* @returns {tool is StructuredToolInterface} Whether the inputted tool is an instance of `StructuredToolInterface`.
|
|
42
|
-
*/
|
|
43
|
-
export declare function isStructuredTool(tool?: StructuredToolInterface | Record<string, any>): tool is StructuredToolInterface;
|
|
44
|
-
/**
|
|
45
|
-
* Confirm whether the inputted tool is an instance of `RunnableToolLike`.
|
|
46
|
-
*
|
|
47
|
-
* @param {unknown | undefined} tool The tool to check if it is an instance of `RunnableToolLike`.
|
|
48
|
-
* @returns {tool is RunnableToolLike} Whether the inputted tool is an instance of `RunnableToolLike`.
|
|
49
|
-
*/
|
|
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,9 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { isLangChainTool, } from "../tools/types.js";
|
|
2
|
+
import { toJsonSchema } from "./json_schema.js";
|
|
3
|
+
// These utility functions were moved to a more appropriate location,
|
|
4
|
+
// but we still export them here for backwards compatibility.
|
|
5
|
+
export { isStructuredTool, isStructuredToolParams, isRunnableToolLike, isLangChainTool, } from "../tools/types.js";
|
|
4
6
|
/**
|
|
5
7
|
* Formats a `StructuredTool` or `RunnableToolLike` instance into a format
|
|
6
|
-
* that is compatible with OpenAI function calling.
|
|
8
|
+
* that is compatible with OpenAI function calling. If `StructuredTool` or
|
|
9
|
+
* `RunnableToolLike` has a zod schema, it uses the `zodToJsonSchema`
|
|
7
10
|
* function to convert the schema of the `StructuredTool` or `RunnableToolLike`
|
|
8
11
|
* into a JSON schema, which is then used as the parameters for the OpenAI function.
|
|
9
12
|
*
|
|
@@ -16,17 +19,17 @@ export function convertToOpenAIFunction(tool, fields) {
|
|
|
16
19
|
return {
|
|
17
20
|
name: tool.name,
|
|
18
21
|
description: tool.description,
|
|
19
|
-
parameters:
|
|
22
|
+
parameters: toJsonSchema(tool.schema),
|
|
20
23
|
// Do not include the `strict` field if it is `undefined`.
|
|
21
24
|
...(fieldsCopy?.strict !== undefined ? { strict: fieldsCopy.strict } : {}),
|
|
22
25
|
};
|
|
23
26
|
}
|
|
24
27
|
/**
|
|
25
28
|
* Formats a `StructuredTool` or `RunnableToolLike` instance into a
|
|
26
|
-
* format that is compatible with OpenAI tool calling.
|
|
27
|
-
* `
|
|
28
|
-
*
|
|
29
|
-
* parameters for the OpenAI tool.
|
|
29
|
+
* format that is compatible with OpenAI tool calling. If `StructuredTool` or
|
|
30
|
+
* `RunnableToolLike` has a zod schema, it uses the `zodToJsonSchema`
|
|
31
|
+
* function to convert the schema of the `StructuredTool` or `RunnableToolLike`
|
|
32
|
+
* into a JSON schema, which is then used as the parameters for the OpenAI tool.
|
|
30
33
|
*
|
|
31
34
|
* @param {StructuredToolInterface | Record<string, any> | RunnableToolLike} tool The tool to convert to an OpenAI tool.
|
|
32
35
|
* @returns {ToolDefinition} The inputted tool in OpenAI tool format.
|
|
@@ -52,56 +55,3 @@ tool, fields) {
|
|
|
52
55
|
}
|
|
53
56
|
return toolDef;
|
|
54
57
|
}
|
|
55
|
-
/**
|
|
56
|
-
* Confirm whether the inputted tool is an instance of `StructuredToolInterface`.
|
|
57
|
-
*
|
|
58
|
-
* @param {StructuredToolInterface | Record<string, any> | undefined} tool The tool to check if it is an instance of `StructuredToolInterface`.
|
|
59
|
-
* @returns {tool is StructuredToolInterface} Whether the inputted tool is an instance of `StructuredToolInterface`.
|
|
60
|
-
*/
|
|
61
|
-
export function isStructuredTool(
|
|
62
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
63
|
-
tool) {
|
|
64
|
-
return (tool !== undefined &&
|
|
65
|
-
Array.isArray(tool.lc_namespace));
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Confirm whether the inputted tool is an instance of `RunnableToolLike`.
|
|
69
|
-
*
|
|
70
|
-
* @param {unknown | undefined} tool The tool to check if it is an instance of `RunnableToolLike`.
|
|
71
|
-
* @returns {tool is RunnableToolLike} Whether the inputted tool is an instance of `RunnableToolLike`.
|
|
72
|
-
*/
|
|
73
|
-
export function isRunnableToolLike(tool) {
|
|
74
|
-
return (tool !== undefined &&
|
|
75
|
-
Runnable.isRunnable(tool) &&
|
|
76
|
-
"lc_name" in tool.constructor &&
|
|
77
|
-
typeof tool.constructor.lc_name === "function" &&
|
|
78
|
-
tool.constructor.lc_name() === "RunnableToolLike");
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Confirm whether or not the tool contains the necessary properties to be considered a `StructuredToolParams`.
|
|
82
|
-
*
|
|
83
|
-
* @param {unknown | undefined} tool The object to check if it is a `StructuredToolParams`.
|
|
84
|
-
* @returns {tool is StructuredToolParams} Whether the inputted object is a `StructuredToolParams`.
|
|
85
|
-
*/
|
|
86
|
-
export function isStructuredToolParams(tool) {
|
|
87
|
-
return (!!tool &&
|
|
88
|
-
typeof tool === "object" &&
|
|
89
|
-
"name" in tool &&
|
|
90
|
-
"schema" in tool &&
|
|
91
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
92
|
-
isZodSchema(tool.schema));
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Whether or not the tool is one of StructuredTool, RunnableTool or StructuredToolParams.
|
|
96
|
-
* It returns `is StructuredToolParams` since that is the most minimal interface of the three,
|
|
97
|
-
* while still containing the necessary properties to be passed to a LLM for tool calling.
|
|
98
|
-
*
|
|
99
|
-
* @param {unknown | undefined} tool The tool to check if it is a LangChain tool.
|
|
100
|
-
* @returns {tool is StructuredToolParams} Whether the inputted tool is a LangChain tool.
|
|
101
|
-
*/
|
|
102
|
-
export function isLangChainTool(tool) {
|
|
103
|
-
return (isStructuredToolParams(tool) ||
|
|
104
|
-
isRunnableToolLike(tool) ||
|
|
105
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
106
|
-
isStructuredTool(tool));
|
|
107
|
-
}
|
|
@@ -1,6 +1,87 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Validator = exports.deepCompareStrict = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
exports.validatesOnlyStrings = exports.toJsonSchema = exports.Validator = exports.deepCompareStrict = void 0;
|
|
4
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
5
|
+
const json_schema_1 = require("@cfworker/json-schema");
|
|
6
|
+
const is_zod_schema_js_1 = require("./types/is_zod_schema.cjs");
|
|
7
|
+
var json_schema_2 = require("@cfworker/json-schema");
|
|
8
|
+
Object.defineProperty(exports, "deepCompareStrict", { enumerable: true, get: function () { return json_schema_2.deepCompareStrict; } });
|
|
9
|
+
Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return json_schema_2.Validator; } });
|
|
10
|
+
/**
|
|
11
|
+
* Converts a Zod schema or JSON schema to a JSON schema.
|
|
12
|
+
* @param schema - The schema to convert.
|
|
13
|
+
* @returns The converted schema.
|
|
14
|
+
*/
|
|
15
|
+
function toJsonSchema(schema) {
|
|
16
|
+
if ((0, is_zod_schema_js_1.isZodSchema)(schema)) {
|
|
17
|
+
return (0, zod_to_json_schema_1.zodToJsonSchema)(schema);
|
|
18
|
+
}
|
|
19
|
+
return schema;
|
|
20
|
+
}
|
|
21
|
+
exports.toJsonSchema = toJsonSchema;
|
|
22
|
+
/**
|
|
23
|
+
* Validates if a JSON schema validates only strings. May return false negatives in some edge cases
|
|
24
|
+
* (like recursive or unresolvable refs).
|
|
25
|
+
*
|
|
26
|
+
* @param schema - The schema to validate.
|
|
27
|
+
* @returns `true` if the schema validates only strings, `false` otherwise.
|
|
28
|
+
*/
|
|
29
|
+
function validatesOnlyStrings(schema) {
|
|
30
|
+
// Null, undefined, or empty schema
|
|
31
|
+
if (!schema ||
|
|
32
|
+
typeof schema !== "object" ||
|
|
33
|
+
Object.keys(schema).length === 0 ||
|
|
34
|
+
Array.isArray(schema)) {
|
|
35
|
+
return false; // Validates anything, not just strings
|
|
36
|
+
}
|
|
37
|
+
// Explicit type constraint
|
|
38
|
+
if ("type" in schema) {
|
|
39
|
+
if (typeof schema.type === "string") {
|
|
40
|
+
return schema.type === "string";
|
|
41
|
+
}
|
|
42
|
+
if (Array.isArray(schema.type)) {
|
|
43
|
+
// not sure why someone would do `"type": ["string"]` or especially `"type": ["string",
|
|
44
|
+
// "string", "string", ...]` but we're not here to judge
|
|
45
|
+
return schema.type.every((t) => t === "string");
|
|
46
|
+
}
|
|
47
|
+
return false; // Invalid or non-string type
|
|
48
|
+
}
|
|
49
|
+
// Enum with only string values
|
|
50
|
+
if ("enum" in schema) {
|
|
51
|
+
return (Array.isArray(schema.enum) &&
|
|
52
|
+
schema.enum.length > 0 &&
|
|
53
|
+
schema.enum.every((val) => typeof val === "string"));
|
|
54
|
+
}
|
|
55
|
+
// String constant
|
|
56
|
+
if ("const" in schema) {
|
|
57
|
+
return typeof schema.const === "string";
|
|
58
|
+
}
|
|
59
|
+
// Schema combinations
|
|
60
|
+
if ("allOf" in schema && Array.isArray(schema.allOf)) {
|
|
61
|
+
// If any subschema validates only strings, then the overall schema validates only strings
|
|
62
|
+
return schema.allOf.some((subschema) => validatesOnlyStrings(subschema));
|
|
63
|
+
}
|
|
64
|
+
if (("anyOf" in schema && Array.isArray(schema.anyOf)) ||
|
|
65
|
+
("oneOf" in schema && Array.isArray(schema.oneOf))) {
|
|
66
|
+
const subschemas = ("anyOf" in schema ? schema.anyOf : schema.oneOf);
|
|
67
|
+
// All subschemas must validate only strings
|
|
68
|
+
return (subschemas.length > 0 &&
|
|
69
|
+
subschemas.every((subschema) => validatesOnlyStrings(subschema)));
|
|
70
|
+
}
|
|
71
|
+
// We're not going to try on this one, it's too complex - we just assume if it has a "not" key and hasn't matched one of the above checks, it's not a string schema.
|
|
72
|
+
if ("not" in schema) {
|
|
73
|
+
return false; // The not case can validate non-strings
|
|
74
|
+
}
|
|
75
|
+
if ("$ref" in schema && typeof schema.$ref === "string") {
|
|
76
|
+
const ref = schema.$ref;
|
|
77
|
+
const resolved = (0, json_schema_1.dereference)(schema);
|
|
78
|
+
if (resolved[ref]) {
|
|
79
|
+
return validatesOnlyStrings(resolved[ref]);
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
// ignore recursive refs and other cases where type is omitted for now
|
|
84
|
+
// ignore other cases for now where type is omitted
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
exports.validatesOnlyStrings = validatesOnlyStrings;
|