@langchain/core 0.1.55-rc.0 → 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/messages/index.js
CHANGED
|
@@ -1,611 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// If both are arrays
|
|
12
|
-
}
|
|
13
|
-
else if (Array.isArray(secondContent)) {
|
|
14
|
-
return [...firstContent, ...secondContent];
|
|
15
|
-
// If the first content is a list and second is a string
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
// Otherwise, add the second content as a new element of the list
|
|
19
|
-
return [...firstContent, { type: "text", text: secondContent }];
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Base class for all types of messages in a conversation. It includes
|
|
24
|
-
* properties like `content`, `name`, and `additional_kwargs`. It also
|
|
25
|
-
* includes methods like `toDict()` and `_getType()`.
|
|
26
|
-
*/
|
|
27
|
-
export class BaseMessage extends Serializable {
|
|
28
|
-
get lc_aliases() {
|
|
29
|
-
// exclude snake case conversion to pascal case
|
|
30
|
-
return {
|
|
31
|
-
additional_kwargs: "additional_kwargs",
|
|
32
|
-
response_metadata: "response_metadata",
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* @deprecated
|
|
37
|
-
* Use {@link BaseMessage.content} instead.
|
|
38
|
-
*/
|
|
39
|
-
get text() {
|
|
40
|
-
return typeof this.content === "string" ? this.content : "";
|
|
41
|
-
}
|
|
42
|
-
constructor(fields,
|
|
43
|
-
/** @deprecated */
|
|
44
|
-
kwargs) {
|
|
45
|
-
if (typeof fields === "string") {
|
|
46
|
-
// eslint-disable-next-line no-param-reassign
|
|
47
|
-
fields = {
|
|
48
|
-
content: fields,
|
|
49
|
-
additional_kwargs: kwargs,
|
|
50
|
-
response_metadata: {},
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
// Make sure the default value for additional_kwargs is passed into super() for serialization
|
|
54
|
-
if (!fields.additional_kwargs) {
|
|
55
|
-
// eslint-disable-next-line no-param-reassign
|
|
56
|
-
fields.additional_kwargs = {};
|
|
57
|
-
}
|
|
58
|
-
if (!fields.response_metadata) {
|
|
59
|
-
// eslint-disable-next-line no-param-reassign
|
|
60
|
-
fields.response_metadata = {};
|
|
61
|
-
}
|
|
62
|
-
super(fields);
|
|
63
|
-
Object.defineProperty(this, "lc_namespace", {
|
|
64
|
-
enumerable: true,
|
|
65
|
-
configurable: true,
|
|
66
|
-
writable: true,
|
|
67
|
-
value: ["langchain_core", "messages"]
|
|
68
|
-
});
|
|
69
|
-
Object.defineProperty(this, "lc_serializable", {
|
|
70
|
-
enumerable: true,
|
|
71
|
-
configurable: true,
|
|
72
|
-
writable: true,
|
|
73
|
-
value: true
|
|
74
|
-
});
|
|
75
|
-
/** The content of the message. */
|
|
76
|
-
Object.defineProperty(this, "content", {
|
|
77
|
-
enumerable: true,
|
|
78
|
-
configurable: true,
|
|
79
|
-
writable: true,
|
|
80
|
-
value: void 0
|
|
81
|
-
});
|
|
82
|
-
/** The name of the message sender in a multi-user chat. */
|
|
83
|
-
Object.defineProperty(this, "name", {
|
|
84
|
-
enumerable: true,
|
|
85
|
-
configurable: true,
|
|
86
|
-
writable: true,
|
|
87
|
-
value: void 0
|
|
88
|
-
});
|
|
89
|
-
/** Additional keyword arguments */
|
|
90
|
-
Object.defineProperty(this, "additional_kwargs", {
|
|
91
|
-
enumerable: true,
|
|
92
|
-
configurable: true,
|
|
93
|
-
writable: true,
|
|
94
|
-
value: void 0
|
|
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
|
-
});
|
|
103
|
-
this.name = fields.name;
|
|
104
|
-
this.content = fields.content;
|
|
105
|
-
this.additional_kwargs = fields.additional_kwargs;
|
|
106
|
-
this.response_metadata = fields.response_metadata;
|
|
107
|
-
}
|
|
108
|
-
toDict() {
|
|
109
|
-
return {
|
|
110
|
-
type: this._getType(),
|
|
111
|
-
data: this.toJSON()
|
|
112
|
-
.kwargs,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
toChunk() {
|
|
116
|
-
const type = this._getType();
|
|
117
|
-
if (type === "human") {
|
|
118
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
119
|
-
return new HumanMessageChunk({ ...this });
|
|
120
|
-
}
|
|
121
|
-
else if (type === "ai") {
|
|
122
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
123
|
-
return new AIMessageChunk({ ...this });
|
|
124
|
-
}
|
|
125
|
-
else if (type === "system") {
|
|
126
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
127
|
-
return new SystemMessageChunk({ ...this });
|
|
128
|
-
}
|
|
129
|
-
else if (type === "function") {
|
|
130
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
131
|
-
return new FunctionMessageChunk({ ...this });
|
|
132
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
133
|
-
}
|
|
134
|
-
else if (ChatMessage.isInstance(this)) {
|
|
135
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
136
|
-
return new ChatMessageChunk({ ...this });
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
throw new Error("Unknown message type.");
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
function isOpenAIToolCallArray(value) {
|
|
144
|
-
return (Array.isArray(value) &&
|
|
145
|
-
value.every((v) => typeof v.index === "number"));
|
|
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
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Represents a chunk of a message, which can be concatenated with other
|
|
212
|
-
* message chunks. It includes a method `_merge_kwargs_dict()` for merging
|
|
213
|
-
* additional keyword arguments from another `BaseMessageChunk` into this
|
|
214
|
-
* one. It also overrides the `__add__()` method to support concatenation
|
|
215
|
-
* of `BaseMessageChunk` instances.
|
|
216
|
-
*/
|
|
217
|
-
export class BaseMessageChunk extends BaseMessage {
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Represents a human message in a conversation.
|
|
221
|
-
*/
|
|
222
|
-
export class HumanMessage extends BaseMessage {
|
|
223
|
-
static lc_name() {
|
|
224
|
-
return "HumanMessage";
|
|
225
|
-
}
|
|
226
|
-
_getType() {
|
|
227
|
-
return "human";
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Represents a chunk of a human message, which can be concatenated with
|
|
232
|
-
* other human message chunks.
|
|
233
|
-
*/
|
|
234
|
-
export class HumanMessageChunk extends BaseMessageChunk {
|
|
235
|
-
static lc_name() {
|
|
236
|
-
return "HumanMessageChunk";
|
|
237
|
-
}
|
|
238
|
-
_getType() {
|
|
239
|
-
return "human";
|
|
240
|
-
}
|
|
241
|
-
concat(chunk) {
|
|
242
|
-
return new HumanMessageChunk({
|
|
243
|
-
content: mergeContent(this.content, chunk.content),
|
|
244
|
-
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
245
|
-
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Represents an AI message in a conversation.
|
|
251
|
-
*/
|
|
252
|
-
export class AIMessage extends BaseMessage {
|
|
253
|
-
static lc_name() {
|
|
254
|
-
return "AIMessage";
|
|
255
|
-
}
|
|
256
|
-
_getType() {
|
|
257
|
-
return "ai";
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Represents a chunk of an AI message, which can be concatenated with
|
|
262
|
-
* other AI message chunks.
|
|
263
|
-
*/
|
|
264
|
-
export class AIMessageChunk extends BaseMessageChunk {
|
|
265
|
-
static lc_name() {
|
|
266
|
-
return "AIMessageChunk";
|
|
267
|
-
}
|
|
268
|
-
_getType() {
|
|
269
|
-
return "ai";
|
|
270
|
-
}
|
|
271
|
-
concat(chunk) {
|
|
272
|
-
return new AIMessageChunk({
|
|
273
|
-
content: mergeContent(this.content, chunk.content),
|
|
274
|
-
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
275
|
-
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Represents a system message in a conversation.
|
|
281
|
-
*/
|
|
282
|
-
export class SystemMessage extends BaseMessage {
|
|
283
|
-
static lc_name() {
|
|
284
|
-
return "SystemMessage";
|
|
285
|
-
}
|
|
286
|
-
_getType() {
|
|
287
|
-
return "system";
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Represents a chunk of a system message, which can be concatenated with
|
|
292
|
-
* other system message chunks.
|
|
293
|
-
*/
|
|
294
|
-
export class SystemMessageChunk extends BaseMessageChunk {
|
|
295
|
-
static lc_name() {
|
|
296
|
-
return "SystemMessageChunk";
|
|
297
|
-
}
|
|
298
|
-
_getType() {
|
|
299
|
-
return "system";
|
|
300
|
-
}
|
|
301
|
-
concat(chunk) {
|
|
302
|
-
return new SystemMessageChunk({
|
|
303
|
-
content: mergeContent(this.content, chunk.content),
|
|
304
|
-
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
305
|
-
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Represents a function message in a conversation.
|
|
311
|
-
*/
|
|
312
|
-
export class FunctionMessage extends BaseMessage {
|
|
313
|
-
static lc_name() {
|
|
314
|
-
return "FunctionMessage";
|
|
315
|
-
}
|
|
316
|
-
constructor(fields,
|
|
317
|
-
/** @deprecated */
|
|
318
|
-
name) {
|
|
319
|
-
if (typeof fields === "string") {
|
|
320
|
-
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
|
|
321
|
-
fields = { content: fields, name: name };
|
|
322
|
-
}
|
|
323
|
-
super(fields);
|
|
324
|
-
}
|
|
325
|
-
_getType() {
|
|
326
|
-
return "function";
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* Represents a chunk of a function message, which can be concatenated
|
|
331
|
-
* with other function message chunks.
|
|
332
|
-
*/
|
|
333
|
-
export class FunctionMessageChunk extends BaseMessageChunk {
|
|
334
|
-
static lc_name() {
|
|
335
|
-
return "FunctionMessageChunk";
|
|
336
|
-
}
|
|
337
|
-
_getType() {
|
|
338
|
-
return "function";
|
|
339
|
-
}
|
|
340
|
-
concat(chunk) {
|
|
341
|
-
return new FunctionMessageChunk({
|
|
342
|
-
content: mergeContent(this.content, chunk.content),
|
|
343
|
-
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
344
|
-
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
345
|
-
name: this.name ?? "",
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Represents a tool message in a conversation.
|
|
351
|
-
*/
|
|
352
|
-
export class ToolMessage extends BaseMessage {
|
|
353
|
-
static lc_name() {
|
|
354
|
-
return "ToolMessage";
|
|
355
|
-
}
|
|
356
|
-
get lc_aliases() {
|
|
357
|
-
// exclude snake case conversion to pascal case
|
|
358
|
-
return { tool_call_id: "tool_call_id" };
|
|
359
|
-
}
|
|
360
|
-
constructor(fields, tool_call_id, name) {
|
|
361
|
-
if (typeof fields === "string") {
|
|
362
|
-
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
|
|
363
|
-
fields = { content: fields, name, tool_call_id: tool_call_id };
|
|
364
|
-
}
|
|
365
|
-
super(fields);
|
|
366
|
-
Object.defineProperty(this, "tool_call_id", {
|
|
367
|
-
enumerable: true,
|
|
368
|
-
configurable: true,
|
|
369
|
-
writable: true,
|
|
370
|
-
value: void 0
|
|
371
|
-
});
|
|
372
|
-
this.tool_call_id = fields.tool_call_id;
|
|
373
|
-
}
|
|
374
|
-
_getType() {
|
|
375
|
-
return "tool";
|
|
376
|
-
}
|
|
377
|
-
static isInstance(message) {
|
|
378
|
-
return message._getType() === "tool";
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* Represents a chunk of a tool message, which can be concatenated
|
|
383
|
-
* with other tool message chunks.
|
|
384
|
-
*/
|
|
385
|
-
export class ToolMessageChunk extends BaseMessageChunk {
|
|
386
|
-
constructor(fields) {
|
|
387
|
-
super(fields);
|
|
388
|
-
Object.defineProperty(this, "tool_call_id", {
|
|
389
|
-
enumerable: true,
|
|
390
|
-
configurable: true,
|
|
391
|
-
writable: true,
|
|
392
|
-
value: void 0
|
|
393
|
-
});
|
|
394
|
-
this.tool_call_id = fields.tool_call_id;
|
|
395
|
-
}
|
|
396
|
-
static lc_name() {
|
|
397
|
-
return "ToolMessageChunk";
|
|
398
|
-
}
|
|
399
|
-
_getType() {
|
|
400
|
-
return "tool";
|
|
401
|
-
}
|
|
402
|
-
concat(chunk) {
|
|
403
|
-
return new ToolMessageChunk({
|
|
404
|
-
content: mergeContent(this.content, chunk.content),
|
|
405
|
-
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
406
|
-
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
407
|
-
tool_call_id: this.tool_call_id,
|
|
408
|
-
});
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
/**
|
|
412
|
-
* Represents a chat message in a conversation.
|
|
413
|
-
*/
|
|
414
|
-
export class ChatMessage extends BaseMessage {
|
|
415
|
-
static lc_name() {
|
|
416
|
-
return "ChatMessage";
|
|
417
|
-
}
|
|
418
|
-
static _chatMessageClass() {
|
|
419
|
-
return ChatMessage;
|
|
420
|
-
}
|
|
421
|
-
constructor(fields, role) {
|
|
422
|
-
if (typeof fields === "string") {
|
|
423
|
-
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
|
|
424
|
-
fields = { content: fields, role: role };
|
|
425
|
-
}
|
|
426
|
-
super(fields);
|
|
427
|
-
Object.defineProperty(this, "role", {
|
|
428
|
-
enumerable: true,
|
|
429
|
-
configurable: true,
|
|
430
|
-
writable: true,
|
|
431
|
-
value: void 0
|
|
432
|
-
});
|
|
433
|
-
this.role = fields.role;
|
|
434
|
-
}
|
|
435
|
-
_getType() {
|
|
436
|
-
return "generic";
|
|
437
|
-
}
|
|
438
|
-
static isInstance(message) {
|
|
439
|
-
return message._getType() === "generic";
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
export function isBaseMessage(messageLike) {
|
|
443
|
-
return typeof messageLike?._getType === "function";
|
|
444
|
-
}
|
|
445
|
-
export function isBaseMessageChunk(messageLike) {
|
|
446
|
-
return (isBaseMessage(messageLike) &&
|
|
447
|
-
typeof messageLike.concat === "function");
|
|
448
|
-
}
|
|
449
|
-
export function coerceMessageLikeToMessage(messageLike) {
|
|
450
|
-
if (typeof messageLike === "string") {
|
|
451
|
-
return new HumanMessage(messageLike);
|
|
452
|
-
}
|
|
453
|
-
else if (isBaseMessage(messageLike)) {
|
|
454
|
-
return messageLike;
|
|
455
|
-
}
|
|
456
|
-
const [type, content] = messageLike;
|
|
457
|
-
if (type === "human" || type === "user") {
|
|
458
|
-
return new HumanMessage({ content });
|
|
459
|
-
}
|
|
460
|
-
else if (type === "ai" || type === "assistant") {
|
|
461
|
-
return new AIMessage({ content });
|
|
462
|
-
}
|
|
463
|
-
else if (type === "system") {
|
|
464
|
-
return new SystemMessage({ content });
|
|
465
|
-
}
|
|
466
|
-
else {
|
|
467
|
-
throw new Error(`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.`);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
/**
|
|
471
|
-
* Represents a chunk of a chat message, which can be concatenated with
|
|
472
|
-
* other chat message chunks.
|
|
473
|
-
*/
|
|
474
|
-
export class ChatMessageChunk extends BaseMessageChunk {
|
|
475
|
-
static lc_name() {
|
|
476
|
-
return "ChatMessageChunk";
|
|
477
|
-
}
|
|
478
|
-
constructor(fields, role) {
|
|
479
|
-
if (typeof fields === "string") {
|
|
480
|
-
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
|
|
481
|
-
fields = { content: fields, role: role };
|
|
482
|
-
}
|
|
483
|
-
super(fields);
|
|
484
|
-
Object.defineProperty(this, "role", {
|
|
485
|
-
enumerable: true,
|
|
486
|
-
configurable: true,
|
|
487
|
-
writable: true,
|
|
488
|
-
value: void 0
|
|
489
|
-
});
|
|
490
|
-
this.role = fields.role;
|
|
491
|
-
}
|
|
492
|
-
_getType() {
|
|
493
|
-
return "generic";
|
|
494
|
-
}
|
|
495
|
-
concat(chunk) {
|
|
496
|
-
return new ChatMessageChunk({
|
|
497
|
-
content: mergeContent(this.content, chunk.content),
|
|
498
|
-
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
499
|
-
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
500
|
-
role: this.role,
|
|
501
|
-
});
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
/**
|
|
505
|
-
* This function is used by memory classes to get a string representation
|
|
506
|
-
* of the chat message history, based on the message content and role.
|
|
507
|
-
*/
|
|
508
|
-
export function getBufferString(messages, humanPrefix = "Human", aiPrefix = "AI") {
|
|
509
|
-
const string_messages = [];
|
|
510
|
-
for (const m of messages) {
|
|
511
|
-
let role;
|
|
512
|
-
if (m._getType() === "human") {
|
|
513
|
-
role = humanPrefix;
|
|
514
|
-
}
|
|
515
|
-
else if (m._getType() === "ai") {
|
|
516
|
-
role = aiPrefix;
|
|
517
|
-
}
|
|
518
|
-
else if (m._getType() === "system") {
|
|
519
|
-
role = "System";
|
|
520
|
-
}
|
|
521
|
-
else if (m._getType() === "function") {
|
|
522
|
-
role = "Function";
|
|
523
|
-
}
|
|
524
|
-
else if (m._getType() === "tool") {
|
|
525
|
-
role = "Tool";
|
|
526
|
-
}
|
|
527
|
-
else if (m._getType() === "generic") {
|
|
528
|
-
role = m.role;
|
|
529
|
-
}
|
|
530
|
-
else {
|
|
531
|
-
throw new Error(`Got unsupported message type: ${m._getType()}`);
|
|
532
|
-
}
|
|
533
|
-
const nameStr = m.name ? `${m.name}, ` : "";
|
|
534
|
-
string_messages.push(`${role}: ${nameStr}${m.content}`);
|
|
535
|
-
}
|
|
536
|
-
return string_messages.join("\n");
|
|
537
|
-
}
|
|
538
|
-
/**
|
|
539
|
-
* Maps messages from an older format (V1) to the current `StoredMessage`
|
|
540
|
-
* format. If the message is already in the `StoredMessage` format, it is
|
|
541
|
-
* returned as is. Otherwise, it transforms the V1 message into a
|
|
542
|
-
* `StoredMessage`. This function is important for maintaining
|
|
543
|
-
* compatibility with older message formats.
|
|
544
|
-
*/
|
|
545
|
-
function mapV1MessageToStoredMessage(message) {
|
|
546
|
-
// TODO: Remove this mapper when we deprecate the old message format.
|
|
547
|
-
if (message.data !== undefined) {
|
|
548
|
-
return message;
|
|
549
|
-
}
|
|
550
|
-
else {
|
|
551
|
-
const v1Message = message;
|
|
552
|
-
return {
|
|
553
|
-
type: v1Message.type,
|
|
554
|
-
data: {
|
|
555
|
-
content: v1Message.text,
|
|
556
|
-
role: v1Message.role,
|
|
557
|
-
name: undefined,
|
|
558
|
-
tool_call_id: undefined,
|
|
559
|
-
},
|
|
560
|
-
};
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
export function mapStoredMessageToChatMessage(message) {
|
|
564
|
-
const storedMessage = mapV1MessageToStoredMessage(message);
|
|
565
|
-
switch (storedMessage.type) {
|
|
566
|
-
case "human":
|
|
567
|
-
return new HumanMessage(storedMessage.data);
|
|
568
|
-
case "ai":
|
|
569
|
-
return new AIMessage(storedMessage.data);
|
|
570
|
-
case "system":
|
|
571
|
-
return new SystemMessage(storedMessage.data);
|
|
572
|
-
case "function":
|
|
573
|
-
if (storedMessage.data.name === undefined) {
|
|
574
|
-
throw new Error("Name must be defined for function messages");
|
|
575
|
-
}
|
|
576
|
-
return new FunctionMessage(storedMessage.data);
|
|
577
|
-
case "tool":
|
|
578
|
-
if (storedMessage.data.tool_call_id === undefined) {
|
|
579
|
-
throw new Error("Tool call ID must be defined for tool messages");
|
|
580
|
-
}
|
|
581
|
-
return new ToolMessage(storedMessage.data);
|
|
582
|
-
case "chat": {
|
|
583
|
-
if (storedMessage.data.role === undefined) {
|
|
584
|
-
throw new Error("Role must be defined for chat messages");
|
|
585
|
-
}
|
|
586
|
-
return new ChatMessage(storedMessage.data);
|
|
587
|
-
}
|
|
588
|
-
default:
|
|
589
|
-
throw new Error(`Got unexpected type: ${storedMessage.type}`);
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
/**
|
|
593
|
-
* Transforms an array of `StoredMessage` instances into an array of
|
|
594
|
-
* `BaseMessage` instances. It uses the `mapV1MessageToStoredMessage`
|
|
595
|
-
* function to ensure all messages are in the `StoredMessage` format, then
|
|
596
|
-
* creates new instances of the appropriate `BaseMessage` subclass based
|
|
597
|
-
* on the type of each message. This function is used to prepare stored
|
|
598
|
-
* messages for use in a chat context.
|
|
599
|
-
*/
|
|
600
|
-
export function mapStoredMessagesToChatMessages(messages) {
|
|
601
|
-
return messages.map(mapStoredMessageToChatMessage);
|
|
602
|
-
}
|
|
603
|
-
/**
|
|
604
|
-
* Transforms an array of `BaseMessage` instances into an array of
|
|
605
|
-
* `StoredMessage` instances. It does this by calling the `toDict` method
|
|
606
|
-
* on each `BaseMessage`, which returns a `StoredMessage`. This function
|
|
607
|
-
* is used to prepare chat messages for storage.
|
|
608
|
-
*/
|
|
609
|
-
export function mapChatMessagesToStoredMessages(messages) {
|
|
610
|
-
return messages.map((message) => message.toDict());
|
|
611
|
-
}
|
|
1
|
+
export * from "./ai.js";
|
|
2
|
+
export * from "./base.js";
|
|
3
|
+
export * from "./chat.js";
|
|
4
|
+
export * from "./function.js";
|
|
5
|
+
export * from "./human.js";
|
|
6
|
+
export * from "./system.js";
|
|
7
|
+
export * from "./utils.js";
|
|
8
|
+
// TODO: Use a star export when we deprecate the
|
|
9
|
+
// existing "ToolCall" type in "base.js".
|
|
10
|
+
export { ToolMessage, ToolMessageChunk, } from "./tool.js";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SystemMessageChunk = exports.SystemMessage = void 0;
|
|
4
|
+
const base_js_1 = require("./base.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Represents a system message in a conversation.
|
|
7
|
+
*/
|
|
8
|
+
class SystemMessage extends base_js_1.BaseMessage {
|
|
9
|
+
static lc_name() {
|
|
10
|
+
return "SystemMessage";
|
|
11
|
+
}
|
|
12
|
+
_getType() {
|
|
13
|
+
return "system";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.SystemMessage = SystemMessage;
|
|
17
|
+
/**
|
|
18
|
+
* Represents a chunk of a system message, which can be concatenated with
|
|
19
|
+
* other system message chunks.
|
|
20
|
+
*/
|
|
21
|
+
class SystemMessageChunk extends base_js_1.BaseMessageChunk {
|
|
22
|
+
static lc_name() {
|
|
23
|
+
return "SystemMessageChunk";
|
|
24
|
+
}
|
|
25
|
+
_getType() {
|
|
26
|
+
return "system";
|
|
27
|
+
}
|
|
28
|
+
concat(chunk) {
|
|
29
|
+
return new SystemMessageChunk({
|
|
30
|
+
content: (0, base_js_1.mergeContent)(this.content, chunk.content),
|
|
31
|
+
additional_kwargs: (0, base_js_1._mergeDicts)(this.additional_kwargs, chunk.additional_kwargs),
|
|
32
|
+
response_metadata: (0, base_js_1._mergeDicts)(this.response_metadata, chunk.response_metadata),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.SystemMessageChunk = SystemMessageChunk;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BaseMessage, BaseMessageChunk, type MessageType } from "./base.js";
|
|
2
|
+
/**
|
|
3
|
+
* Represents a system message in a conversation.
|
|
4
|
+
*/
|
|
5
|
+
export declare class SystemMessage extends BaseMessage {
|
|
6
|
+
static lc_name(): string;
|
|
7
|
+
_getType(): MessageType;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Represents a chunk of a system message, which can be concatenated with
|
|
11
|
+
* other system message chunks.
|
|
12
|
+
*/
|
|
13
|
+
export declare class SystemMessageChunk extends BaseMessageChunk {
|
|
14
|
+
static lc_name(): string;
|
|
15
|
+
_getType(): MessageType;
|
|
16
|
+
concat(chunk: SystemMessageChunk): SystemMessageChunk;
|
|
17
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BaseMessage, BaseMessageChunk, mergeContent, _mergeDicts, } from "./base.js";
|
|
2
|
+
/**
|
|
3
|
+
* Represents a system message in a conversation.
|
|
4
|
+
*/
|
|
5
|
+
export class SystemMessage extends BaseMessage {
|
|
6
|
+
static lc_name() {
|
|
7
|
+
return "SystemMessage";
|
|
8
|
+
}
|
|
9
|
+
_getType() {
|
|
10
|
+
return "system";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Represents a chunk of a system message, which can be concatenated with
|
|
15
|
+
* other system message chunks.
|
|
16
|
+
*/
|
|
17
|
+
export class SystemMessageChunk extends BaseMessageChunk {
|
|
18
|
+
static lc_name() {
|
|
19
|
+
return "SystemMessageChunk";
|
|
20
|
+
}
|
|
21
|
+
_getType() {
|
|
22
|
+
return "system";
|
|
23
|
+
}
|
|
24
|
+
concat(chunk) {
|
|
25
|
+
return new SystemMessageChunk({
|
|
26
|
+
content: mergeContent(this.content, chunk.content),
|
|
27
|
+
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
28
|
+
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|