@langchain/core 0.2.7 → 0.2.8
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/caches.cjs +2 -2
- package/dist/caches.d.ts +1 -1
- package/dist/caches.js +1 -1
- package/dist/callbacks/base.d.ts +1 -1
- package/dist/callbacks/manager.cjs +2 -2
- package/dist/callbacks/manager.d.ts +1 -1
- package/dist/callbacks/manager.js +1 -1
- package/dist/language_models/base.cjs +2 -2
- package/dist/language_models/base.d.ts +1 -1
- package/dist/language_models/base.js +1 -1
- package/dist/messages/ai.cjs +2 -1
- package/dist/messages/ai.js +2 -1
- package/dist/messages/base.cjs +11 -0
- package/dist/messages/base.d.ts +10 -0
- package/dist/messages/base.js +11 -0
- package/dist/messages/chat.cjs +1 -0
- package/dist/messages/chat.js +1 -0
- package/dist/messages/function.cjs +1 -0
- package/dist/messages/function.js +1 -0
- package/dist/messages/human.cjs +1 -0
- package/dist/messages/human.js +1 -0
- package/dist/messages/index.cjs +1 -0
- package/dist/messages/index.d.ts +1 -0
- package/dist/messages/index.js +1 -0
- package/dist/messages/system.cjs +1 -0
- package/dist/messages/system.js +1 -0
- package/dist/messages/tests/message_utils.test.d.ts +1 -0
- package/dist/messages/tests/message_utils.test.js +382 -0
- package/dist/messages/tool.cjs +1 -0
- package/dist/messages/tool.js +1 -0
- package/dist/messages/transformers.cjs +421 -0
- package/dist/messages/transformers.d.ts +465 -0
- package/dist/messages/transformers.js +414 -0
- package/dist/messages/utils.cjs +17 -4
- package/dist/messages/utils.d.ts +3 -3
- package/dist/messages/utils.js +17 -4
- package/dist/outputs.d.ts +1 -1
- package/dist/prompt_values.cjs +5 -4
- package/dist/prompt_values.d.ts +2 -1
- package/dist/prompt_values.js +2 -1
- package/dist/tracers/base.d.ts +1 -1
- package/dist/tracers/log_stream.cjs +2 -2
- package/dist/tracers/log_stream.js +1 -1
- package/dist/tracers/tracer_langchain_v1.cjs +2 -2
- package/dist/tracers/tracer_langchain_v1.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { RunnableLambda } from "../runnables/base.js";
|
|
2
|
+
import { AIMessage, AIMessageChunk } from "./ai.js";
|
|
3
|
+
import { ChatMessage, ChatMessageChunk, } from "./chat.js";
|
|
4
|
+
import { FunctionMessage, FunctionMessageChunk, } from "./function.js";
|
|
5
|
+
import { HumanMessage, HumanMessageChunk } from "./human.js";
|
|
6
|
+
import { SystemMessage, SystemMessageChunk } from "./system.js";
|
|
7
|
+
import { ToolMessage, ToolMessageChunk, } from "./tool.js";
|
|
8
|
+
import { convertToChunk } from "./utils.js";
|
|
9
|
+
const _isMessageType = (msg, types) => {
|
|
10
|
+
const typesAsStrings = [
|
|
11
|
+
...new Set(types?.map((t) => {
|
|
12
|
+
if (typeof t === "string") {
|
|
13
|
+
return t;
|
|
14
|
+
}
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
const instantiatedMsgClass = new t({});
|
|
17
|
+
if (!("_getType" in instantiatedMsgClass) ||
|
|
18
|
+
typeof instantiatedMsgClass._getType !== "function") {
|
|
19
|
+
throw new Error("Invalid type provided.");
|
|
20
|
+
}
|
|
21
|
+
return instantiatedMsgClass._getType();
|
|
22
|
+
})),
|
|
23
|
+
];
|
|
24
|
+
const msgType = msg._getType();
|
|
25
|
+
return typesAsStrings.some((t) => t === msgType);
|
|
26
|
+
};
|
|
27
|
+
export function filterMessages(messagesOrOptions, options) {
|
|
28
|
+
if (Array.isArray(messagesOrOptions)) {
|
|
29
|
+
return _filterMessages(messagesOrOptions, options);
|
|
30
|
+
}
|
|
31
|
+
return RunnableLambda.from((input) => {
|
|
32
|
+
return _filterMessages(input, messagesOrOptions);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function _filterMessages(messages, options = {}) {
|
|
36
|
+
const { includeNames, excludeNames, includeTypes, excludeTypes, includeIds, excludeIds, } = options;
|
|
37
|
+
const filtered = [];
|
|
38
|
+
for (const msg of messages) {
|
|
39
|
+
if (excludeNames && msg.name && excludeNames.includes(msg.name)) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
else if (excludeTypes && _isMessageType(msg, excludeTypes)) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
else if (excludeIds && msg.id && excludeIds.includes(msg.id)) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
// default to inclusion when no inclusion criteria given.
|
|
49
|
+
if (!(includeTypes || includeIds || includeNames)) {
|
|
50
|
+
filtered.push(msg);
|
|
51
|
+
}
|
|
52
|
+
else if (includeNames &&
|
|
53
|
+
msg.name &&
|
|
54
|
+
includeNames.some((iName) => iName === msg.name)) {
|
|
55
|
+
filtered.push(msg);
|
|
56
|
+
}
|
|
57
|
+
else if (includeTypes && _isMessageType(msg, includeTypes)) {
|
|
58
|
+
filtered.push(msg);
|
|
59
|
+
}
|
|
60
|
+
else if (includeIds && msg.id && includeIds.some((id) => id === msg.id)) {
|
|
61
|
+
filtered.push(msg);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return filtered;
|
|
65
|
+
}
|
|
66
|
+
export function mergeMessageRuns(messages) {
|
|
67
|
+
if (Array.isArray(messages)) {
|
|
68
|
+
return _mergeMessageRuns(messages);
|
|
69
|
+
}
|
|
70
|
+
return RunnableLambda.from(_mergeMessageRuns);
|
|
71
|
+
}
|
|
72
|
+
function _mergeMessageRuns(messages) {
|
|
73
|
+
if (!messages.length) {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
const merged = [];
|
|
77
|
+
for (const msg of messages) {
|
|
78
|
+
const curr = msg; // Create a shallow copy of the message
|
|
79
|
+
const last = merged.pop();
|
|
80
|
+
if (!last) {
|
|
81
|
+
merged.push(curr);
|
|
82
|
+
}
|
|
83
|
+
else if (curr._getType() === "tool" ||
|
|
84
|
+
!(curr._getType() === last._getType())) {
|
|
85
|
+
merged.push(last, curr);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const lastChunk = convertToChunk(last);
|
|
89
|
+
const currChunk = convertToChunk(curr);
|
|
90
|
+
const mergedChunks = lastChunk.concat(currChunk);
|
|
91
|
+
if (typeof lastChunk.content === "string" &&
|
|
92
|
+
typeof currChunk.content === "string") {
|
|
93
|
+
mergedChunks.content = `${lastChunk.content}\n${currChunk.content}`;
|
|
94
|
+
}
|
|
95
|
+
merged.push(_chunkToMsg(mergedChunks));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return merged;
|
|
99
|
+
}
|
|
100
|
+
export function trimMessages(messagesOrOptions, options) {
|
|
101
|
+
if (Array.isArray(messagesOrOptions)) {
|
|
102
|
+
const messages = messagesOrOptions;
|
|
103
|
+
if (!options) {
|
|
104
|
+
throw new Error("Options parameter is required when providing messages.");
|
|
105
|
+
}
|
|
106
|
+
return _trimMessagesHelper(messages, options);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const trimmerOptions = messagesOrOptions;
|
|
110
|
+
return RunnableLambda.from((input) => _trimMessagesHelper(input, trimmerOptions));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function _trimMessagesHelper(messages, options) {
|
|
114
|
+
const { maxTokens, tokenCounter, strategy = "last", allowPartial = false, endOn, startOn, includeSystem = false, textSplitter, } = options;
|
|
115
|
+
if (startOn && strategy === "first") {
|
|
116
|
+
throw new Error("`startOn` should only be specified if `strategy` is 'last'.");
|
|
117
|
+
}
|
|
118
|
+
if (includeSystem && strategy === "first") {
|
|
119
|
+
throw new Error("`includeSystem` should only be specified if `strategy` is 'last'.");
|
|
120
|
+
}
|
|
121
|
+
let listTokenCounter;
|
|
122
|
+
if ("getNumTokens" in tokenCounter) {
|
|
123
|
+
listTokenCounter = async (msgs) => {
|
|
124
|
+
const tokenCounts = await Promise.all(msgs.map((msg) => tokenCounter.getNumTokens(msg.content)));
|
|
125
|
+
return tokenCounts.reduce((sum, count) => sum + count, 0);
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
listTokenCounter = async (msgs) => tokenCounter(msgs);
|
|
130
|
+
}
|
|
131
|
+
let textSplitterFunc = defaultTextSplitter;
|
|
132
|
+
if (textSplitter) {
|
|
133
|
+
if ("splitText" in textSplitter) {
|
|
134
|
+
textSplitterFunc = textSplitter.splitText;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
textSplitterFunc = async (text) => textSplitter(text);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (strategy === "first") {
|
|
141
|
+
return _firstMaxTokens(messages, {
|
|
142
|
+
maxTokens,
|
|
143
|
+
tokenCounter: listTokenCounter,
|
|
144
|
+
textSplitter: textSplitterFunc,
|
|
145
|
+
partialStrategy: allowPartial ? "first" : undefined,
|
|
146
|
+
endOn,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
else if (strategy === "last") {
|
|
150
|
+
return _lastMaxTokens(messages, {
|
|
151
|
+
maxTokens,
|
|
152
|
+
tokenCounter: listTokenCounter,
|
|
153
|
+
textSplitter: textSplitterFunc,
|
|
154
|
+
allowPartial,
|
|
155
|
+
includeSystem,
|
|
156
|
+
startOn,
|
|
157
|
+
endOn,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
throw new Error(`Unrecognized strategy: '${strategy}'. Must be one of 'first' or 'last'.`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async function _firstMaxTokens(messages, options) {
|
|
165
|
+
const { maxTokens, tokenCounter, textSplitter, partialStrategy, endOn } = options;
|
|
166
|
+
let messagesCopy = [...messages];
|
|
167
|
+
let idx = 0;
|
|
168
|
+
for (let i = 0; i < messagesCopy.length; i += 1) {
|
|
169
|
+
const remainingMessages = i > 0 ? messagesCopy.slice(0, -i) : messagesCopy;
|
|
170
|
+
if ((await tokenCounter(remainingMessages)) <= maxTokens) {
|
|
171
|
+
idx = messagesCopy.length - i;
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (idx < messagesCopy.length - 1 && partialStrategy) {
|
|
176
|
+
let includedPartial = false;
|
|
177
|
+
if (Array.isArray(messagesCopy[idx].content)) {
|
|
178
|
+
const excluded = messagesCopy[idx];
|
|
179
|
+
if (typeof excluded.content === "string") {
|
|
180
|
+
throw new Error("Expected content to be an array.");
|
|
181
|
+
}
|
|
182
|
+
const numBlock = excluded.content.length;
|
|
183
|
+
const reversedContent = partialStrategy === "last"
|
|
184
|
+
? [...excluded.content].reverse()
|
|
185
|
+
: excluded.content;
|
|
186
|
+
for (let i = 1; i <= numBlock; i += 1) {
|
|
187
|
+
const partialContent = partialStrategy === "first"
|
|
188
|
+
? reversedContent.slice(0, i)
|
|
189
|
+
: reversedContent.slice(-i);
|
|
190
|
+
const fields = Object.fromEntries(Object.entries(excluded).filter(([k]) => k !== "type" && !k.startsWith("lc_")));
|
|
191
|
+
const updatedMessage = _switchTypeToMessage(excluded._getType(), {
|
|
192
|
+
...fields,
|
|
193
|
+
content: partialContent,
|
|
194
|
+
});
|
|
195
|
+
const slicedMessages = [...messagesCopy.slice(0, idx), updatedMessage];
|
|
196
|
+
if ((await tokenCounter(slicedMessages)) <= maxTokens) {
|
|
197
|
+
messagesCopy = slicedMessages;
|
|
198
|
+
idx += 1;
|
|
199
|
+
includedPartial = true;
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (includedPartial && partialStrategy === "last") {
|
|
206
|
+
excluded.content = [...reversedContent].reverse();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (!includedPartial) {
|
|
210
|
+
const excluded = messagesCopy[idx];
|
|
211
|
+
let text;
|
|
212
|
+
if (Array.isArray(excluded.content) &&
|
|
213
|
+
excluded.content.some((block) => typeof block === "string" || block.type === "text")) {
|
|
214
|
+
const textBlock = excluded.content.find((block) => block.type === "text" && block.text);
|
|
215
|
+
text = textBlock?.text;
|
|
216
|
+
}
|
|
217
|
+
else if (typeof excluded.content === "string") {
|
|
218
|
+
text = excluded.content;
|
|
219
|
+
}
|
|
220
|
+
if (text) {
|
|
221
|
+
const splitTexts = await textSplitter(text);
|
|
222
|
+
const numSplits = splitTexts.length;
|
|
223
|
+
if (partialStrategy === "last") {
|
|
224
|
+
splitTexts.reverse();
|
|
225
|
+
}
|
|
226
|
+
for (let _ = 0; _ < numSplits - 1; _ += 1) {
|
|
227
|
+
splitTexts.pop();
|
|
228
|
+
excluded.content = splitTexts.join("");
|
|
229
|
+
if ((await tokenCounter([...messagesCopy.slice(0, idx), excluded])) <=
|
|
230
|
+
maxTokens) {
|
|
231
|
+
if (partialStrategy === "last") {
|
|
232
|
+
excluded.content = [...splitTexts].reverse().join("");
|
|
233
|
+
}
|
|
234
|
+
messagesCopy = [...messagesCopy.slice(0, idx), excluded];
|
|
235
|
+
idx += 1;
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (endOn) {
|
|
243
|
+
const endOnArr = Array.isArray(endOn) ? endOn : [endOn];
|
|
244
|
+
while (idx > 0 && !_isMessageType(messagesCopy[idx - 1], endOnArr)) {
|
|
245
|
+
idx -= 1;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return messagesCopy.slice(0, idx);
|
|
249
|
+
}
|
|
250
|
+
async function _lastMaxTokens(messages, options) {
|
|
251
|
+
const { allowPartial = false, includeSystem = false, endOn, startOn, ...rest } = options;
|
|
252
|
+
if (endOn) {
|
|
253
|
+
const endOnArr = Array.isArray(endOn) ? endOn : [endOn];
|
|
254
|
+
while (messages &&
|
|
255
|
+
!_isMessageType(messages[messages.length - 1], endOnArr)) {
|
|
256
|
+
messages.pop();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const swappedSystem = includeSystem && messages[0]._getType() === "system";
|
|
260
|
+
let reversed_ = swappedSystem
|
|
261
|
+
? messages.slice(0, 1).concat(messages.slice(1).reverse())
|
|
262
|
+
: messages.reverse();
|
|
263
|
+
reversed_ = await _firstMaxTokens(reversed_, {
|
|
264
|
+
...rest,
|
|
265
|
+
partialStrategy: allowPartial ? "last" : undefined,
|
|
266
|
+
endOn: startOn,
|
|
267
|
+
});
|
|
268
|
+
if (swappedSystem) {
|
|
269
|
+
return [reversed_[0], ...reversed_.slice(1).reverse()];
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
return reversed_.reverse();
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const _MSG_CHUNK_MAP = {
|
|
276
|
+
human: {
|
|
277
|
+
message: HumanMessage,
|
|
278
|
+
messageChunk: HumanMessageChunk,
|
|
279
|
+
},
|
|
280
|
+
ai: {
|
|
281
|
+
message: AIMessage,
|
|
282
|
+
messageChunk: AIMessageChunk,
|
|
283
|
+
},
|
|
284
|
+
system: {
|
|
285
|
+
message: SystemMessage,
|
|
286
|
+
messageChunk: SystemMessageChunk,
|
|
287
|
+
},
|
|
288
|
+
tool: {
|
|
289
|
+
message: ToolMessage,
|
|
290
|
+
messageChunk: ToolMessageChunk,
|
|
291
|
+
},
|
|
292
|
+
function: {
|
|
293
|
+
message: FunctionMessage,
|
|
294
|
+
messageChunk: FunctionMessageChunk,
|
|
295
|
+
},
|
|
296
|
+
generic: {
|
|
297
|
+
message: ChatMessage,
|
|
298
|
+
messageChunk: ChatMessageChunk,
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
function _switchTypeToMessage(messageType, fields, returnChunk) {
|
|
302
|
+
let chunk;
|
|
303
|
+
let msg;
|
|
304
|
+
switch (messageType) {
|
|
305
|
+
case "human":
|
|
306
|
+
if (returnChunk) {
|
|
307
|
+
chunk = new HumanMessageChunk(fields);
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
msg = new HumanMessage(fields);
|
|
311
|
+
}
|
|
312
|
+
break;
|
|
313
|
+
case "ai":
|
|
314
|
+
if (returnChunk) {
|
|
315
|
+
let aiChunkFields = {
|
|
316
|
+
...fields,
|
|
317
|
+
};
|
|
318
|
+
if ("tool_calls" in aiChunkFields) {
|
|
319
|
+
aiChunkFields = {
|
|
320
|
+
...aiChunkFields,
|
|
321
|
+
tool_call_chunks: aiChunkFields.tool_calls?.map((tc) => ({
|
|
322
|
+
...tc,
|
|
323
|
+
index: undefined,
|
|
324
|
+
args: JSON.stringify(tc.args),
|
|
325
|
+
})),
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
chunk = new AIMessageChunk(aiChunkFields);
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
msg = new AIMessage(fields);
|
|
332
|
+
}
|
|
333
|
+
break;
|
|
334
|
+
case "system":
|
|
335
|
+
if (returnChunk) {
|
|
336
|
+
chunk = new SystemMessageChunk(fields);
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
msg = new SystemMessage(fields);
|
|
340
|
+
}
|
|
341
|
+
break;
|
|
342
|
+
case "tool":
|
|
343
|
+
if ("tool_call_id" in fields) {
|
|
344
|
+
if (returnChunk) {
|
|
345
|
+
chunk = new ToolMessageChunk(fields);
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
msg = new ToolMessage(fields);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
throw new Error("Can not convert ToolMessage to ToolMessageChunk if 'tool_call_id' field is not defined.");
|
|
353
|
+
}
|
|
354
|
+
break;
|
|
355
|
+
case "function":
|
|
356
|
+
if (returnChunk) {
|
|
357
|
+
chunk = new FunctionMessageChunk(fields);
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
if (!fields.name) {
|
|
361
|
+
throw new Error("FunctionMessage must have a 'name' field");
|
|
362
|
+
}
|
|
363
|
+
msg = new FunctionMessage(fields);
|
|
364
|
+
}
|
|
365
|
+
break;
|
|
366
|
+
case "generic":
|
|
367
|
+
if ("role" in fields) {
|
|
368
|
+
if (returnChunk) {
|
|
369
|
+
chunk = new ChatMessageChunk(fields);
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
msg = new ChatMessage(fields);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
throw new Error("Can not convert ChatMessage to ChatMessageChunk if 'role' field is not defined.");
|
|
377
|
+
}
|
|
378
|
+
break;
|
|
379
|
+
default:
|
|
380
|
+
throw new Error(`Unrecognized message type ${messageType}`);
|
|
381
|
+
}
|
|
382
|
+
if (returnChunk && chunk) {
|
|
383
|
+
return chunk;
|
|
384
|
+
}
|
|
385
|
+
if (msg) {
|
|
386
|
+
return msg;
|
|
387
|
+
}
|
|
388
|
+
throw new Error(`Unrecognized message type ${messageType}`);
|
|
389
|
+
}
|
|
390
|
+
function _chunkToMsg(chunk) {
|
|
391
|
+
const chunkType = chunk._getType();
|
|
392
|
+
let msg;
|
|
393
|
+
const fields = Object.fromEntries(Object.entries(chunk).filter(([k]) => !["type", "tool_call_chunks"].includes(k) && !k.startsWith("lc_")));
|
|
394
|
+
if (chunkType in _MSG_CHUNK_MAP) {
|
|
395
|
+
msg = _switchTypeToMessage(chunkType, fields);
|
|
396
|
+
}
|
|
397
|
+
if (!msg) {
|
|
398
|
+
throw new Error(`Unrecognized message chunk class ${chunkType}. Supported classes are ${Object.keys(_MSG_CHUNK_MAP)}`);
|
|
399
|
+
}
|
|
400
|
+
return msg;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* The default text splitter function that splits text by newlines.
|
|
404
|
+
*
|
|
405
|
+
* @param {string} text
|
|
406
|
+
* @returns A promise that resolves to an array of strings split by newlines.
|
|
407
|
+
*/
|
|
408
|
+
export function defaultTextSplitter(text) {
|
|
409
|
+
const splits = text.split("\n");
|
|
410
|
+
return Promise.resolve([
|
|
411
|
+
...splits.slice(0, -1).map((s) => `${s}\n`),
|
|
412
|
+
splits[splits.length - 1],
|
|
413
|
+
]);
|
|
414
|
+
}
|
package/dist/messages/utils.cjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.convertToChunk = exports.mapChatMessagesToStoredMessages = exports.mapStoredMessagesToChatMessages = exports.mapStoredMessageToChatMessage = exports.getBufferString = exports.coerceMessageLikeToMessage = void 0;
|
|
4
|
-
const base_js_1 = require("./base.cjs");
|
|
5
|
-
const human_js_1 = require("./human.cjs");
|
|
6
4
|
const ai_js_1 = require("./ai.cjs");
|
|
7
|
-
const
|
|
5
|
+
const base_js_1 = require("./base.cjs");
|
|
8
6
|
const chat_js_1 = require("./chat.cjs");
|
|
9
7
|
const function_js_1 = require("./function.cjs");
|
|
8
|
+
const human_js_1 = require("./human.cjs");
|
|
9
|
+
const system_js_1 = require("./system.cjs");
|
|
10
10
|
const tool_js_1 = require("./tool.cjs");
|
|
11
11
|
function coerceMessageLikeToMessage(messageLike) {
|
|
12
12
|
if (typeof messageLike === "string") {
|
|
@@ -149,8 +149,21 @@ function convertToChunk(message) {
|
|
|
149
149
|
return new human_js_1.HumanMessageChunk({ ...message });
|
|
150
150
|
}
|
|
151
151
|
else if (type === "ai") {
|
|
152
|
+
let aiChunkFields = {
|
|
153
|
+
...message,
|
|
154
|
+
};
|
|
155
|
+
if ("tool_calls" in aiChunkFields) {
|
|
156
|
+
aiChunkFields = {
|
|
157
|
+
...aiChunkFields,
|
|
158
|
+
tool_call_chunks: aiChunkFields.tool_calls?.map((tc) => ({
|
|
159
|
+
...tc,
|
|
160
|
+
index: undefined,
|
|
161
|
+
args: JSON.stringify(tc.args),
|
|
162
|
+
})),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
152
165
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
153
|
-
return new ai_js_1.AIMessageChunk({ ...
|
|
166
|
+
return new ai_js_1.AIMessageChunk({ ...aiChunkFields });
|
|
154
167
|
}
|
|
155
168
|
else if (type === "system") {
|
|
156
169
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
package/dist/messages/utils.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { BaseMessageLike, BaseMessage, StoredMessage } from "./base.js";
|
|
2
|
-
import { HumanMessage, HumanMessageChunk } from "./human.js";
|
|
3
1
|
import { AIMessage, AIMessageChunk } from "./ai.js";
|
|
4
|
-
import {
|
|
2
|
+
import { BaseMessageLike, BaseMessage, StoredMessage } from "./base.js";
|
|
5
3
|
import { ChatMessage, ChatMessageChunk } from "./chat.js";
|
|
6
4
|
import { FunctionMessage, FunctionMessageChunk } from "./function.js";
|
|
5
|
+
import { HumanMessage, HumanMessageChunk } from "./human.js";
|
|
6
|
+
import { SystemMessage, SystemMessageChunk } from "./system.js";
|
|
7
7
|
import { ToolMessage } from "./tool.js";
|
|
8
8
|
export declare function coerceMessageLikeToMessage(messageLike: BaseMessageLike): BaseMessage;
|
|
9
9
|
/**
|
package/dist/messages/utils.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { isBaseMessage, } from "./base.js";
|
|
2
|
-
import { HumanMessage, HumanMessageChunk } from "./human.js";
|
|
3
1
|
import { AIMessage, AIMessageChunk } from "./ai.js";
|
|
4
|
-
import {
|
|
2
|
+
import { isBaseMessage, } from "./base.js";
|
|
5
3
|
import { ChatMessage, ChatMessageChunk, } from "./chat.js";
|
|
6
4
|
import { FunctionMessage, FunctionMessageChunk, } from "./function.js";
|
|
5
|
+
import { HumanMessage, HumanMessageChunk } from "./human.js";
|
|
6
|
+
import { SystemMessage, SystemMessageChunk } from "./system.js";
|
|
7
7
|
import { ToolMessage } from "./tool.js";
|
|
8
8
|
export function coerceMessageLikeToMessage(messageLike) {
|
|
9
9
|
if (typeof messageLike === "string") {
|
|
@@ -141,8 +141,21 @@ export function convertToChunk(message) {
|
|
|
141
141
|
return new HumanMessageChunk({ ...message });
|
|
142
142
|
}
|
|
143
143
|
else if (type === "ai") {
|
|
144
|
+
let aiChunkFields = {
|
|
145
|
+
...message,
|
|
146
|
+
};
|
|
147
|
+
if ("tool_calls" in aiChunkFields) {
|
|
148
|
+
aiChunkFields = {
|
|
149
|
+
...aiChunkFields,
|
|
150
|
+
tool_call_chunks: aiChunkFields.tool_calls?.map((tc) => ({
|
|
151
|
+
...tc,
|
|
152
|
+
index: undefined,
|
|
153
|
+
args: JSON.stringify(tc.args),
|
|
154
|
+
})),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
144
157
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
145
|
-
return new AIMessageChunk({ ...
|
|
158
|
+
return new AIMessageChunk({ ...aiChunkFields });
|
|
146
159
|
}
|
|
147
160
|
else if (type === "system") {
|
|
148
161
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
package/dist/outputs.d.ts
CHANGED
package/dist/prompt_values.cjs
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ImagePromptValue = exports.ChatPromptValue = exports.StringPromptValue = exports.BasePromptValue = void 0;
|
|
4
4
|
const serializable_js_1 = require("./load/serializable.cjs");
|
|
5
|
-
const
|
|
5
|
+
const human_js_1 = require("./messages/human.cjs");
|
|
6
|
+
const utils_js_1 = require("./messages/utils.cjs");
|
|
6
7
|
/**
|
|
7
8
|
* Base PromptValue class. All prompt values should extend this class.
|
|
8
9
|
*/
|
|
@@ -43,7 +44,7 @@ class StringPromptValue extends BasePromptValue {
|
|
|
43
44
|
return this.value;
|
|
44
45
|
}
|
|
45
46
|
toChatMessages() {
|
|
46
|
-
return [new
|
|
47
|
+
return [new human_js_1.HumanMessage(this.value)];
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
50
|
exports.StringPromptValue = StringPromptValue;
|
|
@@ -82,7 +83,7 @@ class ChatPromptValue extends BasePromptValue {
|
|
|
82
83
|
this.messages = fields.messages;
|
|
83
84
|
}
|
|
84
85
|
toString() {
|
|
85
|
-
return (0,
|
|
86
|
+
return (0, utils_js_1.getBufferString)(this.messages);
|
|
86
87
|
}
|
|
87
88
|
toChatMessages() {
|
|
88
89
|
return this.messages;
|
|
@@ -135,7 +136,7 @@ class ImagePromptValue extends BasePromptValue {
|
|
|
135
136
|
}
|
|
136
137
|
toChatMessages() {
|
|
137
138
|
return [
|
|
138
|
-
new
|
|
139
|
+
new human_js_1.HumanMessage({
|
|
139
140
|
content: [
|
|
140
141
|
{
|
|
141
142
|
type: "image_url",
|
package/dist/prompt_values.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Serializable } from "./load/serializable.js";
|
|
2
|
-
import { type BaseMessage
|
|
2
|
+
import { type BaseMessage } from "./messages/base.js";
|
|
3
|
+
import { HumanMessage } from "./messages/human.js";
|
|
3
4
|
export interface BasePromptValueInterface extends Serializable {
|
|
4
5
|
toString(): string;
|
|
5
6
|
toChatMessages(): BaseMessage[];
|
package/dist/prompt_values.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Serializable } from "./load/serializable.js";
|
|
2
|
-
import { HumanMessage
|
|
2
|
+
import { HumanMessage } from "./messages/human.js";
|
|
3
|
+
import { getBufferString } from "./messages/utils.js";
|
|
3
4
|
/**
|
|
4
5
|
* Base PromptValue class. All prompt values should extend this class.
|
|
5
6
|
*/
|
package/dist/tracers/base.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { KVMap, BaseRun } from "langsmith/schemas";
|
|
|
2
2
|
import type { ChainValues } from "../utils/types/index.js";
|
|
3
3
|
import type { AgentAction, AgentFinish } from "../agents.js";
|
|
4
4
|
import type { LLMResult } from "../outputs.js";
|
|
5
|
-
import type { BaseMessage } from "../messages/
|
|
5
|
+
import type { BaseMessage } from "../messages/base.js";
|
|
6
6
|
import { Serialized } from "../load/serializable.js";
|
|
7
7
|
import { BaseCallbackHandler, BaseCallbackHandlerInput, HandleLLMNewTokenCallbackFields, NewTokenIndices } from "../callbacks/base.js";
|
|
8
8
|
import type { Document } from "../documents/document.js";
|
|
@@ -4,7 +4,7 @@ exports.LogStreamCallbackHandler = exports.isLogStreamHandler = exports.RunLog =
|
|
|
4
4
|
const index_js_1 = require("../utils/fast-json-patch/index.cjs");
|
|
5
5
|
const base_js_1 = require("./base.cjs");
|
|
6
6
|
const stream_js_1 = require("../utils/stream.cjs");
|
|
7
|
-
const
|
|
7
|
+
const ai_js_1 = require("../messages/ai.cjs");
|
|
8
8
|
/**
|
|
9
9
|
* List of jsonpatch JSONPatchOperations, which describe how to create the run state
|
|
10
10
|
* from an empty dict. This is the minimal representation of the log, designed to
|
|
@@ -401,7 +401,7 @@ class LogStreamCallbackHandler extends base_js_1.BaseTracer {
|
|
|
401
401
|
streamedOutputValue = kwargs?.chunk;
|
|
402
402
|
}
|
|
403
403
|
else {
|
|
404
|
-
streamedOutputValue = new
|
|
404
|
+
streamedOutputValue = new ai_js_1.AIMessageChunk(token);
|
|
405
405
|
}
|
|
406
406
|
}
|
|
407
407
|
else {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { applyPatch, } from "../utils/fast-json-patch/index.js";
|
|
2
2
|
import { BaseTracer } from "./base.js";
|
|
3
3
|
import { IterableReadableStream } from "../utils/stream.js";
|
|
4
|
-
import { AIMessageChunk } from "../messages/
|
|
4
|
+
import { AIMessageChunk } from "../messages/ai.js";
|
|
5
5
|
/**
|
|
6
6
|
* List of jsonpatch JSONPatchOperations, which describe how to create the run state
|
|
7
7
|
* from an empty dict. This is the minimal representation of the log, designed to
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LangChainTracerV1 = void 0;
|
|
4
|
-
const
|
|
4
|
+
const utils_js_1 = require("../messages/utils.cjs");
|
|
5
5
|
const env_js_1 = require("../utils/env.cjs");
|
|
6
6
|
const base_js_1 = require("./base.cjs");
|
|
7
7
|
/** @deprecated Use LangChainTracer instead. */
|
|
@@ -63,7 +63,7 @@ class LangChainTracerV1 extends base_js_1.BaseTracer {
|
|
|
63
63
|
if (run.run_type === "llm") {
|
|
64
64
|
const prompts = run.inputs.prompts
|
|
65
65
|
? run.inputs.prompts
|
|
66
|
-
: run.inputs.messages.map((x) => (0,
|
|
66
|
+
: run.inputs.messages.map((x) => (0, utils_js_1.getBufferString)(x));
|
|
67
67
|
const llmRun = {
|
|
68
68
|
uuid: run.id,
|
|
69
69
|
start_time: run.start_time,
|