@langchain/anthropic 0.2.17 → 0.3.0-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/README.md CHANGED
@@ -18,17 +18,17 @@ You can do so by adding appropriate fields to your project's `package.json` like
18
18
  "version": "0.0.0",
19
19
  "dependencies": {
20
20
  "@langchain/anthropic": "^0.0.9",
21
- "langchain": "0.0.207"
21
+ "@langchain/core": "^0.3.0"
22
22
  },
23
23
  "resolutions": {
24
- "@langchain/core": "0.1.5"
24
+ "@langchain/core": "^0.3.0"
25
25
  },
26
26
  "overrides": {
27
- "@langchain/core": "0.1.5"
27
+ "@langchain/core": "^0.3.0"
28
28
  },
29
29
  "pnpm": {
30
30
  "overrides": {
31
- "@langchain/core": "0.1.5"
31
+ "@langchain/core": "^0.3.0"
32
32
  }
33
33
  }
34
34
  }
@@ -593,31 +593,38 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
593
593
  *
594
594
  * @param {ChatAnthropicCallOptions["tools"]} tools The tools to format
595
595
  * @returns {AnthropicTool[] | undefined} The formatted tools, or undefined if none are passed.
596
+ * @throws {Error} If a mix of AnthropicTools and StructuredTools are passed.
596
597
  */
597
598
  formatStructuredToolToAnthropic(tools) {
598
599
  if (!tools || !tools.length) {
599
600
  return undefined;
600
601
  }
601
- return tools.map((tool) => {
602
- if (isAnthropicTool(tool)) {
603
- return tool;
604
- }
605
- if ((0, base_1.isOpenAITool)(tool)) {
606
- return {
607
- name: tool.function.name,
608
- description: tool.function.description,
609
- input_schema: tool.function.parameters,
610
- };
611
- }
612
- if ((0, function_calling_1.isLangChainTool)(tool)) {
613
- return {
614
- name: tool.name,
615
- description: tool.description,
616
- input_schema: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema),
617
- };
618
- }
619
- throw new Error(`Unknown tool type passed to ChatAnthropic: ${JSON.stringify(tool, null, 2)}`);
620
- });
602
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
603
+ if (tools.every((tool) => isAnthropicTool(tool))) {
604
+ // If the tool is already an anthropic tool, return it
605
+ return tools;
606
+ }
607
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
608
+ if (tools.every((tool) => (0, base_1.isOpenAITool)(tool))) {
609
+ // Formatted as OpenAI tool, convert to Anthropic tool
610
+ return tools.map((tc) => ({
611
+ name: tc.function.name,
612
+ description: tc.function.description,
613
+ input_schema: tc.function.parameters,
614
+ }));
615
+ }
616
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
617
+ if (tools.some((tool) => isAnthropicTool(tool))) {
618
+ throw new Error(`Can not pass in a mix of tool schemas to ChatAnthropic`);
619
+ }
620
+ if (tools.every(function_calling_1.isLangChainTool)) {
621
+ return tools.map((t) => ({
622
+ name: t.name,
623
+ description: t.description,
624
+ input_schema: (0, zod_to_json_schema_1.zodToJsonSchema)(t.schema),
625
+ }));
626
+ }
627
+ throw new Error("Unsupported tool type passed to ChatAnthropic");
621
628
  }
