@langchain/core 0.1.55 → 0.1.56
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/agents.d.ts +1 -1
- package/dist/caches.d.ts +1 -1
- package/dist/language_models/chat_models.d.ts +14 -3
- package/dist/messages/ai.cjs +213 -0
- package/dist/messages/ai.d.ts +37 -0
- package/dist/messages/ai.js +207 -0
- package/dist/messages/base.cjs +212 -0
- package/dist/messages/base.d.ts +137 -0
- package/dist/messages/base.js +201 -0
- package/dist/messages/chat.cjs +71 -0
- package/dist/messages/chat.d.ts +28 -0
- package/dist/messages/chat.js +66 -0
- package/dist/messages/function.cjs +46 -0
- package/dist/messages/function.d.ts +24 -0
- package/dist/messages/function.js +41 -0
- package/dist/messages/human.cjs +36 -0
- package/dist/messages/human.d.ts +17 -0
- package/dist/messages/human.js +31 -0
- package/dist/messages/index.cjs +27 -633
- package/dist/messages/index.d.ts +8 -273
- package/dist/messages/index.js +10 -611
- package/dist/messages/system.cjs +36 -0
- package/dist/messages/system.d.ts +17 -0
- package/dist/messages/system.js +31 -0
- package/dist/messages/tool.cjs +101 -0
- package/dist/messages/tool.d.ts +101 -0
- package/dist/messages/tool.js +95 -0
- package/dist/messages/utils.cjs +172 -0
- package/dist/messages/utils.d.ts +31 -0
- package/dist/messages/utils.js +163 -0
- package/dist/output_parsers/json.cjs +6 -93
- package/dist/output_parsers/json.d.ts +2 -2
- package/dist/output_parsers/json.js +2 -88
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +79 -13
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +18 -0
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +75 -12
- package/dist/output_parsers/transform.cjs +7 -6
- package/dist/output_parsers/transform.d.ts +1 -1
- package/dist/output_parsers/transform.js +3 -2
- package/dist/utils/function_calling.cjs +18 -6
- package/dist/utils/function_calling.d.ts +2 -1
- package/dist/utils/function_calling.js +16 -5
- package/dist/utils/json.cjs +93 -0
- package/dist/utils/json.d.ts +2 -0
- package/dist/utils/json.js +88 -0
- package/dist/utils/testing/index.cjs +3 -0
- package/dist/utils/testing/index.js +3 -0
- package/messages/tool.cjs +1 -0
- package/messages/tool.d.cts +1 -0
- package/messages/tool.d.ts +1 -0
- package/messages/tool.js +1 -0
- package/package.json +14 -1
package/dist/agents.d.ts
CHANGED
package/dist/caches.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ import { type StoredGeneration } from "./messages/index.js";
|
|
|
13
13
|
export declare const getCacheKey: (...strings: string[]) => string;
|
|
14
14
|
export declare function deserializeStoredGeneration(storedGeneration: StoredGeneration): {
|
|
15
15
|
text: string;
|
|
16
|
-
message: import("./messages/
|
|
16
|
+
message: import("./messages/tool.js").ToolMessage | import("./messages/ai.js").AIMessage | import("./messages/chat.js").ChatMessage | import("./messages/function.js").FunctionMessage | import("./messages/human.js").HumanMessage | import("./messages/system.js").SystemMessage;
|
|
17
17
|
} | {
|
|
18
18
|
text: string;
|
|
19
19
|
message?: undefined;
|
|
@@ -5,6 +5,8 @@ import { BaseLanguageModel, type BaseLanguageModelCallOptions, type BaseLanguage
|
|
|
5
5
|
import { type CallbackManagerForLLMRun, type Callbacks } from "../callbacks/manager.js";
|
|
6
6
|
import type { RunnableConfig } from "../runnables/config.js";
|
|
7
7
|
import type { BaseCache } from "../caches.js";
|
|
8
|
+
import { StructuredToolInterface } from "../tools.js";
|
|
9
|
+
import { RunnableInterface } from "../runnables/types.js";
|
|
8
10
|
/**
|
|
9
11
|
* Represents a serialized chat model.
|
|
10
12
|
*/
|
|
@@ -44,21 +46,30 @@ interface ChatModelGenerateCachedParameters<T extends BaseChatModel<CallOptions>
|
|
|
44
46
|
* Base class for chat models. It extends the BaseLanguageModel class and
|
|
45
47
|
* provides methods for generating chat based on input messages.
|
|
46
48
|
*/
|
|
47
|
-
export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions> extends BaseLanguageModel<
|
|
49
|
+
export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions, OutputMessageType extends BaseMessageChunk = BaseMessageChunk> extends BaseLanguageModel<OutputMessageType, CallOptions> {
|
|
48
50
|
ParsedCallOptions: Omit<CallOptions, keyof RunnableConfig & "timeout">;
|
|
49
51
|
lc_namespace: string[];
|
|
50
52
|
constructor(fields: BaseChatModelParams);
|
|
51
53
|
_combineLLMOutput?(...llmOutputs: LLMResult["llmOutput"][]): LLMResult["llmOutput"];
|
|
52
54
|
protected _separateRunnableConfigFromCallOptions(options?: Partial<CallOptions>): [RunnableConfig, this["ParsedCallOptions"]];
|
|
55
|
+
/**
|
|
56
|
+
* Bind tool-like objects to this chat model.
|
|
57
|
+
*
|
|
58
|
+
* @param tools A list of tool definitions to bind to this chat model.
|
|
59
|
+
* Can be a structured tool or an object matching the provider's
|
|
60
|
+
* specific tool schema.
|
|
61
|
+
* @param kwargs Any additional parameters to bind.
|
|
62
|
+
*/
|
|
63
|
+
bindTools?(tools: (StructuredToolInterface | Record<string, unknown>)[], kwargs?: Partial<CallOptions>): RunnableInterface<BaseLanguageModelInput, OutputMessageType, CallOptions>;
|
|
53
64
|
/**
|
|
54
65
|
* Invokes the chat model with a single input.
|
|
55
66
|
* @param input The input for the language model.
|
|
56
67
|
* @param options The call options.
|
|
57
68
|
* @returns A Promise that resolves to a BaseMessageChunk.
|
|
58
69
|
*/
|
|
59
|
-
invoke(input: BaseLanguageModelInput, options?: CallOptions): Promise<
|
|
70
|
+
invoke(input: BaseLanguageModelInput, options?: CallOptions): Promise<OutputMessageType>;
|
|
60
71
|
_streamResponseChunks(_messages: BaseMessage[], _options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
61
|
-
_streamIterator(input: BaseLanguageModelInput, options?: CallOptions): AsyncGenerator<
|
|
72
|
+
_streamIterator(input: BaseLanguageModelInput, options?: CallOptions): AsyncGenerator<OutputMessageType>;
|
|
62
73
|
/** @ignore */
|
|
63
74
|
_generateUncached(messages: BaseMessageLike[][], parsedOptions: this["ParsedCallOptions"], handledOptions: RunnableConfig): Promise<LLMResult>;
|
|
64
75
|
_generateCached({ messages, cache, llmStringKey, parsedOptions, handledOptions, }: ChatModelGenerateCachedParameters<typeof this>): Promise<LLMResult & {
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AIMessageChunk = exports.isAIMessage = exports.AIMessage = void 0;
|
|
4
|
+
const json_js_1 = require("../utils/json.cjs");
|
|
5
|
+
const base_js_1 = require("./base.cjs");
|
|
6
|
+
const tool_js_1 = require("./tool.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* Represents an AI message in a conversation.
|
|
9
|
+
*/
|
|
10
|
+
class AIMessage extends base_js_1.BaseMessage {
|
|
11
|
+
get lc_aliases() {
|
|
12
|
+
// exclude snake case conversion to pascal case
|
|
13
|
+
return {
|
|
14
|
+
...super.lc_aliases,
|
|
15
|
+
tool_calls: "tool_calls",
|
|
16
|
+
invalid_tool_calls: "invalid_tool_calls",
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
constructor(fields,
|
|
20
|
+
/** @deprecated */
|
|
21
|
+
kwargs) {
|
|
22
|
+
let initParams;
|
|
23
|
+
if (typeof fields === "string") {
|
|
24
|
+
initParams = {
|
|
25
|
+
content: fields,
|
|
26
|
+
tool_calls: [],
|
|
27
|
+
invalid_tool_calls: [],
|
|
28
|
+
additional_kwargs: kwargs ?? {},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
initParams = fields;
|
|
33
|
+
const rawToolCalls = initParams.additional_kwargs?.tool_calls;
|
|
34
|
+
const toolCalls = initParams.tool_calls;
|
|
35
|
+
if (rawToolCalls !== undefined &&
|
|
36
|
+
rawToolCalls.length > 0 &&
|
|
37
|
+
(toolCalls === undefined || toolCalls.length === 0)) {
|
|
38
|
+
console.warn([
|
|
39
|
+
"New LangChain packages are available that more efficiently handle",
|
|
40
|
+
"tool calling.\n\nPlease upgrade your packages to versions that set",
|
|
41
|
+
"message tool calls. e.g., `yarn add @langchain/anthropic`,",
|
|
42
|
+
"yarn add @langchain/openai`, etc.",
|
|
43
|
+
].join(" "));
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
if (rawToolCalls !== undefined && toolCalls === undefined) {
|
|
47
|
+
const [toolCalls, invalidToolCalls] = (0, tool_js_1.defaultToolCallParser)(rawToolCalls);
|
|
48
|
+
initParams.tool_calls = toolCalls ?? [];
|
|
49
|
+
initParams.invalid_tool_calls = invalidToolCalls ?? [];
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
initParams.tool_calls = initParams.tool_calls ?? [];
|
|
53
|
+
initParams.invalid_tool_calls = initParams.invalid_tool_calls ?? [];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
// Do nothing if parsing fails
|
|
58
|
+
initParams.tool_calls = [];
|
|
59
|
+
initParams.invalid_tool_calls = [];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Sadly, TypeScript only allows super() calls at root if the class has
|
|
63
|
+
// properties with initializers, so we have to check types twice.
|
|
64
|
+
super(initParams);
|
|
65
|
+
// These are typed as optional to avoid breaking changes and allow for casting
|
|
66
|
+
// from BaseMessage.
|
|
67
|
+
Object.defineProperty(this, "tool_calls", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: []
|
|
72
|
+
});
|
|
73
|
+
Object.defineProperty(this, "invalid_tool_calls", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: []
|
|
78
|
+
});
|
|
79
|
+
if (typeof initParams !== "string") {
|
|
80
|
+
this.tool_calls = initParams.tool_calls ?? this.tool_calls;
|
|
81
|
+
this.invalid_tool_calls =
|
|
82
|
+
initParams.invalid_tool_calls ?? this.invalid_tool_calls;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
static lc_name() {
|
|
86
|
+
return "AIMessage";
|
|
87
|
+
}
|
|
88
|
+
_getType() {
|
|
89
|
+
return "ai";
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.AIMessage = AIMessage;
|
|
93
|
+
function isAIMessage(x) {
|
|
94
|
+
return x._getType() === "ai";
|
|
95
|
+
}
|
|
96
|
+
exports.isAIMessage = isAIMessage;
|
|
97
|
+
/**
|
|
98
|
+
* Represents a chunk of an AI message, which can be concatenated with
|
|
99
|
+
* other AI message chunks.
|
|
100
|
+
*/
|
|
101
|
+
class AIMessageChunk extends base_js_1.BaseMessageChunk {
|
|
102
|
+
constructor(fields) {
|
|
103
|
+
let initParams;
|
|
104
|
+
if (typeof fields === "string") {
|
|
105
|
+
initParams = {
|
|
106
|
+
content: fields,
|
|
107
|
+
tool_calls: [],
|
|
108
|
+
invalid_tool_calls: [],
|
|
109
|
+
tool_call_chunks: [],
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
else if (fields.tool_call_chunks === undefined) {
|
|
113
|
+
initParams = {
|
|
114
|
+
...fields,
|
|
115
|
+
tool_calls: [],
|
|
116
|
+
invalid_tool_calls: [],
|
|
117
|
+
tool_call_chunks: [],
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const toolCalls = [];
|
|
122
|
+
const invalidToolCalls = [];
|
|
123
|
+
for (const toolCallChunk of fields.tool_call_chunks) {
|
|
124
|
+
let parsedArgs = {};
|
|
125
|
+
try {
|
|
126
|
+
parsedArgs = (0, json_js_1.parsePartialJson)(toolCallChunk.args ?? "{}") ?? {};
|
|
127
|
+
if (typeof parsedArgs !== "object" || Array.isArray(parsedArgs)) {
|
|
128
|
+
throw new Error("Malformed tool call chunk args.");
|
|
129
|
+
}
|
|
130
|
+
toolCalls.push({
|
|
131
|
+
name: toolCallChunk.name ?? "",
|
|
132
|
+
args: parsedArgs,
|
|
133
|
+
id: toolCallChunk.id,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
catch (e) {
|
|
137
|
+
invalidToolCalls.push({
|
|
138
|
+
name: toolCallChunk.name,
|
|
139
|
+
args: toolCallChunk.args,
|
|
140
|
+
id: toolCallChunk.id,
|
|
141
|
+
error: "Malformed args.",
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
initParams = {
|
|
146
|
+
...fields,
|
|
147
|
+
tool_calls: toolCalls,
|
|
148
|
+
invalid_tool_calls: invalidToolCalls,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
// Sadly, TypeScript only allows super() calls at root if the class has
|
|
152
|
+
// properties with initializers, so we have to check types twice.
|
|
153
|
+
super(initParams);
|
|
154
|
+
// Must redeclare tool call fields since there is no multiple inheritance in JS.
|
|
155
|
+
// These are typed as optional to avoid breaking changes and allow for casting
|
|
156
|
+
// from BaseMessage.
|
|
157
|
+
Object.defineProperty(this, "tool_calls", {
|
|
158
|
+
enumerable: true,
|
|
159
|
+
configurable: true,
|
|
160
|
+
writable: true,
|
|
161
|
+
value: []
|
|
162
|
+
});
|
|
163
|
+
Object.defineProperty(this, "invalid_tool_calls", {
|
|
164
|
+
enumerable: true,
|
|
165
|
+
configurable: true,
|
|
166
|
+
writable: true,
|
|
167
|
+
value: []
|
|
168
|
+
});
|
|
169
|
+
Object.defineProperty(this, "tool_call_chunks", {
|
|
170
|
+
enumerable: true,
|
|
171
|
+
configurable: true,
|
|
172
|
+
writable: true,
|
|
173
|
+
value: []
|
|
174
|
+
});
|
|
175
|
+
this.tool_call_chunks =
|
|
176
|
+
initParams?.tool_call_chunks ?? this.tool_call_chunks;
|
|
177
|
+
this.tool_calls = initParams?.tool_calls ?? this.tool_calls;
|
|
178
|
+
this.invalid_tool_calls =
|
|
179
|
+
initParams?.invalid_tool_calls ?? this.invalid_tool_calls;
|
|
180
|
+
}
|
|
181
|
+
get lc_aliases() {
|
|
182
|
+
// exclude snake case conversion to pascal case
|
|
183
|
+
return {
|
|
184
|
+
...super.lc_aliases,
|
|
185
|
+
tool_calls: "tool_calls",
|
|
186
|
+
invalid_tool_calls: "invalid_tool_calls",
|
|
187
|
+
tool_call_chunks: "tool_call_chunks",
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
static lc_name() {
|
|
191
|
+
return "AIMessageChunk";
|
|
192
|
+
}
|
|
193
|
+
_getType() {
|
|
194
|
+
return "ai";
|
|
195
|
+
}
|
|
196
|
+
concat(chunk) {
|
|
197
|
+
const combinedFields = {
|
|
198
|
+
content: (0, base_js_1.mergeContent)(this.content, chunk.content),
|
|
199
|
+
additional_kwargs: (0, base_js_1._mergeDicts)(this.additional_kwargs, chunk.additional_kwargs),
|
|
200
|
+
response_metadata: (0, base_js_1._mergeDicts)(this.response_metadata, chunk.response_metadata),
|
|
201
|
+
tool_call_chunks: [],
|
|
202
|
+
};
|
|
203
|
+
if (this.tool_call_chunks !== undefined ||
|
|
204
|
+
chunk.tool_call_chunks !== undefined) {
|
|
205
|
+
const rawToolCalls = (0, base_js_1._mergeLists)(this.tool_call_chunks, chunk.tool_call_chunks);
|
|
206
|
+
if (rawToolCalls !== undefined && rawToolCalls.length > 0) {
|
|
207
|
+
combinedFields.tool_call_chunks = rawToolCalls;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return new AIMessageChunk(combinedFields);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
exports.AIMessageChunk = AIMessageChunk;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { BaseMessage, BaseMessageChunk, type MessageType, BaseMessageFields } from "./base.js";
|
|
2
|
+
import { InvalidToolCall, ToolCall, ToolCallChunk } from "./tool.js";
|
|
3
|
+
export type AIMessageFields = BaseMessageFields & {
|
|
4
|
+
tool_calls?: ToolCall[];
|
|
5
|
+
invalid_tool_calls?: InvalidToolCall[];
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Represents an AI message in a conversation.
|
|
9
|
+
*/
|
|
10
|
+
export declare class AIMessage extends BaseMessage {
|
|
11
|
+
tool_calls?: ToolCall[];
|
|
12
|
+
invalid_tool_calls?: InvalidToolCall[];
|
|
13
|
+
get lc_aliases(): Record<string, string>;
|
|
14
|
+
constructor(fields: string | AIMessageFields,
|
|
15
|
+
/** @deprecated */
|
|
16
|
+
kwargs?: Record<string, unknown>);
|
|
17
|
+
static lc_name(): string;
|
|
18
|
+
_getType(): MessageType;
|
|
19
|
+
}
|
|
20
|
+
export declare function isAIMessage(x: BaseMessage): x is AIMessage;
|
|
21
|
+
export type AIMessageChunkFields = AIMessageFields & {
|
|
22
|
+
tool_call_chunks?: ToolCallChunk[];
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Represents a chunk of an AI message, which can be concatenated with
|
|
26
|
+
* other AI message chunks.
|
|
27
|
+
*/
|
|
28
|
+
export declare class AIMessageChunk extends BaseMessageChunk {
|
|
29
|
+
tool_calls?: ToolCall[];
|
|
30
|
+
invalid_tool_calls?: InvalidToolCall[];
|
|
31
|
+
tool_call_chunks?: ToolCallChunk[];
|
|
32
|
+
constructor(fields: string | AIMessageChunkFields);
|
|
33
|
+
get lc_aliases(): Record<string, string>;
|
|
34
|
+
static lc_name(): string;
|
|
35
|
+
_getType(): MessageType;
|
|
36
|
+
concat(chunk: AIMessageChunk): AIMessageChunk;
|
|
37
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { parsePartialJson } from "../utils/json.js";
|
|
2
|
+
import { BaseMessage, BaseMessageChunk, mergeContent, _mergeDicts, _mergeLists, } from "./base.js";
|
|
3
|
+
import { defaultToolCallParser, } from "./tool.js";
|
|
4
|
+
/**
|
|
5
|
+
* Represents an AI message in a conversation.
|
|
6
|
+
*/
|
|
7
|
+
export class AIMessage extends BaseMessage {
|
|
8
|
+
get lc_aliases() {
|
|
9
|
+
// exclude snake case conversion to pascal case
|
|
10
|
+
return {
|
|
11
|
+
...super.lc_aliases,
|
|
12
|
+
tool_calls: "tool_calls",
|
|
13
|
+
invalid_tool_calls: "invalid_tool_calls",
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
constructor(fields,
|
|
17
|
+
/** @deprecated */
|
|
18
|
+
kwargs) {
|
|
19
|
+
let initParams;
|
|
20
|
+
if (typeof fields === "string") {
|
|
21
|
+
initParams = {
|
|
22
|
+
content: fields,
|
|
23
|
+
tool_calls: [],
|
|
24
|
+
invalid_tool_calls: [],
|
|
25
|
+
additional_kwargs: kwargs ?? {},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
initParams = fields;
|
|
30
|
+
const rawToolCalls = initParams.additional_kwargs?.tool_calls;
|
|
31
|
+
const toolCalls = initParams.tool_calls;
|
|
32
|
+
if (rawToolCalls !== undefined &&
|
|
33
|
+
rawToolCalls.length > 0 &&
|
|
34
|
+
(toolCalls === undefined || toolCalls.length === 0)) {
|
|
35
|
+
console.warn([
|
|
36
|
+
"New LangChain packages are available that more efficiently handle",
|
|
37
|
+
"tool calling.\n\nPlease upgrade your packages to versions that set",
|
|
38
|
+
"message tool calls. e.g., `yarn add @langchain/anthropic`,",
|
|
39
|
+
"yarn add @langchain/openai`, etc.",
|
|
40
|
+
].join(" "));
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
if (rawToolCalls !== undefined && toolCalls === undefined) {
|
|
44
|
+
const [toolCalls, invalidToolCalls] = defaultToolCallParser(rawToolCalls);
|
|
45
|
+
initParams.tool_calls = toolCalls ?? [];
|
|
46
|
+
initParams.invalid_tool_calls = invalidToolCalls ?? [];
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
initParams.tool_calls = initParams.tool_calls ?? [];
|
|
50
|
+
initParams.invalid_tool_calls = initParams.invalid_tool_calls ?? [];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
// Do nothing if parsing fails
|
|
55
|
+
initParams.tool_calls = [];
|
|
56
|
+
initParams.invalid_tool_calls = [];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Sadly, TypeScript only allows super() calls at root if the class has
|
|
60
|
+
// properties with initializers, so we have to check types twice.
|
|
61
|
+
super(initParams);
|
|
62
|
+
// These are typed as optional to avoid breaking changes and allow for casting
|
|
63
|
+
// from BaseMessage.
|
|
64
|
+
Object.defineProperty(this, "tool_calls", {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
configurable: true,
|
|
67
|
+
writable: true,
|
|
68
|
+
value: []
|
|
69
|
+
});
|
|
70
|
+
Object.defineProperty(this, "invalid_tool_calls", {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
configurable: true,
|
|
73
|
+
writable: true,
|
|
74
|
+
value: []
|
|
75
|
+
});
|
|
76
|
+
if (typeof initParams !== "string") {
|
|
77
|
+
this.tool_calls = initParams.tool_calls ?? this.tool_calls;
|
|
78
|
+
this.invalid_tool_calls =
|
|
79
|
+
initParams.invalid_tool_calls ?? this.invalid_tool_calls;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
static lc_name() {
|
|
83
|
+
return "AIMessage";
|
|
84
|
+
}
|
|
85
|
+
_getType() {
|
|
86
|
+
return "ai";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export function isAIMessage(x) {
|
|
90
|
+
return x._getType() === "ai";
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Represents a chunk of an AI message, which can be concatenated with
|
|
94
|
+
* other AI message chunks.
|
|
95
|
+
*/
|
|
96
|
+
export class AIMessageChunk extends BaseMessageChunk {
|
|
97
|
+
constructor(fields) {
|
|
98
|
+
let initParams;
|
|
99
|
+
if (typeof fields === "string") {
|
|
100
|
+
initParams = {
|
|
101
|
+
content: fields,
|
|
102
|
+
tool_calls: [],
|
|
103
|
+
invalid_tool_calls: [],
|
|
104
|
+
tool_call_chunks: [],
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
else if (fields.tool_call_chunks === undefined) {
|
|
108
|
+
initParams = {
|
|
109
|
+
...fields,
|
|
110
|
+
tool_calls: [],
|
|
111
|
+
invalid_tool_calls: [],
|
|
112
|
+
tool_call_chunks: [],
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const toolCalls = [];
|
|
117
|
+
const invalidToolCalls = [];
|
|
118
|
+
for (const toolCallChunk of fields.tool_call_chunks) {
|
|
119
|
+
let parsedArgs = {};
|
|
120
|
+
try {
|
|
121
|
+
parsedArgs = parsePartialJson(toolCallChunk.args ?? "{}") ?? {};
|
|
122
|
+
if (typeof parsedArgs !== "object" || Array.isArray(parsedArgs)) {
|
|
123
|
+
throw new Error("Malformed tool call chunk args.");
|
|
124
|
+
}
|
|
125
|
+
toolCalls.push({
|
|
126
|
+
name: toolCallChunk.name ?? "",
|
|
127
|
+
args: parsedArgs,
|
|
128
|
+
id: toolCallChunk.id,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
invalidToolCalls.push({
|
|
133
|
+
name: toolCallChunk.name,
|
|
134
|
+
args: toolCallChunk.args,
|
|
135
|
+
id: toolCallChunk.id,
|
|
136
|
+
error: "Malformed args.",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
initParams = {
|
|
141
|
+
...fields,
|
|
142
|
+
tool_calls: toolCalls,
|
|
143
|
+
invalid_tool_calls: invalidToolCalls,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
// Sadly, TypeScript only allows super() calls at root if the class has
|
|
147
|
+
// properties with initializers, so we have to check types twice.
|
|
148
|
+
super(initParams);
|
|
149
|
+
// Must redeclare tool call fields since there is no multiple inheritance in JS.
|
|
150
|
+
// These are typed as optional to avoid breaking changes and allow for casting
|
|
151
|
+
// from BaseMessage.
|
|
152
|
+
Object.defineProperty(this, "tool_calls", {
|
|
153
|
+
enumerable: true,
|
|
154
|
+
configurable: true,
|
|
155
|
+
writable: true,
|
|
156
|
+
value: []
|
|
157
|
+
});
|
|
158
|
+
Object.defineProperty(this, "invalid_tool_calls", {
|
|
159
|
+
enumerable: true,
|
|
160
|
+
configurable: true,
|
|
161
|
+
writable: true,
|
|
162
|
+
value: []
|
|
163
|
+
});
|
|
164
|
+
Object.defineProperty(this, "tool_call_chunks", {
|
|
165
|
+
enumerable: true,
|
|
166
|
+
configurable: true,
|
|
167
|
+
writable: true,
|
|
168
|
+
value: []
|
|
169
|
+
});
|
|
170
|
+
this.tool_call_chunks =
|
|
171
|
+
initParams?.tool_call_chunks ?? this.tool_call_chunks;
|
|
172
|
+
this.tool_calls = initParams?.tool_calls ?? this.tool_calls;
|
|
173
|
+
this.invalid_tool_calls =
|
|
174
|
+
initParams?.invalid_tool_calls ?? this.invalid_tool_calls;
|
|
175
|
+
}
|
|
176
|
+
get lc_aliases() {
|
|
177
|
+
// exclude snake case conversion to pascal case
|
|
178
|
+
return {
|
|
179
|
+
...super.lc_aliases,
|
|
180
|
+
tool_calls: "tool_calls",
|
|
181
|
+
invalid_tool_calls: "invalid_tool_calls",
|
|
182
|
+
tool_call_chunks: "tool_call_chunks",
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
static lc_name() {
|
|
186
|
+
return "AIMessageChunk";
|
|
187
|
+
}
|
|
188
|
+
_getType() {
|
|
189
|
+
return "ai";
|
|
190
|
+
}
|
|
191
|
+
concat(chunk) {
|
|
192
|
+
const combinedFields = {
|
|
193
|
+
content: mergeContent(this.content, chunk.content),
|
|
194
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
195
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
196
|
+
tool_call_chunks: [],
|
|
197
|
+
};
|
|
198
|
+
if (this.tool_call_chunks !== undefined ||
|
|
199
|
+
chunk.tool_call_chunks !== undefined) {
|
|
200
|
+
const rawToolCalls = _mergeLists(this.tool_call_chunks, chunk.tool_call_chunks);
|
|
201
|
+
if (rawToolCalls !== undefined && rawToolCalls.length > 0) {
|
|
202
|
+
combinedFields.tool_call_chunks = rawToolCalls;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return new AIMessageChunk(combinedFields);
|
|
206
|
+
}
|
|
207
|
+
}
|