@langchain/core 0.2.20 → 0.2.22-rc.0
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 +8 -0
- package/dist/language_models/chat_models.d.ts +3 -2
- package/dist/runnables/remote.cjs +34 -3
- package/dist/runnables/remote.d.ts +17 -0
- package/dist/runnables/remote.js +34 -3
- package/dist/tools/index.d.ts +15 -0
- package/dist/utils/function_calling.cjs +50 -6
- package/dist/utils/function_calling.d.ts +31 -3
- package/dist/utils/function_calling.js +47 -5
- package/package.json +2 -2
|
@@ -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:
|
|
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.
|
|
@@ -229,6 +229,19 @@ function serialize(input) {
|
|
|
229
229
|
}
|
|
230
230
|
return input;
|
|
231
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Client for interacting with LangChain runnables
|
|
234
|
+
* that are hosted as LangServe endpoints.
|
|
235
|
+
*
|
|
236
|
+
* Allows you to interact with hosted runnables using the standard
|
|
237
|
+
* `.invoke()`, `.stream()`, `.streamEvents()`, etc. methods that
|
|
238
|
+
* other runnables support.
|
|
239
|
+
*
|
|
240
|
+
* @param url - The base URL of the LangServe endpoint.
|
|
241
|
+
* @param options - Optional configuration for the remote runnable, including timeout and headers.
|
|
242
|
+
* @param fetch - Optional custom fetch implementation.
|
|
243
|
+
* @param fetchRequestOptions - Optional additional options for fetch requests.
|
|
244
|
+
*/
|
|
232
245
|
class RemoteRunnable extends base_js_1.Runnable {
|
|
233
246
|
constructor(fields) {
|
|
234
247
|
super(fields);
|
|
@@ -244,25 +257,43 @@ class RemoteRunnable extends base_js_1.Runnable {
|
|
|
244
257
|
writable: true,
|
|
245
258
|
value: void 0
|
|
246
259
|
});
|
|
260
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
261
|
+
Object.defineProperty(this, "fetchImplementation", {
|
|
262
|
+
enumerable: true,
|
|
263
|
+
configurable: true,
|
|
264
|
+
writable: true,
|
|
265
|
+
value: fetch
|
|
266
|
+
});
|
|
267
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
268
|
+
Object.defineProperty(this, "fetchRequestOptions", {
|
|
269
|
+
enumerable: true,
|
|
270
|
+
configurable: true,
|
|
271
|
+
writable: true,
|
|
272
|
+
value: void 0
|
|
273
|
+
});
|
|
247
274
|
Object.defineProperty(this, "lc_namespace", {
|
|
248
275
|
enumerable: true,
|
|
249
276
|
configurable: true,
|
|
250
277
|
writable: true,
|
|
251
278
|
value: ["langchain", "schema", "runnable", "remote"]
|
|
252
279
|
});
|
|
253
|
-
const { url, options } = fields;
|
|
280
|
+
const { url, options, fetch: fetchImplementation, fetchRequestOptions, } = fields;
|
|
254
281
|
this.url = url.replace(/\/$/, ""); // remove trailing slash
|
|
255
282
|
this.options = options;
|
|
283
|
+
this.fetchImplementation = fetchImplementation ?? this.fetchImplementation;
|
|
284
|
+
this.fetchRequestOptions = fetchRequestOptions;
|
|
256
285
|
}
|
|
257
286
|
async post(path, body, signal) {
|
|
258
|
-
return
|
|
287
|
+
return this.fetchImplementation(`${this.url}${path}`, {
|
|
259
288
|
method: "POST",
|
|
260
289
|
body: JSON.stringify(serialize(body)),
|
|
290
|
+
signal: signal ?? AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
291
|
+
...this.fetchRequestOptions,
|
|
261
292
|
headers: {
|
|
262
293
|
"Content-Type": "application/json",
|
|
294
|
+
...this.fetchRequestOptions?.headers,
|
|
263
295
|
...this.options?.headers,
|
|
264
296
|
},
|
|
265
|
-
signal: signal ?? AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
266
297
|
});
|
|
267
298
|
}
|
|
268
299
|
async _invoke(input, options, _) {
|
|
@@ -7,13 +7,30 @@ type RemoteRunnableOptions = {
|
|
|
7
7
|
timeout?: number;
|
|
8
8
|
headers?: Record<string, unknown>;
|
|
9
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* Client for interacting with LangChain runnables
|
|
12
|
+
* that are hosted as LangServe endpoints.
|
|
13
|
+
*
|
|
14
|
+
* Allows you to interact with hosted runnables using the standard
|
|
15
|
+
* `.invoke()`, `.stream()`, `.streamEvents()`, etc. methods that
|
|
16
|
+
* other runnables support.
|
|
17
|
+
*
|
|
18
|
+
* @param url - The base URL of the LangServe endpoint.
|
|
19
|
+
* @param options - Optional configuration for the remote runnable, including timeout and headers.
|
|
20
|
+
* @param fetch - Optional custom fetch implementation.
|
|
21
|
+
* @param fetchRequestOptions - Optional additional options for fetch requests.
|
|
22
|
+
*/
|
|
10
23
|
export declare class RemoteRunnable<RunInput, RunOutput, CallOptions extends RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> {
|
|
11
24
|
private url;
|
|
12
25
|
private options?;
|
|
26
|
+
fetchImplementation: (...args: any[]) => any;
|
|
27
|
+
fetchRequestOptions?: Record<string, any>;
|
|
13
28
|
lc_namespace: string[];
|
|
14
29
|
constructor(fields: {
|
|
15
30
|
url: string;
|
|
16
31
|
options?: RemoteRunnableOptions;
|
|
32
|
+
fetch?: (...args: any[]) => any;
|
|
33
|
+
fetchRequestOptions?: Record<string, any>;
|
|
17
34
|
});
|
|
18
35
|
private post;
|
|
19
36
|
_invoke(input: RunInput, options?: Partial<CallOptions>, _?: CallbackManagerForChainRun): Promise<RunOutput>;
|
package/dist/runnables/remote.js
CHANGED
|
@@ -226,6 +226,19 @@ function serialize(input) {
|
|
|
226
226
|
}
|
|
227
227
|
return input;
|
|
228
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Client for interacting with LangChain runnables
|
|
231
|
+
* that are hosted as LangServe endpoints.
|
|
232
|
+
*
|
|
233
|
+
* Allows you to interact with hosted runnables using the standard
|
|
234
|
+
* `.invoke()`, `.stream()`, `.streamEvents()`, etc. methods that
|
|
235
|
+
* other runnables support.
|
|
236
|
+
*
|
|
237
|
+
* @param url - The base URL of the LangServe endpoint.
|
|
238
|
+
* @param options - Optional configuration for the remote runnable, including timeout and headers.
|
|
239
|
+
* @param fetch - Optional custom fetch implementation.
|
|
240
|
+
* @param fetchRequestOptions - Optional additional options for fetch requests.
|
|
241
|
+
*/
|
|
229
242
|
export class RemoteRunnable extends Runnable {
|
|
230
243
|
constructor(fields) {
|
|
231
244
|
super(fields);
|
|
@@ -241,25 +254,43 @@ export class RemoteRunnable extends Runnable {
|
|
|
241
254
|
writable: true,
|
|
242
255
|
value: void 0
|
|
243
256
|
});
|
|
257
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
258
|
+
Object.defineProperty(this, "fetchImplementation", {
|
|
259
|
+
enumerable: true,
|
|
260
|
+
configurable: true,
|
|
261
|
+
writable: true,
|
|
262
|
+
value: fetch
|
|
263
|
+
});
|
|
264
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
265
|
+
Object.defineProperty(this, "fetchRequestOptions", {
|
|
266
|
+
enumerable: true,
|
|
267
|
+
configurable: true,
|
|
268
|
+
writable: true,
|
|
269
|
+
value: void 0
|
|
270
|
+
});
|
|
244
271
|
Object.defineProperty(this, "lc_namespace", {
|
|
245
272
|
enumerable: true,
|
|
246
273
|
configurable: true,
|
|
247
274
|
writable: true,
|
|
248
275
|
value: ["langchain", "schema", "runnable", "remote"]
|
|
249
276
|
});
|
|
250
|
-
const { url, options } = fields;
|
|
277
|
+
const { url, options, fetch: fetchImplementation, fetchRequestOptions, } = fields;
|
|
251
278
|
this.url = url.replace(/\/$/, ""); // remove trailing slash
|
|
252
279
|
this.options = options;
|
|
280
|
+
this.fetchImplementation = fetchImplementation ?? this.fetchImplementation;
|
|
281
|
+
this.fetchRequestOptions = fetchRequestOptions;
|
|
253
282
|
}
|
|
254
283
|
async post(path, body, signal) {
|
|
255
|
-
return
|
|
284
|
+
return this.fetchImplementation(`${this.url}${path}`, {
|
|
256
285
|
method: "POST",
|
|
257
286
|
body: JSON.stringify(serialize(body)),
|
|
287
|
+
signal: signal ?? AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
288
|
+
...this.fetchRequestOptions,
|
|
258
289
|
headers: {
|
|
259
290
|
"Content-Type": "application/json",
|
|
291
|
+
...this.fetchRequestOptions?.headers,
|
|
260
292
|
...this.options?.headers,
|
|
261
293
|
},
|
|
262
|
-
signal: signal ?? AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
263
294
|
});
|
|
264
295
|
}
|
|
265
296
|
async _invoke(input, options, _) {
|
package/dist/tools/index.d.ts
CHANGED
|
@@ -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
|
-
|
|
37
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
33
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "0.2.22-rc.0",
|
|
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": "^
|
|
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",
|