622
629
  bindTools(tools, kwargs) {
623
630
  return this.bind({
@@ -507,6 +507,7 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
507
507
  *
508
508
  * @param {ChatAnthropicCallOptions["tools"]} tools The tools to format
509
509
  * @returns {AnthropicTool[] | undefined} The formatted tools, or undefined if none are passed.
510
+ * @throws {Error} If a mix of AnthropicTools and StructuredTools are passed.
510
511
  */
511
512
  formatStructuredToolToAnthropic(tools: ChatAnthropicCallOptions["tools"]): AnthropicTool[] | undefined;
512
513
  bindTools(tools: ChatAnthropicToolType[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, CallOptions>;
@@ -552,17 +553,8 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
552
553
  generations: import("@langchain/core/outputs").ChatGeneration[];
553
554
  llmOutput: {
554
555
  id: string;
555
- /** Does nucleus sampling, in which we compute the
556
- * cumulative distribution over all the options for each
557
- * subsequent token in decreasing probability order and
558
- * cut it off once it reaches a particular probability
559
- * specified by top_p. Defaults to -1, which disables it.
560
- * Note that you should either alter temperature or top_p,
561
- * but not both.
562
- */
563
556
  model: Anthropic.Messages.Model;
564
557
  stop_reason: "tool_use" | "stop_sequence" | "end_turn" | "max_tokens" | null;
565
- /** Anthropic API URL */
566
558
  stop_sequence: string | null;
567
559
  usage: Anthropic.Messages.Usage;
568
560
  };
@@ -590,31 +590,38 @@ export class ChatAnthropicMessages extends BaseChatModel {
590
590
  *
591
591
  * @param {ChatAnthropicCallOptions["tools"]} tools The tools to format
592
592
  * @returns {AnthropicTool[] | undefined} The formatted tools, or undefined if none are passed.
593
+ * @throws {Error} If a mix of AnthropicTools and StructuredTools are passed.
593
594
  */
594
595
  formatStructuredToolToAnthropic(tools) {
595
596
  if (!tools || !tools.length) {
596
597
  return undefined;
597
598
  }
598
- return tools.map((tool) => {
599
- if (isAnthropicTool(tool)) {
600
- return tool;
601
- }
602
- if (isOpenAITool(tool)) {
603
- return {
604
- name: tool.function.name,
605
- description: tool.function.description,
606
- input_schema: tool.function.parameters,
607
- };
608
- }
609
- if (isLangChainTool(tool)) {
610
- return {
611
- name: tool.name,
612
- description: tool.description,
613
- input_schema: zodToJsonSchema(tool.schema),
614
- };
615
- }
616
- throw new Error(`Unknown tool type passed to ChatAnthropic: ${JSON.stringify(tool, null, 2)}`);
617
- });
599
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
600
+ if (tools.every((tool) => isAnthropicTool(tool))) {
601
+ // If the tool is already an anthropic tool, return it
602
+ return tools;
603
+ }
604
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
605
+ if (tools.every((tool) => isOpenAITool(tool))) {
606
+ // Formatted as OpenAI tool, convert to Anthropic tool
607
+ return tools.map((tc) => ({
608
+ name: tc.function.name,
609
+ description: tc.function.description,
610
+ input_schema: tc.function.parameters,
611
+ }));
612
+ }
613
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
614
+ if (tools.some((tool) => isAnthropicTool(tool))) {
615
+ throw new Error(`Can not pass in a mix of tool schemas to ChatAnthropic`);
616
+ }
617
+ if (tools.every(isLangChainTool)) {
618
+ return tools.map((t) => ({
619
+ name: t.name,
620
+ description: t.description,
621
+ input_schema: zodToJsonSchema(t.schema),
622
+ }));
623
+ }
624
+ throw new Error("Unsupported tool type passed to ChatAnthropic");
618
625
  }
619
626
  bindTools(tools, kwargs) {
620
627
  return this.bind({
@@ -103,7 +103,6 @@ function _formatContent(content) {
103
103
  }
104
104
  else {
105
105
  const contentBlocks = content.map((contentPart) => {
106
- const cacheControl = "cache_control" in contentPart ? contentPart.cache_control : undefined;
107
106
  if (contentPart.type === "image_url") {
108
107
  let source;
109
108
  if (typeof contentPart.image_url === "string") {
@@ -115,7 +114,6 @@ function _formatContent(content) {
115
114
  return {
116
115
  type: "image",
117
116
  source,
118
- ...(cacheControl ? { cache_control: cacheControl } : {}),
119
117
  };
120
118
  }
121
119
  else if (textTypes.find((t) => t === contentPart.type) &&
@@ -124,7 +122,6 @@ function _formatContent(content) {
124
122
  return {
125
123
  type: "text",
126
124
  text: contentPart.text,
127
- ...(cacheControl ? { cache_control: cacheControl } : {}),
128
125
  };
129
126
  }
130
127
  else if (toolTypes.find((t) => t === contentPart.type)) {
@@ -150,7 +147,6 @@ function _formatContent(content) {
150
147
  // TODO: Fix when SDK types are fixed
151
148
  return {
152
149
  ...contentPartCopy,
153
- ...(cacheControl ? { cache_control: cacheControl } : {}),
154
150
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
155
151
  };
156
152
  }
@@ -99,7 +99,6 @@ function _formatContent(content) {
99
99
  }
100
100
  else {
101
101
  const contentBlocks = content.map((contentPart) => {
102
- const cacheControl = "cache_control" in contentPart ? contentPart.cache_control : undefined;
103
102
  if (contentPart.type === "image_url") {
104
103
  let source;
105
104
  if (typeof contentPart.image_url === "string") {
@@ -111,7 +110,6 @@ function _formatContent(content) {
111
110
  return {
112
111
  type: "image",
113
112
  source,
114
- ...(cacheControl ? { cache_control: cacheControl } : {}),
115
113
  };
116
114
  }
117
115
  else if (textTypes.find((t) => t === contentPart.type) &&
@@ -120,7 +118,6 @@ function _formatContent(content) {
120
118
  return {
121
119
  type: "text",
122
120
  text: contentPart.text,
123
- ...(cacheControl ? { cache_control: cacheControl } : {}),
124
121
  };
125
122
  }
126
123
  else if (toolTypes.find((t) => t === contentPart.type)) {
@@ -146,7 +143,6 @@ function _formatContent(content) {
146
143
  // TODO: Fix when SDK types are fixed
147
144
  return {
148
145
  ...contentPartCopy,
149
- ...(cacheControl ? { cache_control: cacheControl } : {}),
150
146
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
151
147
  };
152
148
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/anthropic",
3
- "version": "0.2.17",
3
+ "version": "0.3.0-rc.0",
4
4
  "description": "Anthropic integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -36,14 +36,17 @@
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
38
  "@anthropic-ai/sdk": "^0.25.2",
39
- "@langchain/core": ">=0.2.21 <0.3.0",
40
39
  "fast-xml-parser": "^4.4.1",
41
40
  "zod": "^3.22.4",
42
41
  "zod-to-json-schema": "^3.22.4"
43
42
  },
43
+ "peerDependencies": {
44
+ "@langchain/core": ">=0.2.21 <0.4.0 || 0.3.0-rc.0"
45
+ },
44
46
  "devDependencies": {
45
47
  "@anthropic-ai/vertex-sdk": "^0.4.1",
46
48
  "@jest/globals": "^29.5.0",
49
+ "@langchain/core": "workspace:*",
47
50
  "@langchain/scripts": ">=0.1.0 <0.2.0",
48
51
  "@langchain/standard-tests": "0.0.0",
49
52
  "@swc/core": "^1.3.90",