@langchain/anthropic 0.3.5 → 0.3.6-rc.0
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/chat_models.cjs +1 -2
- package/dist/chat_models.js +2 -3
- package/dist/utils/message_outputs.cjs +15 -0
- package/dist/utils/message_outputs.js +15 -0
- package/dist/utils/tools.cjs +1 -49
- package/dist/utils/tools.d.ts +0 -3
- package/dist/utils/tools.js +0 -47
- package/package.json +1 -1
package/dist/chat_models.cjs
CHANGED
|
@@ -688,7 +688,6 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
688
688
|
if (!result)
|
|
689
689
|
continue;
|
|
690
690
|
const { chunk } = result;
|
|
691
|
-
const newToolCallChunk = (0, tools_js_1.extractToolCallChunk)(chunk);
|
|
692
691
|
// Extract the text content token for text field and runManager.
|
|
693
692
|
const token = extractToken(chunk);
|
|
694
693
|
const generationChunk = new outputs_1.ChatGenerationChunk({
|
|
@@ -696,7 +695,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
696
695
|
// Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().
|
|
697
696
|
content: chunk.content,
|
|
698
697
|
additional_kwargs: chunk.additional_kwargs,
|
|
699
|
-
tool_call_chunks:
|
|
698
|
+
tool_call_chunks: chunk.tool_call_chunks,
|
|
700
699
|
usage_metadata: shouldStreamUsage ? chunk.usage_metadata : undefined,
|
|
701
700
|
response_metadata: chunk.response_metadata,
|
|
702
701
|
id: chunk.id,
|
package/dist/chat_models.js
CHANGED
|
@@ -9,7 +9,7 @@ import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnable
|
|
|
9
9
|
import { isZodSchema } from "@langchain/core/utils/types";
|
|
10
10
|
import { isLangChainTool } from "@langchain/core/utils/function_calling";
|
|
11
11
|
import { AnthropicToolsOutputParser } from "./output_parsers.js";
|
|
12
|
-
import {
|
|
12
|
+
import { handleToolChoice } from "./utils/tools.js";
|
|
13
13
|
import { _convertMessagesToAnthropicPayload } from "./utils/message_inputs.js";
|
|
14
14
|
import { _makeMessageChunkFromAnthropicEvent, anthropicResponseToChatMessages, } from "./utils/message_outputs.js";
|
|
15
15
|
import { wrapAnthropicClientError } from "./utils/errors.js";
|
|
@@ -685,7 +685,6 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
685
685
|
if (!result)
|
|
686
686
|
continue;
|
|
687
687
|
const { chunk } = result;
|
|
688
|
-
const newToolCallChunk = extractToolCallChunk(chunk);
|
|
689
688
|
// Extract the text content token for text field and runManager.
|
|
690
689
|
const token = extractToken(chunk);
|
|
691
690
|
const generationChunk = new ChatGenerationChunk({
|
|
@@ -693,7 +692,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
693
692
|
// Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().
|
|
694
693
|
content: chunk.content,
|
|
695
694
|
additional_kwargs: chunk.additional_kwargs,
|
|
696
|
-
tool_call_chunks:
|
|
695
|
+
tool_call_chunks: chunk.tool_call_chunks,
|
|
697
696
|
usage_metadata: shouldStreamUsage ? chunk.usage_metadata : undefined,
|
|
698
697
|
response_metadata: chunk.response_metadata,
|
|
699
698
|
id: chunk.id,
|
|
@@ -44,6 +44,7 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
44
44
|
}
|
|
45
45
|
else if (data.type === "content_block_start" &&
|
|
46
46
|
data.content_block.type === "tool_use") {
|
|
47
|
+
const toolCallContentBlock = data.content_block;
|
|
47
48
|
return {
|
|
48
49
|
chunk: new messages_1.AIMessageChunk({
|
|
49
50
|
content: fields.coerceContentToString
|
|
@@ -56,6 +57,14 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
56
57
|
},
|
|
57
58
|
],
|
|
58
59
|
additional_kwargs: {},
|
|
60
|
+
tool_call_chunks: [
|
|
61
|
+
{
|
|
62
|
+
id: toolCallContentBlock.id,
|
|
63
|
+
index: data.index,
|
|
64
|
+
name: toolCallContentBlock.name,
|
|
65
|
+
args: "",
|
|
66
|
+
},
|
|
67
|
+
],
|
|
59
68
|
}),
|
|
60
69
|
};
|
|
61
70
|
}
|
|
@@ -92,6 +101,12 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
92
101
|
},
|
|
93
102
|
],
|
|
94
103
|
additional_kwargs: {},
|
|
104
|
+
tool_call_chunks: [
|
|
105
|
+
{
|
|
106
|
+
index: data.index,
|
|
107
|
+
args: data.delta.partial_json,
|
|
108
|
+
},
|
|
109
|
+
],
|
|
95
110
|
}),
|
|
96
111
|
};
|
|
97
112
|
}
|
|
@@ -41,6 +41,7 @@ export function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
41
41
|
}
|
|
42
42
|
else if (data.type === "content_block_start" &&
|
|
43
43
|
data.content_block.type === "tool_use") {
|
|
44
|
+
const toolCallContentBlock = data.content_block;
|
|
44
45
|
return {
|
|
45
46
|
chunk: new AIMessageChunk({
|
|
46
47
|
content: fields.coerceContentToString
|
|
@@ -53,6 +54,14 @@ export function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
53
54
|
},
|
|
54
55
|
],
|
|
55
56
|
additional_kwargs: {},
|
|
57
|
+
tool_call_chunks: [
|
|
58
|
+
{
|
|
59
|
+
id: toolCallContentBlock.id,
|
|
60
|
+
index: data.index,
|
|
61
|
+
name: toolCallContentBlock.name,
|
|
62
|
+
args: "",
|
|
63
|
+
},
|
|
64
|
+
],
|
|
56
65
|
}),
|
|
57
66
|
};
|
|
58
67
|
}
|
|
@@ -89,6 +98,12 @@ export function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
89
98
|
},
|
|
90
99
|
],
|
|
91
100
|
additional_kwargs: {},
|
|
101
|
+
tool_call_chunks: [
|
|
102
|
+
{
|
|
103
|
+
index: data.index,
|
|
104
|
+
args: data.delta.partial_json,
|
|
105
|
+
},
|
|
106
|
+
],
|
|
92
107
|
}),
|
|
93
108
|
};
|
|
94
109
|
}
|
package/dist/utils/tools.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.handleToolChoice = void 0;
|
|
4
4
|
function handleToolChoice(toolChoice) {
|
|
5
5
|
if (!toolChoice) {
|
|
6
6
|
return undefined;
|
|
@@ -26,51 +26,3 @@ function handleToolChoice(toolChoice) {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
exports.handleToolChoice = handleToolChoice;
|
|
29
|
-
function extractToolCallChunk(chunk) {
|
|
30
|
-
let newToolCallChunk;
|
|
31
|
-
// Initial chunk for tool calls from anthropic contains identifying information like ID and name.
|
|
32
|
-
// This chunk does not contain any input JSON.
|
|
33
|
-
const toolUseChunks = Array.isArray(chunk.content)
|
|
34
|
-
? chunk.content.find((c) => c.type === "tool_use")
|
|
35
|
-
: undefined;
|
|
36
|
-
if (toolUseChunks &&
|
|
37
|
-
"index" in toolUseChunks &&
|
|
38
|
-
"name" in toolUseChunks &&
|
|
39
|
-
"id" in toolUseChunks) {
|
|
40
|
-
newToolCallChunk = {
|
|
41
|
-
args: "",
|
|
42
|
-
id: toolUseChunks.id,
|
|
43
|
-
name: toolUseChunks.name,
|
|
44
|
-
index: toolUseChunks.index,
|
|
45
|
-
type: "tool_call_chunk",
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
// Chunks after the initial chunk only contain the index and partial JSON.
|
|
49
|
-
const inputJsonDeltaChunks = Array.isArray(chunk.content)
|
|
50
|
-
? chunk.content.find((c) => c.type === "input_json_delta")
|
|
51
|
-
: undefined;
|
|
52
|
-
if (inputJsonDeltaChunks &&
|
|
53
|
-
"index" in inputJsonDeltaChunks &&
|
|
54
|
-
"input" in inputJsonDeltaChunks) {
|
|
55
|
-
if (typeof inputJsonDeltaChunks.input === "string") {
|
|
56
|
-
newToolCallChunk = {
|
|
57
|
-
id: inputJsonDeltaChunks.id,
|
|
58
|
-
name: inputJsonDeltaChunks.name,
|
|
59
|
-
args: inputJsonDeltaChunks.input,
|
|
60
|
-
index: inputJsonDeltaChunks.index,
|
|
61
|
-
type: "tool_call_chunk",
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
newToolCallChunk = {
|
|
66
|
-
id: inputJsonDeltaChunks.id,
|
|
67
|
-
name: inputJsonDeltaChunks.name,
|
|
68
|
-
args: JSON.stringify(inputJsonDeltaChunks.input, null, 2),
|
|
69
|
-
index: inputJsonDeltaChunks.index,
|
|
70
|
-
type: "tool_call_chunk",
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return newToolCallChunk;
|
|
75
|
-
}
|
|
76
|
-
exports.extractToolCallChunk = extractToolCallChunk;
|
package/dist/utils/tools.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
1
|
import type { MessageCreateParams } from "@anthropic-ai/sdk/resources/index.mjs";
|
|
2
|
-
import { AIMessageChunk } from "@langchain/core/messages";
|
|
3
|
-
import { ToolCallChunk } from "@langchain/core/messages/tool";
|
|
4
2
|
import { AnthropicToolChoice } from "../types.js";
|
|
5
3
|
export declare function handleToolChoice(toolChoice?: AnthropicToolChoice): MessageCreateParams.ToolChoiceAuto | MessageCreateParams.ToolChoiceAny | MessageCreateParams.ToolChoiceTool | undefined;
|
|
6
|
-
export declare function extractToolCallChunk(chunk: AIMessageChunk): ToolCallChunk | undefined;
|
package/dist/utils/tools.js
CHANGED
|
@@ -22,50 +22,3 @@ export function handleToolChoice(toolChoice) {
|
|
|
22
22
|
return toolChoice;
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
export function extractToolCallChunk(chunk) {
|
|
26
|
-
let newToolCallChunk;
|
|
27
|
-
// Initial chunk for tool calls from anthropic contains identifying information like ID and name.
|
|
28
|
-
// This chunk does not contain any input JSON.
|
|
29
|
-
const toolUseChunks = Array.isArray(chunk.content)
|
|
30
|
-
? chunk.content.find((c) => c.type === "tool_use")
|
|
31
|
-
: undefined;
|
|
32
|
-
if (toolUseChunks &&
|
|
33
|
-
"index" in toolUseChunks &&
|
|
34
|
-
"name" in toolUseChunks &&
|
|
35
|
-
"id" in toolUseChunks) {
|
|
36
|
-
newToolCallChunk = {
|
|
37
|
-
args: "",
|
|
38
|
-
id: toolUseChunks.id,
|
|
39
|
-
name: toolUseChunks.name,
|
|
40
|
-
index: toolUseChunks.index,
|
|
41
|
-
type: "tool_call_chunk",
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
// Chunks after the initial chunk only contain the index and partial JSON.
|
|
45
|
-
const inputJsonDeltaChunks = Array.isArray(chunk.content)
|
|
46
|
-
? chunk.content.find((c) => c.type === "input_json_delta")
|
|
47
|
-
: undefined;
|
|
48
|
-
if (inputJsonDeltaChunks &&
|
|
49
|
-
"index" in inputJsonDeltaChunks &&
|
|
50
|
-
"input" in inputJsonDeltaChunks) {
|
|
51
|
-
if (typeof inputJsonDeltaChunks.input === "string") {
|
|
52
|
-
newToolCallChunk = {
|
|
53
|
-
id: inputJsonDeltaChunks.id,
|
|
54
|
-
name: inputJsonDeltaChunks.name,
|
|
55
|
-
args: inputJsonDeltaChunks.input,
|
|
56
|
-
index: inputJsonDeltaChunks.index,
|
|
57
|
-
type: "tool_call_chunk",
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
newToolCallChunk = {
|
|
62
|
-
id: inputJsonDeltaChunks.id,
|
|
63
|
-
name: inputJsonDeltaChunks.name,
|
|
64
|
-
args: JSON.stringify(inputJsonDeltaChunks.input, null, 2),
|
|
65
|
-
index: inputJsonDeltaChunks.index,
|
|
66
|
-
type: "tool_call_chunk",
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return newToolCallChunk;
|
|
71
|
-
}
|