@langchain/anthropic 0.2.4 → 0.2.5
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 +7 -16
- package/dist/chat_models.d.ts +5 -8
- package/dist/chat_models.js +7 -16
- package/dist/utils.cjs +28 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +24 -0
- package/package.json +1 -1
package/dist/chat_models.cjs
CHANGED
|
@@ -12,6 +12,7 @@ const runnables_1 = require("@langchain/core/runnables");
|
|
|
12
12
|
const types_1 = require("@langchain/core/utils/types");
|
|
13
13
|
const stream_1 = require("@langchain/core/utils/stream");
|
|
14
14
|
const output_parsers_js_1 = require("./output_parsers.cjs");
|
|
15
|
+
const utils_js_1 = require("./utils.cjs");
|
|
15
16
|
function _toolsInParams(params) {
|
|
16
17
|
return !!(params.tools && params.tools.length > 0);
|
|
17
18
|
}
|
|
@@ -49,6 +50,7 @@ function anthropicResponseToChatMessages(messages, additionalKwargs) {
|
|
|
49
50
|
additional_kwargs: additionalKwargs,
|
|
50
51
|
usage_metadata: usageMetadata,
|
|
51
52
|
response_metadata: additionalKwargs,
|
|
53
|
+
id: additionalKwargs.id,
|
|
52
54
|
}),
|
|
53
55
|
},
|
|
54
56
|
];
|
|
@@ -65,6 +67,7 @@ function anthropicResponseToChatMessages(messages, additionalKwargs) {
|
|
|
65
67
|
tool_calls: toolCalls,
|
|
66
68
|
usage_metadata: usageMetadata,
|
|
67
69
|
response_metadata: additionalKwargs,
|
|
70
|
+
id: additionalKwargs.id,
|
|
68
71
|
}),
|
|
69
72
|
},
|
|
70
73
|
];
|
|
@@ -96,11 +99,13 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
96
99
|
total_tokens: usage.input_tokens + usage.output_tokens,
|
|
97
100
|
};
|
|
98
101
|
}
|
|
102
|
+
console.log("data.message", data.message);
|
|
99
103
|
return {
|
|
100
104
|
chunk: new messages_1.AIMessageChunk({
|
|
101
105
|
content: fields.coerceContentToString ? "" : [],
|
|
102
106
|
additional_kwargs: filteredAdditionalKwargs,
|
|
103
107
|
usage_metadata: usageMetadata,
|
|
108
|
+
id: data.message.id,
|
|
104
109
|
}),
|
|
105
110
|
usageData: usageDataCopy,
|
|
106
111
|
};
|
|
@@ -667,22 +672,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
667
672
|
* Get the parameters used to invoke the model
|
|
668
673
|
*/
|
|
669
674
|
invocationParams(options) {
|
|
670
|
-
|
|
671
|
-
if (options?.tool_choice) {
|
|
672
|
-
if (options?.tool_choice === "any") {
|
|
673
|
-
tool_choice = {
|
|
674
|
-
type: "any",
|
|
675
|
-
};
|
|
676
|
-
}
|
|
677
|
-
else if (options?.tool_choice === "auto") {
|
|
678
|
-
tool_choice = {
|
|
679
|
-
type: "auto",
|
|
680
|
-
};
|
|
681
|
-
}
|
|
682
|
-
else {
|
|
683
|
-
tool_choice = options?.tool_choice;
|
|
684
|
-
}
|
|
685
|
-
}
|
|
675
|
+
const tool_choice = (0, utils_js_1.handleToolChoice)(options?.tool_choice);
|
|
686
676
|
return {
|
|
687
677
|
model: this.model,
|
|
688
678
|
temperature: this.temperature,
|
|
@@ -768,6 +758,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
768
758
|
tool_call_chunks: newToolCallChunk ? [newToolCallChunk] : undefined,
|
|
769
759
|
usage_metadata: chunk.usage_metadata,
|
|
770
760
|
response_metadata: chunk.response_metadata,
|
|
761
|
+
id: chunk.id,
|
|
771
762
|
}),
|
|
772
763
|
text: token ?? "",
|
|
773
764
|
});
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -3,24 +3,21 @@ import type { Stream } from "@anthropic-ai/sdk/streaming";
|
|
|
3
3
|
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
|
|
4
4
|
import { AIMessageChunk, type BaseMessage } from "@langchain/core/messages";
|
|
5
5
|
import { ChatGeneration, ChatGenerationChunk, type ChatResult } from "@langchain/core/outputs";
|
|
6
|
-
import { BaseChatModel, LangSmithParams, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
|
|
7
|
-
import { type StructuredOutputMethodOptions, type
|
|
6
|
+
import { BaseChatModel, BaseChatModelCallOptions, LangSmithParams, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
|
|
7
|
+
import { type StructuredOutputMethodOptions, type BaseLanguageModelInput, type ToolDefinition } from "@langchain/core/language_models/base";
|
|
8
8
|
import { StructuredToolInterface } from "@langchain/core/tools";
|
|
9
9
|
import { Runnable, RunnableToolLike } from "@langchain/core/runnables";
|
|
10
10
|
import { ToolCall } from "@langchain/core/messages/tool";
|
|
11
11
|
import { z } from "zod";
|
|
12
12
|
import type { Tool as AnthropicTool } from "@anthropic-ai/sdk/resources/index.mjs";
|
|
13
13
|
import { AnthropicToolResponse } from "./types.js";
|
|
14
|
+
import { AnthropicToolChoice, AnthropicToolTypes } from "./utils.js";
|
|
14
15
|
type AnthropicMessageCreateParams = Anthropic.MessageCreateParamsNonStreaming;
|
|
15
16
|
type AnthropicStreamingMessageCreateParams = Anthropic.MessageCreateParamsStreaming;
|
|
16
17
|
type AnthropicMessageStreamEvent = Anthropic.MessageStreamEvent;
|
|
17
18
|
type AnthropicRequestOptions = Anthropic.RequestOptions;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
name: string;
|
|
21
|
-
} | "any" | "auto";
|
|
22
|
-
export interface ChatAnthropicCallOptions extends BaseLanguageModelCallOptions, Pick<AnthropicInput, "streamUsage"> {
|
|
23
|
-
tools?: (StructuredToolInterface | AnthropicTool | Record<string, unknown> | ToolDefinition | RunnableToolLike)[];
|
|
19
|
+
export interface ChatAnthropicCallOptions extends BaseChatModelCallOptions, Pick<AnthropicInput, "streamUsage"> {
|
|
20
|
+
tools?: AnthropicToolTypes[];
|
|
24
21
|
/**
|
|
25
22
|
* Whether or not to specify what tool the model should use
|
|
26
23
|
* @default "auto"
|
package/dist/chat_models.js
CHANGED
|
@@ -9,6 +9,7 @@ import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnable
|
|
|
9
9
|
import { isZodSchema } from "@langchain/core/utils/types";
|
|
10
10
|
import { concat } from "@langchain/core/utils/stream";
|
|
11
11
|
import { AnthropicToolsOutputParser, extractToolCalls, } from "./output_parsers.js";
|
|
12
|
+
import { handleToolChoice, } from "./utils.js";
|
|
12
13
|
function _toolsInParams(params) {
|
|
13
14
|
return !!(params.tools && params.tools.length > 0);
|
|
14
15
|
}
|
|
@@ -46,6 +47,7 @@ function anthropicResponseToChatMessages(messages, additionalKwargs) {
|
|
|
46
47
|
additional_kwargs: additionalKwargs,
|
|
47
48
|
usage_metadata: usageMetadata,
|
|
48
49
|
response_metadata: additionalKwargs,
|
|
50
|
+
id: additionalKwargs.id,
|
|
49
51
|
}),
|
|
50
52
|
},
|
|
51
53
|
];
|
|
@@ -62,6 +64,7 @@ function anthropicResponseToChatMessages(messages, additionalKwargs) {
|
|
|
62
64
|
tool_calls: toolCalls,
|
|
63
65
|
usage_metadata: usageMetadata,
|
|
64
66
|
response_metadata: additionalKwargs,
|
|
67
|
+
id: additionalKwargs.id,
|
|
65
68
|
}),
|
|
66
69
|
},
|
|
67
70
|
];
|
|
@@ -93,11 +96,13 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
93
96
|
total_tokens: usage.input_tokens + usage.output_tokens,
|
|
94
97
|
};
|
|
95
98
|
}
|
|
99
|
+
console.log("data.message", data.message);
|
|
96
100
|
return {
|
|
97
101
|
chunk: new AIMessageChunk({
|
|
98
102
|
content: fields.coerceContentToString ? "" : [],
|
|
99
103
|
additional_kwargs: filteredAdditionalKwargs,
|
|
100
104
|
usage_metadata: usageMetadata,
|
|
105
|
+
id: data.message.id,
|
|
101
106
|
}),
|
|
102
107
|
usageData: usageDataCopy,
|
|
103
108
|
};
|
|
@@ -663,22 +668,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
663
668
|
* Get the parameters used to invoke the model
|
|
664
669
|
*/
|
|
665
670
|
invocationParams(options) {
|
|
666
|
-
|
|
667
|
-
if (options?.tool_choice) {
|
|
668
|
-
if (options?.tool_choice === "any") {
|
|
669
|
-
tool_choice = {
|
|
670
|
-
type: "any",
|
|
671
|
-
};
|
|
672
|
-
}
|
|
673
|
-
else if (options?.tool_choice === "auto") {
|
|
674
|
-
tool_choice = {
|
|
675
|
-
type: "auto",
|
|
676
|
-
};
|
|
677
|
-
}
|
|
678
|
-
else {
|
|
679
|
-
tool_choice = options?.tool_choice;
|
|
680
|
-
}
|
|
681
|
-
}
|
|
671
|
+
const tool_choice = handleToolChoice(options?.tool_choice);
|
|
682
672
|
return {
|
|
683
673
|
model: this.model,
|
|
684
674
|
temperature: this.temperature,
|
|
@@ -764,6 +754,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
764
754
|
tool_call_chunks: newToolCallChunk ? [newToolCallChunk] : undefined,
|
|
765
755
|
usage_metadata: chunk.usage_metadata,
|
|
766
756
|
response_metadata: chunk.response_metadata,
|
|
757
|
+
id: chunk.id,
|
|
767
758
|
}),
|
|
768
759
|
text: token ?? "",
|
|
769
760
|
});
|
package/dist/utils.cjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleToolChoice = void 0;
|
|
4
|
+
function handleToolChoice(toolChoice) {
|
|
5
|
+
if (!toolChoice) {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
else if (toolChoice === "any") {
|
|
9
|
+
return {
|
|
10
|
+
type: "any",
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
else if (toolChoice === "auto") {
|
|
14
|
+
return {
|
|
15
|
+
type: "auto",
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
else if (typeof toolChoice === "string") {
|
|
19
|
+
return {
|
|
20
|
+
type: "tool",
|
|
21
|
+
name: toolChoice,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
return toolChoice;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.handleToolChoice = handleToolChoice;
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { MessageCreateParams, Tool as AnthropicTool } from "@anthropic-ai/sdk/resources/index.mjs";
|
|
2
|
+
import { ToolDefinition } from "@langchain/core/language_models/base";
|
|
3
|
+
import { RunnableToolLike } from "@langchain/core/runnables";
|
|
4
|
+
import { StructuredToolInterface } from "@langchain/core/tools";
|
|
5
|
+
export type AnthropicToolChoice = {
|
|
6
|
+
type: "tool";
|
|
7
|
+
name: string;
|
|
8
|
+
} | "any" | "auto" | "none" | string;
|
|
9
|
+
export type AnthropicToolTypes = StructuredToolInterface | AnthropicTool | Record<string, unknown> | ToolDefinition | RunnableToolLike;
|
|
10
|
+
export declare function handleToolChoice(toolChoice?: AnthropicToolChoice): MessageCreateParams.ToolChoiceAuto | MessageCreateParams.ToolChoiceAny | MessageCreateParams.ToolChoiceTool | undefined;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function handleToolChoice(toolChoice) {
|
|
2
|
+
if (!toolChoice) {
|
|
3
|
+
return undefined;
|
|
4
|
+
}
|
|
5
|
+
else if (toolChoice === "any") {
|
|
6
|
+
return {
|
|
7
|
+
type: "any",
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
else if (toolChoice === "auto") {
|
|
11
|
+
return {
|
|
12
|
+
type: "auto",
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
else if (typeof toolChoice === "string") {
|
|
16
|
+
return {
|
|
17
|
+
type: "tool",
|
|
18
|
+
name: toolChoice,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return toolChoice;
|
|
23
|
+
}
|
|
24
|
+
}
|