-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathcommon.ts
712 lines (676 loc) Β· 21.4 KB
/
common.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
import type {
MessageContentComplex,
BaseMessage,
UsageMetadata,
} from "@langchain/core/messages";
import {
AIMessage,
AIMessageChunk,
ToolMessage,
} from "@langchain/core/messages";
import type { ToolCall } from "@langchain/core/messages/tool";
import { isOpenAITool } from "@langchain/core/language_models/base";
import type {
Message as BedrockMessage,
SystemContentBlock as BedrockSystemContentBlock,
Tool as BedrockTool,
ContentBlock,
ImageFormat,
ConverseResponse,
ContentBlockDeltaEvent,
ConverseStreamMetadataEvent,
ContentBlockStartEvent,
ReasoningContentBlock,
ReasoningContentBlockDelta,
ReasoningTextBlock,
} from "@aws-sdk/client-bedrock-runtime";
import type { DocumentType as __DocumentType } from "@smithy/types";
import { isLangChainTool } from "@langchain/core/utils/function_calling";
import { zodToJsonSchema } from "zod-to-json-schema";
import { ChatGenerationChunk } from "@langchain/core/outputs";
import {
ChatBedrockConverseToolType,
BedrockToolChoice,
MessageContentReasoningBlock,
MessageContentReasoningBlockReasoningText,
MessageContentReasoningBlockReasoningTextPartial,
MessageContentReasoningBlockRedacted,
} from "./types.js";
export function extractImageInfo(base64: string): ContentBlock.ImageMember {
// Extract the format from the base64 string
const formatMatch = base64.match(/^data:image\/(\w+);base64,/);
let format: ImageFormat | undefined;
if (formatMatch) {
const extractedFormat = formatMatch[1].toLowerCase();
if (["gif", "jpeg", "png", "webp"].includes(extractedFormat)) {
format = extractedFormat as ImageFormat;
}
}
// Remove the data URL prefix if present
const base64Data = base64.replace(/^data:image\/\w+;base64,/, "");
// Convert base64 to Uint8Array
const binaryString = atob(base64Data);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i += 1) {
bytes[i] = binaryString.charCodeAt(i);
}
return {
image: {
format,
source: {
bytes,
},
},
};
}
export function convertToConverseMessages(messages: BaseMessage[]): {
converseMessages: BedrockMessage[];
converseSystem: BedrockSystemContentBlock[];
} {
const converseSystem: BedrockSystemContentBlock[] = messages
.filter((msg) => msg._getType() === "system")
.map((msg) => {
if (typeof msg.content === "string") {
return { text: msg.content };
} else if (msg.content.length === 1 && msg.content[0].type === "text") {
return { text: msg.content[0].text };
}
throw new Error(
"System message content must be either a string, or a content array containing a single text object."
);
});
const converseMessages: BedrockMessage[] = messages
.filter((msg) => msg._getType() !== "system")
.map((msg) => {
if (msg._getType() === "ai") {
const castMsg = msg as AIMessage;
const assistantMsg: BedrockMessage = {
role: "assistant",
content: [],
};
if (typeof castMsg.content === "string" && castMsg.content !== "") {
assistantMsg.content?.push({
text: castMsg.content,
});
} else if (Array.isArray(castMsg.content)) {
const concatenatedBlocks = concatenateLangchainReasoningBlocks(
castMsg.content
);
const contentBlocks: ContentBlock[] = concatenatedBlocks.map(
(block) => {
if (block.type === "text" && block.text !== "") {
return {
text: block.text,
};
} else if (block.type === "reasoning_content") {
return {
reasoningContent:
langchainReasoningBlockToBedrockReasoningBlock(
block as MessageContentReasoningBlock
),
};
} else {
const blockValues = Object.fromEntries(
Object.entries(block).filter(([key]) => key !== "type")
);
throw new Error(
`Unsupported content block type: ${
block.type
} with content of ${JSON.stringify(blockValues, null, 2)}`
);
}
}
);
assistantMsg.content = [
...(assistantMsg.content ? assistantMsg.content : []),
...contentBlocks,
];
}
// Important: this must be placed after any reasoning content blocks, else claude models will return an error.
if (castMsg.tool_calls && castMsg.tool_calls.length) {
assistantMsg.content = [
...(assistantMsg.content ? assistantMsg.content : []),
...castMsg.tool_calls.map((tc) => ({
toolUse: {
toolUseId: tc.id,
name: tc.name,
input: tc.args,
},
})),
];
}
return assistantMsg;
} else if (msg._getType() === "human" || msg._getType() === "generic") {
if (typeof msg.content === "string" && msg.content !== "") {
return {
role: "user" as const,
content: [
{
text: msg.content,
},
],
};
} else if (Array.isArray(msg.content)) {
const contentBlocks: ContentBlock[] = msg.content.flatMap((block) => {
if (block.type === "image_url") {
const base64: string =
typeof block.image_url === "string"
? block.image_url
: block.image_url.url;
return extractImageInfo(base64);
} else if (block.type === "text") {
return {
text: block.text,
};
} else if (
block.type === "document" &&
block.document !== undefined
) {
return {
document: block.document,
};
} else if (block.type === "image" && block.image !== undefined) {
return {
image: block.image,
};
} else {
throw new Error(`Unsupported content block type: ${block.type}`);
}
});
return {
role: "user" as const,
content: contentBlocks,
};
} else {
throw new Error(
`Invalid message content: empty string. '${msg._getType()}' must contain non-empty content.`
);
}
} else if (msg._getType() === "tool") {
const castMsg = msg as ToolMessage;
if (typeof castMsg.content === "string") {
return {
// Tool use messages are always from the user
role: "user" as const,
content: [
{
toolResult: {
toolUseId: castMsg.tool_call_id,
content: [
{
text: castMsg.content,
},
],
},
},
],
};
} else {
return {
// Tool use messages are always from the user
role: "user" as const,
content: [
{
toolResult: {
toolUseId: castMsg.tool_call_id,
content: [
{
json: castMsg.content,
},
],
},
},
],
};
}
} else {
throw new Error(`Unsupported message type: ${msg._getType()}`);
}
});
// Combine consecutive user tool result messages into a single message
const combinedConverseMessages = converseMessages.reduce<BedrockMessage[]>(
(acc, curr) => {
const lastMessage = acc[acc.length - 1];
if (
lastMessage &&
lastMessage.role === "user" &&
lastMessage.content?.some((c) => "toolResult" in c) &&
curr.role === "user" &&
curr.content?.some((c) => "toolResult" in c)
) {
lastMessage.content = lastMessage.content.concat(curr.content);
} else {
acc.push(curr);
}
return acc;
},
[]
);
return { converseMessages: combinedConverseMessages, converseSystem };
}
export function isBedrockTool(tool: unknown): tool is BedrockTool {
if (typeof tool === "object" && tool && "toolSpec" in tool) {
return true;
}
return false;
}
export function convertToConverseTools(
tools: ChatBedrockConverseToolType[]
): BedrockTool[] {
if (tools.every(isOpenAITool)) {
return tools.map((tool) => ({
toolSpec: {
name: tool.function.name,
description: tool.function.description,
inputSchema: {
json: tool.function.parameters as __DocumentType,
},
},
}));
} else if (tools.every(isLangChainTool)) {
return tools.map((tool) => ({
toolSpec: {
name: tool.name,
description: tool.description,
inputSchema: {
json: zodToJsonSchema(tool.schema) as __DocumentType,
},
},
}));
} else if (tools.every(isBedrockTool)) {
return tools;
}
throw new Error(
"Invalid tools passed. Must be an array of StructuredToolInterface, ToolDefinition, or BedrockTool."
);
}
export type BedrockConverseToolChoice =
| "any"
| "auto"
| string
| BedrockToolChoice;
export function convertToBedrockToolChoice(
toolChoice: BedrockConverseToolChoice,
tools: BedrockTool[],
fields: {
model: string;
supportsToolChoiceValues?: Array<"auto" | "any" | "tool">;
}
): BedrockToolChoice {
const supportsToolChoiceValues = fields.supportsToolChoiceValues ?? [];
let bedrockToolChoice: BedrockToolChoice;
if (typeof toolChoice === "string") {
switch (toolChoice) {
case "any":
bedrockToolChoice = {
any: {},
};
break;
case "auto":
bedrockToolChoice = {
auto: {},
};
break;
default: {
const foundTool = tools.find(
(tool) => tool.toolSpec?.name === toolChoice
);
if (!foundTool) {
throw new Error(
`Tool with name ${toolChoice} not found in tools list.`
);
}
bedrockToolChoice = {
tool: {
name: toolChoice,
},
};
}
}
} else {
bedrockToolChoice = toolChoice;
}
const toolChoiceType = Object.keys(bedrockToolChoice)[0] as
| "auto"
| "any"
| "tool";
if (!supportsToolChoiceValues.includes(toolChoiceType)) {
let supportedTxt = "";
if (supportsToolChoiceValues.length) {
supportedTxt =
`Model ${fields.model} does not currently support 'tool_choice' ` +
`of type ${toolChoiceType}. The following 'tool_choice' types ` +
`are supported: ${supportsToolChoiceValues.join(", ")}.`;
} else {
supportedTxt = `Model ${fields.model} does not currently support 'tool_choice'.`;
}
throw new Error(
`${supportedTxt} Please see` +
"https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolChoice.html" +
"for the latest documentation on models that support tool choice."
);
}
return bedrockToolChoice;
}
export function convertConverseMessageToLangChainMessage(
message: BedrockMessage,
responseMetadata: Omit<ConverseResponse, "output">
): BaseMessage {
if (!message.content) {
throw new Error("No message content found in response.");
}
if (message.role !== "assistant") {
throw new Error(
`Unsupported message role received in ChatBedrockConverse response: ${message.role}`
);
}
let requestId: string | undefined;
if (
"$metadata" in responseMetadata &&
responseMetadata.$metadata &&
typeof responseMetadata.$metadata === "object" &&
"requestId" in responseMetadata.$metadata
) {
requestId = responseMetadata.$metadata.requestId as string;
}
let tokenUsage: UsageMetadata | undefined;
if (responseMetadata.usage) {
const input_tokens = responseMetadata.usage.inputTokens ?? 0;
const output_tokens = responseMetadata.usage.outputTokens ?? 0;
tokenUsage = {
input_tokens,
output_tokens,
total_tokens:
responseMetadata.usage.totalTokens ?? input_tokens + output_tokens,
};
}
if (
message.content?.length === 1 &&
"text" in message.content[0] &&
typeof message.content[0].text === "string"
) {
return new AIMessage({
content: message.content[0].text,
response_metadata: responseMetadata,
usage_metadata: tokenUsage,
id: requestId,
});
} else {
const toolCalls: ToolCall[] = [];
const content: MessageContentComplex[] = [];
message.content.forEach((c) => {
if (
"toolUse" in c &&
c.toolUse &&
c.toolUse.name &&
c.toolUse.input &&
typeof c.toolUse.input === "object"
) {
toolCalls.push({
id: c.toolUse.toolUseId,
name: c.toolUse.name,
args: c.toolUse.input,
type: "tool_call",
});
} else if ("text" in c && typeof c.text === "string") {
content.push({ type: "text", text: c.text });
} else if ("reasoningContent" in c) {
content.push(
bedrockReasoningBlockToLangchainReasoningBlock(
c.reasoningContent as ReasoningContentBlock
)
);
} else {
content.push(c);
}
});
return new AIMessage({
content: content.length ? content : "",
tool_calls: toolCalls.length ? toolCalls : undefined,
response_metadata: responseMetadata,
usage_metadata: tokenUsage,
id: requestId,
});
}
}
export function handleConverseStreamContentBlockDelta(
contentBlockDelta: ContentBlockDeltaEvent
): ChatGenerationChunk {
if (!contentBlockDelta.delta) {
throw new Error("No delta found in content block.");
}
if (typeof contentBlockDelta.delta.text === "string") {
return new ChatGenerationChunk({
text: contentBlockDelta.delta.text,
message: new AIMessageChunk({
content: contentBlockDelta.delta.text,
}),
});
} else if (contentBlockDelta.delta.toolUse) {
const index = contentBlockDelta.contentBlockIndex;
return new ChatGenerationChunk({
text: "",
message: new AIMessageChunk({
content: "",
tool_call_chunks: [
{
args: contentBlockDelta.delta.toolUse.input,
index,
type: "tool_call_chunk",
},
],
}),
});
} else if (contentBlockDelta.delta.reasoningContent) {
return new ChatGenerationChunk({
text: "",
message: new AIMessageChunk({
content: [
bedrockReasoningDeltaToLangchainPartialReasoningBlock(
contentBlockDelta.delta.reasoningContent
),
],
}),
});
} else {
throw new Error(
`Unsupported content block type(s): ${JSON.stringify(
contentBlockDelta.delta,
null,
2
)}`
);
}
}
export function handleConverseStreamContentBlockStart(
contentBlockStart: ContentBlockStartEvent
): ChatGenerationChunk {
const index = contentBlockStart.contentBlockIndex;
if (contentBlockStart.start?.toolUse) {
return new ChatGenerationChunk({
text: "",
message: new AIMessageChunk({
content: "",
tool_call_chunks: [
{
name: contentBlockStart.start.toolUse.name,
id: contentBlockStart.start.toolUse.toolUseId,
index,
type: "tool_call_chunk",
},
],
}),
});
}
throw new Error("Unsupported content block start event.");
}
export function handleConverseStreamMetadata(
metadata: ConverseStreamMetadataEvent,
extra: {
streamUsage: boolean;
}
): ChatGenerationChunk {
const inputTokens = metadata.usage?.inputTokens ?? 0;
const outputTokens = metadata.usage?.outputTokens ?? 0;
const usage_metadata: UsageMetadata = {
input_tokens: inputTokens,
output_tokens: outputTokens,
total_tokens: metadata.usage?.totalTokens ?? inputTokens + outputTokens,
};
return new ChatGenerationChunk({
text: "",
message: new AIMessageChunk({
content: "",
usage_metadata: extra.streamUsage ? usage_metadata : undefined,
response_metadata: {
// Use the same key as returned from the Converse API
metadata,
},
}),
});
}
export function bedrockReasoningDeltaToLangchainPartialReasoningBlock(
reasoningContent: ReasoningContentBlockDelta
):
| MessageContentReasoningBlockReasoningTextPartial
| MessageContentReasoningBlockRedacted {
const { text, redactedContent, signature } = reasoningContent;
if (typeof text === "string") {
return {
type: "reasoning_content",
reasoningText: { text },
};
}
if (signature) {
return {
type: "reasoning_content",
reasoningText: { signature },
};
}
if (redactedContent) {
return {
type: "reasoning_content",
redactedContent: Buffer.from(redactedContent).toString("base64"),
};
}
throw new Error("Invalid reasoning content");
}
export function bedrockReasoningBlockToLangchainReasoningBlock(
reasoningContent: ReasoningContentBlock
): MessageContentReasoningBlock {
const { reasoningText, redactedContent } = reasoningContent;
if (reasoningText) {
return {
type: "reasoning_content",
reasoningText: reasoningText as Required<ReasoningTextBlock>,
};
}
if (redactedContent) {
return {
type: "reasoning_content",
redactedContent: Buffer.from(redactedContent).toString("base64"),
};
}
throw new Error("Invalid reasoning content");
}
export function langchainReasoningBlockToBedrockReasoningBlock(
content: MessageContentReasoningBlock
): ReasoningContentBlock {
if (content.type !== "reasoning_content") {
throw new Error("Invalid reasoning content");
}
if ("reasoningText" in content) {
return {
reasoningText: content.reasoningText as ReasoningTextBlock,
};
}
if ("redactedContent" in content) {
return {
redactedContent: Buffer.from(content.redactedContent, "base64"),
};
}
throw new Error("Invalid reasoning content");
}
export function concatenateLangchainReasoningBlocks(
content: Array<MessageContentComplex | MessageContentReasoningBlock>
): MessageContentComplex[] {
const concatenatedBlocks: MessageContentComplex[] = [];
let concatenatedBlock: Partial<MessageContentReasoningBlock> = {};
for (const block of content) {
if (block.type !== "reasoning_content") {
// if it's some other block type, end the current block, but keep it so we preserve order
if (Object.keys(concatenatedBlock).length > 0) {
concatenatedBlocks.push(
concatenatedBlock as MessageContentReasoningBlock
);
concatenatedBlock = {};
}
concatenatedBlocks.push(block);
continue;
}
// non-redacted block
if ("reasoningText" in block && typeof block.reasoningText === "object") {
if ("redactedContent" in concatenatedBlock) {
// new type of block, so end the previous one
concatenatedBlocks.push(
concatenatedBlock as MessageContentReasoningBlock
);
concatenatedBlock = {};
}
const { text, signature } = block.reasoningText as Partial<
MessageContentReasoningBlockReasoningText["reasoningText"]
>;
const { text: prevText, signature: prevSignature } = (
"reasoningText" in concatenatedBlock
? concatenatedBlock.reasoningText
: {}
) as Partial<MessageContentReasoningBlockReasoningText["reasoningText"]>;
concatenatedBlock = {
type: "reasoning_content",
reasoningText: {
...((concatenatedBlock as MessageContentReasoningBlockReasoningText)
.reasoningText ?? {}),
...(prevText !== undefined || text !== undefined
? { text: (prevText ?? "") + (text ?? "") }
: {}),
...(prevSignature !== undefined || signature !== undefined
? { signature: (prevSignature ?? "") + (signature ?? "") }
: {}),
},
};
// if a partial block chunk has a signature, the next one will begin a new reasoning block.
// full blocks always have signatures, so we start one now, anyway
if ("signature" in block.reasoningText) {
concatenatedBlocks.push(
concatenatedBlock as MessageContentReasoningBlock
);
concatenatedBlock = {};
}
}
if ("redactedContent" in block) {
if ("reasoningText" in concatenatedBlock) {
// New type of block, so end the previous one. We should't really hit
// this, as we'll have created a new block upon encountering the
// signature above, but better safe than sorry.
concatenatedBlocks.push(
concatenatedBlock as MessageContentReasoningBlock
);
concatenatedBlock = {};
}
const { redactedContent } = block;
const prevRedactedContent = (
"redactedContent" in concatenatedBlock
? concatenatedBlock.redactedContent!
: ""
) as Partial<MessageContentReasoningBlockRedacted["redactedContent"]>;
concatenatedBlock = {
type: "reasoning_content",
redactedContent: prevRedactedContent + redactedContent,
};
}
}
if (Object.keys(concatenatedBlock).length > 0) {
concatenatedBlocks.push(concatenatedBlock as MessageContentReasoningBlock);
}
return concatenatedBlocks;
}