-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathutils.ts
318 lines (308 loc) Β· 10 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { addLangChainErrorFields } from "../errors/index.js";
import { SerializedConstructor } from "../load/serializable.js";
import { _isToolCall } from "../tools/utils.js";
import { AIMessage, AIMessageChunk, AIMessageChunkFields } from "./ai.js";
import {
BaseMessageLike,
BaseMessage,
isBaseMessage,
StoredMessage,
StoredMessageV1,
BaseMessageFields,
_isMessageFieldWithRole,
} from "./base.js";
import {
ChatMessage,
ChatMessageFieldsWithRole,
ChatMessageChunk,
} from "./chat.js";
import {
FunctionMessage,
FunctionMessageFieldsWithName,
FunctionMessageChunk,
} from "./function.js";
import { HumanMessage, HumanMessageChunk } from "./human.js";
import { SystemMessage, SystemMessageChunk } from "./system.js";
import {
ToolCall,
ToolMessage,
ToolMessageFieldsWithToolCallId,
} from "./tool.js";
function _coerceToolCall(
toolCall: ToolCall | Record<string, unknown>
): ToolCall {
if (_isToolCall(toolCall)) {
return toolCall;
} else if (
typeof toolCall.id === "string" &&
toolCall.type === "function" &&
typeof toolCall.function === "object" &&
toolCall.function !== null &&
"arguments" in toolCall.function &&
typeof toolCall.function.arguments === "string" &&
"name" in toolCall.function &&
typeof toolCall.function.name === "string"
) {
// Handle OpenAI tool call format
return {
id: toolCall.id,
args: JSON.parse(toolCall.function.arguments),
name: toolCall.function.name,
type: "tool_call",
};
} else {
// TODO: Throw an error?
return toolCall as ToolCall;
}
}
function isSerializedConstructor(x: unknown): x is SerializedConstructor {
return (
typeof x === "object" &&
x != null &&
(x as SerializedConstructor).lc === 1 &&
Array.isArray((x as SerializedConstructor).id) &&
(x as SerializedConstructor).kwargs != null &&
typeof (x as SerializedConstructor).kwargs === "object"
);
}
function _constructMessageFromParams(
params:
| (BaseMessageFields & { type: string } & Record<string, unknown>)
| SerializedConstructor
) {
let type: string;
let rest: BaseMessageFields & Record<string, unknown>;
// Support serialized messages
if (isSerializedConstructor(params)) {
const className = params.id.at(-1);
if (className === "HumanMessage" || className === "HumanMessageChunk") {
type = "user";
} else if (className === "AIMessage" || className === "AIMessageChunk") {
type = "assistant";
} else if (
className === "SystemMessage" ||
className === "SystemMessageChunk"
) {
type = "system";
} else {
type = "unknown";
}
rest = params.kwargs as BaseMessageFields;
} else {
const { type: extractedType, ...otherParams } = params;
type = extractedType;
rest = otherParams;
}
if (type === "human" || type === "user") {
return new HumanMessage(rest);
} else if (type === "ai" || type === "assistant") {
const { tool_calls: rawToolCalls, ...other } = rest;
if (!Array.isArray(rawToolCalls)) {
return new AIMessage(rest);
}
const tool_calls = rawToolCalls.map(_coerceToolCall);
return new AIMessage({ ...other, tool_calls });
} else if (type === "system") {
return new SystemMessage(rest);
} else if (type === "developer") {
return new SystemMessage({
...rest,
additional_kwargs: {
...rest.additional_kwargs,
__openai_role__: "developer",
},
});
} else if (type === "tool" && "tool_call_id" in rest) {
return new ToolMessage({
...rest,
content: rest.content,
tool_call_id: rest.tool_call_id as string,
name: rest.name,
});
} else {
const error = addLangChainErrorFields(
new Error(
`Unable to coerce message from array: only human, AI, system, developer, or tool message coercion is currently supported.\n\nReceived: ${JSON.stringify(
params,
null,
2
)}`
),
"MESSAGE_COERCION_FAILURE"
);
throw error;
}
}
export function coerceMessageLikeToMessage(
messageLike: BaseMessageLike
): BaseMessage {
if (typeof messageLike === "string") {
return new HumanMessage(messageLike);
} else if (isBaseMessage(messageLike)) {
return messageLike;
}
if (Array.isArray(messageLike)) {
const [type, content] = messageLike;
return _constructMessageFromParams({ type, content });
} else if (_isMessageFieldWithRole(messageLike)) {
const { role: type, ...rest } = messageLike;
return _constructMessageFromParams({ ...rest, type });
} else {
return _constructMessageFromParams(messageLike);
}
}
/**
* This function is used by memory classes to get a string representation
* of the chat message history, based on the message content and role.
*/
export function getBufferString(
messages: BaseMessage[],
humanPrefix = "Human",
aiPrefix = "AI"
): string {
const string_messages: string[] = [];
for (const m of messages) {
let role: string;
if (m._getType() === "human") {
role = humanPrefix;
} else if (m._getType() === "ai") {
role = aiPrefix;
} else if (m._getType() === "system") {
role = "System";
} else if (m._getType() === "function") {
role = "Function";
} else if (m._getType() === "tool") {
role = "Tool";
} else if (m._getType() === "generic") {
role = (m as ChatMessage).role;
} else {
throw new Error(`Got unsupported message type: ${m._getType()}`);
}
const nameStr = m.name ? `${m.name}, ` : "";
const readableContent =
typeof m.content === "string"
? m.content
: JSON.stringify(m.content, null, 2);
string_messages.push(`${role}: ${nameStr}${readableContent}`);
}
return string_messages.join("\n");
}
/**
* Maps messages from an older format (V1) to the current `StoredMessage`
* format. If the message is already in the `StoredMessage` format, it is
* returned as is. Otherwise, it transforms the V1 message into a
* `StoredMessage`. This function is important for maintaining
* compatibility with older message formats.
*/
function mapV1MessageToStoredMessage(
message: StoredMessage | StoredMessageV1
): StoredMessage {
// TODO: Remove this mapper when we deprecate the old message format.
if ((message as StoredMessage).data !== undefined) {
return message as StoredMessage;
} else {
const v1Message = message as StoredMessageV1;
return {
type: v1Message.type,
data: {
content: v1Message.text,
role: v1Message.role,
name: undefined,
tool_call_id: undefined,
},
};
}
}
export function mapStoredMessageToChatMessage(message: StoredMessage) {
const storedMessage = mapV1MessageToStoredMessage(message);
switch (storedMessage.type) {
case "human":
return new HumanMessage(storedMessage.data);
case "ai":
return new AIMessage(storedMessage.data);
case "system":
return new SystemMessage(storedMessage.data);
case "function":
if (storedMessage.data.name === undefined) {
throw new Error("Name must be defined for function messages");
}
return new FunctionMessage(
storedMessage.data as FunctionMessageFieldsWithName
);
case "tool":
if (storedMessage.data.tool_call_id === undefined) {
throw new Error("Tool call ID must be defined for tool messages");
}
return new ToolMessage(
storedMessage.data as ToolMessageFieldsWithToolCallId
);
case "generic": {
if (storedMessage.data.role === undefined) {
throw new Error("Role must be defined for chat messages");
}
return new ChatMessage(storedMessage.data as ChatMessageFieldsWithRole);
}
default:
throw new Error(`Got unexpected type: ${storedMessage.type}`);
}
}
/**
* Transforms an array of `StoredMessage` instances into an array of
* `BaseMessage` instances. It uses the `mapV1MessageToStoredMessage`
* function to ensure all messages are in the `StoredMessage` format, then
* creates new instances of the appropriate `BaseMessage` subclass based
* on the type of each message. This function is used to prepare stored
* messages for use in a chat context.
*/
export function mapStoredMessagesToChatMessages(
messages: StoredMessage[]
): BaseMessage[] {
return messages.map(mapStoredMessageToChatMessage);
}
/**
* Transforms an array of `BaseMessage` instances into an array of
* `StoredMessage` instances. It does this by calling the `toDict` method
* on each `BaseMessage`, which returns a `StoredMessage`. This function
* is used to prepare chat messages for storage.
*/
export function mapChatMessagesToStoredMessages(
messages: BaseMessage[]
): StoredMessage[] {
return messages.map((message) => message.toDict());
}
export function convertToChunk(message: BaseMessage) {
const type = message._getType();
if (type === "human") {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return new HumanMessageChunk({ ...message });
} else if (type === "ai") {
let aiChunkFields: AIMessageChunkFields = {
...message,
};
if ("tool_calls" in aiChunkFields) {
aiChunkFields = {
...aiChunkFields,
tool_call_chunks: aiChunkFields.tool_calls?.map((tc) => ({
...tc,
type: "tool_call_chunk",
index: undefined,
args: JSON.stringify(tc.args),
})),
};
}
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return new AIMessageChunk({ ...aiChunkFields });
} else if (type === "system") {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return new SystemMessageChunk({ ...message });
} else if (type === "function") {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return new FunctionMessageChunk({ ...message });
// eslint-disable-next-line @typescript-eslint/no-use-before-define
} else if (ChatMessage.isInstance(message)) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return new ChatMessageChunk({ ...message });
} else {
throw new Error("Unknown message type.");
}
}