@langchain/anthropic 0.2.4 → 0.2.6

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.
@@ -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
  ];
@@ -101,6 +104,7 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
101
104
  content: fields.coerceContentToString ? "" : [],
102
105
  additional_kwargs: filteredAdditionalKwargs,
103
106
  usage_metadata: usageMetadata,
107
+ id: data.message.id,
104
108
  }),
105
109
  usageData: usageDataCopy,
106
110
  };
@@ -667,22 +671,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
667
671
  * Get the parameters used to invoke the model
668
672
  */
669
673
  invocationParams(options) {
670
- let tool_choice;
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
- }
674
+ const tool_choice = (0, utils_js_1.handleToolChoice)(options?.tool_choice);
686
675
  return {
687
676
  model: this.model,
688
677
  temperature: this.temperature,
@@ -768,6 +757,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
768
757
  tool_call_chunks: newToolCallChunk ? [newToolCallChunk] : undefined,
769
758
  usage_metadata: chunk.usage_metadata,
770
759
  response_metadata: chunk.response_metadata,
760
+ id: chunk.id,
771
761
  }),
772
762
  text: token ?? "",
773
763
  });
@@ -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 BaseLanguageModelCallOptions, type BaseLanguageModelInput, type ToolDefinition } from "@langchain/core/language_models/base";
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
- type AnthropicToolChoice = {
19
- type: "tool";
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"
@@ -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
  ];
@@ -98,6 +101,7 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
98
101
  content: fields.coerceContentToString ? "" : [],
99
102
  additional_kwargs: filteredAdditionalKwargs,
100
103
  usage_metadata: usageMetadata,
104
+ id: data.message.id,
101
105
  }),
102
106
  usageData: usageDataCopy,
103
107
  };
@@ -663,22 +667,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
663
667
  * Get the parameters used to invoke the model
664
668
  */
665
669
  invocationParams(options) {
666
- let tool_choice;
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
- }
670
+ const tool_choice = handleToolChoice(options?.tool_choice);
682
671
  return {
683
672
  model: this.model,
684
673
  temperature: this.temperature,
@@ -764,6 +753,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
764
753
  tool_call_chunks: newToolCallChunk ? [newToolCallChunk] : undefined,
765
754
  usage_metadata: chunk.usage_metadata,
766
755
  response_metadata: chunk.response_metadata,
756
+ id: chunk.id,
767
757
  }),
768
758
  text: token ?? "",
769
759
  });
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;
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/anthropic",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "Anthropic integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {