@langchain/core 0.3.0-rc.0 → 0.3.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/README.md +1 -1
- package/dist/callbacks/dispatch/web.cjs +1 -1
- package/dist/callbacks/dispatch/web.js +1 -1
- package/dist/language_models/base.d.ts +0 -8
- package/dist/language_models/chat_models.d.ts +1 -1
- package/dist/messages/ai.cjs +5 -1
- package/dist/messages/ai.d.ts +1 -0
- package/dist/messages/ai.js +3 -0
- package/dist/messages/base.d.ts +17 -4
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +64 -33
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +15 -14
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +65 -34
- package/dist/output_parsers/transform.cjs +3 -0
- package/dist/output_parsers/transform.d.ts +1 -0
- package/dist/output_parsers/transform.js +3 -0
- package/dist/runnables/base.d.ts +40 -40
- package/dist/utils/function_calling.cjs +1 -0
- package/dist/utils/function_calling.js +1 -0
- package/dist/utils/stream.cjs +10 -0
- package/dist/utils/stream.d.ts +3 -1
- package/dist/utils/stream.js +10 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -83,7 +83,7 @@ Streaming (and streaming of intermediate steps) is needed to show the user that
|
|
|
83
83
|
Async interfaces are nice when moving into production.
|
|
84
84
|
Rather than having to write multiple implementations for all of those, LCEL allows you to write a runnable once and invoke it in many different ways.
|
|
85
85
|
|
|
86
|
-
For more check out the [LCEL docs](https://js.langchain.com/
|
|
86
|
+
For more check out the [LCEL docs](https://js.langchain.com/docs/concepts#langchain-expression-language).
|
|
87
87
|
|
|
88
88
|

|
|
89
89
|
|
|
@@ -50,7 +50,7 @@ payload, config) {
|
|
|
50
50
|
"and explicitly pass in a config parameter.",
|
|
51
51
|
`\n\nOr, if you are calling this from a custom tool, ensure you're using the "tool" helper constructor as documented here:`,
|
|
52
52
|
"\n |",
|
|
53
|
-
"\n └-> https://js.langchain.com/
|
|
53
|
+
"\n └-> https://js.langchain.com/docs/how_to/custom_tools#tool-function",
|
|
54
54
|
"\n",
|
|
55
55
|
].join(" "));
|
|
56
56
|
}
|
|
@@ -47,7 +47,7 @@ payload, config) {
|
|
|
47
47
|
"and explicitly pass in a config parameter.",
|
|
48
48
|
`\n\nOr, if you are calling this from a custom tool, ensure you're using the "tool" helper constructor as documented here:`,
|
|
49
49
|
"\n |",
|
|
50
|
-
"\n └-> https://js.langchain.com/
|
|
50
|
+
"\n └-> https://js.langchain.com/docs/how_to/custom_tools#tool-function",
|
|
51
51
|
"\n",
|
|
52
52
|
].join(" "));
|
|
53
53
|
}
|
|
@@ -89,14 +89,6 @@ 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;
|
|
100
92
|
}
|
|
101
93
|
export interface ToolDefinition {
|
|
102
94
|
type: "function";
|
|
@@ -2,7 +2,7 @@ import { z } from "zod";
|
|
|
2
2
|
import { type BaseMessage, BaseMessageChunk, type BaseMessageLike } from "../messages/index.js";
|
|
3
3
|
import type { BasePromptValueInterface } from "../prompt_values.js";
|
|
4
4
|
import { LLMResult, ChatGenerationChunk, type ChatResult, type Generation } from "../outputs.js";
|
|
5
|
-
import { BaseLanguageModel, StructuredOutputMethodOptions, ToolDefinition, type BaseLanguageModelCallOptions, type BaseLanguageModelInput, type BaseLanguageModelParams } from "./base.js";
|
|
5
|
+
import { BaseLanguageModel, type StructuredOutputMethodOptions, type ToolDefinition, type BaseLanguageModelCallOptions, type BaseLanguageModelInput, type BaseLanguageModelParams } from "./base.js";
|
|
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";
|
package/dist/messages/ai.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AIMessageChunk = exports.isAIMessage = exports.AIMessage = void 0;
|
|
3
|
+
exports.AIMessageChunk = exports.isAIMessageChunk = exports.isAIMessage = exports.AIMessage = void 0;
|
|
4
4
|
const json_js_1 = require("../utils/json.cjs");
|
|
5
5
|
const base_js_1 = require("./base.cjs");
|
|
6
6
|
const tool_js_1 = require("./tool.cjs");
|
|
@@ -112,6 +112,10 @@ function isAIMessage(x) {
|
|
|
112
112
|
return x._getType() === "ai";
|
|
113
113
|
}
|
|
114
114
|
exports.isAIMessage = isAIMessage;
|
|
115
|
+
function isAIMessageChunk(x) {
|
|
116
|
+
return x._getType() === "ai";
|
|
117
|
+
}
|
|
118
|
+
exports.isAIMessageChunk = isAIMessageChunk;
|
|
115
119
|
/**
|
|
116
120
|
* Represents a chunk of an AI message, which can be concatenated with
|
|
117
121
|
* other AI message chunks.
|
package/dist/messages/ai.d.ts
CHANGED
|
@@ -41,6 +41,7 @@ export declare class AIMessage extends BaseMessage {
|
|
|
41
41
|
get _printableFields(): Record<string, unknown>;
|
|
42
42
|
}
|
|
43
43
|
export declare function isAIMessage(x: BaseMessage): x is AIMessage;
|
|
44
|
+
export declare function isAIMessageChunk(x: BaseMessageChunk): x is AIMessageChunk;
|
|
44
45
|
export type AIMessageChunkFields = AIMessageFields & {
|
|
45
46
|
tool_call_chunks?: ToolCallChunk[];
|
|
46
47
|
};
|
package/dist/messages/ai.js
CHANGED
|
@@ -107,6 +107,9 @@ export class AIMessage extends BaseMessage {
|
|
|
107
107
|
export function isAIMessage(x) {
|
|
108
108
|
return x._getType() === "ai";
|
|
109
109
|
}
|
|
110
|
+
export function isAIMessageChunk(x) {
|
|
111
|
+
return x._getType() === "ai";
|
|
112
|
+
}
|
|
110
113
|
/**
|
|
111
114
|
* Represents a chunk of an AI message, which can be concatenated with
|
|
112
115
|
* other AI message chunks.
|
package/dist/messages/base.d.ts
CHANGED
|
@@ -59,7 +59,13 @@ export type BaseMessageFields = {
|
|
|
59
59
|
content: MessageContent;
|
|
60
60
|
name?: string;
|
|
61
61
|
additional_kwargs?: {
|
|
62
|
+
/**
|
|
63
|
+
* @deprecated Use "tool_calls" field on AIMessages instead
|
|
64
|
+
*/
|
|
62
65
|
function_call?: FunctionCall;
|
|
66
|
+
/**
|
|
67
|
+
* @deprecated Use "tool_calls" field on AIMessages instead
|
|
68
|
+
*/
|
|
63
69
|
tool_calls?: OpenAIToolCall[];
|
|
64
70
|
[key: string]: unknown;
|
|
65
71
|
};
|
|
@@ -119,6 +125,9 @@ export declare abstract class BaseMessage extends Serializable implements BaseMe
|
|
|
119
125
|
_updateId(value: string | undefined): void;
|
|
120
126
|
get [Symbol.toStringTag](): any;
|
|
121
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* @deprecated Use "tool_calls" field on AIMessages instead
|
|
130
|
+
*/
|
|
122
131
|
export type OpenAIToolCall = {
|
|
123
132
|
/**
|
|
124
133
|
* The ID of the tool call.
|
|
@@ -154,11 +163,15 @@ export type MessageFieldWithRole = {
|
|
|
154
163
|
name?: string;
|
|
155
164
|
} & Record<string, unknown>;
|
|
156
165
|
export declare function _isMessageFieldWithRole(x: BaseMessageLike): x is MessageFieldWithRole;
|
|
157
|
-
export type BaseMessageLike = BaseMessage |
|
|
158
|
-
type: MessageType | "user" | "assistant" | "placeholder";
|
|
159
|
-
} & BaseMessageFields & Record<string, unknown>) | MessageFieldWithRole | [
|
|
166
|
+
export type BaseMessageLike = BaseMessage | MessageFieldWithRole | [
|
|
160
167
|
StringWithAutocomplete<MessageType | "user" | "assistant" | "placeholder">,
|
|
161
168
|
MessageContent
|
|
162
|
-
] | string
|
|
169
|
+
] | string
|
|
170
|
+
/**
|
|
171
|
+
* @deprecated Specifying "type" is deprecated and will be removed in 0.4.0.
|
|
172
|
+
*/
|
|
173
|
+
| ({
|
|
174
|
+
type: MessageType | "user" | "assistant" | "placeholder";
|
|
175
|
+
} & BaseMessageFields & Record<string, unknown>);
|
|
163
176
|
export declare function isBaseMessage(messageLike?: unknown): messageLike is BaseMessage;
|
|
164
177
|
export declare function isBaseMessageChunk(messageLike?: unknown): messageLike is BaseMessageChunk;
|
|
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.JsonOutputKeyToolsParser = exports.JsonOutputToolsParser = exports.makeInvalidToolCall = exports.convertLangChainToolCallToOpenAI = exports.parseToolCall = void 0;
|
|
4
4
|
const base_js_1 = require("../base.cjs");
|
|
5
5
|
const json_js_1 = require("../json.cjs");
|
|
6
|
+
const transform_js_1 = require("../transform.cjs");
|
|
7
|
+
const ai_js_1 = require("../../messages/ai.cjs");
|
|
6
8
|
function parseToolCall(
|
|
7
9
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
10
|
rawToolCall, options) {
|
|
@@ -74,7 +76,7 @@ exports.makeInvalidToolCall = makeInvalidToolCall;
|
|
|
74
76
|
/**
|
|
75
77
|
* Class for parsing the output of a tool-calling LLM into a JSON object.
|
|
76
78
|
*/
|
|
77
|
-
class JsonOutputToolsParser extends
|
|
79
|
+
class JsonOutputToolsParser extends transform_js_1.BaseCumulativeTransformOutputParser {
|
|
78
80
|
static lc_name() {
|
|
79
81
|
return "JsonOutputToolsParser";
|
|
80
82
|
}
|
|
@@ -100,40 +102,56 @@ class JsonOutputToolsParser extends base_js_1.BaseLLMOutputParser {
|
|
|
100
102
|
});
|
|
101
103
|
this.returnId = fields?.returnId ?? this.returnId;
|
|
102
104
|
}
|
|
105
|
+
_diff() {
|
|
106
|
+
throw new Error("Not supported.");
|
|
107
|
+
}
|
|
108
|
+
async parse() {
|
|
109
|
+
throw new Error("Not implemented.");
|
|
110
|
+
}
|
|
111
|
+
async parseResult(generations) {
|
|
112
|
+
const result = await this.parsePartialResult(generations, false);
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
103
115
|
/**
|
|
104
116
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
105
117
|
* only the arguments of the function call are returned.
|
|
106
118
|
* @param generations The output of the LLM to parse.
|
|
107
119
|
* @returns A JSON object representation of the function call or its arguments.
|
|
108
120
|
*/
|
|
109
|
-
async
|
|
110
|
-
|
|
121
|
+
async parsePartialResult(generations, partial = true
|
|
122
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
123
|
+
) {
|
|
124
|
+
const message = generations[0].message;
|
|
125
|
+
let toolCalls;
|
|
126
|
+
if ((0, ai_js_1.isAIMessage)(message) && message.tool_calls?.length) {
|
|
127
|
+
toolCalls = message.tool_calls.map((toolCall) => {
|
|
128
|
+
const { id, ...rest } = toolCall;
|
|
129
|
+
if (!this.returnId) {
|
|
130
|
+
return rest;
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
id,
|
|
134
|
+
...rest,
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
else if (message.additional_kwargs.tool_calls !== undefined) {
|
|
139
|
+
const rawToolCalls = JSON.parse(JSON.stringify(message.additional_kwargs.tool_calls));
|
|
140
|
+
toolCalls = rawToolCalls.map((rawToolCall) => {
|
|
141
|
+
return parseToolCall(rawToolCall, { returnId: this.returnId, partial });
|
|
142
|
+
});
|
|
143
|
+
}
|
|
111
144
|
if (!toolCalls) {
|
|
112
|
-
|
|
145
|
+
return [];
|
|
113
146
|
}
|
|
114
|
-
const clonedToolCalls = JSON.parse(JSON.stringify(toolCalls));
|
|
115
147
|
const parsedToolCalls = [];
|
|
116
|
-
for (const toolCall of
|
|
117
|
-
|
|
118
|
-
if (parsedToolCall !== undefined) {
|
|
119
|
-
// backward-compatibility with previous
|
|
120
|
-
// versions of Langchain JS, which uses `name` and `arguments`
|
|
121
|
-
// @ts-expect-error name and arguemnts are defined by Object.defineProperty
|
|
148
|
+
for (const toolCall of toolCalls) {
|
|
149
|
+
if (toolCall !== undefined) {
|
|
122
150
|
const backwardsCompatibleToolCall = {
|
|
123
|
-
type:
|
|
124
|
-
args:
|
|
125
|
-
id:
|
|
151
|
+
type: toolCall.name,
|
|
152
|
+
args: toolCall.args,
|
|
153
|
+
id: toolCall.id,
|
|
126
154
|
};
|
|
127
|
-
Object.defineProperty(backwardsCompatibleToolCall, "name", {
|
|
128
|
-
get() {
|
|
129
|
-
return this.type;
|
|
130
|
-
},
|
|
131
|
-
});
|
|
132
|
-
Object.defineProperty(backwardsCompatibleToolCall, "arguments", {
|
|
133
|
-
get() {
|
|
134
|
-
return this.args;
|
|
135
|
-
},
|
|
136
|
-
});
|
|
137
155
|
parsedToolCalls.push(backwardsCompatibleToolCall);
|
|
138
156
|
}
|
|
139
157
|
}
|
|
@@ -145,7 +163,7 @@ exports.JsonOutputToolsParser = JsonOutputToolsParser;
|
|
|
145
163
|
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
146
164
|
* expecting only a single tool to be called.
|
|
147
165
|
*/
|
|
148
|
-
class JsonOutputKeyToolsParser extends
|
|
166
|
+
class JsonOutputKeyToolsParser extends JsonOutputToolsParser {
|
|
149
167
|
static lc_name() {
|
|
150
168
|
return "JsonOutputKeyToolsParser";
|
|
151
169
|
}
|
|
@@ -183,12 +201,6 @@ class JsonOutputKeyToolsParser extends base_js_1.BaseLLMOutputParser {
|
|
|
183
201
|
writable: true,
|
|
184
202
|
value: false
|
|
185
203
|
});
|
|
186
|
-
Object.defineProperty(this, "initialParser", {
|
|
187
|
-
enumerable: true,
|
|
188
|
-
configurable: true,
|
|
189
|
-
writable: true,
|
|
190
|
-
value: void 0
|
|
191
|
-
});
|
|
192
204
|
Object.defineProperty(this, "zodSchema", {
|
|
193
205
|
enumerable: true,
|
|
194
206
|
configurable: true,
|
|
@@ -197,7 +209,6 @@ class JsonOutputKeyToolsParser extends base_js_1.BaseLLMOutputParser {
|
|
|
197
209
|
});
|
|
198
210
|
this.keyName = params.keyName;
|
|
199
211
|
this.returnSingle = params.returnSingle ?? this.returnSingle;
|
|
200
|
-
this.initialParser = new JsonOutputToolsParser(params);
|
|
201
212
|
this.zodSchema = params.zodSchema;
|
|
202
213
|
}
|
|
203
214
|
async _validateResult(result) {
|
|
@@ -213,11 +224,31 @@ class JsonOutputKeyToolsParser extends base_js_1.BaseLLMOutputParser {
|
|
|
213
224
|
}
|
|
214
225
|
}
|
|
215
226
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
227
|
+
async parsePartialResult(generations) {
|
|
228
|
+
const results = await super.parsePartialResult(generations);
|
|
229
|
+
const matchingResults = results.filter((result) => result.type === this.keyName);
|
|
230
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
231
|
+
let returnedValues = matchingResults;
|
|
232
|
+
if (!matchingResults.length) {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
if (!this.returnId) {
|
|
236
|
+
returnedValues = matchingResults.map((result) => result.args);
|
|
237
|
+
}
|
|
238
|
+
if (this.returnSingle) {
|
|
239
|
+
return returnedValues[0];
|
|
240
|
+
}
|
|
241
|
+
return returnedValues;
|
|
242
|
+
}
|
|
243
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
216
244
|
async parseResult(generations) {
|
|
217
|
-
const results = await
|
|
245
|
+
const results = await super.parsePartialResult(generations, false);
|
|
218
246
|
const matchingResults = results.filter((result) => result.type === this.keyName);
|
|
219
247
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
220
248
|
let returnedValues = matchingResults;
|
|
249
|
+
if (!matchingResults.length) {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
221
252
|
if (!this.returnId) {
|
|
222
253
|
returnedValues = matchingResults.map((result) => result.args);
|
|
223
254
|
}
|
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { ChatGeneration } from "../../outputs.js";
|
|
3
|
-
import { BaseLLMOutputParser } from "../base.js";
|
|
2
|
+
import { ChatGeneration, ChatGenerationChunk } from "../../outputs.js";
|
|
4
3
|
import { InvalidToolCall, ToolCall } from "../../messages/tool.js";
|
|
4
|
+
import { BaseCumulativeTransformOutputParser, BaseCumulativeTransformOutputParserInput } from "../transform.js";
|
|
5
5
|
export type ParsedToolCall = {
|
|
6
6
|
id?: string;
|
|
7
7
|
type: string;
|
|
8
8
|
args: Record<string, any>;
|
|
9
|
-
/** @deprecated Use `type` instead. Will be removed in 0.2.0. */
|
|
10
|
-
name: string;
|
|
11
|
-
/** @deprecated Use `args` instead. Will be removed in 0.2.0. */
|
|
12
|
-
arguments: Record<string, any>;
|
|
13
9
|
};
|
|
14
10
|
export type JsonOutputToolsParserParams = {
|
|
15
11
|
/** Whether to return the tool call id. */
|
|
16
12
|
returnId?: boolean;
|
|
17
|
-
};
|
|
13
|
+
} & BaseCumulativeTransformOutputParserInput;
|
|
18
14
|
export declare function parseToolCall(rawToolCall: Record<string, any>, options: {
|
|
19
15
|
returnId?: boolean;
|
|
20
16
|
partial: true;
|
|
@@ -23,6 +19,10 @@ export declare function parseToolCall(rawToolCall: Record<string, any>, options?
|
|
|
23
19
|
returnId?: boolean;
|
|
24
20
|
partial?: false;
|
|
25
21
|
}): ToolCall;
|
|
22
|
+
export declare function parseToolCall(rawToolCall: Record<string, any>, options?: {
|
|
23
|
+
returnId?: boolean;
|
|
24
|
+
partial?: boolean;
|
|
25
|
+
}): ToolCall | undefined;
|
|
26
26
|
export declare function convertLangChainToolCallToOpenAI(toolCall: ToolCall): {
|
|
27
27
|
id: string;
|
|
28
28
|
type: string;
|
|
@@ -35,32 +35,33 @@ export declare function makeInvalidToolCall(rawToolCall: Record<string, any>, er
|
|
|
35
35
|
/**
|
|
36
36
|
* Class for parsing the output of a tool-calling LLM into a JSON object.
|
|
37
37
|
*/
|
|
38
|
-
export declare class JsonOutputToolsParser extends
|
|
38
|
+
export declare class JsonOutputToolsParser<T> extends BaseCumulativeTransformOutputParser<T> {
|
|
39
39
|
static lc_name(): string;
|
|
40
40
|
returnId: boolean;
|
|
41
41
|
lc_namespace: string[];
|
|
42
42
|
lc_serializable: boolean;
|
|
43
43
|
constructor(fields?: JsonOutputToolsParserParams);
|
|
44
|
+
protected _diff(): void;
|
|
45
|
+
parse(): Promise<T>;
|
|
46
|
+
parseResult(generations: ChatGeneration[]): Promise<T>;
|
|
44
47
|
/**
|
|
45
48
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
46
49
|
* only the arguments of the function call are returned.
|
|
47
50
|
* @param generations The output of the LLM to parse.
|
|
48
51
|
* @returns A JSON object representation of the function call or its arguments.
|
|
49
52
|
*/
|
|
50
|
-
|
|
53
|
+
parsePartialResult(generations: ChatGenerationChunk[] | ChatGeneration[], partial?: boolean): Promise<any>;
|
|
51
54
|
}
|
|
52
55
|
export type JsonOutputKeyToolsParserParams<T extends Record<string, any> = Record<string, any>> = {
|
|
53
56
|
keyName: string;
|
|
54
57
|
returnSingle?: boolean;
|
|
55
|
-
/** Whether to return the tool call id. */
|
|
56
|
-
returnId?: boolean;
|
|
57
58
|
zodSchema?: z.ZodType<T>;
|
|
58
|
-
};
|
|
59
|
+
} & JsonOutputToolsParserParams;
|
|
59
60
|
/**
|
|
60
61
|
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
61
62
|
* expecting only a single tool to be called.
|
|
62
63
|
*/
|
|
63
|
-
export declare class JsonOutputKeyToolsParser<T extends Record<string, any> = Record<string, any>> extends
|
|
64
|
+
export declare class JsonOutputKeyToolsParser<T extends Record<string, any> = Record<string, any>> extends JsonOutputToolsParser<T> {
|
|
64
65
|
static lc_name(): string;
|
|
65
66
|
lc_namespace: string[];
|
|
66
67
|
lc_serializable: boolean;
|
|
@@ -69,9 +70,9 @@ export declare class JsonOutputKeyToolsParser<T extends Record<string, any> = Re
|
|
|
69
70
|
keyName: string;
|
|
70
71
|
/** Whether to return only the first tool call. */
|
|
71
72
|
returnSingle: boolean;
|
|
72
|
-
initialParser: JsonOutputToolsParser;
|
|
73
73
|
zodSchema?: z.ZodType<T>;
|
|
74
74
|
constructor(params: JsonOutputKeyToolsParserParams<T>);
|
|
75
75
|
protected _validateResult(result: unknown): Promise<T>;
|
|
76
|
+
parsePartialResult(generations: ChatGeneration[]): Promise<any>;
|
|
76
77
|
parseResult(generations: ChatGeneration[]): Promise<any>;
|
|
77
78
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OutputParserException } from "../base.js";
|
|
2
2
|
import { parsePartialJson } from "../json.js";
|
|
3
|
+
import { BaseCumulativeTransformOutputParser, } from "../transform.js";
|
|
4
|
+
import { isAIMessage } from "../../messages/ai.js";
|
|
3
5
|
export function parseToolCall(
|
|
4
6
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
7
|
rawToolCall, options) {
|
|
@@ -68,7 +70,7 @@ rawToolCall, errorMsg) {
|
|
|
68
70
|
/**
|
|
69
71
|
* Class for parsing the output of a tool-calling LLM into a JSON object.
|
|
70
72
|
*/
|
|
71
|
-
export class JsonOutputToolsParser extends
|
|
73
|
+
export class JsonOutputToolsParser extends BaseCumulativeTransformOutputParser {
|
|
72
74
|
static lc_name() {
|
|
73
75
|
return "JsonOutputToolsParser";
|
|
74
76
|
}
|
|
@@ -94,40 +96,56 @@ export class JsonOutputToolsParser extends BaseLLMOutputParser {
|
|
|
94
96
|
});
|
|
95
97
|
this.returnId = fields?.returnId ?? this.returnId;
|
|
96
98
|
}
|
|
99
|
+
_diff() {
|
|
100
|
+
throw new Error("Not supported.");
|
|
101
|
+
}
|
|
102
|
+
async parse() {
|
|
103
|
+
throw new Error("Not implemented.");
|
|
104
|
+
}
|
|
105
|
+
async parseResult(generations) {
|
|
106
|
+
const result = await this.parsePartialResult(generations, false);
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
97
109
|
/**
|
|
98
110
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
99
111
|
* only the arguments of the function call are returned.
|
|
100
112
|
* @param generations The output of the LLM to parse.
|
|
101
113
|
* @returns A JSON object representation of the function call or its arguments.
|
|
102
114
|
*/
|
|
103
|
-
async
|
|
104
|
-
|
|
115
|
+
async parsePartialResult(generations, partial = true
|
|
116
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
117
|
+
) {
|
|
118
|
+
const message = generations[0].message;
|
|
119
|
+
let toolCalls;
|
|
120
|
+
if (isAIMessage(message) && message.tool_calls?.length) {
|
|
121
|
+
toolCalls = message.tool_calls.map((toolCall) => {
|
|
122
|
+
const { id, ...rest } = toolCall;
|
|
123
|
+
if (!this.returnId) {
|
|
124
|
+
return rest;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
id,
|
|
128
|
+
...rest,
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
else if (message.additional_kwargs.tool_calls !== undefined) {
|
|
133
|
+
const rawToolCalls = JSON.parse(JSON.stringify(message.additional_kwargs.tool_calls));
|
|
134
|
+
toolCalls = rawToolCalls.map((rawToolCall) => {
|
|
135
|
+
return parseToolCall(rawToolCall, { returnId: this.returnId, partial });
|
|
136
|
+
});
|
|
137
|
+
}
|
|
105
138
|
if (!toolCalls) {
|
|
106
|
-
|
|
139
|
+
return [];
|
|
107
140
|
}
|
|
108
|
-
const clonedToolCalls = JSON.parse(JSON.stringify(toolCalls));
|
|
109
141
|
const parsedToolCalls = [];
|
|
110
|
-
for (const toolCall of
|
|
111
|
-
|
|
112
|
-
if (parsedToolCall !== undefined) {
|
|
113
|
-
// backward-compatibility with previous
|
|
114
|
-
// versions of Langchain JS, which uses `name` and `arguments`
|
|
115
|
-
// @ts-expect-error name and arguemnts are defined by Object.defineProperty
|
|
142
|
+
for (const toolCall of toolCalls) {
|
|
143
|
+
if (toolCall !== undefined) {
|
|
116
144
|
const backwardsCompatibleToolCall = {
|
|
117
|
-
type:
|
|
118
|
-
args:
|
|
119
|
-
id:
|
|
145
|
+
type: toolCall.name,
|
|
146
|
+
args: toolCall.args,
|
|
147
|
+
id: toolCall.id,
|
|
120
148
|
};
|
|
121
|
-
Object.defineProperty(backwardsCompatibleToolCall, "name", {
|
|
122
|
-
get() {
|
|
123
|
-
return this.type;
|
|
124
|
-
},
|
|
125
|
-
});
|
|
126
|
-
Object.defineProperty(backwardsCompatibleToolCall, "arguments", {
|
|
127
|
-
get() {
|
|
128
|
-
return this.args;
|
|
129
|
-
},
|
|
130
|
-
});
|
|
131
149
|
parsedToolCalls.push(backwardsCompatibleToolCall);
|
|
132
150
|
}
|
|
133
151
|
}
|
|
@@ -138,7 +156,7 @@ export class JsonOutputToolsParser extends BaseLLMOutputParser {
|
|
|
138
156
|
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
139
157
|
* expecting only a single tool to be called.
|
|
140
158
|
*/
|
|
141
|
-
export class JsonOutputKeyToolsParser extends
|
|
159
|
+
export class JsonOutputKeyToolsParser extends JsonOutputToolsParser {
|
|
142
160
|
static lc_name() {
|
|
143
161
|
return "JsonOutputKeyToolsParser";
|
|
144
162
|
}
|
|
@@ -176,12 +194,6 @@ export class JsonOutputKeyToolsParser extends BaseLLMOutputParser {
|
|
|
176
194
|
writable: true,
|
|
177
195
|
value: false
|
|
178
196
|
});
|
|
179
|
-
Object.defineProperty(this, "initialParser", {
|
|
180
|
-
enumerable: true,
|
|
181
|
-
configurable: true,
|
|
182
|
-
writable: true,
|
|
183
|
-
value: void 0
|
|
184
|
-
});
|
|
185
197
|
Object.defineProperty(this, "zodSchema", {
|
|
186
198
|
enumerable: true,
|
|
187
199
|
configurable: true,
|
|
@@ -190,7 +202,6 @@ export class JsonOutputKeyToolsParser extends BaseLLMOutputParser {
|
|
|
190
202
|
});
|
|
191
203
|
this.keyName = params.keyName;
|
|
192
204
|
this.returnSingle = params.returnSingle ?? this.returnSingle;
|
|
193
|
-
this.initialParser = new JsonOutputToolsParser(params);
|
|
194
205
|
this.zodSchema = params.zodSchema;
|
|
195
206
|
}
|
|
196
207
|
async _validateResult(result) {
|
|
@@ -206,11 +217,31 @@ export class JsonOutputKeyToolsParser extends BaseLLMOutputParser {
|
|
|
206
217
|
}
|
|
207
218
|
}
|
|
208
219
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
220
|
+
async parsePartialResult(generations) {
|
|
221
|
+
const results = await super.parsePartialResult(generations);
|
|
222
|
+
const matchingResults = results.filter((result) => result.type === this.keyName);
|
|
223
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
224
|
+
let returnedValues = matchingResults;
|
|
225
|
+
if (!matchingResults.length) {
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
if (!this.returnId) {
|
|
229
|
+
returnedValues = matchingResults.map((result) => result.args);
|
|
230
|
+
}
|
|
231
|
+
if (this.returnSingle) {
|
|
232
|
+
return returnedValues[0];
|
|
233
|
+
}
|
|
234
|
+
return returnedValues;
|
|
235
|
+
}
|
|
236
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
209
237
|
async parseResult(generations) {
|
|
210
|
-
const results = await
|
|
238
|
+
const results = await super.parsePartialResult(generations, false);
|
|
211
239
|
const matchingResults = results.filter((result) => result.type === this.keyName);
|
|
212
240
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
213
241
|
let returnedValues = matchingResults;
|
|
242
|
+
if (!matchingResults.length) {
|
|
243
|
+
return undefined;
|
|
244
|
+
}
|
|
214
245
|
if (!this.returnId) {
|
|
215
246
|
returnedValues = matchingResults.map((result) => result.args);
|
|
216
247
|
}
|
|
@@ -30,4 +30,5 @@ export declare abstract class BaseCumulativeTransformOutputParser<T = unknown> e
|
|
|
30
30
|
protected abstract _diff(prev: any | undefined, next: any): any;
|
|
31
31
|
abstract parsePartialResult(generations: Generation[] | ChatGeneration[]): Promise<T | undefined>;
|
|
32
32
|
_transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
|
|
33
|
+
getFormatInstructions(): string;
|
|
33
34
|
}
|
package/dist/runnables/base.d.ts
CHANGED
|
@@ -182,39 +182,39 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
182
182
|
* **ATTENTION** This reference table is for the V2 version of the schema.
|
|
183
183
|
*
|
|
184
184
|
* ```md
|
|
185
|
-
*
|
|
186
|
-
* | event |
|
|
187
|
-
*
|
|
188
|
-
* | on_chat_model_start |
|
|
189
|
-
*
|
|
190
|
-
* | on_chat_model_stream |
|
|
191
|
-
*
|
|
192
|
-
* | on_chat_model_end |
|
|
193
|
-
*
|
|
194
|
-
* | on_llm_start |
|
|
195
|
-
*
|
|
196
|
-
* | on_llm_stream |
|
|
197
|
-
*
|
|
198
|
-
* | on_llm_end |
|
|
199
|
-
*
|
|
200
|
-
* | on_chain_start |
|
|
201
|
-
*
|
|
202
|
-
* | on_chain_stream |
|
|
203
|
-
*
|
|
204
|
-
* | on_chain_end |
|
|
205
|
-
*
|
|
206
|
-
* | on_tool_start |
|
|
207
|
-
*
|
|
208
|
-
* | on_tool_end |
|
|
209
|
-
*
|
|
210
|
-
* | on_retriever_start |
|
|
211
|
-
*
|
|
212
|
-
* | on_retriever_end |
|
|
213
|
-
*
|
|
214
|
-
* | on_prompt_start |
|
|
215
|
-
*
|
|
216
|
-
* | on_prompt_end |
|
|
217
|
-
*
|
|
185
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
186
|
+
* | event | input | output/chunk |
|
|
187
|
+
* +======================+=============================+==========================================+
|
|
188
|
+
* | on_chat_model_start | {"messages": BaseMessage[]} | |
|
|
189
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
190
|
+
* | on_chat_model_stream | | AIMessageChunk("hello") |
|
|
191
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
192
|
+
* | on_chat_model_end | {"messages": BaseMessage[]} | AIMessageChunk("hello world") |
|
|
193
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
194
|
+
* | on_llm_start | {'input': 'hello'} | |
|
|
195
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
196
|
+
* | on_llm_stream | | 'Hello' |
|
|
197
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
198
|
+
* | on_llm_end | 'Hello human!' | |
|
|
199
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
200
|
+
* | on_chain_start | | |
|
|
201
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
202
|
+
* | on_chain_stream | | "hello world!" |
|
|
203
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
204
|
+
* | on_chain_end | [Document(...)] | "hello world!, goodbye world!" |
|
|
205
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
206
|
+
* | on_tool_start | {"x": 1, "y": "2"} | |
|
|
207
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
208
|
+
* | on_tool_end | | {"x": 1, "y": "2"} |
|
|
209
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
210
|
+
* | on_retriever_start | {"query": "hello"} | |
|
|
211
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
212
|
+
* | on_retriever_end | {"query": "hello"} | [Document(...), ..] |
|
|
213
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
214
|
+
* | on_prompt_start | {"question": "hello"} | |
|
|
215
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
216
|
+
* | on_prompt_end | {"question": "hello"} | ChatPromptValue(messages: BaseMessage[]) |
|
|
217
|
+
* +----------------------+-----------------------------+------------------------------------------+
|
|
218
218
|
* ```
|
|
219
219
|
*
|
|
220
220
|
* The "on_chain_*" events are the default for Runnables that don't fit one of the above categories.
|
|
@@ -226,13 +226,13 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
226
226
|
* A custom event has following format:
|
|
227
227
|
*
|
|
228
228
|
* ```md
|
|
229
|
-
*
|
|
230
|
-
* | Attribute | Type | Description
|
|
231
|
-
*
|
|
232
|
-
* | name | str | A user defined name for the event.
|
|
233
|
-
*
|
|
234
|
-
* | data | Any | The data associated with the event. This can be anything
|
|
235
|
-
*
|
|
229
|
+
* +-----------+------+------------------------------------------------------------+
|
|
230
|
+
* | Attribute | Type | Description |
|
|
231
|
+
* +===========+======+============================================================+
|
|
232
|
+
* | name | str | A user defined name for the event. |
|
|
233
|
+
* +-----------+------+------------------------------------------------------------+
|
|
234
|
+
* | data | Any | The data associated with the event. This can be anything. |
|
|
235
|
+
* +-----------+------+------------------------------------------------------------+
|
|
236
236
|
* ```
|
|
237
237
|
*
|
|
238
238
|
* Here's an example:
|
package/dist/utils/stream.cjs
CHANGED
|
@@ -68,6 +68,11 @@ class IterableReadableStream extends ReadableStream {
|
|
|
68
68
|
[Symbol.asyncIterator]() {
|
|
69
69
|
return this;
|
|
70
70
|
}
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
72
|
+
// @ts-ignore Not present in Node 18 types, required in latest Node 22
|
|
73
|
+
async [Symbol.asyncDispose]() {
|
|
74
|
+
await this.return();
|
|
75
|
+
}
|
|
71
76
|
static fromReadableStream(stream) {
|
|
72
77
|
// From https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams#reading_the_stream
|
|
73
78
|
const reader = stream.getReader();
|
|
@@ -249,6 +254,11 @@ class AsyncGeneratorWithSetup {
|
|
|
249
254
|
[Symbol.asyncIterator]() {
|
|
250
255
|
return this;
|
|
251
256
|
}
|
|
257
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
258
|
+
// @ts-ignore Not present in Node 18 types, required in latest Node 22
|
|
259
|
+
async [Symbol.asyncDispose]() {
|
|
260
|
+
await this.return();
|
|
261
|
+
}
|
|
252
262
|
}
|
|
253
263
|
exports.AsyncGeneratorWithSetup = AsyncGeneratorWithSetup;
|
|
254
264
|
async function pipeGeneratorWithSetup(to, generator, startSetup, signal, ...args) {
|
package/dist/utils/stream.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export declare class IterableReadableStream<T> extends ReadableStream<T> impleme
|
|
|
6
6
|
return(): Promise<IteratorResult<T>>;
|
|
7
7
|
throw(e: any): Promise<IteratorResult<T>>;
|
|
8
8
|
[Symbol.asyncIterator](): this;
|
|
9
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
9
10
|
static fromReadableStream<T>(stream: ReadableStream<T>): IterableReadableStream<T>;
|
|
10
11
|
static fromAsyncGenerator<T>(generator: AsyncGenerator<T>): IterableReadableStream<T>;
|
|
11
12
|
}
|
|
@@ -25,9 +26,10 @@ export declare class AsyncGeneratorWithSetup<S = unknown, T = unknown, TReturn =
|
|
|
25
26
|
signal?: AbortSignal;
|
|
26
27
|
});
|
|
27
28
|
next(...args: [] | [TNext]): Promise<IteratorResult<T>>;
|
|
28
|
-
return(value
|
|
29
|
+
return(value?: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T>>;
|
|
29
30
|
throw(e: Error): Promise<IteratorResult<T>>;
|
|
30
31
|
[Symbol.asyncIterator](): this;
|
|
32
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
31
33
|
}
|
|
32
34
|
export declare function pipeGeneratorWithSetup<S, A extends unknown[], T, TReturn, TNext, U, UReturn, UNext>(to: (g: AsyncGenerator<T, TReturn, TNext>, s: S, ...args: A) => AsyncGenerator<U, UReturn, UNext>, generator: AsyncGenerator<T, TReturn, TNext>, startSetup: () => Promise<S>, signal: AbortSignal | undefined, ...args: A): Promise<{
|
|
33
35
|
output: AsyncGenerator<U, UReturn, UNext>;
|
package/dist/utils/stream.js
CHANGED
|
@@ -65,6 +65,11 @@ export class IterableReadableStream extends ReadableStream {
|
|
|
65
65
|
[Symbol.asyncIterator]() {
|
|
66
66
|
return this;
|
|
67
67
|
}
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
69
|
+
// @ts-ignore Not present in Node 18 types, required in latest Node 22
|
|
70
|
+
async [Symbol.asyncDispose]() {
|
|
71
|
+
await this.return();
|
|
72
|
+
}
|
|
68
73
|
static fromReadableStream(stream) {
|
|
69
74
|
// From https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams#reading_the_stream
|
|
70
75
|
const reader = stream.getReader();
|
|
@@ -243,6 +248,11 @@ export class AsyncGeneratorWithSetup {
|
|
|
243
248
|
[Symbol.asyncIterator]() {
|
|
244
249
|
return this;
|
|
245
250
|
}
|
|
251
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
252
|
+
// @ts-ignore Not present in Node 18 types, required in latest Node 22
|
|
253
|
+
async [Symbol.asyncDispose]() {
|
|
254
|
+
await this.return();
|
|
255
|
+
}
|
|
246
256
|
}
|
|
247
257
|
export async function pipeGeneratorWithSetup(to, generator, startSetup, signal, ...args) {
|
|
248
258
|
const gen = new AsyncGeneratorWithSetup({
|