@librechat/agents 1.7.8 → 1.7.9
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/cjs/llm/anthropic/llm.cjs +117 -0
- package/dist/cjs/llm/anthropic/llm.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +251 -0
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +135 -0
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -0
- package/dist/cjs/llm/providers.cjs +3 -2
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/llm/text.cjs +73 -0
- package/dist/cjs/llm/text.cjs.map +1 -0
- package/dist/esm/llm/anthropic/llm.mjs +115 -0
- package/dist/esm/llm/anthropic/llm.mjs.map +1 -0
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +248 -0
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -0
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +133 -0
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -0
- package/dist/esm/llm/providers.mjs +3 -2
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/llm/text.mjs +71 -0
- package/dist/esm/llm/text.mjs.map +1 -0
- package/dist/types/llm/anthropic/llm.d.ts +13 -0
- package/dist/types/llm/anthropic/types.d.ts +20 -0
- package/dist/types/llm/anthropic/utils/message_inputs.d.ts +14 -0
- package/dist/types/llm/anthropic/utils/message_outputs.d.ts +16 -0
- package/dist/types/llm/text.d.ts +21 -0
- package/package.json +3 -3
- package/src/llm/anthropic/llm.ts +151 -0
- package/src/llm/anthropic/types.ts +32 -0
- package/src/llm/anthropic/utils/message_inputs.ts +279 -0
- package/src/llm/anthropic/utils/message_outputs.ts +217 -0
- package/src/llm/providers.ts +4 -2
- package/src/llm/text.ts +90 -0
- package/src/scripts/code_exec.ts +1 -1
- package/src/scripts/code_exec_simple.ts +1 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var messages = require('@langchain/core/messages');
|
|
4
|
+
var anthropic = require('@langchain/anthropic');
|
|
5
|
+
var outputs = require('@langchain/core/outputs');
|
|
6
|
+
var message_outputs = require('./utils/message_outputs.cjs');
|
|
7
|
+
var message_inputs = require('./utils/message_inputs.cjs');
|
|
8
|
+
var text = require('../text.cjs');
|
|
9
|
+
|
|
10
|
+
function _toolsInParams(params) {
|
|
11
|
+
return !!(params.tools && params.tools.length > 0);
|
|
12
|
+
}
|
|
13
|
+
function extractToken(chunk) {
|
|
14
|
+
if (typeof chunk.content === 'string') {
|
|
15
|
+
return [chunk.content, 'string'];
|
|
16
|
+
}
|
|
17
|
+
else if (Array.isArray(chunk.content) &&
|
|
18
|
+
chunk.content.length >= 1 &&
|
|
19
|
+
'input' in chunk.content[0]) {
|
|
20
|
+
return typeof chunk.content[0].input === 'string'
|
|
21
|
+
? [chunk.content[0].input, 'input']
|
|
22
|
+
: [JSON.stringify(chunk.content[0].input), 'input'];
|
|
23
|
+
}
|
|
24
|
+
else if (Array.isArray(chunk.content) &&
|
|
25
|
+
chunk.content.length >= 1 &&
|
|
26
|
+
'text' in chunk.content[0]) {
|
|
27
|
+
return [chunk.content[0].text, 'content'];
|
|
28
|
+
}
|
|
29
|
+
return [undefined];
|
|
30
|
+
}
|
|
31
|
+
function cloneChunk(text, tokenType, chunk) {
|
|
32
|
+
if (tokenType === 'string') {
|
|
33
|
+
return new messages.AIMessageChunk(Object.assign({}, chunk, { content: text }));
|
|
34
|
+
}
|
|
35
|
+
else if (tokenType === 'input') {
|
|
36
|
+
return chunk;
|
|
37
|
+
}
|
|
38
|
+
const content = chunk.content[0];
|
|
39
|
+
if (tokenType === 'content' && content.type === 'text') {
|
|
40
|
+
return new messages.AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));
|
|
41
|
+
}
|
|
42
|
+
else if (tokenType === 'content' && content.type === 'text_delta') {
|
|
43
|
+
return new messages.AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));
|
|
44
|
+
}
|
|
45
|
+
return chunk;
|
|
46
|
+
}
|
|
47
|
+
class CustomAnthropic extends anthropic.ChatAnthropicMessages {
|
|
48
|
+
_lc_stream_delay;
|
|
49
|
+
constructor(fields) {
|
|
50
|
+
super(fields);
|
|
51
|
+
this._lc_stream_delay = fields._lc_stream_delay ?? 25;
|
|
52
|
+
}
|
|
53
|
+
async *_streamResponseChunks(messages$1, options, runManager) {
|
|
54
|
+
const params = this.invocationParams(options);
|
|
55
|
+
const formattedMessages = message_inputs._convertMessagesToAnthropicPayload(messages$1);
|
|
56
|
+
const coerceContentToString = !_toolsInParams({
|
|
57
|
+
...params,
|
|
58
|
+
...formattedMessages,
|
|
59
|
+
stream: false,
|
|
60
|
+
});
|
|
61
|
+
const stream = await this.createStreamWithRetry({
|
|
62
|
+
...params,
|
|
63
|
+
...formattedMessages,
|
|
64
|
+
stream: true,
|
|
65
|
+
}, {
|
|
66
|
+
headers: options.headers,
|
|
67
|
+
});
|
|
68
|
+
for await (const data of stream) {
|
|
69
|
+
if (options.signal?.aborted) {
|
|
70
|
+
stream.controller.abort();
|
|
71
|
+
throw new Error('AbortError: User aborted the request.');
|
|
72
|
+
}
|
|
73
|
+
const shouldStreamUsage = this.streamUsage ?? options.streamUsage;
|
|
74
|
+
const result = message_outputs._makeMessageChunkFromAnthropicEvent(data, {
|
|
75
|
+
streamUsage: shouldStreamUsage,
|
|
76
|
+
coerceContentToString,
|
|
77
|
+
});
|
|
78
|
+
if (!result)
|
|
79
|
+
continue;
|
|
80
|
+
const { chunk } = result;
|
|
81
|
+
// Extract the text content token for text field and runManager.
|
|
82
|
+
const [token = '', tokenType] = extractToken(chunk);
|
|
83
|
+
const createGenerationChunk = (text, incomingChunk) => {
|
|
84
|
+
return new outputs.ChatGenerationChunk({
|
|
85
|
+
message: new messages.AIMessageChunk({
|
|
86
|
+
// Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().
|
|
87
|
+
content: incomingChunk.content,
|
|
88
|
+
additional_kwargs: incomingChunk.additional_kwargs,
|
|
89
|
+
tool_call_chunks: incomingChunk.tool_call_chunks,
|
|
90
|
+
usage_metadata: shouldStreamUsage ? incomingChunk.usage_metadata : undefined,
|
|
91
|
+
response_metadata: incomingChunk.response_metadata,
|
|
92
|
+
id: incomingChunk.id,
|
|
93
|
+
}),
|
|
94
|
+
text,
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
if (!tokenType || tokenType === 'input') {
|
|
98
|
+
const generationChunk = createGenerationChunk(token, chunk);
|
|
99
|
+
yield generationChunk;
|
|
100
|
+
await runManager?.handleLLMNewToken(token, undefined, undefined, undefined, undefined, { chunk: generationChunk });
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const textStream = new text.TextStream(token, {
|
|
104
|
+
delay: this._lc_stream_delay,
|
|
105
|
+
});
|
|
106
|
+
for await (const currentToken of textStream.generateText()) {
|
|
107
|
+
const newChunk = cloneChunk(currentToken, tokenType, chunk);
|
|
108
|
+
const generationChunk = createGenerationChunk(currentToken, newChunk);
|
|
109
|
+
yield generationChunk;
|
|
110
|
+
await runManager?.handleLLMNewToken(token, undefined, undefined, undefined, undefined, { chunk: generationChunk });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
exports.CustomAnthropic = CustomAnthropic;
|
|
117
|
+
//# sourceMappingURL=llm.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm.cjs","sources":["../../../../src/llm/anthropic/llm.ts"],"sourcesContent":["import { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatAnthropicMessages } from '@langchain/anthropic';\nimport { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { AnthropicInput } from '@langchain/anthropic';\nimport type { AnthropicMessageCreateParams } from '@/llm/anthropic/types';\nimport { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs';\nimport { _convertMessagesToAnthropicPayload } from './utils/message_inputs';\nimport { TextStream } from '@/llm/text';\n\nfunction _toolsInParams(params: AnthropicMessageCreateParams): boolean {\n return !!(params.tools && params.tools.length > 0);\n}\n\nfunction extractToken(chunk: AIMessageChunk): [string, 'string' | 'input' | 'content'] | [undefined] {\n if (typeof chunk.content === 'string') {\n return [chunk.content, 'string'];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'input' in chunk.content[0]\n ) {\n return typeof chunk.content[0].input === 'string'\n ? [chunk.content[0].input, 'input']\n : [JSON.stringify(chunk.content[0].input), 'input'];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'text' in chunk.content[0]\n ) {\n return [chunk.content[0].text, 'content'];\n }\n return [undefined];\n}\n\nfunction cloneChunk(text: string, tokenType: string, chunk: AIMessageChunk): AIMessageChunk {\n if (tokenType === 'string') {\n return new AIMessageChunk(Object.assign({}, chunk, { content: text }));\n } else if (tokenType === 'input') {\n return chunk;\n }\n const content = chunk.content[0] as MessageContentComplex;\n if (tokenType === 'content' && content.type === 'text') {\n return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));\n } else if (tokenType === 'content' && content.type === 'text_delta') {\n return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));\n }\n\n return chunk;\n}\n\nexport type CustomAnthropicInput = AnthropicInput & { _lc_stream_delay?: number };\n\nexport class CustomAnthropic extends ChatAnthropicMessages {\n _lc_stream_delay: number;\n constructor(fields: CustomAnthropicInput) {\n super(fields);\n this._lc_stream_delay = fields._lc_stream_delay ?? 25;\n }\n\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const params = this.invocationParams(options);\n const formattedMessages = _convertMessagesToAnthropicPayload(messages);\n const coerceContentToString = !_toolsInParams({\n ...params,\n ...formattedMessages,\n stream: false,\n });\n\n const stream = await this.createStreamWithRetry(\n {\n ...params,\n ...formattedMessages,\n stream: true,\n },\n {\n headers: options.headers,\n }\n );\n\n for await (const data of stream) {\n if (options.signal?.aborted) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n const shouldStreamUsage = this.streamUsage ?? options.streamUsage;\n const result = _makeMessageChunkFromAnthropicEvent(data, {\n streamUsage: shouldStreamUsage,\n coerceContentToString,\n });\n if (!result) continue;\n\n const { chunk } = result;\n\n // Extract the text content token for text field and runManager.\n const [token = '', tokenType] = extractToken(chunk);\n const createGenerationChunk = (text: string, incomingChunk: AIMessageChunk): ChatGenerationChunk => {\n return new ChatGenerationChunk({\n message: new AIMessageChunk({\n // Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().\n content: incomingChunk.content,\n additional_kwargs: incomingChunk.additional_kwargs,\n tool_call_chunks: incomingChunk.tool_call_chunks,\n usage_metadata: shouldStreamUsage ? incomingChunk.usage_metadata : undefined,\n response_metadata: incomingChunk.response_metadata,\n id: incomingChunk.id,\n }),\n text,\n });\n };\n\n if (!tokenType || tokenType === 'input') {\n const generationChunk = createGenerationChunk(token, chunk);\n yield generationChunk;\n await runManager?.handleLLMNewToken(\n token,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: generationChunk }\n );\n continue;\n }\n\n const textStream = new TextStream(token, {\n delay: this._lc_stream_delay,\n });\n for await (const currentToken of textStream.generateText()) {\n const newChunk = cloneChunk(currentToken, tokenType, chunk);\n const generationChunk = createGenerationChunk(currentToken, newChunk);\n yield generationChunk;\n\n await runManager?.handleLLMNewToken(\n token,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: generationChunk }\n );\n }\n }\n }\n\n}\n"],"names":["AIMessageChunk","ChatAnthropicMessages","messages","_convertMessagesToAnthropicPayload","_makeMessageChunkFromAnthropicEvent","ChatGenerationChunk","TextStream"],"mappings":";;;;;;;;;AAWA,SAAS,cAAc,CAAC,MAAoC,EAAA;AAC1D,IAAA,OAAO,CAAC,EAAE,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACrD,CAAC;AAED,SAAS,YAAY,CAAC,KAAqB,EAAA;AACzC,IAAA,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,EAAE;AACrC,QAAA,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;KAClC;AAAM,SAAA,IACL,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC;AAC5B,QAAA,KAAK,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC;QACzB,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAC3B;QACA,OAAO,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,KAAK,QAAQ;AAC/C,cAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,OAAO,CAAC;AACnC,cAAE,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC;KACvD;AAAM,SAAA,IACL,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC;AAC5B,QAAA,KAAK,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAC1B;AACA,QAAA,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;KAC3C;IACD,OAAO,CAAC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AAS,EACT,SAAS,EACT,EAAE,KAAK,EAAE,eAAe,EAAE,CAC3B,CAAC;aACH;SACF;KACF;AAEF;;;;"}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var messages = require('@langchain/core/messages');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* This util file contains functions for converting LangChain messages to Anthropic messages.
|
|
7
|
+
*/
|
|
8
|
+
function _formatImage(imageUrl) {
|
|
9
|
+
const regex = /^data:(image\/.+);base64,(.+)$/;
|
|
10
|
+
const match = imageUrl.match(regex);
|
|
11
|
+
if (match === null) {
|
|
12
|
+
throw new Error([
|
|
13
|
+
'Anthropic only supports base64-encoded images currently.',
|
|
14
|
+
'Example: data:image/png;base64,/9j/4AAQSk...',
|
|
15
|
+
].join('\n\n'));
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
type: 'base64',
|
|
19
|
+
media_type: match[1] ?? '',
|
|
20
|
+
data: match[2] ?? '',
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function _mergeMessages(messages$1) {
|
|
25
|
+
// Merge runs of human/tool messages into single human messages with content blocks.
|
|
26
|
+
const merged = [];
|
|
27
|
+
for (const message of messages$1) {
|
|
28
|
+
if (message._getType() === 'tool') {
|
|
29
|
+
if (typeof message.content === 'string') {
|
|
30
|
+
const previousMessage = merged[merged.length - 1];
|
|
31
|
+
if (previousMessage &&
|
|
32
|
+
previousMessage._getType() === 'human' &&
|
|
33
|
+
Array.isArray(previousMessage.content) &&
|
|
34
|
+
'type' in previousMessage.content[0] &&
|
|
35
|
+
previousMessage.content[0].type === 'tool_result') {
|
|
36
|
+
// If the previous message was a tool result, we merge this tool message into it.
|
|
37
|
+
previousMessage.content.push({
|
|
38
|
+
type: 'tool_result',
|
|
39
|
+
content: message.content,
|
|
40
|
+
tool_use_id: message.tool_call_id,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
// If not, we create a new human message with the tool result.
|
|
45
|
+
merged.push(new messages.HumanMessage({
|
|
46
|
+
content: [
|
|
47
|
+
{
|
|
48
|
+
type: 'tool_result',
|
|
49
|
+
content: message.content,
|
|
50
|
+
tool_use_id: message.tool_call_id,
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
merged.push(new messages.HumanMessage({
|
|
58
|
+
content: [
|
|
59
|
+
{
|
|
60
|
+
type: 'tool_result',
|
|
61
|
+
content: _formatContent(message.content),
|
|
62
|
+
tool_use_id: message.tool_call_id,
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
const previousMessage = merged[merged.length - 1];
|
|
70
|
+
if (previousMessage &&
|
|
71
|
+
previousMessage._getType() === 'human' &&
|
|
72
|
+
message._getType() === 'human') {
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
+
let combinedContent;
|
|
75
|
+
if (typeof previousMessage.content === 'string') {
|
|
76
|
+
combinedContent = [{ type: 'text', text: previousMessage.content }];
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
combinedContent = previousMessage.content;
|
|
80
|
+
}
|
|
81
|
+
if (typeof message.content === 'string') {
|
|
82
|
+
combinedContent.push({ type: 'text', text: message.content });
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
combinedContent = combinedContent.concat(message.content);
|
|
86
|
+
}
|
|
87
|
+
previousMessage.content = combinedContent;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
merged.push(message);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return merged;
|
|
95
|
+
}
|
|
96
|
+
function _convertLangChainToolCallToAnthropic(toolCall) {
|
|
97
|
+
if (toolCall.id === undefined) {
|
|
98
|
+
throw new Error('Anthropic requires all tool calls to have an "id".');
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
type: 'tool_use',
|
|
102
|
+
id: toolCall.id,
|
|
103
|
+
name: toolCall.name,
|
|
104
|
+
input: toolCall.args,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
108
|
+
function _formatContent(content) {
|
|
109
|
+
const toolTypes = ['tool_use', 'tool_result', 'input_json_delta'];
|
|
110
|
+
const textTypes = ['text', 'text_delta'];
|
|
111
|
+
if (typeof content === 'string') {
|
|
112
|
+
return content;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
const contentBlocks = content.map((contentPart) => {
|
|
116
|
+
const cacheControl = 'cache_control' in contentPart ? contentPart.cache_control : undefined;
|
|
117
|
+
if (contentPart.type === 'image_url') {
|
|
118
|
+
let source;
|
|
119
|
+
if (typeof contentPart.image_url === 'string') {
|
|
120
|
+
source = _formatImage(contentPart.image_url);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
source = _formatImage(contentPart.image_url.url);
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
type: 'image', // Explicitly setting the type as "image"
|
|
127
|
+
source,
|
|
128
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
else if (textTypes.find((t) => t === contentPart.type) &&
|
|
132
|
+
'text' in contentPart) {
|
|
133
|
+
// Assuming contentPart is of type MessageContentText here
|
|
134
|
+
return {
|
|
135
|
+
type: 'text', // Explicitly setting the type as "text"
|
|
136
|
+
text: contentPart.text,
|
|
137
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
else if (toolTypes.find((t) => t === contentPart.type)) {
|
|
141
|
+
const contentPartCopy = { ...contentPart };
|
|
142
|
+
if ('index' in contentPartCopy) {
|
|
143
|
+
// Anthropic does not support passing the index field here, so we remove it.
|
|
144
|
+
delete contentPartCopy.index;
|
|
145
|
+
}
|
|
146
|
+
if (contentPartCopy.type === 'input_json_delta') {
|
|
147
|
+
// `input_json_delta` type only represents yielding partial tool inputs
|
|
148
|
+
// and is not a valid type for Anthropic messages.
|
|
149
|
+
contentPartCopy.type = 'tool_use';
|
|
150
|
+
}
|
|
151
|
+
if ('input' in contentPartCopy) {
|
|
152
|
+
// Anthropic tool use inputs should be valid objects, when applicable.
|
|
153
|
+
try {
|
|
154
|
+
contentPartCopy.input = JSON.parse(contentPartCopy.input);
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// no-op
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// TODO: Fix when SDK types are fixed
|
|
161
|
+
return {
|
|
162
|
+
...contentPartCopy,
|
|
163
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
164
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
throw new Error('Unsupported message content format');
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
return contentBlocks;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Formats messages as a prompt for the model.
|
|
176
|
+
* Used in LangSmith, export is important here.
|
|
177
|
+
* @param messages The base messages to format as a prompt.
|
|
178
|
+
* @returns The formatted prompt.
|
|
179
|
+
*/
|
|
180
|
+
function _convertMessagesToAnthropicPayload(messages$1) {
|
|
181
|
+
const mergedMessages = _mergeMessages(messages$1);
|
|
182
|
+
let system;
|
|
183
|
+
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {
|
|
184
|
+
system = messages$1[0].content;
|
|
185
|
+
}
|
|
186
|
+
const conversationMessages = system !== undefined ? mergedMessages.slice(1) : mergedMessages;
|
|
187
|
+
const formattedMessages = conversationMessages.map((message) => {
|
|
188
|
+
let role;
|
|
189
|
+
if (message._getType() === 'human') {
|
|
190
|
+
role = 'user';
|
|
191
|
+
}
|
|
192
|
+
else if (message._getType() === 'ai') {
|
|
193
|
+
role = 'assistant';
|
|
194
|
+
}
|
|
195
|
+
else if (message._getType() === 'tool') {
|
|
196
|
+
role = 'user';
|
|
197
|
+
}
|
|
198
|
+
else if (message._getType() === 'system') {
|
|
199
|
+
throw new Error('System messages are only permitted as the first passed message.');
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
throw new Error(`Message type "${message._getType()}" is not supported.`);
|
|
203
|
+
}
|
|
204
|
+
if (messages.isAIMessage(message) && !!message.tool_calls?.length) {
|
|
205
|
+
if (typeof message.content === 'string') {
|
|
206
|
+
if (message.content === '') {
|
|
207
|
+
return {
|
|
208
|
+
role,
|
|
209
|
+
content: message.tool_calls.map(_convertLangChainToolCallToAnthropic),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
return {
|
|
214
|
+
role,
|
|
215
|
+
content: [
|
|
216
|
+
{ type: 'text', text: message.content },
|
|
217
|
+
...message.tool_calls.map(_convertLangChainToolCallToAnthropic),
|
|
218
|
+
],
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
const { content } = message;
|
|
224
|
+
const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) => content.find((contentPart) => (contentPart.type === 'tool_use' ||
|
|
225
|
+
contentPart.type === 'input_json_delta') &&
|
|
226
|
+
contentPart.id === toolCall.id));
|
|
227
|
+
if (hasMismatchedToolCalls) {
|
|
228
|
+
console.warn('The "tool_calls" field on a message is only respected if content is a string.');
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
role,
|
|
232
|
+
content: _formatContent(message.content),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
return {
|
|
238
|
+
role,
|
|
239
|
+
content: _formatContent(message.content),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
return {
|
|
244
|
+
messages: formattedMessages,
|
|
245
|
+
system,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
exports._convertLangChainToolCallToAnthropic = _convertLangChainToolCallToAnthropic;
|
|
250
|
+
exports._convertMessagesToAnthropicPayload = _convertMessagesToAnthropicPayload;
|
|
251
|
+
//# sourceMappingURL=message_inputs.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message_inputs.cjs","sources":["../../../../../src/llm/anthropic/utils/message_inputs.ts"],"sourcesContent":["/**\n * This util file contains functions for converting LangChain messages to Anthropic messages.\n */\nimport {\n BaseMessage,\n SystemMessage,\n HumanMessage,\n AIMessage,\n ToolMessage,\n MessageContent,\n isAIMessage,\n} from '@langchain/core/messages';\nimport { ToolCall } from '@langchain/core/messages/tool';\nimport type {\n AnthropicMessageCreateParams,\n AnthropicToolResponse,\n} from '@/llm/anthropic/types';\n\nfunction _formatImage(imageUrl: string): { type: string; media_type: string; data: string } {\n const regex = /^data:(image\\/.+);base64,(.+)$/;\n const match = imageUrl.match(regex);\n if (match === null) {\n throw new Error(\n [\n 'Anthropic only supports base64-encoded images currently.',\n 'Example: data:image/png;base64,/9j/4AAQSk...',\n ].join('\\n\\n')\n );\n }\n return {\n type: 'base64',\n media_type: match[1] ?? '',\n data: match[2] ?? '',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n}\n\nfunction _mergeMessages(\n messages: BaseMessage[]\n): (SystemMessage | HumanMessage | AIMessage)[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const merged = [];\n for (const message of messages) {\n if (message._getType() === 'tool') {\n if (typeof message.content === 'string') {\n const previousMessage = merged[merged.length - 1] as BaseMessage | undefined;\n if (\n previousMessage &&\n previousMessage._getType() === 'human' &&\n Array.isArray(previousMessage.content) &&\n 'type' in previousMessage.content[0] &&\n previousMessage.content[0].type === 'tool_result'\n ) {\n // If the previous message was a tool result, we merge this tool message into it.\n previousMessage.content.push({\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n });\n } else {\n // If not, we create a new human message with the tool result.\n merged.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n merged.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: _formatContent(message.content),\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n const previousMessage = merged[merged.length - 1] as BaseMessage | undefined;\n if (\n previousMessage &&\n previousMessage._getType() === 'human' &&\n message._getType() === 'human'\n ) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let combinedContent: Record<string, any>[];\n if (typeof previousMessage.content === 'string') {\n combinedContent = [{ type: 'text', text: previousMessage.content }];\n } else {\n combinedContent = previousMessage.content;\n }\n if (typeof message.content === 'string') {\n combinedContent.push({ type: 'text', text: message.content });\n } else {\n combinedContent = combinedContent.concat(message.content);\n }\n previousMessage.content = combinedContent;\n } else {\n merged.push(message);\n }\n }\n }\n return merged;\n}\n\nexport function _convertLangChainToolCallToAnthropic(\n toolCall: ToolCall\n): AnthropicToolResponse {\n if (toolCall.id === undefined) {\n throw new Error('Anthropic requires all tool calls to have an \"id\".');\n }\n return {\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.args,\n };\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _formatContent(content: MessageContent): string | Record<string, any>[] {\n const toolTypes = ['tool_use', 'tool_result', 'input_json_delta'];\n const textTypes = ['text', 'text_delta'];\n\n if (typeof content === 'string') {\n return content;\n } else {\n const contentBlocks = content.map((contentPart) => {\n const cacheControl =\n 'cache_control' in contentPart ? contentPart.cache_control : undefined;\n\n if (contentPart.type === 'image_url') {\n let source;\n if (typeof contentPart.image_url === 'string') {\n source = _formatImage(contentPart.image_url);\n } else {\n source = _formatImage(contentPart.image_url.url);\n }\n return {\n type: 'image' as const, // Explicitly setting the type as \"image\"\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (\n textTypes.find((t) => t === contentPart.type) &&\n 'text' in contentPart\n ) {\n // Assuming contentPart is of type MessageContentText here\n return {\n type: 'text' as const, // Explicitly setting the type as \"text\"\n text: contentPart.text,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (toolTypes.find((t) => t === contentPart.type)) {\n const contentPartCopy = { ...contentPart };\n if ('index' in contentPartCopy) {\n // Anthropic does not support passing the index field here, so we remove it.\n delete contentPartCopy.index;\n }\n\n if (contentPartCopy.type === 'input_json_delta') {\n // `input_json_delta` type only represents yielding partial tool inputs\n // and is not a valid type for Anthropic messages.\n contentPartCopy.type = 'tool_use';\n }\n\n if ('input' in contentPartCopy) {\n // Anthropic tool use inputs should be valid objects, when applicable.\n try {\n contentPartCopy.input = JSON.parse(contentPartCopy.input);\n } catch {\n // no-op\n }\n }\n\n // TODO: Fix when SDK types are fixed\n return {\n ...contentPartCopy,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n } else {\n throw new Error('Unsupported message content format');\n }\n });\n return contentBlocks;\n }\n}\n\n/**\n * Formats messages as a prompt for the model.\n * Used in LangSmith, export is important here.\n * @param messages The base messages to format as a prompt.\n * @returns The formatted prompt.\n */\nexport function _convertMessagesToAnthropicPayload(\n messages: BaseMessage[]\n): AnthropicMessageCreateParams {\n const mergedMessages = _mergeMessages(messages);\n let system;\n if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {\n system = messages[0].content;\n }\n const conversationMessages =\n system !== undefined ? mergedMessages.slice(1) : mergedMessages;\n const formattedMessages = conversationMessages.map((message) => {\n let role;\n if (message._getType() === 'human') {\n role = 'user' as const;\n } else if (message._getType() === 'ai') {\n role = 'assistant' as const;\n } else if (message._getType() === 'tool') {\n role = 'user' as const;\n } else if (message._getType() === 'system') {\n throw new Error(\n 'System messages are only permitted as the first passed message.'\n );\n } else {\n throw new Error(`Message type \"${message._getType()}\" is not supported.`);\n }\n if (isAIMessage(message) && !!message.tool_calls?.length) {\n if (typeof message.content === 'string') {\n if (message.content === '') {\n return {\n role,\n content: message.tool_calls.map(\n _convertLangChainToolCallToAnthropic\n ),\n };\n } else {\n return {\n role,\n content: [\n { type: 'text', text: message.content },\n ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n const { content } = message;\n const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) =>\n content.find(\n (contentPart) =>\n (contentPart.type === 'tool_use' ||\n contentPart.type === 'input_json_delta') &&\n contentPart.id === toolCall.id\n )\n );\n if (hasMismatchedToolCalls) {\n console.warn(\n 'The \"tool_calls\" field on a message is only respected if content is a string.'\n );\n }\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n } else {\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n });\n return {\n messages: formattedMessages,\n system,\n } as AnthropicMessageCreateParams;\n}"],"names":["messages","HumanMessage","isAIMessage"],"mappings":";;;;AAAA;;AAEG;AAgBH,SAAS,YAAY,CAAC,QAAgB,EAAA;IACpC,MAAM,KAAK,GAAG,gCAAgC,CAAC;IAC/C,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACpC,IAAA,IAAI,KAAK,KAAK,IAAI,EAAE;QAClB,MAAM,IAAI,KAAK,CACb;YACE,0DAA0D;YAC1D,8CAA8C;AAC/C,SAAA,CAAC,IAAI,CAAC,MAAM,CAAC,CACf,CAAC;KACH;IACD,OAAO;AACL,QAAA,IAAI,EAAE,QAAQ;AACd,QAAA,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE;AAC1B,QAAA,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE;;KAEd,CAAC;A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IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE;AACvC,4BAAA,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,oCAAoC,CAAC;AAChE,yBAAA;qBACF,CAAC;iBACH;aACF;iBAAM;AACL,gBAAA,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;AAC5B,gBAAA,MAAM,sBAAsB,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,QAAQ,KAChE,OAAO,CAAC,IAAI,CACV,CAAC,WAAW,KACV,CAAC,WAAW,CAAC,IAAI,KAAK,UAAU;AAC9B,oBAAA,WAAW,CAAC,IAAI,KAAK,kBAAkB;oBACzC,WAAW,CAAC,EAAE,KAAK,QAAQ,CAAC,EAAE,CACjC,CACF,CAAC;gBACF,IAAI,sBAAsB,EAAE;AAC1B,oBAAA,OAAO,CAAC,IAAI,CACV,+EAA+E,CAChF,CAAC;iBACH;gBACD,OAAO;oBACL,IAAI;AACJ,oBAAA,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC;iBACzC,CAAC;aACH;SACF;aAAM;YACL,OAAO;gBACL,IAAI;AACJ,gBAAA,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC;aACzC,CAAC;SACH;AACH,KAAC,CAAC,CAAC;IACH,OAAO;AACL,QAAA,QAAQ,EAAE,iBAAiB;QAC3B,MAAM;KACyB,CAAC;AACpC;;;;;"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var messages = require('@langchain/core/messages');
|
|
4
|
+
|
|
5
|
+
function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
6
|
+
if (data.type === 'message_start') {
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
8
|
+
const { content, usage, ...additionalKwargs } = data.message;
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
const filteredAdditionalKwargs = {};
|
|
11
|
+
for (const [key, value] of Object.entries(additionalKwargs)) {
|
|
12
|
+
if (value !== undefined && value !== null) {
|
|
13
|
+
filteredAdditionalKwargs[key] = value;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const usageMetadata = {
|
|
17
|
+
input_tokens: usage.input_tokens,
|
|
18
|
+
output_tokens: usage.output_tokens,
|
|
19
|
+
total_tokens: usage.input_tokens + usage.output_tokens,
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
chunk: new messages.AIMessageChunk({
|
|
23
|
+
content: fields.coerceContentToString ? '' : [],
|
|
24
|
+
additional_kwargs: filteredAdditionalKwargs,
|
|
25
|
+
usage_metadata: fields.streamUsage ? usageMetadata : undefined,
|
|
26
|
+
id: data.message.id,
|
|
27
|
+
}),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
else if (data.type === 'message_delta') {
|
|
31
|
+
const usageMetadata = {
|
|
32
|
+
input_tokens: 0,
|
|
33
|
+
output_tokens: data.usage.output_tokens,
|
|
34
|
+
total_tokens: data.usage.output_tokens,
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
chunk: new messages.AIMessageChunk({
|
|
38
|
+
content: fields.coerceContentToString ? '' : [],
|
|
39
|
+
additional_kwargs: { ...data.delta },
|
|
40
|
+
usage_metadata: fields.streamUsage ? usageMetadata : undefined,
|
|
41
|
+
}),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
else if (data.type === 'content_block_start' &&
|
|
45
|
+
data.content_block.type === 'tool_use') {
|
|
46
|
+
const toolCallContentBlock = data.content_block;
|
|
47
|
+
return {
|
|
48
|
+
chunk: new messages.AIMessageChunk({
|
|
49
|
+
content: fields.coerceContentToString
|
|
50
|
+
? ''
|
|
51
|
+
: [
|
|
52
|
+
{
|
|
53
|
+
index: data.index,
|
|
54
|
+
...data.content_block,
|
|
55
|
+
input: '',
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
additional_kwargs: {},
|
|
59
|
+
tool_call_chunks: [
|
|
60
|
+
{
|
|
61
|
+
id: toolCallContentBlock.id,
|
|
62
|
+
index: data.index,
|
|
63
|
+
name: toolCallContentBlock.name,
|
|
64
|
+
args: '',
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
}),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
else if (data.type === 'content_block_delta' &&
|
|
71
|
+
data.delta.type === 'text_delta') {
|
|
72
|
+
const content = data.delta.text;
|
|
73
|
+
if (content !== undefined) {
|
|
74
|
+
return {
|
|
75
|
+
chunk: new messages.AIMessageChunk({
|
|
76
|
+
content: fields.coerceContentToString
|
|
77
|
+
? content
|
|
78
|
+
: [
|
|
79
|
+
{
|
|
80
|
+
index: data.index,
|
|
81
|
+
...data.delta,
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
additional_kwargs: {},
|
|
85
|
+
}),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else if (data.type === 'content_block_delta' &&
|
|
90
|
+
data.delta.type === 'input_json_delta') {
|
|
91
|
+
return {
|
|
92
|
+
chunk: new messages.AIMessageChunk({
|
|
93
|
+
content: fields.coerceContentToString
|
|
94
|
+
? ''
|
|
95
|
+
: [
|
|
96
|
+
{
|
|
97
|
+
index: data.index,
|
|
98
|
+
input: data.delta.partial_json,
|
|
99
|
+
type: data.delta.type,
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
additional_kwargs: {},
|
|
103
|
+
tool_call_chunks: [
|
|
104
|
+
{
|
|
105
|
+
index: data.index,
|
|
106
|
+
args: data.delta.partial_json,
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
}),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
else if (data.type === 'content_block_start' &&
|
|
113
|
+
data.content_block.type === 'text') {
|
|
114
|
+
const content = data.content_block.text;
|
|
115
|
+
if (content !== undefined) {
|
|
116
|
+
return {
|
|
117
|
+
chunk: new messages.AIMessageChunk({
|
|
118
|
+
content: fields.coerceContentToString
|
|
119
|
+
? content
|
|
120
|
+
: [
|
|
121
|
+
{
|
|
122
|
+
index: data.index,
|
|
123
|
+
...data.content_block,
|
|
124
|
+
},
|
|
125
|
+
],
|
|
126
|
+
additional_kwargs: {},
|
|
127
|
+
}),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
exports._makeMessageChunkFromAnthropicEvent = _makeMessageChunkFromAnthropicEvent;
|
|
135
|
+
//# sourceMappingURL=message_outputs.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message_outputs.cjs","sources":["../../../../../src/llm/anthropic/utils/message_outputs.ts"],"sourcesContent":["/**\n * This util file contains functions for converting Anthropic messages to LangChain messages.\n */\nimport Anthropic from '@anthropic-ai/sdk';\nimport {\n AIMessage,\n UsageMetadata,\n AIMessageChunk,\n} from '@langchain/core/messages';\nimport { ToolCall } from '@langchain/core/messages/tool';\nimport { ChatGeneration } from '@langchain/core/outputs';\nimport { AnthropicMessageResponse } from '../types.js';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function extractToolCalls(content: Record<string, any>[]): ToolCall[] {\n const toolCalls: ToolCall[] = [];\n for (const block of content) {\n if (block.type === 'tool_use') {\n toolCalls.push({\n name: block.name,\n args: block.input,\n id: block.id,\n type: 'tool_call',\n });\n }\n }\n return toolCalls;\n}\n\nexport function _makeMessageChunkFromAnthropicEvent(\n data: Anthropic.Messages.RawMessageStreamEvent,\n fields: {\n streamUsage: boolean;\n coerceContentToString: boolean;\n }\n): {\n chunk: AIMessageChunk;\n} | null {\n if (data.type === 'message_start') {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { content, usage, ...additionalKwargs } = data.message;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const filteredAdditionalKwargs: Record<string, any> = {};\n for (const [key, value] of Object.entries(additionalKwargs)) {\n if (value !== undefined && value !== null) {\n filteredAdditionalKwargs[key] = value;\n }\n }\n const usageMetadata: UsageMetadata = {\n input_tokens: usage.input_tokens,\n output_tokens: usage.output_tokens,\n total_tokens: usage.input_tokens + usage.output_tokens,\n };\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString ? '' : [],\n additional_kwargs: filteredAdditionalKwargs,\n usage_metadata: fields.streamUsage ? usageMetadata : undefined,\n id: data.message.id,\n }),\n };\n } else if (data.type === 'message_delta') {\n const usageMetadata: UsageMetadata = {\n input_tokens: 0,\n output_tokens: data.usage.output_tokens,\n total_tokens: data.usage.output_tokens,\n };\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString ? '' : [],\n additional_kwargs: { ...data.delta },\n usage_metadata: fields.streamUsage ? usageMetadata : undefined,\n }),\n };\n } else if (\n data.type === 'content_block_start' &&\n data.content_block.type === 'tool_use'\n ) {\n const toolCallContentBlock =\n data.content_block as Anthropic.Messages.ToolUseBlock;\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? ''\n : [\n {\n index: data.index,\n ...data.content_block,\n input: '',\n },\n ],\n additional_kwargs: {},\n tool_call_chunks: [\n {\n id: toolCallContentBlock.id,\n index: data.index,\n name: toolCallContentBlock.name,\n args: '',\n },\n ],\n }),\n };\n } else if (\n data.type === 'content_block_delta' &&\n data.delta.type === 'text_delta'\n ) {\n const content = data.delta.text;\n if (content !== undefined) {\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? content\n : [\n {\n index: data.index,\n ...data.delta,\n },\n ],\n additional_kwargs: {},\n }),\n };\n }\n } else if (\n data.type === 'content_block_delta' &&\n data.delta.type === 'input_json_delta'\n ) {\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? ''\n : [\n {\n index: data.index,\n input: data.delta.partial_json,\n type: data.delta.type,\n },\n ],\n additional_kwargs: {},\n tool_call_chunks: [\n {\n index: data.index,\n args: data.delta.partial_json,\n },\n ],\n }),\n };\n } else if (\n data.type === 'content_block_start' &&\n data.content_block.type === 'text'\n ) {\n const content = data.content_block.text;\n if (content !== undefined) {\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? content\n : [\n {\n index: data.index,\n ...data.content_block,\n },\n ],\n additional_kwargs: {},\n }),\n };\n }\n }\n\n return null;\n}\n\nexport function anthropicResponseToChatMessages(\n messages: AnthropicMessageResponse[],\n additionalKwargs: Record<string, unknown>\n): ChatGeneration[] {\n const usage: Record<string, number> | null | undefined =\n additionalKwargs.usage as Record<string, number> | null | undefined;\n const usageMetadata =\n usage != null\n ? {\n input_tokens: usage.input_tokens ?? 0,\n output_tokens: usage.output_tokens ?? 0,\n total_tokens: (usage.input_tokens ?? 0) + (usage.output_tokens ?? 0),\n }\n : undefined;\n if (messages.length === 1 && messages[0].type === 'text') {\n return [\n {\n text: messages[0].text,\n message: new AIMessage({\n content: messages[0].text,\n additional_kwargs: additionalKwargs,\n usage_metadata: usageMetadata,\n response_metadata: additionalKwargs,\n id: additionalKwargs.id as string,\n }),\n },\n ];\n } else {\n const toolCalls = extractToolCalls(messages);\n const generations: ChatGeneration[] = [\n {\n text: '',\n message: new AIMessage({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n content: messages as any,\n additional_kwargs: additionalKwargs,\n tool_calls: toolCalls,\n usage_metadata: usageMetadata,\n response_metadata: additionalKwargs,\n id: additionalKwargs.id as string,\n }),\n },\n ];\n return generations;\n }\n}"],"names":["AIMessageChunk"],"mappings":";;;;AA6BgB,SAAA,mCAAmC,CACjD,IAA8C,EAC9C,MAGC,EAAA;AAID,IAAA,IAAI,IAAI,CAAC,IAAI,KAAK,eAAe,EAAE;;AAEjC,QAAA,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,gBAAgB,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;;QAE7D,MAAM,wBAAwB,GAAwB,EAAE,CAAC;A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5
|
var aws = require('@langchain/aws');
|
|
6
|
-
var anthropic = require('@langchain/anthropic');
|
|
7
6
|
var mistralai = require('@langchain/mistralai');
|
|
8
7
|
var googleVertexai = require('@langchain/google-vertexai');
|
|
9
8
|
var web = require('@langchain/community/chat_models/bedrock/web');
|
|
10
9
|
var _enum = require('../common/enum.cjs');
|
|
10
|
+
var llm = require('./anthropic/llm.cjs');
|
|
11
11
|
|
|
12
12
|
// src/llm/providers.ts
|
|
13
13
|
const llmProviders = {
|
|
@@ -17,7 +17,8 @@ const llmProviders = {
|
|
|
17
17
|
[_enum.Providers.BEDROCK_LEGACY]: web.BedrockChat,
|
|
18
18
|
[_enum.Providers.MISTRALAI]: mistralai.ChatMistralAI,
|
|
19
19
|
[_enum.Providers.BEDROCK]: aws.ChatBedrockConverse,
|
|
20
|
-
[_enum.Providers.ANTHROPIC]:
|
|
20
|
+
[_enum.Providers.ANTHROPIC]: llm.CustomAnthropic,
|
|
21
|
+
// [Providers.ANTHROPIC]: CustomAnthropic,
|
|
21
22
|
};
|
|
22
23
|
const manualToolStreamProviders = new Set([_enum.Providers.ANTHROPIC, _enum.Providers.BEDROCK, _enum.Providers.OLLAMA]);
|
|
23
24
|
const getChatModelClass = (provider) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"providers.cjs","sources":["../../../src/llm/providers.ts"],"sourcesContent":["// src/llm/providers.ts\nimport { ChatOpenAI } from '@langchain/openai';\nimport { ChatOllama } from '@langchain/ollama';\nimport { ChatBedrockConverse } from '@langchain/aws';\
|
|
1
|
+
{"version":3,"file":"providers.cjs","sources":["../../../src/llm/providers.ts"],"sourcesContent":["// src/llm/providers.ts\nimport { ChatOpenAI } from '@langchain/openai';\nimport { ChatOllama } from '@langchain/ollama';\nimport { ChatBedrockConverse } from '@langchain/aws';\n// import { ChatAnthropic } from '@langchain/anthropic';\nimport { ChatMistralAI } from '@langchain/mistralai';\nimport { ChatVertexAI } from '@langchain/google-vertexai';\nimport { BedrockChat } from '@langchain/community/chat_models/bedrock/web';\nimport type { ChatModelConstructorMap, ProviderOptionsMap, ChatModelMap } from '@/types';\nimport { Providers } from '@/common';\nimport { CustomAnthropic } from '@/llm/anthropic/llm';\n\nexport const llmProviders: Partial<ChatModelConstructorMap> = {\n [Providers.OPENAI]: ChatOpenAI,\n [Providers.OLLAMA]: ChatOllama,\n [Providers.VERTEXAI]: ChatVertexAI,\n [Providers.BEDROCK_LEGACY]: BedrockChat,\n [Providers.MISTRALAI]: ChatMistralAI,\n [Providers.BEDROCK]: ChatBedrockConverse,\n [Providers.ANTHROPIC]: CustomAnthropic,\n // [Providers.ANTHROPIC]: CustomAnthropic,\n};\n\nexport const manualToolStreamProviders = new Set<Providers | string>([Providers.ANTHROPIC, Providers.BEDROCK, Providers.OLLAMA]);\n\nexport const getChatModelClass = <P extends Providers>(\n provider: P\n): new (config: ProviderOptionsMap[P]) => ChatModelMap[P] => {\n const ChatModelClass = llmProviders[provider];\n if (!ChatModelClass) {\n throw new Error(`Unsupported LLM provider: ${provider}`);\n }\n\n return ChatModelClass;\n};"],"names":["Providers","ChatOpenAI","ChatOllama","ChatVertexAI","BedrockChat","ChatMistralAI","ChatBedrockConverse","CustomAnthropic"],"mappings":";;;;;;;;;;;AAAA;AAYa,MAAA,YAAY,GAAqC;AAC5D,IAAA,CAACA,eAAS,CAAC,MAAM,GAAGC,iBAAU;AAC9B,IAAA,CAACD,eAAS,CAAC,MAAM,GAAGE,iBAAU;AAC9B,IAAA,CAACF,eAAS,CAAC,QAAQ,GAAGG,2BAAY;AAClC,IAAA,CAACH,eAAS,CAAC,cAAc,GAAGI,eAAW;AACvC,IAAA,CAACJ,eAAS,CAAC,SAAS,GAAGK,uBAAa;AACpC,IAAA,CAACL,eAAS,CAAC,OAAO,GAAGM,uBAAmB;AACxC,IAAA,CAACN,eAAS,CAAC,SAAS,GAAGO,mBAAe;;EAEtC;MAEW,yBAAyB,GAAG,IAAI,GAAG,CAAqB,CAACP,eAAS,CAAC,SAAS,EAAEA,eAAS,CAAC,OAAO,EAAEA,eAAS,CAAC,MAAM,CAAC,EAAE;AAEpH,MAAA,iBAAiB,GAAG,CAC/B,QAAW,KAC+C;AAC1D,IAAA,MAAM,cAAc,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;IAC9C,IAAI,CAAC,cAAc,EAAE;AACnB,QAAA,MAAM,IAAI,KAAK,CAAC,6BAA6B,QAAQ,CAAA,CAAE,CAAC,CAAC;KAC1D;AAED,IAAA,OAAO,cAAc,CAAC;AACxB;;;;;;"}
|