@langchain/core 0.1.45 → 0.1.46
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.
|
@@ -80,6 +80,8 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
80
80
|
let generationChunk;
|
|
81
81
|
try {
|
|
82
82
|
for await (const chunk of this._streamResponseChunks(messages, callOptions, runManagers?.[0])) {
|
|
83
|
+
chunk.message.response_metadata =
|
|
84
|
+
_combineGenerationInfoAndMetadata(chunk);
|
|
83
85
|
yield chunk.message;
|
|
84
86
|
if (!generationChunk) {
|
|
85
87
|
generationChunk = chunk;
|
|
@@ -118,6 +120,10 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
118
120
|
await Promise.all(results.map(async (pResult, i) => {
|
|
119
121
|
if (pResult.status === "fulfilled") {
|
|
120
122
|
const result = pResult.value;
|
|
123
|
+
for (const generation of result.generations) {
|
|
124
|
+
generation.message.response_metadata =
|
|
125
|
+
_combineGenerationInfoAndMetadata(generation);
|
|
126
|
+
}
|
|
121
127
|
generations[i] = result.generations;
|
|
122
128
|
llmOutputs[i] = result.llmOutput;
|
|
123
129
|
return runManagers?.[i]?.handleLLMEnd({
|
|
@@ -365,3 +371,9 @@ class SimpleChatModel extends BaseChatModel {
|
|
|
365
371
|
}
|
|
366
372
|
}
|
|
367
373
|
exports.SimpleChatModel = SimpleChatModel;
|
|
374
|
+
function _combineGenerationInfoAndMetadata(generation) {
|
|
375
|
+
return {
|
|
376
|
+
...generation.generationInfo,
|
|
377
|
+
...generation.message.response_metadata,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
@@ -76,6 +76,8 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
76
76
|
let generationChunk;
|
|
77
77
|
try {
|
|
78
78
|
for await (const chunk of this._streamResponseChunks(messages, callOptions, runManagers?.[0])) {
|
|
79
|
+
chunk.message.response_metadata =
|
|
80
|
+
_combineGenerationInfoAndMetadata(chunk);
|
|
79
81
|
yield chunk.message;
|
|
80
82
|
if (!generationChunk) {
|
|
81
83
|
generationChunk = chunk;
|
|
@@ -114,6 +116,10 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
114
116
|
await Promise.all(results.map(async (pResult, i) => {
|
|
115
117
|
if (pResult.status === "fulfilled") {
|
|
116
118
|
const result = pResult.value;
|
|
119
|
+
for (const generation of result.generations) {
|
|
120
|
+
generation.message.response_metadata =
|
|
121
|
+
_combineGenerationInfoAndMetadata(generation);
|
|
122
|
+
}
|
|
117
123
|
generations[i] = result.generations;
|
|
118
124
|
llmOutputs[i] = result.llmOutput;
|
|
119
125
|
return runManagers?.[i]?.handleLLMEnd({
|
|
@@ -359,3 +365,9 @@ export class SimpleChatModel extends BaseChatModel {
|
|
|
359
365
|
};
|
|
360
366
|
}
|
|
361
367
|
}
|
|
368
|
+
function _combineGenerationInfoAndMetadata(generation) {
|
|
369
|
+
return {
|
|
370
|
+
...generation.generationInfo,
|
|
371
|
+
...generation.message.response_metadata,
|
|
372
|
+
};
|
|
373
|
+
}
|
package/dist/messages/index.cjs
CHANGED
|
@@ -30,7 +30,10 @@ function mergeContent(firstContent, secondContent) {
|
|
|
30
30
|
class BaseMessage extends serializable_js_1.Serializable {
|
|
31
31
|
get lc_aliases() {
|
|
32
32
|
// exclude snake case conversion to pascal case
|
|
33
|
-
return {
|
|
33
|
+
return {
|
|
34
|
+
additional_kwargs: "additional_kwargs",
|
|
35
|
+
response_metadata: "response_metadata",
|
|
36
|
+
};
|
|
34
37
|
}
|
|
35
38
|
/**
|
|
36
39
|
* @deprecated
|
|
@@ -44,13 +47,21 @@ class BaseMessage extends serializable_js_1.Serializable {
|
|
|
44
47
|
kwargs) {
|
|
45
48
|
if (typeof fields === "string") {
|
|
46
49
|
// eslint-disable-next-line no-param-reassign
|
|
47
|
-
fields = {
|
|
50
|
+
fields = {
|
|
51
|
+
content: fields,
|
|
52
|
+
additional_kwargs: kwargs,
|
|
53
|
+
response_metadata: {},
|
|
54
|
+
};
|
|
48
55
|
}
|
|
49
56
|
// Make sure the default value for additional_kwargs is passed into super() for serialization
|
|
50
57
|
if (!fields.additional_kwargs) {
|
|
51
58
|
// eslint-disable-next-line no-param-reassign
|
|
52
59
|
fields.additional_kwargs = {};
|
|
53
60
|
}
|
|
61
|
+
if (!fields.response_metadata) {
|
|
62
|
+
// eslint-disable-next-line no-param-reassign
|
|
63
|
+
fields.response_metadata = {};
|
|
64
|
+
}
|
|
54
65
|
super(fields);
|
|
55
66
|
Object.defineProperty(this, "lc_namespace", {
|
|
56
67
|
enumerable: true,
|
|
@@ -85,9 +96,17 @@ class BaseMessage extends serializable_js_1.Serializable {
|
|
|
85
96
|
writable: true,
|
|
86
97
|
value: void 0
|
|
87
98
|
});
|
|
99
|
+
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
100
|
+
Object.defineProperty(this, "response_metadata", {
|
|
101
|
+
enumerable: true,
|
|
102
|
+
configurable: true,
|
|
103
|
+
writable: true,
|
|
104
|
+
value: void 0
|
|
105
|
+
});
|
|
88
106
|
this.name = fields.name;
|
|
89
107
|
this.content = fields.content;
|
|
90
108
|
this.additional_kwargs = fields.additional_kwargs;
|
|
109
|
+
this.response_metadata = fields.response_metadata;
|
|
91
110
|
}
|
|
92
111
|
toDict() {
|
|
93
112
|
return {
|
|
@@ -129,6 +148,69 @@ function isOpenAIToolCallArray(value) {
|
|
|
129
148
|
return (Array.isArray(value) &&
|
|
130
149
|
value.every((v) => typeof v.index === "number"));
|
|
131
150
|
}
|
|
151
|
+
function _mergeDicts(
|
|
152
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
153
|
+
left,
|
|
154
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
155
|
+
right
|
|
156
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
157
|
+
) {
|
|
158
|
+
const merged = { ...left };
|
|
159
|
+
for (const [key, value] of Object.entries(right)) {
|
|
160
|
+
if (merged[key] == null) {
|
|
161
|
+
merged[key] = value;
|
|
162
|
+
}
|
|
163
|
+
else if (value == null) {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
else if (typeof merged[key] !== typeof value ||
|
|
167
|
+
Array.isArray(merged[key]) !== Array.isArray(value)) {
|
|
168
|
+
throw new Error(`field[${key}] already exists in the message chunk, but with a different type.`);
|
|
169
|
+
}
|
|
170
|
+
else if (typeof merged[key] === "string") {
|
|
171
|
+
merged[key] = merged[key] + value;
|
|
172
|
+
}
|
|
173
|
+
else if (!Array.isArray(merged[key]) && typeof merged[key] === "object") {
|
|
174
|
+
merged[key] = _mergeDicts(merged[key], value);
|
|
175
|
+
}
|
|
176
|
+
else if (key === "tool_calls" &&
|
|
177
|
+
isOpenAIToolCallArray(merged[key]) &&
|
|
178
|
+
isOpenAIToolCallArray(value)) {
|
|
179
|
+
for (const toolCall of value) {
|
|
180
|
+
if (merged[key]?.[toolCall.index] !== undefined) {
|
|
181
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
182
|
+
merged[key] = merged[key]?.map((value, i) => {
|
|
183
|
+
if (i !== toolCall.index) {
|
|
184
|
+
return value;
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
...value,
|
|
188
|
+
...toolCall,
|
|
189
|
+
function: {
|
|
190
|
+
name: toolCall.function.name ?? value.function.name,
|
|
191
|
+
arguments: (value.function.arguments ?? "") +
|
|
192
|
+
(toolCall.function.arguments ?? ""),
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
merged[key][toolCall.index] = toolCall;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
else if (Array.isArray(merged[key])) {
|
|
203
|
+
merged[key] = merged[key].concat(value);
|
|
204
|
+
}
|
|
205
|
+
else if (merged[key] === value) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
console.warn(`field[${key}] already exists in this message chunk and value has unsupported type.`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return merged;
|
|
213
|
+
}
|
|
132
214
|
/**
|
|
133
215
|
* Represents a chunk of a message, which can be concatenated with other
|
|
134
216
|
* message chunks. It includes a method `_merge_kwargs_dict()` for merging
|
|
@@ -137,53 +219,6 @@ function isOpenAIToolCallArray(value) {
|
|
|
137
219
|
* of `BaseMessageChunk` instances.
|
|
138
220
|
*/
|
|
139
221
|
class BaseMessageChunk extends BaseMessage {
|
|
140
|
-
static _mergeAdditionalKwargs(left, right) {
|
|
141
|
-
const merged = { ...left };
|
|
142
|
-
for (const [key, value] of Object.entries(right)) {
|
|
143
|
-
if (merged[key] === undefined) {
|
|
144
|
-
merged[key] = value;
|
|
145
|
-
}
|
|
146
|
-
else if (typeof merged[key] !== typeof value) {
|
|
147
|
-
throw new Error(`additional_kwargs[${key}] already exists in the message chunk, but with a different type.`);
|
|
148
|
-
}
|
|
149
|
-
else if (typeof merged[key] === "string") {
|
|
150
|
-
merged[key] = merged[key] + value;
|
|
151
|
-
}
|
|
152
|
-
else if (!Array.isArray(merged[key]) &&
|
|
153
|
-
typeof merged[key] === "object") {
|
|
154
|
-
merged[key] = this._mergeAdditionalKwargs(merged[key], value);
|
|
155
|
-
}
|
|
156
|
-
else if (key === "tool_calls" &&
|
|
157
|
-
isOpenAIToolCallArray(merged[key]) &&
|
|
158
|
-
isOpenAIToolCallArray(value)) {
|
|
159
|
-
for (const toolCall of value) {
|
|
160
|
-
if (merged[key]?.[toolCall.index] !== undefined) {
|
|
161
|
-
merged[key] = merged[key]?.map((value, i) => {
|
|
162
|
-
if (i !== toolCall.index) {
|
|
163
|
-
return value;
|
|
164
|
-
}
|
|
165
|
-
return {
|
|
166
|
-
...value,
|
|
167
|
-
...toolCall,
|
|
168
|
-
function: {
|
|
169
|
-
name: toolCall.function.name ?? value.function.name,
|
|
170
|
-
arguments: (value.function.arguments ?? "") +
|
|
171
|
-
(toolCall.function.arguments ?? ""),
|
|
172
|
-
},
|
|
173
|
-
};
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
merged[key][toolCall.index] = toolCall;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
throw new Error(`additional_kwargs[${key}] already exists in this message chunk.`);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return merged;
|
|
186
|
-
}
|
|
187
222
|
}
|
|
188
223
|
exports.BaseMessageChunk = BaseMessageChunk;
|
|
189
224
|
/**
|
|
@@ -212,7 +247,8 @@ class HumanMessageChunk extends BaseMessageChunk {
|
|
|
212
247
|
concat(chunk) {
|
|
213
248
|
return new HumanMessageChunk({
|
|
214
249
|
content: mergeContent(this.content, chunk.content),
|
|
215
|
-
additional_kwargs:
|
|
250
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
251
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
216
252
|
});
|
|
217
253
|
}
|
|
218
254
|
}
|
|
@@ -243,7 +279,8 @@ class AIMessageChunk extends BaseMessageChunk {
|
|
|
243
279
|
concat(chunk) {
|
|
244
280
|
return new AIMessageChunk({
|
|
245
281
|
content: mergeContent(this.content, chunk.content),
|
|
246
|
-
additional_kwargs:
|
|
282
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
283
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
247
284
|
});
|
|
248
285
|
}
|
|
249
286
|
}
|
|
@@ -274,7 +311,8 @@ class SystemMessageChunk extends BaseMessageChunk {
|
|
|
274
311
|
concat(chunk) {
|
|
275
312
|
return new SystemMessageChunk({
|
|
276
313
|
content: mergeContent(this.content, chunk.content),
|
|
277
|
-
additional_kwargs:
|
|
314
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
315
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
278
316
|
});
|
|
279
317
|
}
|
|
280
318
|
}
|
|
@@ -314,7 +352,8 @@ class FunctionMessageChunk extends BaseMessageChunk {
|
|
|
314
352
|
concat(chunk) {
|
|
315
353
|
return new FunctionMessageChunk({
|
|
316
354
|
content: mergeContent(this.content, chunk.content),
|
|
317
|
-
additional_kwargs:
|
|
355
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
356
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
318
357
|
name: this.name ?? "",
|
|
319
358
|
});
|
|
320
359
|
}
|
|
@@ -377,7 +416,8 @@ class ToolMessageChunk extends BaseMessageChunk {
|
|
|
377
416
|
concat(chunk) {
|
|
378
417
|
return new ToolMessageChunk({
|
|
379
418
|
content: mergeContent(this.content, chunk.content),
|
|
380
|
-
additional_kwargs:
|
|
419
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
420
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
381
421
|
tool_call_id: this.tool_call_id,
|
|
382
422
|
});
|
|
383
423
|
}
|
|
@@ -474,7 +514,8 @@ class ChatMessageChunk extends BaseMessageChunk {
|
|
|
474
514
|
concat(chunk) {
|
|
475
515
|
return new ChatMessageChunk({
|
|
476
516
|
content: mergeContent(this.content, chunk.content),
|
|
477
|
-
additional_kwargs:
|
|
517
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
518
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
478
519
|
role: this.role,
|
|
479
520
|
});
|
|
480
521
|
}
|
package/dist/messages/index.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export interface StoredMessageData {
|
|
|
6
6
|
name: string | undefined;
|
|
7
7
|
tool_call_id: string | undefined;
|
|
8
8
|
additional_kwargs?: Record<string, any>;
|
|
9
|
+
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
10
|
+
response_metadata?: Record<string, any>;
|
|
9
11
|
}
|
|
10
12
|
export interface StoredMessage {
|
|
11
13
|
type: string;
|
|
@@ -65,6 +67,8 @@ export interface BaseMessageFields {
|
|
|
65
67
|
tool_calls?: ToolCall[];
|
|
66
68
|
[key: string]: unknown;
|
|
67
69
|
};
|
|
70
|
+
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
71
|
+
response_metadata?: Record<string, any>;
|
|
68
72
|
}
|
|
69
73
|
export interface ChatMessageFieldsWithRole extends BaseMessageFields {
|
|
70
74
|
role: string;
|
|
@@ -95,6 +99,8 @@ export declare abstract class BaseMessage extends Serializable implements BaseMe
|
|
|
95
99
|
name?: string;
|
|
96
100
|
/** Additional keyword arguments */
|
|
97
101
|
additional_kwargs: NonNullable<BaseMessageFields["additional_kwargs"]>;
|
|
102
|
+
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
103
|
+
response_metadata: NonNullable<BaseMessageFields["response_metadata"]>;
|
|
98
104
|
/** The type of the message. */
|
|
99
105
|
abstract _getType(): MessageType;
|
|
100
106
|
constructor(fields: string | BaseMessageFields,
|
|
@@ -115,7 +121,6 @@ export type OpenAIToolCall = ToolCall & {
|
|
|
115
121
|
*/
|
|
116
122
|
export declare abstract class BaseMessageChunk extends BaseMessage {
|
|
117
123
|
abstract concat(chunk: BaseMessageChunk): BaseMessageChunk;
|
|
118
|
-
static _mergeAdditionalKwargs(left: NonNullable<BaseMessageFields["additional_kwargs"]>, right: NonNullable<BaseMessageFields["additional_kwargs"]>): NonNullable<BaseMessageFields["additional_kwargs"]>;
|
|
119
124
|
}
|
|
120
125
|
/**
|
|
121
126
|
* Represents a human message in a conversation.
|
package/dist/messages/index.js
CHANGED
|
@@ -27,7 +27,10 @@ function mergeContent(firstContent, secondContent) {
|
|
|
27
27
|
export class BaseMessage extends Serializable {
|
|
28
28
|
get lc_aliases() {
|
|
29
29
|
// exclude snake case conversion to pascal case
|
|
30
|
-
return {
|
|
30
|
+
return {
|
|
31
|
+
additional_kwargs: "additional_kwargs",
|
|
32
|
+
response_metadata: "response_metadata",
|
|
33
|
+
};
|
|
31
34
|
}
|
|
32
35
|
/**
|
|
33
36
|
* @deprecated
|
|
@@ -41,13 +44,21 @@ export class BaseMessage extends Serializable {
|
|
|
41
44
|
kwargs) {
|
|
42
45
|
if (typeof fields === "string") {
|
|
43
46
|
// eslint-disable-next-line no-param-reassign
|
|
44
|
-
fields = {
|
|
47
|
+
fields = {
|
|
48
|
+
content: fields,
|
|
49
|
+
additional_kwargs: kwargs,
|
|
50
|
+
response_metadata: {},
|
|
51
|
+
};
|
|
45
52
|
}
|
|
46
53
|
// Make sure the default value for additional_kwargs is passed into super() for serialization
|
|
47
54
|
if (!fields.additional_kwargs) {
|
|
48
55
|
// eslint-disable-next-line no-param-reassign
|
|
49
56
|
fields.additional_kwargs = {};
|
|
50
57
|
}
|
|
58
|
+
if (!fields.response_metadata) {
|
|
59
|
+
// eslint-disable-next-line no-param-reassign
|
|
60
|
+
fields.response_metadata = {};
|
|
61
|
+
}
|
|
51
62
|
super(fields);
|
|
52
63
|
Object.defineProperty(this, "lc_namespace", {
|
|
53
64
|
enumerable: true,
|
|
@@ -82,9 +93,17 @@ export class BaseMessage extends Serializable {
|
|
|
82
93
|
writable: true,
|
|
83
94
|
value: void 0
|
|
84
95
|
});
|
|
96
|
+
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
97
|
+
Object.defineProperty(this, "response_metadata", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
configurable: true,
|
|
100
|
+
writable: true,
|
|
101
|
+
value: void 0
|
|
102
|
+
});
|
|
85
103
|
this.name = fields.name;
|
|
86
104
|
this.content = fields.content;
|
|
87
105
|
this.additional_kwargs = fields.additional_kwargs;
|
|
106
|
+
this.response_metadata = fields.response_metadata;
|
|
88
107
|
}
|
|
89
108
|
toDict() {
|
|
90
109
|
return {
|
|
@@ -125,6 +144,69 @@ function isOpenAIToolCallArray(value) {
|
|
|
125
144
|
return (Array.isArray(value) &&
|
|
126
145
|
value.every((v) => typeof v.index === "number"));
|
|
127
146
|
}
|
|
147
|
+
function _mergeDicts(
|
|
148
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
149
|
+
left,
|
|
150
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
151
|
+
right
|
|
152
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
153
|
+
) {
|
|
154
|
+
const merged = { ...left };
|
|
155
|
+
for (const [key, value] of Object.entries(right)) {
|
|
156
|
+
if (merged[key] == null) {
|
|
157
|
+
merged[key] = value;
|
|
158
|
+
}
|
|
159
|
+
else if (value == null) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
else if (typeof merged[key] !== typeof value ||
|
|
163
|
+
Array.isArray(merged[key]) !== Array.isArray(value)) {
|
|
164
|
+
throw new Error(`field[${key}] already exists in the message chunk, but with a different type.`);
|
|
165
|
+
}
|
|
166
|
+
else if (typeof merged[key] === "string") {
|
|
167
|
+
merged[key] = merged[key] + value;
|
|
168
|
+
}
|
|
169
|
+
else if (!Array.isArray(merged[key]) && typeof merged[key] === "object") {
|
|
170
|
+
merged[key] = _mergeDicts(merged[key], value);
|
|
171
|
+
}
|
|
172
|
+
else if (key === "tool_calls" &&
|
|
173
|
+
isOpenAIToolCallArray(merged[key]) &&
|
|
174
|
+
isOpenAIToolCallArray(value)) {
|
|
175
|
+
for (const toolCall of value) {
|
|
176
|
+
if (merged[key]?.[toolCall.index] !== undefined) {
|
|
177
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
178
|
+
merged[key] = merged[key]?.map((value, i) => {
|
|
179
|
+
if (i !== toolCall.index) {
|
|
180
|
+
return value;
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
...value,
|
|
184
|
+
...toolCall,
|
|
185
|
+
function: {
|
|
186
|
+
name: toolCall.function.name ?? value.function.name,
|
|
187
|
+
arguments: (value.function.arguments ?? "") +
|
|
188
|
+
(toolCall.function.arguments ?? ""),
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
merged[key][toolCall.index] = toolCall;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else if (Array.isArray(merged[key])) {
|
|
199
|
+
merged[key] = merged[key].concat(value);
|
|
200
|
+
}
|
|
201
|
+
else if (merged[key] === value) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
console.warn(`field[${key}] already exists in this message chunk and value has unsupported type.`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return merged;
|
|
209
|
+
}
|
|
128
210
|
/**
|
|
129
211
|
* Represents a chunk of a message, which can be concatenated with other
|
|
130
212
|
* message chunks. It includes a method `_merge_kwargs_dict()` for merging
|
|
@@ -133,53 +215,6 @@ function isOpenAIToolCallArray(value) {
|
|
|
133
215
|
* of `BaseMessageChunk` instances.
|
|
134
216
|
*/
|
|
135
217
|
export class BaseMessageChunk extends BaseMessage {
|
|
136
|
-
static _mergeAdditionalKwargs(left, right) {
|
|
137
|
-
const merged = { ...left };
|
|
138
|
-
for (const [key, value] of Object.entries(right)) {
|
|
139
|
-
if (merged[key] === undefined) {
|
|
140
|
-
merged[key] = value;
|
|
141
|
-
}
|
|
142
|
-
else if (typeof merged[key] !== typeof value) {
|
|
143
|
-
throw new Error(`additional_kwargs[${key}] already exists in the message chunk, but with a different type.`);
|
|
144
|
-
}
|
|
145
|
-
else if (typeof merged[key] === "string") {
|
|
146
|
-
merged[key] = merged[key] + value;
|
|
147
|
-
}
|
|
148
|
-
else if (!Array.isArray(merged[key]) &&
|
|
149
|
-
typeof merged[key] === "object") {
|
|
150
|
-
merged[key] = this._mergeAdditionalKwargs(merged[key], value);
|
|
151
|
-
}
|
|
152
|
-
else if (key === "tool_calls" &&
|
|
153
|
-
isOpenAIToolCallArray(merged[key]) &&
|
|
154
|
-
isOpenAIToolCallArray(value)) {
|
|
155
|
-
for (const toolCall of value) {
|
|
156
|
-
if (merged[key]?.[toolCall.index] !== undefined) {
|
|
157
|
-
merged[key] = merged[key]?.map((value, i) => {
|
|
158
|
-
if (i !== toolCall.index) {
|
|
159
|
-
return value;
|
|
160
|
-
}
|
|
161
|
-
return {
|
|
162
|
-
...value,
|
|
163
|
-
...toolCall,
|
|
164
|
-
function: {
|
|
165
|
-
name: toolCall.function.name ?? value.function.name,
|
|
166
|
-
arguments: (value.function.arguments ?? "") +
|
|
167
|
-
(toolCall.function.arguments ?? ""),
|
|
168
|
-
},
|
|
169
|
-
};
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
merged[key][toolCall.index] = toolCall;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
throw new Error(`additional_kwargs[${key}] already exists in this message chunk.`);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
return merged;
|
|
182
|
-
}
|
|
183
218
|
}
|
|
184
219
|
/**
|
|
185
220
|
* Represents a human message in a conversation.
|
|
@@ -206,7 +241,8 @@ export class HumanMessageChunk extends BaseMessageChunk {
|
|
|
206
241
|
concat(chunk) {
|
|
207
242
|
return new HumanMessageChunk({
|
|
208
243
|
content: mergeContent(this.content, chunk.content),
|
|
209
|
-
additional_kwargs:
|
|
244
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
245
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
210
246
|
});
|
|
211
247
|
}
|
|
212
248
|
}
|
|
@@ -235,7 +271,8 @@ export class AIMessageChunk extends BaseMessageChunk {
|
|
|
235
271
|
concat(chunk) {
|
|
236
272
|
return new AIMessageChunk({
|
|
237
273
|
content: mergeContent(this.content, chunk.content),
|
|
238
|
-
additional_kwargs:
|
|
274
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
275
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
239
276
|
});
|
|
240
277
|
}
|
|
241
278
|
}
|
|
@@ -264,7 +301,8 @@ export class SystemMessageChunk extends BaseMessageChunk {
|
|
|
264
301
|
concat(chunk) {
|
|
265
302
|
return new SystemMessageChunk({
|
|
266
303
|
content: mergeContent(this.content, chunk.content),
|
|
267
|
-
additional_kwargs:
|
|
304
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
305
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
268
306
|
});
|
|
269
307
|
}
|
|
270
308
|
}
|
|
@@ -302,7 +340,8 @@ export class FunctionMessageChunk extends BaseMessageChunk {
|
|
|
302
340
|
concat(chunk) {
|
|
303
341
|
return new FunctionMessageChunk({
|
|
304
342
|
content: mergeContent(this.content, chunk.content),
|
|
305
|
-
additional_kwargs:
|
|
343
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
344
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
306
345
|
name: this.name ?? "",
|
|
307
346
|
});
|
|
308
347
|
}
|
|
@@ -363,7 +402,8 @@ export class ToolMessageChunk extends BaseMessageChunk {
|
|
|
363
402
|
concat(chunk) {
|
|
364
403
|
return new ToolMessageChunk({
|
|
365
404
|
content: mergeContent(this.content, chunk.content),
|
|
366
|
-
additional_kwargs:
|
|
405
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
406
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
367
407
|
tool_call_id: this.tool_call_id,
|
|
368
408
|
});
|
|
369
409
|
}
|
|
@@ -455,7 +495,8 @@ export class ChatMessageChunk extends BaseMessageChunk {
|
|
|
455
495
|
concat(chunk) {
|
|
456
496
|
return new ChatMessageChunk({
|
|
457
497
|
content: mergeContent(this.content, chunk.content),
|
|
458
|
-
additional_kwargs:
|
|
498
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
499
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
459
500
|
role: this.role,
|
|
460
501
|
});
|
|
461
502
|
}
|