@langchain/anthropic 0.3.31 → 1.0.0-alpha.2

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.
Files changed (101) hide show
  1. package/README.md +8 -8
  2. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  3. package/dist/chat_models.cjs +781 -1012
  4. package/dist/chat_models.cjs.map +1 -0
  5. package/dist/chat_models.d.cts +620 -0
  6. package/dist/chat_models.d.cts.map +1 -0
  7. package/dist/chat_models.d.ts +227 -211
  8. package/dist/chat_models.d.ts.map +1 -0
  9. package/dist/chat_models.js +775 -1003
  10. package/dist/chat_models.js.map +1 -0
  11. package/dist/index.cjs +6 -20
  12. package/dist/index.d.cts +4 -0
  13. package/dist/index.d.ts +4 -3
  14. package/dist/index.js +4 -2
  15. package/dist/output_parsers.cjs +65 -104
  16. package/dist/output_parsers.cjs.map +1 -0
  17. package/dist/output_parsers.js +64 -100
  18. package/dist/output_parsers.js.map +1 -0
  19. package/dist/types.d.cts +33 -0
  20. package/dist/types.d.cts.map +1 -0
  21. package/dist/types.d.ts +30 -32
  22. package/dist/types.d.ts.map +1 -0
  23. package/dist/utils/content.cjs +151 -0
  24. package/dist/utils/content.cjs.map +1 -0
  25. package/dist/utils/content.js +146 -0
  26. package/dist/utils/content.js.map +1 -0
  27. package/dist/utils/errors.cjs +16 -27
  28. package/dist/utils/errors.cjs.map +1 -0
  29. package/dist/utils/errors.js +17 -25
  30. package/dist/utils/errors.js.map +1 -0
  31. package/dist/utils/index.cjs +7 -0
  32. package/dist/utils/index.cjs.map +1 -0
  33. package/dist/utils/index.js +6 -0
  34. package/dist/utils/index.js.map +1 -0
  35. package/dist/utils/message_inputs.cjs +223 -534
  36. package/dist/utils/message_inputs.cjs.map +1 -0
  37. package/dist/utils/message_inputs.js +224 -532
  38. package/dist/utils/message_inputs.js.map +1 -0
  39. package/dist/utils/message_outputs.cjs +186 -250
  40. package/dist/utils/message_outputs.cjs.map +1 -0
  41. package/dist/utils/message_outputs.js +185 -247
  42. package/dist/utils/message_outputs.js.map +1 -0
  43. package/dist/utils/prompts.cjs +46 -45
  44. package/dist/utils/prompts.cjs.map +1 -0
  45. package/dist/utils/prompts.d.cts +45 -0
  46. package/dist/utils/prompts.d.cts.map +1 -0
  47. package/dist/utils/prompts.d.ts +8 -2
  48. package/dist/utils/prompts.d.ts.map +1 -0
  49. package/dist/utils/prompts.js +46 -42
  50. package/dist/utils/prompts.js.map +1 -0
  51. package/dist/utils/standard.cjs +285 -0
  52. package/dist/utils/standard.cjs.map +1 -0
  53. package/dist/utils/standard.js +285 -0
  54. package/dist/utils/standard.js.map +1 -0
  55. package/dist/utils/tools.cjs +15 -30
  56. package/dist/utils/tools.cjs.map +1 -0
  57. package/dist/utils/tools.js +15 -28
  58. package/dist/utils/tools.js.map +1 -0
  59. package/package.json +45 -71
  60. package/dist/experimental/index.cjs +0 -17
  61. package/dist/experimental/index.d.ts +0 -1
  62. package/dist/experimental/index.js +0 -1
  63. package/dist/experimental/tool_calling.cjs +0 -318
  64. package/dist/experimental/tool_calling.d.ts +0 -57
  65. package/dist/experimental/tool_calling.js +0 -314
  66. package/dist/experimental/utils/tool_calling.cjs +0 -106
  67. package/dist/experimental/utils/tool_calling.d.ts +0 -10
  68. package/dist/experimental/utils/tool_calling.js +0 -101
  69. package/dist/load/import_constants.cjs +0 -5
  70. package/dist/load/import_constants.d.ts +0 -1
  71. package/dist/load/import_constants.js +0 -2
  72. package/dist/load/import_map.cjs +0 -39
  73. package/dist/load/import_map.d.ts +0 -2
  74. package/dist/load/import_map.js +0 -3
  75. package/dist/load/import_type.cjs +0 -3
  76. package/dist/load/import_type.d.ts +0 -5
  77. package/dist/load/import_type.js +0 -2
  78. package/dist/load/index.cjs +0 -63
  79. package/dist/load/index.d.ts +0 -14
  80. package/dist/load/index.js +0 -25
  81. package/dist/load/map_keys.cjs +0 -2
  82. package/dist/load/map_keys.d.ts +0 -3
  83. package/dist/load/map_keys.js +0 -1
  84. package/dist/load/serializable.cjs +0 -17
  85. package/dist/load/serializable.d.ts +0 -1
  86. package/dist/load/serializable.js +0 -1
  87. package/dist/output_parsers.d.ts +0 -22
  88. package/dist/types.cjs +0 -48
  89. package/dist/types.js +0 -45
  90. package/dist/utils/errors.d.ts +0 -3
  91. package/dist/utils/message_inputs.d.ts +0 -14
  92. package/dist/utils/message_outputs.d.ts +0 -14
  93. package/dist/utils/tools.d.ts +0 -3
  94. package/experimental.cjs +0 -1
  95. package/experimental.d.cts +0 -1
  96. package/experimental.d.ts +0 -1
  97. package/experimental.js +0 -1
  98. package/index.cjs +0 -1
  99. package/index.d.cts +0 -1
  100. package/index.d.ts +0 -1
  101. package/index.js +0 -1
@@ -1,1021 +1,793 @@
1
- import { Anthropic } from "@anthropic-ai/sdk";
1
+ import { AnthropicToolsOutputParser } from "./output_parsers.js";
2
+ import { handleToolChoice } from "./utils/tools.js";
3
+ import { _convertMessagesToAnthropicPayload } from "./utils/message_inputs.js";
4
+ import { _makeMessageChunkFromAnthropicEvent, anthropicResponseToChatMessages } from "./utils/message_outputs.js";
5
+ import { wrapAnthropicClientError } from "./utils/errors.js";
6
+ import { Anthropic as Anthropic$1 } from "@anthropic-ai/sdk";
2
7
  import { AIMessageChunk } from "@langchain/core/messages";
3
8
  import { ChatGenerationChunk } from "@langchain/core/outputs";
4
9
  import { getEnvironmentVariable } from "@langchain/core/utils/env";
5
- import { BaseChatModel, } from "@langchain/core/language_models/chat_models";
6
- import { isOpenAITool, } from "@langchain/core/language_models/base";
10
+ import { BaseChatModel } from "@langchain/core/language_models/chat_models";
11
+ import { isOpenAITool } from "@langchain/core/language_models/base";
7
12
  import { toJsonSchema } from "@langchain/core/utils/json_schema";
8
- import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
9
- import { isInteropZodSchema, } from "@langchain/core/utils/types";
13
+ import { RunnablePassthrough, RunnableSequence } from "@langchain/core/runnables";
14
+ import { isInteropZodSchema } from "@langchain/core/utils/types";
10
15
  import { isLangChainTool } from "@langchain/core/utils/function_calling";
11
- import { AnthropicToolsOutputParser } from "./output_parsers.js";
12
- import { handleToolChoice } from "./utils/tools.js";
13
- import { _convertMessagesToAnthropicPayload } from "./utils/message_inputs.js";
14
- import { _makeMessageChunkFromAnthropicEvent, anthropicResponseToChatMessages, } from "./utils/message_outputs.js";
15
- import { wrapAnthropicClientError } from "./utils/errors.js";
16
+
17
+ //#region src/chat_models.ts
16
18
  function _toolsInParams(params) {
17
- return !!(params.tools && params.tools.length > 0);
19
+ return !!(params.tools && params.tools.length > 0);
18
20
  }
19
21
  function _documentsInParams(params) {
20
- for (const message of params.messages ?? []) {
21
- if (typeof message.content === "string") {
22
- continue;
23
- }
24
- for (const block of message.content ?? []) {
25
- if (typeof block === "object" &&
26
- block != null &&
27
- block.type === "document" &&
28
- typeof block.citations === "object" &&
29
- block.citations?.enabled) {
30
- return true;
31
- }
32
- }
33
- }
34
- return false;
22
+ for (const message of params.messages ?? []) {
23
+ if (typeof message.content === "string") continue;
24
+ for (const block of message.content ?? []) if (typeof block === "object" && block != null && block.type === "document" && typeof block.citations === "object" && block.citations?.enabled) return true;
25
+ }
26
+ return false;
35
27
  }
36
28
  function _thinkingInParams(params) {
37
- return !!(params.thinking && params.thinking.type === "enabled");
29
+ return !!(params.thinking && params.thinking.type === "enabled");
38
30
  }
39
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
31
  function isAnthropicTool(tool) {
41
- return "input_schema" in tool;
32
+ return "input_schema" in tool;
42
33
  }
43
34
  function isBuiltinTool(tool) {
44
- const builtInToolPrefixes = [
45
- "text_editor_",
46
- "computer_",
47
- "bash_",
48
- "web_search_",
49
- "web_fetch_",
50
- "str_replace_editor_",
51
- "str_replace_based_edit_tool_",
52
- "code_execution_",
53
- "memory_",
54
- ];
55
- return (typeof tool === "object" &&
56
- tool !== null &&
57
- "type" in tool &&
58
- "name" in tool &&
59
- builtInToolPrefixes.some((prefix) => typeof tool.type === "string" && tool.type.startsWith(prefix)));
35
+ const builtInToolPrefixes = [
36
+ "text_editor_",
37
+ "computer_",
38
+ "bash_",
39
+ "web_search_",
40
+ "web_fetch_",
41
+ "str_replace_editor_",
42
+ "str_replace_based_edit_tool_",
43
+ "code_execution_",
44
+ "memory_"
45
+ ];
46
+ return typeof tool === "object" && tool !== null && "type" in tool && "name" in tool && typeof tool.type === "string" && builtInToolPrefixes.some((prefix) => typeof tool.type === "string" && tool.type.startsWith(prefix));
60
47
  }
61
48
  function extractToken(chunk) {
62
- if (typeof chunk.content === "string") {
63
- return chunk.content;
64
- }
65
- else if (Array.isArray(chunk.content) &&
66
- chunk.content.length >= 1 &&
67
- "input" in chunk.content[0]) {
68
- return typeof chunk.content[0].input === "string"
69
- ? chunk.content[0].input
70
- : JSON.stringify(chunk.content[0].input);
71
- }
72
- else if (Array.isArray(chunk.content) &&
73
- chunk.content.length >= 1 &&
74
- "text" in chunk.content[0]) {
75
- return chunk.content[0].text;
76
- }
77
- return undefined;
49
+ if (typeof chunk.content === "string") return chunk.content;
50
+ else if (Array.isArray(chunk.content) && chunk.content.length >= 1 && "input" in chunk.content[0]) return typeof chunk.content[0].input === "string" ? chunk.content[0].input : JSON.stringify(chunk.content[0].input);
51
+ else if (Array.isArray(chunk.content) && chunk.content.length >= 1 && "text" in chunk.content[0] && typeof chunk.content[0].text === "string") return chunk.content[0].text;
52
+ return void 0;
78
53
  }
79
54
  /**
80
- * Anthropic chat model integration.
81
- *
82
- * Setup:
83
- * Install `@langchain/anthropic` and set an environment variable named `ANTHROPIC_API_KEY`.
84
- *
85
- * ```bash
86
- * npm install @langchain/anthropic
87
- * export ANTHROPIC_API_KEY="your-api-key"
88
- * ```
89
- *
90
- * ## [Constructor args](https://api.js.langchain.com/classes/langchain_anthropic.ChatAnthropic.html#constructor)
91
- *
92
- * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_anthropic.ChatAnthropicCallOptions.html)
93
- *
94
- * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
95
- * They can also be passed via `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
96
- *
97
- * ```typescript
98
- * // When calling `.bind`, call options should be passed via the first argument
99
- * const llmWithArgsBound = llm.bindTools([...]).withConfig({
100
- * stop: ["\n"],
101
- * });
102
- *
103
- * // When calling `.bindTools`, call options should be passed via the second argument
104
- * const llmWithTools = llm.bindTools(
105
- * [...],
106
- * {
107
- * tool_choice: "auto",
108
- * }
109
- * );
110
- * ```
111
- *
112
- * ## Examples
113
- *
114
- * <details open>
115
- * <summary><strong>Instantiate</strong></summary>
116
- *
117
- * ```typescript
118
- * import { ChatAnthropic } from '@langchain/anthropic';
119
- *
120
- * const llm = new ChatAnthropic({
121
- * model: "claude-3-5-sonnet-20240620",
122
- * temperature: 0,
123
- * maxTokens: undefined,
124
- * maxRetries: 2,
125
- * // apiKey: "...",
126
- * // baseUrl: "...",
127
- * // other params...
128
- * });
129
- * ```
130
- * </details>
131
- *
132
- * <br />
133
- *
134
- * <details>
135
- * <summary><strong>Invoking</strong></summary>
136
- *
137
- * ```typescript
138
- * const input = `Translate "I love programming" into French.`;
139
- *
140
- * // Models also accept a list of chat messages or a formatted prompt
141
- * const result = await llm.invoke(input);
142
- * console.log(result);
143
- * ```
144
- *
145
- * ```txt
146
- * AIMessage {
147
- * "id": "msg_01QDpd78JUHpRP6bRRNyzbW3",
148
- * "content": "Here's the translation to French:\n\nJ'adore la programmation.",
149
- * "response_metadata": {
150
- * "id": "msg_01QDpd78JUHpRP6bRRNyzbW3",
151
- * "model": "claude-3-5-sonnet-20240620",
152
- * "stop_reason": "end_turn",
153
- * "stop_sequence": null,
154
- * "usage": {
155
- * "input_tokens": 25,
156
- * "output_tokens": 19
157
- * },
158
- * "type": "message",
159
- * "role": "assistant"
160
- * },
161
- * "usage_metadata": {
162
- * "input_tokens": 25,
163
- * "output_tokens": 19,
164
- * "total_tokens": 44
165
- * }
166
- * }
167
- * ```
168
- * </details>
169
- *
170
- * <br />
171
- *
172
- * <details>
173
- * <summary><strong>Streaming Chunks</strong></summary>
174
- *
175
- * ```typescript
176
- * for await (const chunk of await llm.stream(input)) {
177
- * console.log(chunk);
178
- * }
179
- * ```
180
- *
181
- * ```txt
182
- * AIMessageChunk {
183
- * "id": "msg_01N8MwoYxiKo9w4chE4gXUs4",
184
- * "content": "",
185
- * "additional_kwargs": {
186
- * "id": "msg_01N8MwoYxiKo9w4chE4gXUs4",
187
- * "type": "message",
188
- * "role": "assistant",
189
- * "model": "claude-3-5-sonnet-20240620"
190
- * },
191
- * "usage_metadata": {
192
- * "input_tokens": 25,
193
- * "output_tokens": 1,
194
- * "total_tokens": 26
195
- * }
196
- * }
197
- * AIMessageChunk {
198
- * "content": "",
199
- * }
200
- * AIMessageChunk {
201
- * "content": "Here",
202
- * }
203
- * AIMessageChunk {
204
- * "content": "'s",
205
- * }
206
- * AIMessageChunk {
207
- * "content": " the translation to",
208
- * }
209
- * AIMessageChunk {
210
- * "content": " French:\n\nJ",
211
- * }
212
- * AIMessageChunk {
213
- * "content": "'adore la programmation",
214
- * }
215
- * AIMessageChunk {
216
- * "content": ".",
217
- * }
218
- * AIMessageChunk {
219
- * "content": "",
220
- * "additional_kwargs": {
221
- * "stop_reason": "end_turn",
222
- * "stop_sequence": null
223
- * },
224
- * "usage_metadata": {
225
- * "input_tokens": 0,
226
- * "output_tokens": 19,
227
- * "total_tokens": 19
228
- * }
229
- * }
230
- * ```
231
- * </details>
232
- *
233
- * <br />
234
- *
235
- * <details>
236
- * <summary><strong>Aggregate Streamed Chunks</strong></summary>
237
- *
238
- * ```typescript
239
- * import { AIMessageChunk } from '@langchain/core/messages';
240
- * import { concat } from '@langchain/core/utils/stream';
241
- *
242
- * const stream = await llm.stream(input);
243
- * let full: AIMessageChunk | undefined;
244
- * for await (const chunk of stream) {
245
- * full = !full ? chunk : concat(full, chunk);
246
- * }
247
- * console.log(full);
248
- * ```
249
- *
250
- * ```txt
251
- * AIMessageChunk {
252
- * "id": "msg_01SBTb5zSGXfjUc7yQ8EKEEA",
253
- * "content": "Here's the translation to French:\n\nJ'adore la programmation.",
254
- * "additional_kwargs": {
255
- * "id": "msg_01SBTb5zSGXfjUc7yQ8EKEEA",
256
- * "type": "message",
257
- * "role": "assistant",
258
- * "model": "claude-3-5-sonnet-20240620",
259
- * "stop_reason": "end_turn",
260
- * "stop_sequence": null
261
- * },
262
- * "usage_metadata": {
263
- * "input_tokens": 25,
264
- * "output_tokens": 20,
265
- * "total_tokens": 45
266
- * }
267
- * }
268
- * ```
269
- * </details>
270
- *
271
- * <br />
272
- *
273
- * <details>
274
- * <summary><strong>Bind tools</strong></summary>
275
- *
276
- * ```typescript
277
- * import { z } from 'zod';
278
- *
279
- * const GetWeather = {
280
- * name: "GetWeather",
281
- * description: "Get the current weather in a given location",
282
- * schema: z.object({
283
- * location: z.string().describe("The city and state, e.g. San Francisco, CA")
284
- * }),
285
- * }
286
- *
287
- * const GetPopulation = {
288
- * name: "GetPopulation",
289
- * description: "Get the current population in a given location",
290
- * schema: z.object({
291
- * location: z.string().describe("The city and state, e.g. San Francisco, CA")
292
- * }),
293
- * }
294
- *
295
- * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
296
- * const aiMsg = await llmWithTools.invoke(
297
- * "Which city is hotter today and which is bigger: LA or NY?"
298
- * );
299
- * console.log(aiMsg.tool_calls);
300
- * ```
301
- *
302
- * ```txt
303
- * [
304
- * {
305
- * name: 'GetWeather',
306
- * args: { location: 'Los Angeles, CA' },
307
- * id: 'toolu_01WjW3Dann6BPJVtLhovdBD5',
308
- * type: 'tool_call'
309
- * },
310
- * {
311
- * name: 'GetWeather',
312
- * args: { location: 'New York, NY' },
313
- * id: 'toolu_01G6wfJgqi5zRmJomsmkyZXe',
314
- * type: 'tool_call'
315
- * },
316
- * {
317
- * name: 'GetPopulation',
318
- * args: { location: 'Los Angeles, CA' },
319
- * id: 'toolu_0165qYWBA2VFyUst5RA18zew',
320
- * type: 'tool_call'
321
- * },
322
- * {
323
- * name: 'GetPopulation',
324
- * args: { location: 'New York, NY' },
325
- * id: 'toolu_01PGNyP33vxr13tGqr7i3rDo',
326
- * type: 'tool_call'
327
- * }
328
- * ]
329
- * ```
330
- * </details>
331
- *
332
- * <br />
333
- *
334
- * <details>
335
- * <summary><strong>Structured Output</strong></summary>
336
- *
337
- * ```typescript
338
- * import { z } from 'zod';
339
- *
340
- * const Joke = z.object({
341
- * setup: z.string().describe("The setup of the joke"),
342
- * punchline: z.string().describe("The punchline to the joke"),
343
- * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
344
- * }).describe('Joke to tell user.');
345
- *
346
- * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
347
- * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
348
- * console.log(jokeResult);
349
- * ```
350
- *
351
- * ```txt
352
- * {
353
- * setup: "Why don't cats play poker in the jungle?",
354
- * punchline: 'Too many cheetahs!',
355
- * rating: 7
356
- * }
357
- * ```
358
- * </details>
359
- *
360
- * <br />
361
- *
362
- * <details>
363
- * <summary><strong>Multimodal</strong></summary>
364
- *
365
- * ```typescript
366
- * import { HumanMessage } from '@langchain/core/messages';
367
- *
368
- * const imageUrl = "https://example.com/image.jpg";
369
- * const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());
370
- * const base64Image = Buffer.from(imageData).toString('base64');
371
- *
372
- * const message = new HumanMessage({
373
- * content: [
374
- * { type: "text", text: "describe the weather in this image" },
375
- * {
376
- * type: "image_url",
377
- * image_url: { url: `data:image/jpeg;base64,${base64Image}` },
378
- * },
379
- * ]
380
- * });
381
- *
382
- * const imageDescriptionAiMsg = await llm.invoke([message]);
383
- * console.log(imageDescriptionAiMsg.content);
384
- * ```
385
- *
386
- * ```txt
387
- * The weather in this image appears to be beautiful and clear. The sky is a vibrant blue with scattered white clouds, suggesting a sunny and pleasant day. The clouds are wispy and light, indicating calm conditions without any signs of storms or heavy weather. The bright green grass on the rolling hills looks lush and well-watered, which could mean recent rainfall or good growing conditions. Overall, the scene depicts a perfect spring or early summer day with mild temperatures, plenty of sunshine, and gentle breezes - ideal weather for enjoying the outdoors or for plant growth.
388
- * ```
389
- * </details>
390
- *
391
- * <br />
392
- *
393
- * <details>
394
- * <summary><strong>Usage Metadata</strong></summary>
395
- *
396
- * ```typescript
397
- * const aiMsgForMetadata = await llm.invoke(input);
398
- * console.log(aiMsgForMetadata.usage_metadata);
399
- * ```
400
- *
401
- * ```txt
402
- * { input_tokens: 25, output_tokens: 19, total_tokens: 44 }
403
- * ```
404
- * </details>
405
- *
406
- * <br />
407
- *
408
- * <details>
409
- * <summary><strong>Stream Usage Metadata</strong></summary>
410
- *
411
- * ```typescript
412
- * const streamForMetadata = await llm.stream(
413
- * input,
414
- * {
415
- * streamUsage: true
416
- * }
417
- * );
418
- * let fullForMetadata: AIMessageChunk | undefined;
419
- * for await (const chunk of streamForMetadata) {
420
- * fullForMetadata = !fullForMetadata ? chunk : concat(fullForMetadata, chunk);
421
- * }
422
- * console.log(fullForMetadata?.usage_metadata);
423
- * ```
424
- *
425
- * ```txt
426
- * { input_tokens: 25, output_tokens: 20, total_tokens: 45 }
427
- * ```
428
- * </details>
429
- *
430
- * <br />
431
- *
432
- * <details>
433
- * <summary><strong>Response Metadata</strong></summary>
434
- *
435
- * ```typescript
436
- * const aiMsgForResponseMetadata = await llm.invoke(input);
437
- * console.log(aiMsgForResponseMetadata.response_metadata);
438
- * ```
439
- *
440
- * ```txt
441
- * {
442
- * id: 'msg_01STxeQxJmp4sCSpioD6vK3L',
443
- * model: 'claude-3-5-sonnet-20240620',
444
- * stop_reason: 'end_turn',
445
- * stop_sequence: null,
446
- * usage: { input_tokens: 25, output_tokens: 19 },
447
- * type: 'message',
448
- * role: 'assistant'
449
- * }
450
- * ```
451
- * </details>
452
- *
453
- * <br />
454
- */
455
- export class ChatAnthropicMessages extends BaseChatModel {
456
- static lc_name() {
457
- return "ChatAnthropic";
458
- }
459
- get lc_secrets() {
460
- return {
461
- anthropicApiKey: "ANTHROPIC_API_KEY",
462
- apiKey: "ANTHROPIC_API_KEY",
463
- };
464
- }
465
- get lc_aliases() {
466
- return {
467
- modelName: "model",
468
- };
469
- }
470
- constructor(fields) {
471
- super(fields ?? {});
472
- Object.defineProperty(this, "lc_serializable", {
473
- enumerable: true,
474
- configurable: true,
475
- writable: true,
476
- value: true
477
- });
478
- Object.defineProperty(this, "anthropicApiKey", {
479
- enumerable: true,
480
- configurable: true,
481
- writable: true,
482
- value: void 0
483
- });
484
- Object.defineProperty(this, "apiKey", {
485
- enumerable: true,
486
- configurable: true,
487
- writable: true,
488
- value: void 0
489
- });
490
- Object.defineProperty(this, "apiUrl", {
491
- enumerable: true,
492
- configurable: true,
493
- writable: true,
494
- value: void 0
495
- });
496
- Object.defineProperty(this, "temperature", {
497
- enumerable: true,
498
- configurable: true,
499
- writable: true,
500
- value: 1
501
- });
502
- Object.defineProperty(this, "topK", {
503
- enumerable: true,
504
- configurable: true,
505
- writable: true,
506
- value: -1
507
- });
508
- Object.defineProperty(this, "topP", {
509
- enumerable: true,
510
- configurable: true,
511
- writable: true,
512
- value: -1
513
- });
514
- Object.defineProperty(this, "maxTokens", {
515
- enumerable: true,
516
- configurable: true,
517
- writable: true,
518
- value: 2048
519
- });
520
- Object.defineProperty(this, "modelName", {
521
- enumerable: true,
522
- configurable: true,
523
- writable: true,
524
- value: "claude-2.1"
525
- });
526
- Object.defineProperty(this, "model", {
527
- enumerable: true,
528
- configurable: true,
529
- writable: true,
530
- value: "claude-2.1"
531
- });
532
- Object.defineProperty(this, "invocationKwargs", {
533
- enumerable: true,
534
- configurable: true,
535
- writable: true,
536
- value: void 0
537
- });
538
- Object.defineProperty(this, "stopSequences", {
539
- enumerable: true,
540
- configurable: true,
541
- writable: true,
542
- value: void 0
543
- });
544
- Object.defineProperty(this, "streaming", {
545
- enumerable: true,
546
- configurable: true,
547
- writable: true,
548
- value: false
549
- });
550
- Object.defineProperty(this, "clientOptions", {
551
- enumerable: true,
552
- configurable: true,
553
- writable: true,
554
- value: void 0
555
- });
556
- Object.defineProperty(this, "thinking", {
557
- enumerable: true,
558
- configurable: true,
559
- writable: true,
560
- value: { type: "disabled" }
561
- });
562
- Object.defineProperty(this, "contextManagement", {
563
- enumerable: true,
564
- configurable: true,
565
- writable: true,
566
- value: void 0
567
- });
568
- // Used for non-streaming requests
569
- Object.defineProperty(this, "batchClient", {
570
- enumerable: true,
571
- configurable: true,
572
- writable: true,
573
- value: void 0
574
- });
575
- // Used for streaming requests
576
- Object.defineProperty(this, "streamingClient", {
577
- enumerable: true,
578
- configurable: true,
579
- writable: true,
580
- value: void 0
581
- });
582
- Object.defineProperty(this, "streamUsage", {
583
- enumerable: true,
584
- configurable: true,
585
- writable: true,
586
- value: true
587
- });
588
- /**
589
- * Optional method that returns an initialized underlying Anthropic client.
590
- * Useful for accessing Anthropic models hosted on other cloud services
591
- * such as Google Vertex.
592
- */
593
- Object.defineProperty(this, "createClient", {
594
- enumerable: true,
595
- configurable: true,
596
- writable: true,
597
- value: void 0
598
- });
599
- this.anthropicApiKey =
600
- fields?.apiKey ??
601
- fields?.anthropicApiKey ??
602
- getEnvironmentVariable("ANTHROPIC_API_KEY");
603
- if (!this.anthropicApiKey && !fields?.createClient) {
604
- throw new Error("Anthropic API key not found");
605
- }
606
- this.clientOptions = fields?.clientOptions ?? {};
607
- /** Keep anthropicApiKey for backwards compatibility */
608
- this.apiKey = this.anthropicApiKey;
609
- // Support overriding the default API URL (i.e., https://api.anthropic.com)
610
- this.apiUrl = fields?.anthropicApiUrl;
611
- /** Keep modelName for backwards compatibility */
612
- this.modelName = fields?.model ?? fields?.modelName ?? this.model;
613
- this.model = this.modelName;
614
- this.invocationKwargs = fields?.invocationKwargs ?? {};
615
- // Default to `undefined` for `topP` for Opus 4.1 and Sonnet 4.5 models
616
- if (this.model.includes("opus-4-1") || this.model.includes("sonnet-4-5")) {
617
- this.topP = fields?.topP === null ? undefined : fields?.topP;
618
- }
619
- else {
620
- this.topP = fields?.topP ?? this.topP;
621
- }
622
- // If the user passes `null`, set it to `undefined`. Otherwise, use their value or the default. We have to check for null, because
623
- // there's no way for us to know if they explicitly set it to `undefined`, or never passed a value
624
- this.temperature =
625
- fields?.temperature === null
626
- ? undefined
627
- : fields?.temperature ?? this.temperature;
628
- this.topK = fields?.topK ?? this.topK;
629
- this.maxTokens =
630
- fields?.maxTokensToSample ?? fields?.maxTokens ?? this.maxTokens;
631
- this.stopSequences = fields?.stopSequences ?? this.stopSequences;
632
- this.streaming = fields?.streaming ?? false;
633
- this.streamUsage = fields?.streamUsage ?? this.streamUsage;
634
- this.thinking = fields?.thinking ?? this.thinking;
635
- this.contextManagement =
636
- fields?.contextManagement ?? this.contextManagement;
637
- this.createClient =
638
- fields?.createClient ??
639
- ((options) => new Anthropic(options));
640
- }
641
- getLsParams(options) {
642
- const params = this.invocationParams(options);
643
- return {
644
- ls_provider: "anthropic",
645
- ls_model_name: this.model,
646
- ls_model_type: "chat",
647
- ls_temperature: params.temperature ?? undefined,
648
- ls_max_tokens: params.max_tokens ?? undefined,
649
- ls_stop: options.stop,
650
- };
651
- }
652
- /**
653
- * Formats LangChain StructuredTools to AnthropicTools.
654
- *
655
- * @param {ChatAnthropicCallOptions["tools"]} tools The tools to format
656
- * @returns {AnthropicTool[] | undefined} The formatted tools, or undefined if none are passed.
657
- */
658
- formatStructuredToolToAnthropic(tools) {
659
- if (!tools || !tools.length) {
660
- return undefined;
661
- }
662
- return tools.map((tool) => {
663
- if (isBuiltinTool(tool)) {
664
- return tool;
665
- }
666
- if (isAnthropicTool(tool)) {
667
- return tool;
668
- }
669
- if (isOpenAITool(tool)) {
670
- return {
671
- name: tool.function.name,
672
- description: tool.function.description,
673
- input_schema: tool.function
674
- .parameters,
675
- };
676
- }
677
- if (isLangChainTool(tool)) {
678
- return {
679
- name: tool.name,
680
- description: tool.description,
681
- input_schema: (isInteropZodSchema(tool.schema)
682
- ? toJsonSchema(tool.schema)
683
- : tool.schema),
684
- };
685
- }
686
- throw new Error(`Unknown tool type passed to ChatAnthropic: ${JSON.stringify(tool, null, 2)}`);
687
- });
688
- }
689
- bindTools(tools, kwargs) {
690
- return this.withConfig({
691
- tools: this.formatStructuredToolToAnthropic(tools),
692
- ...kwargs,
693
- });
694
- }
695
- /**
696
- * Get the parameters used to invoke the model
697
- */
698
- invocationParams(options) {
699
- const tool_choice = handleToolChoice(options?.tool_choice);
700
- if (this.thinking.type === "enabled") {
701
- if (this.topK !== -1) {
702
- throw new Error("topK is not supported when thinking is enabled");
703
- }
704
- if (this.model.includes("opus-4-1") || this.model.includes("sonnet-4-5")
705
- ? this.topP !== undefined
706
- : this.topP !== -1) {
707
- throw new Error("topP is not supported when thinking is enabled");
708
- }
709
- if (this.temperature !== 1) {
710
- throw new Error("temperature is not supported when thinking is enabled");
711
- }
712
- return {
713
- model: this.model,
714
- stop_sequences: options?.stop ?? this.stopSequences,
715
- stream: this.streaming,
716
- max_tokens: this.maxTokens,
717
- tools: this.formatStructuredToolToAnthropic(options?.tools),
718
- tool_choice,
719
- thinking: this.thinking,
720
- ...this.invocationKwargs,
721
- };
722
- }
723
- return {
724
- model: this.model,
725
- temperature: this.temperature,
726
- top_k: this.topK,
727
- top_p: this.topP,
728
- stop_sequences: options?.stop ?? this.stopSequences,
729
- stream: this.streaming,
730
- max_tokens: this.maxTokens,
731
- tools: this.formatStructuredToolToAnthropic(options?.tools),
732
- tool_choice,
733
- thinking: this.thinking,
734
- context_management: this.contextManagement,
735
- ...this.invocationKwargs,
736
- };
737
- }
738
- /** @ignore */
739
- _identifyingParams() {
740
- return {
741
- model_name: this.model,
742
- ...this.invocationParams(),
743
- };
744
- }
745
- /**
746
- * Get the identifying parameters for the model
747
- */
748
- identifyingParams() {
749
- return {
750
- model_name: this.model,
751
- ...this.invocationParams(),
752
- };
753
- }
754
- async *_streamResponseChunks(messages, options, runManager) {
755
- const params = this.invocationParams(options);
756
- const formattedMessages = _convertMessagesToAnthropicPayload(messages);
757
- const payload = {
758
- ...params,
759
- ...formattedMessages,
760
- stream: true,
761
- };
762
- const coerceContentToString = !_toolsInParams(payload) &&
763
- !_documentsInParams(payload) &&
764
- !_thinkingInParams(payload);
765
- const stream = await this.createStreamWithRetry(payload, {
766
- headers: options.headers,
767
- });
768
- for await (const data of stream) {
769
- if (options.signal?.aborted) {
770
- stream.controller.abort();
771
- throw new Error("AbortError: User aborted the request.");
772
- }
773
- const shouldStreamUsage = this.streamUsage ?? options.streamUsage;
774
- const result = _makeMessageChunkFromAnthropicEvent(data, {
775
- streamUsage: shouldStreamUsage,
776
- coerceContentToString,
777
- });
778
- if (!result)
779
- continue;
780
- const { chunk } = result;
781
- // Extract the text content token for text field and runManager.
782
- const token = extractToken(chunk);
783
- const generationChunk = new ChatGenerationChunk({
784
- message: new AIMessageChunk({
785
- // Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().
786
- content: chunk.content,
787
- additional_kwargs: chunk.additional_kwargs,
788
- tool_call_chunks: chunk.tool_call_chunks,
789
- usage_metadata: shouldStreamUsage ? chunk.usage_metadata : undefined,
790
- response_metadata: chunk.response_metadata,
791
- id: chunk.id,
792
- }),
793
- text: token ?? "",
794
- });
795
- yield generationChunk;
796
- await runManager?.handleLLMNewToken(token ?? "", undefined, undefined, undefined, undefined, { chunk: generationChunk });
797
- }
798
- }
799
- /** @ignore */
800
- async _generateNonStreaming(messages, params, requestOptions) {
801
- const response = await this.completionWithRetry({
802
- ...params,
803
- stream: false,
804
- ..._convertMessagesToAnthropicPayload(messages),
805
- }, requestOptions);
806
- const { content, ...additionalKwargs } = response;
807
- const generations = anthropicResponseToChatMessages(content, additionalKwargs);
808
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
809
- const { role: _role, type: _type, ...rest } = additionalKwargs;
810
- return { generations, llmOutput: rest };
811
- }
812
- /** @ignore */
813
- async _generate(messages, options, runManager) {
814
- if (this.stopSequences && options.stop) {
815
- throw new Error(`"stopSequence" parameter found in input and default params`);
816
- }
817
- const params = this.invocationParams(options);
818
- if (params.stream) {
819
- let finalChunk;
820
- const stream = this._streamResponseChunks(messages, options, runManager);
821
- for await (const chunk of stream) {
822
- if (finalChunk === undefined) {
823
- finalChunk = chunk;
824
- }
825
- else {
826
- finalChunk = finalChunk.concat(chunk);
827
- }
828
- }
829
- if (finalChunk === undefined) {
830
- throw new Error("No chunks returned from Anthropic API.");
831
- }
832
- return {
833
- generations: [
834
- {
835
- text: finalChunk.text,
836
- message: finalChunk.message,
837
- },
838
- ],
839
- };
840
- }
841
- else {
842
- return this._generateNonStreaming(messages, params, {
843
- signal: options.signal,
844
- headers: options.headers,
845
- });
846
- }
847
- }
848
- /**
849
- * Creates a streaming request with retry.
850
- * @param request The parameters for creating a completion.
851
- * @param options
852
- * @returns A streaming request.
853
- */
854
- async createStreamWithRetry(request, options) {
855
- if (!this.streamingClient) {
856
- const options_ = this.apiUrl ? { baseURL: this.apiUrl } : undefined;
857
- this.streamingClient = this.createClient({
858
- dangerouslyAllowBrowser: true,
859
- ...this.clientOptions,
860
- ...options_,
861
- apiKey: this.apiKey,
862
- // Prefer LangChain built-in retries
863
- maxRetries: 0,
864
- });
865
- }
866
- const makeCompletionRequest = async () => {
867
- try {
868
- return await this.streamingClient.messages.create({
869
- ...request,
870
- ...this.invocationKwargs,
871
- stream: true,
872
- }, options);
873
- }
874
- catch (e) {
875
- const error = wrapAnthropicClientError(e);
876
- throw error;
877
- }
878
- };
879
- return this.caller.call(makeCompletionRequest);
880
- }
881
- /** @ignore */
882
- async completionWithRetry(request, options) {
883
- if (!this.batchClient) {
884
- const options = this.apiUrl ? { baseURL: this.apiUrl } : undefined;
885
- this.batchClient = this.createClient({
886
- dangerouslyAllowBrowser: true,
887
- ...this.clientOptions,
888
- ...options,
889
- apiKey: this.apiKey,
890
- maxRetries: 0,
891
- });
892
- }
893
- const makeCompletionRequest = async () => {
894
- try {
895
- return await this.batchClient.messages.create({
896
- ...request,
897
- ...this.invocationKwargs,
898
- }, options);
899
- }
900
- catch (e) {
901
- const error = wrapAnthropicClientError(e);
902
- throw error;
903
- }
904
- };
905
- return this.caller.callWithOptions({ signal: options.signal ?? undefined }, makeCompletionRequest);
906
- }
907
- _llmType() {
908
- return "anthropic";
909
- }
910
- withStructuredOutput(outputSchema, config) {
911
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
912
- const schema = outputSchema;
913
- const name = config?.name;
914
- const method = config?.method;
915
- const includeRaw = config?.includeRaw;
916
- if (method === "jsonMode") {
917
- throw new Error(`Anthropic only supports "functionCalling" as a method.`);
918
- }
919
- let functionName = name ?? "extract";
920
- let outputParser;
921
- let tools;
922
- if (isInteropZodSchema(schema)) {
923
- const jsonSchema = toJsonSchema(schema);
924
- tools = [
925
- {
926
- name: functionName,
927
- description: jsonSchema.description ?? "A function available to call.",
928
- input_schema: jsonSchema,
929
- },
930
- ];
931
- outputParser = new AnthropicToolsOutputParser({
932
- returnSingle: true,
933
- keyName: functionName,
934
- zodSchema: schema,
935
- });
936
- }
937
- else {
938
- let anthropicTools;
939
- if (typeof schema.name === "string" &&
940
- typeof schema.description === "string" &&
941
- typeof schema.input_schema === "object" &&
942
- schema.input_schema != null) {
943
- anthropicTools = schema;
944
- functionName = schema.name;
945
- }
946
- else {
947
- anthropicTools = {
948
- name: functionName,
949
- description: schema.description ?? "",
950
- input_schema: schema,
951
- };
952
- }
953
- tools = [anthropicTools];
954
- outputParser = new AnthropicToolsOutputParser({
955
- returnSingle: true,
956
- keyName: functionName,
957
- });
958
- }
959
- let llm;
960
- if (this.thinking?.type === "enabled") {
961
- const thinkingAdmonition = "Anthropic structured output relies on forced tool calling, " +
962
- "which is not supported when `thinking` is enabled. This method will raise " +
963
- "OutputParserException if tool calls are not " +
964
- "generated. Consider disabling `thinking` or adjust your prompt to ensure " +
965
- "the tool is called.";
966
- console.warn(thinkingAdmonition);
967
- llm = this.withConfig({
968
- tools,
969
- ls_structured_output_format: {
970
- kwargs: { method: "functionCalling" },
971
- schema: toJsonSchema(schema),
972
- },
973
- });
974
- const raiseIfNoToolCalls = (message) => {
975
- if (!message.tool_calls || message.tool_calls.length === 0) {
976
- throw new Error(thinkingAdmonition);
977
- }
978
- return message;
979
- };
980
- llm = llm.pipe(raiseIfNoToolCalls);
981
- }
982
- else {
983
- llm = this.withConfig({
984
- tools,
985
- tool_choice: {
986
- type: "tool",
987
- name: functionName,
988
- },
989
- ls_structured_output_format: {
990
- kwargs: { method: "functionCalling" },
991
- schema: toJsonSchema(schema),
992
- },
993
- });
994
- }
995
- if (!includeRaw) {
996
- return llm.pipe(outputParser).withConfig({
997
- runName: "ChatAnthropicStructuredOutput",
998
- });
999
- }
1000
- const parserAssign = RunnablePassthrough.assign({
1001
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1002
- parsed: (input, config) => outputParser.invoke(input.raw, config),
1003
- });
1004
- const parserNone = RunnablePassthrough.assign({
1005
- parsed: () => null,
1006
- });
1007
- const parsedWithFallback = parserAssign.withFallbacks({
1008
- fallbacks: [parserNone],
1009
- });
1010
- return RunnableSequence.from([
1011
- {
1012
- raw: llm,
1013
- },
1014
- parsedWithFallback,
1015
- ]).withConfig({
1016
- runName: "StructuredOutputRunnable",
1017
- });
1018
- }
1019
- }
1020
- export class ChatAnthropic extends ChatAnthropicMessages {
1021
- }
55
+ * Anthropic chat model integration.
56
+ *
57
+ * Setup:
58
+ * Install `@langchain/anthropic` and set an environment variable named `ANTHROPIC_API_KEY`.
59
+ *
60
+ * ```bash
61
+ * npm install @langchain/anthropic
62
+ * export ANTHROPIC_API_KEY="your-api-key"
63
+ * ```
64
+ *
65
+ * ## [Constructor args](https://api.js.langchain.com/classes/langchain_anthropic.ChatAnthropic.html#constructor)
66
+ *
67
+ * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_anthropic.ChatAnthropicCallOptions.html)
68
+ *
69
+ * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
70
+ * They can also be passed via `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
71
+ *
72
+ * ```typescript
73
+ * // When calling `.bind`, call options should be passed via the first argument
74
+ * const llmWithArgsBound = llm.bindTools([...]).withConfig({
75
+ * stop: ["\n"],
76
+ * });
77
+ *
78
+ * // When calling `.bindTools`, call options should be passed via the second argument
79
+ * const llmWithTools = llm.bindTools(
80
+ * [...],
81
+ * {
82
+ * tool_choice: "auto",
83
+ * }
84
+ * );
85
+ * ```
86
+ *
87
+ * ## Examples
88
+ *
89
+ * <details open>
90
+ * <summary><strong>Instantiate</strong></summary>
91
+ *
92
+ * ```typescript
93
+ * import { ChatAnthropic } from '@langchain/anthropic';
94
+ *
95
+ * const llm = new ChatAnthropic({
96
+ * model: "claude-3-5-sonnet-20240620",
97
+ * temperature: 0,
98
+ * maxTokens: undefined,
99
+ * maxRetries: 2,
100
+ * // apiKey: "...",
101
+ * // baseUrl: "...",
102
+ * // other params...
103
+ * });
104
+ * ```
105
+ * </details>
106
+ *
107
+ * <br />
108
+ *
109
+ * <details>
110
+ * <summary><strong>Invoking</strong></summary>
111
+ *
112
+ * ```typescript
113
+ * const input = `Translate "I love programming" into French.`;
114
+ *
115
+ * // Models also accept a list of chat messages or a formatted prompt
116
+ * const result = await llm.invoke(input);
117
+ * console.log(result);
118
+ * ```
119
+ *
120
+ * ```txt
121
+ * AIMessage {
122
+ * "id": "msg_01QDpd78JUHpRP6bRRNyzbW3",
123
+ * "content": "Here's the translation to French:\n\nJ'adore la programmation.",
124
+ * "response_metadata": {
125
+ * "id": "msg_01QDpd78JUHpRP6bRRNyzbW3",
126
+ * "model": "claude-3-5-sonnet-20240620",
127
+ * "stop_reason": "end_turn",
128
+ * "stop_sequence": null,
129
+ * "usage": {
130
+ * "input_tokens": 25,
131
+ * "output_tokens": 19
132
+ * },
133
+ * "type": "message",
134
+ * "role": "assistant"
135
+ * },
136
+ * "usage_metadata": {
137
+ * "input_tokens": 25,
138
+ * "output_tokens": 19,
139
+ * "total_tokens": 44
140
+ * }
141
+ * }
142
+ * ```
143
+ * </details>
144
+ *
145
+ * <br />
146
+ *
147
+ * <details>
148
+ * <summary><strong>Streaming Chunks</strong></summary>
149
+ *
150
+ * ```typescript
151
+ * for await (const chunk of await llm.stream(input)) {
152
+ * console.log(chunk);
153
+ * }
154
+ * ```
155
+ *
156
+ * ```txt
157
+ * AIMessageChunk {
158
+ * "id": "msg_01N8MwoYxiKo9w4chE4gXUs4",
159
+ * "content": "",
160
+ * "additional_kwargs": {
161
+ * "id": "msg_01N8MwoYxiKo9w4chE4gXUs4",
162
+ * "type": "message",
163
+ * "role": "assistant",
164
+ * "model": "claude-3-5-sonnet-20240620"
165
+ * },
166
+ * "usage_metadata": {
167
+ * "input_tokens": 25,
168
+ * "output_tokens": 1,
169
+ * "total_tokens": 26
170
+ * }
171
+ * }
172
+ * AIMessageChunk {
173
+ * "content": "",
174
+ * }
175
+ * AIMessageChunk {
176
+ * "content": "Here",
177
+ * }
178
+ * AIMessageChunk {
179
+ * "content": "'s",
180
+ * }
181
+ * AIMessageChunk {
182
+ * "content": " the translation to",
183
+ * }
184
+ * AIMessageChunk {
185
+ * "content": " French:\n\nJ",
186
+ * }
187
+ * AIMessageChunk {
188
+ * "content": "'adore la programmation",
189
+ * }
190
+ * AIMessageChunk {
191
+ * "content": ".",
192
+ * }
193
+ * AIMessageChunk {
194
+ * "content": "",
195
+ * "additional_kwargs": {
196
+ * "stop_reason": "end_turn",
197
+ * "stop_sequence": null
198
+ * },
199
+ * "usage_metadata": {
200
+ * "input_tokens": 0,
201
+ * "output_tokens": 19,
202
+ * "total_tokens": 19
203
+ * }
204
+ * }
205
+ * ```
206
+ * </details>
207
+ *
208
+ * <br />
209
+ *
210
+ * <details>
211
+ * <summary><strong>Aggregate Streamed Chunks</strong></summary>
212
+ *
213
+ * ```typescript
214
+ * import { AIMessageChunk } from '@langchain/core/messages';
215
+ * import { concat } from '@langchain/core/utils/stream';
216
+ *
217
+ * const stream = await llm.stream(input);
218
+ * let full: AIMessageChunk | undefined;
219
+ * for await (const chunk of stream) {
220
+ * full = !full ? chunk : concat(full, chunk);
221
+ * }
222
+ * console.log(full);
223
+ * ```
224
+ *
225
+ * ```txt
226
+ * AIMessageChunk {
227
+ * "id": "msg_01SBTb5zSGXfjUc7yQ8EKEEA",
228
+ * "content": "Here's the translation to French:\n\nJ'adore la programmation.",
229
+ * "additional_kwargs": {
230
+ * "id": "msg_01SBTb5zSGXfjUc7yQ8EKEEA",
231
+ * "type": "message",
232
+ * "role": "assistant",
233
+ * "model": "claude-3-5-sonnet-20240620",
234
+ * "stop_reason": "end_turn",
235
+ * "stop_sequence": null
236
+ * },
237
+ * "usage_metadata": {
238
+ * "input_tokens": 25,
239
+ * "output_tokens": 20,
240
+ * "total_tokens": 45
241
+ * }
242
+ * }
243
+ * ```
244
+ * </details>
245
+ *
246
+ * <br />
247
+ *
248
+ * <details>
249
+ * <summary><strong>Bind tools</strong></summary>
250
+ *
251
+ * ```typescript
252
+ * import { z } from 'zod';
253
+ *
254
+ * const GetWeather = {
255
+ * name: "GetWeather",
256
+ * description: "Get the current weather in a given location",
257
+ * schema: z.object({
258
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
259
+ * }),
260
+ * }
261
+ *
262
+ * const GetPopulation = {
263
+ * name: "GetPopulation",
264
+ * description: "Get the current population in a given location",
265
+ * schema: z.object({
266
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
267
+ * }),
268
+ * }
269
+ *
270
+ * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
271
+ * const aiMsg = await llmWithTools.invoke(
272
+ * "Which city is hotter today and which is bigger: LA or NY?"
273
+ * );
274
+ * console.log(aiMsg.tool_calls);
275
+ * ```
276
+ *
277
+ * ```txt
278
+ * [
279
+ * {
280
+ * name: 'GetWeather',
281
+ * args: { location: 'Los Angeles, CA' },
282
+ * id: 'toolu_01WjW3Dann6BPJVtLhovdBD5',
283
+ * type: 'tool_call'
284
+ * },
285
+ * {
286
+ * name: 'GetWeather',
287
+ * args: { location: 'New York, NY' },
288
+ * id: 'toolu_01G6wfJgqi5zRmJomsmkyZXe',
289
+ * type: 'tool_call'
290
+ * },
291
+ * {
292
+ * name: 'GetPopulation',
293
+ * args: { location: 'Los Angeles, CA' },
294
+ * id: 'toolu_0165qYWBA2VFyUst5RA18zew',
295
+ * type: 'tool_call'
296
+ * },
297
+ * {
298
+ * name: 'GetPopulation',
299
+ * args: { location: 'New York, NY' },
300
+ * id: 'toolu_01PGNyP33vxr13tGqr7i3rDo',
301
+ * type: 'tool_call'
302
+ * }
303
+ * ]
304
+ * ```
305
+ * </details>
306
+ *
307
+ * <br />
308
+ *
309
+ * <details>
310
+ * <summary><strong>Structured Output</strong></summary>
311
+ *
312
+ * ```typescript
313
+ * import { z } from 'zod';
314
+ *
315
+ * const Joke = z.object({
316
+ * setup: z.string().describe("The setup of the joke"),
317
+ * punchline: z.string().describe("The punchline to the joke"),
318
+ * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
319
+ * }).describe('Joke to tell user.');
320
+ *
321
+ * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
322
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
323
+ * console.log(jokeResult);
324
+ * ```
325
+ *
326
+ * ```txt
327
+ * {
328
+ * setup: "Why don't cats play poker in the jungle?",
329
+ * punchline: 'Too many cheetahs!',
330
+ * rating: 7
331
+ * }
332
+ * ```
333
+ * </details>
334
+ *
335
+ * <br />
336
+ *
337
+ * <details>
338
+ * <summary><strong>Multimodal</strong></summary>
339
+ *
340
+ * ```typescript
341
+ * import { HumanMessage } from '@langchain/core/messages';
342
+ *
343
+ * const imageUrl = "https://example.com/image.jpg";
344
+ * const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());
345
+ * const base64Image = Buffer.from(imageData).toString('base64');
346
+ *
347
+ * const message = new HumanMessage({
348
+ * content: [
349
+ * { type: "text", text: "describe the weather in this image" },
350
+ * {
351
+ * type: "image_url",
352
+ * image_url: { url: `data:image/jpeg;base64,${base64Image}` },
353
+ * },
354
+ * ]
355
+ * });
356
+ *
357
+ * const imageDescriptionAiMsg = await llm.invoke([message]);
358
+ * console.log(imageDescriptionAiMsg.content);
359
+ * ```
360
+ *
361
+ * ```txt
362
+ * The weather in this image appears to be beautiful and clear. The sky is a vibrant blue with scattered white clouds, suggesting a sunny and pleasant day. The clouds are wispy and light, indicating calm conditions without any signs of storms or heavy weather. The bright green grass on the rolling hills looks lush and well-watered, which could mean recent rainfall or good growing conditions. Overall, the scene depicts a perfect spring or early summer day with mild temperatures, plenty of sunshine, and gentle breezes - ideal weather for enjoying the outdoors or for plant growth.
363
+ * ```
364
+ * </details>
365
+ *
366
+ * <br />
367
+ *
368
+ * <details>
369
+ * <summary><strong>Usage Metadata</strong></summary>
370
+ *
371
+ * ```typescript
372
+ * const aiMsgForMetadata = await llm.invoke(input);
373
+ * console.log(aiMsgForMetadata.usage_metadata);
374
+ * ```
375
+ *
376
+ * ```txt
377
+ * { input_tokens: 25, output_tokens: 19, total_tokens: 44 }
378
+ * ```
379
+ * </details>
380
+ *
381
+ * <br />
382
+ *
383
+ * <details>
384
+ * <summary><strong>Stream Usage Metadata</strong></summary>
385
+ *
386
+ * ```typescript
387
+ * const streamForMetadata = await llm.stream(
388
+ * input,
389
+ * {
390
+ * streamUsage: true
391
+ * }
392
+ * );
393
+ * let fullForMetadata: AIMessageChunk | undefined;
394
+ * for await (const chunk of streamForMetadata) {
395
+ * fullForMetadata = !fullForMetadata ? chunk : concat(fullForMetadata, chunk);
396
+ * }
397
+ * console.log(fullForMetadata?.usage_metadata);
398
+ * ```
399
+ *
400
+ * ```txt
401
+ * { input_tokens: 25, output_tokens: 20, total_tokens: 45 }
402
+ * ```
403
+ * </details>
404
+ *
405
+ * <br />
406
+ *
407
+ * <details>
408
+ * <summary><strong>Response Metadata</strong></summary>
409
+ *
410
+ * ```typescript
411
+ * const aiMsgForResponseMetadata = await llm.invoke(input);
412
+ * console.log(aiMsgForResponseMetadata.response_metadata);
413
+ * ```
414
+ *
415
+ * ```txt
416
+ * {
417
+ * id: 'msg_01STxeQxJmp4sCSpioD6vK3L',
418
+ * model: 'claude-3-5-sonnet-20240620',
419
+ * stop_reason: 'end_turn',
420
+ * stop_sequence: null,
421
+ * usage: { input_tokens: 25, output_tokens: 19 },
422
+ * type: 'message',
423
+ * role: 'assistant'
424
+ * }
425
+ * ```
426
+ * </details>
427
+ *
428
+ * <br />
429
+ */
430
+ var ChatAnthropicMessages = class extends BaseChatModel {
431
+ static lc_name() {
432
+ return "ChatAnthropic";
433
+ }
434
+ get lc_secrets() {
435
+ return {
436
+ anthropicApiKey: "ANTHROPIC_API_KEY",
437
+ apiKey: "ANTHROPIC_API_KEY"
438
+ };
439
+ }
440
+ get lc_aliases() {
441
+ return { modelName: "model" };
442
+ }
443
+ lc_serializable = true;
444
+ anthropicApiKey;
445
+ apiKey;
446
+ apiUrl;
447
+ temperature = 1;
448
+ topK = -1;
449
+ topP = -1;
450
+ maxTokens = 2048;
451
+ modelName = "claude-2.1";
452
+ model = "claude-2.1";
453
+ invocationKwargs;
454
+ stopSequences;
455
+ streaming = false;
456
+ clientOptions;
457
+ thinking = { type: "disabled" };
458
+ contextManagement;
459
+ batchClient;
460
+ streamingClient;
461
+ streamUsage = true;
462
+ /**
463
+ * Optional method that returns an initialized underlying Anthropic client.
464
+ * Useful for accessing Anthropic models hosted on other cloud services
465
+ * such as Google Vertex.
466
+ */
467
+ createClient;
468
+ constructor(fields) {
469
+ super(fields ?? {});
470
+ this.anthropicApiKey = fields?.apiKey ?? fields?.anthropicApiKey ?? getEnvironmentVariable("ANTHROPIC_API_KEY");
471
+ if (!this.anthropicApiKey && !fields?.createClient) throw new Error("Anthropic API key not found");
472
+ this.clientOptions = fields?.clientOptions ?? {};
473
+ /** Keep anthropicApiKey for backwards compatibility */
474
+ this.apiKey = this.anthropicApiKey;
475
+ this.apiUrl = fields?.anthropicApiUrl;
476
+ /** Keep modelName for backwards compatibility */
477
+ this.modelName = fields?.model ?? fields?.modelName ?? this.model;
478
+ this.model = this.modelName;
479
+ this.invocationKwargs = fields?.invocationKwargs ?? {};
480
+ if (this.model.includes("opus-4-1") || this.model.includes("sonnet-4-5")) this.topP = fields?.topP === null ? void 0 : fields?.topP;
481
+ else this.topP = fields?.topP ?? this.topP;
482
+ this.temperature = fields?.temperature === null ? void 0 : fields?.temperature ?? this.temperature;
483
+ this.topK = fields?.topK ?? this.topK;
484
+ this.maxTokens = fields?.maxTokens ?? this.maxTokens;
485
+ this.stopSequences = fields?.stopSequences ?? this.stopSequences;
486
+ this.streaming = fields?.streaming ?? false;
487
+ this.streamUsage = fields?.streamUsage ?? this.streamUsage;
488
+ this.thinking = fields?.thinking ?? this.thinking;
489
+ this.contextManagement = fields?.contextManagement ?? this.contextManagement;
490
+ this.createClient = fields?.createClient ?? ((options) => new Anthropic$1(options));
491
+ }
492
+ getLsParams(options) {
493
+ const params = this.invocationParams(options);
494
+ return {
495
+ ls_provider: "anthropic",
496
+ ls_model_name: this.model,
497
+ ls_model_type: "chat",
498
+ ls_temperature: params.temperature ?? void 0,
499
+ ls_max_tokens: params.max_tokens ?? void 0,
500
+ ls_stop: options.stop
501
+ };
502
+ }
503
+ /**
504
+ * Formats LangChain StructuredTools to AnthropicTools.
505
+ *
506
+ * @param {ChatAnthropicCallOptions["tools"]} tools The tools to format
507
+ * @returns {AnthropicTool[] | undefined} The formatted tools, or undefined if none are passed.
508
+ */
509
+ formatStructuredToolToAnthropic(tools) {
510
+ if (!tools || !tools.length) return void 0;
511
+ return tools.map((tool) => {
512
+ if (isBuiltinTool(tool)) return tool;
513
+ if (isAnthropicTool(tool)) return tool;
514
+ if (isOpenAITool(tool)) return {
515
+ name: tool.function.name,
516
+ description: tool.function.description,
517
+ input_schema: tool.function.parameters
518
+ };
519
+ if (isLangChainTool(tool)) return {
520
+ name: tool.name,
521
+ description: tool.description,
522
+ input_schema: isInteropZodSchema(tool.schema) ? toJsonSchema(tool.schema) : tool.schema
523
+ };
524
+ throw new Error(`Unknown tool type passed to ChatAnthropic: ${JSON.stringify(tool, null, 2)}`);
525
+ });
526
+ }
527
+ bindTools(tools, kwargs) {
528
+ return this.withConfig({
529
+ tools: this.formatStructuredToolToAnthropic(tools),
530
+ ...kwargs
531
+ });
532
+ }
533
+ /**
534
+ * Get the parameters used to invoke the model
535
+ */
536
+ invocationParams(options) {
537
+ const tool_choice = handleToolChoice(options?.tool_choice);
538
+ if (this.thinking.type === "enabled") {
539
+ if (this.topK !== -1) throw new Error("topK is not supported when thinking is enabled");
540
+ if (this.model.includes("opus-4-1") || this.model.includes("sonnet-4-5") ? this.topP !== void 0 : this.topP !== -1) throw new Error("topP is not supported when thinking is enabled");
541
+ if (this.temperature !== 1) throw new Error("temperature is not supported when thinking is enabled");
542
+ return {
543
+ model: this.model,
544
+ stop_sequences: options?.stop ?? this.stopSequences,
545
+ stream: this.streaming,
546
+ max_tokens: this.maxTokens,
547
+ tools: this.formatStructuredToolToAnthropic(options?.tools),
548
+ tool_choice,
549
+ thinking: this.thinking,
550
+ context_management: this.contextManagement,
551
+ ...this.invocationKwargs,
552
+ container: options?.container
553
+ };
554
+ }
555
+ return {
556
+ model: this.model,
557
+ temperature: this.temperature,
558
+ top_k: this.topK,
559
+ top_p: this.topP,
560
+ stop_sequences: options?.stop ?? this.stopSequences,
561
+ stream: this.streaming,
562
+ max_tokens: this.maxTokens,
563
+ tools: this.formatStructuredToolToAnthropic(options?.tools),
564
+ tool_choice,
565
+ thinking: this.thinking,
566
+ context_management: this.contextManagement,
567
+ ...this.invocationKwargs,
568
+ container: options?.container
569
+ };
570
+ }
571
+ /** @ignore */
572
+ _identifyingParams() {
573
+ return {
574
+ model_name: this.model,
575
+ ...this.invocationParams()
576
+ };
577
+ }
578
+ /**
579
+ * Get the identifying parameters for the model
580
+ */
581
+ identifyingParams() {
582
+ return {
583
+ model_name: this.model,
584
+ ...this.invocationParams()
585
+ };
586
+ }
587
+ async *_streamResponseChunks(messages, options, runManager) {
588
+ const params = this.invocationParams(options);
589
+ const formattedMessages = _convertMessagesToAnthropicPayload(messages);
590
+ const payload = {
591
+ ...params,
592
+ ...formattedMessages,
593
+ stream: true
594
+ };
595
+ const coerceContentToString = !_toolsInParams(payload) && !_documentsInParams(payload) && !_thinkingInParams(payload);
596
+ const stream = await this.createStreamWithRetry(payload, { headers: options.headers });
597
+ for await (const data of stream) {
598
+ if (options.signal?.aborted) {
599
+ stream.controller.abort();
600
+ throw new Error("AbortError: User aborted the request.");
601
+ }
602
+ const shouldStreamUsage = this.streamUsage ?? options.streamUsage;
603
+ const result = _makeMessageChunkFromAnthropicEvent(data, {
604
+ streamUsage: shouldStreamUsage,
605
+ coerceContentToString
606
+ });
607
+ if (!result) continue;
608
+ const { chunk } = result;
609
+ const token = extractToken(chunk);
610
+ const generationChunk = new ChatGenerationChunk({
611
+ message: new AIMessageChunk({
612
+ content: chunk.content,
613
+ additional_kwargs: chunk.additional_kwargs,
614
+ tool_call_chunks: chunk.tool_call_chunks,
615
+ usage_metadata: shouldStreamUsage ? chunk.usage_metadata : void 0,
616
+ response_metadata: chunk.response_metadata,
617
+ id: chunk.id
618
+ }),
619
+ text: token ?? ""
620
+ });
621
+ yield generationChunk;
622
+ await runManager?.handleLLMNewToken(token ?? "", void 0, void 0, void 0, void 0, { chunk: generationChunk });
623
+ }
624
+ }
625
+ /** @ignore */
626
+ async _generateNonStreaming(messages, params, requestOptions) {
627
+ const response = await this.completionWithRetry({
628
+ ...params,
629
+ stream: false,
630
+ ..._convertMessagesToAnthropicPayload(messages)
631
+ }, requestOptions);
632
+ const { content,...additionalKwargs } = response;
633
+ const generations = anthropicResponseToChatMessages(content, additionalKwargs);
634
+ const { role: _role, type: _type,...rest } = additionalKwargs;
635
+ return {
636
+ generations,
637
+ llmOutput: rest
638
+ };
639
+ }
640
+ /** @ignore */
641
+ async _generate(messages, options, runManager) {
642
+ if (this.stopSequences && options.stop) throw new Error(`"stopSequence" parameter found in input and default params`);
643
+ const params = this.invocationParams(options);
644
+ if (params.stream) {
645
+ let finalChunk;
646
+ const stream = this._streamResponseChunks(messages, options, runManager);
647
+ for await (const chunk of stream) if (finalChunk === void 0) finalChunk = chunk;
648
+ else finalChunk = finalChunk.concat(chunk);
649
+ if (finalChunk === void 0) throw new Error("No chunks returned from Anthropic API.");
650
+ return { generations: [{
651
+ text: finalChunk.text,
652
+ message: finalChunk.message
653
+ }] };
654
+ } else return this._generateNonStreaming(messages, params, {
655
+ signal: options.signal,
656
+ headers: options.headers
657
+ });
658
+ }
659
+ /**
660
+ * Creates a streaming request with retry.
661
+ * @param request The parameters for creating a completion.
662
+ * @param options
663
+ * @returns A streaming request.
664
+ */
665
+ async createStreamWithRetry(request, options) {
666
+ if (!this.streamingClient) {
667
+ const options_ = this.apiUrl ? { baseURL: this.apiUrl } : void 0;
668
+ this.streamingClient = this.createClient({
669
+ dangerouslyAllowBrowser: true,
670
+ ...this.clientOptions,
671
+ ...options_,
672
+ apiKey: this.apiKey,
673
+ maxRetries: 0
674
+ });
675
+ }
676
+ const makeCompletionRequest = async () => {
677
+ try {
678
+ return await this.streamingClient.messages.create({
679
+ ...request,
680
+ ...this.invocationKwargs,
681
+ stream: true
682
+ }, options);
683
+ } catch (e) {
684
+ const error = wrapAnthropicClientError(e);
685
+ throw error;
686
+ }
687
+ };
688
+ return this.caller.call(makeCompletionRequest);
689
+ }
690
+ /** @ignore */
691
+ async completionWithRetry(request, options) {
692
+ if (!this.batchClient) {
693
+ const options$1 = this.apiUrl ? { baseURL: this.apiUrl } : void 0;
694
+ this.batchClient = this.createClient({
695
+ dangerouslyAllowBrowser: true,
696
+ ...this.clientOptions,
697
+ ...options$1,
698
+ apiKey: this.apiKey,
699
+ maxRetries: 0
700
+ });
701
+ }
702
+ const makeCompletionRequest = async () => {
703
+ try {
704
+ return await this.batchClient.messages.create({
705
+ ...request,
706
+ ...this.invocationKwargs
707
+ }, options);
708
+ } catch (e) {
709
+ const error = wrapAnthropicClientError(e);
710
+ throw error;
711
+ }
712
+ };
713
+ return this.caller.callWithOptions({ signal: options.signal ?? void 0 }, makeCompletionRequest);
714
+ }
715
+ _llmType() {
716
+ return "anthropic";
717
+ }
718
+ withStructuredOutput(outputSchema, config) {
719
+ const schema = outputSchema;
720
+ const name = config?.name;
721
+ const method = config?.method;
722
+ const includeRaw = config?.includeRaw;
723
+ if (method === "jsonMode") throw new Error(`Anthropic only supports "functionCalling" as a method.`);
724
+ let functionName = name ?? "extract";
725
+ let outputParser;
726
+ let tools;
727
+ if (isInteropZodSchema(schema)) {
728
+ const jsonSchema = toJsonSchema(schema);
729
+ tools = [{
730
+ name: functionName,
731
+ description: jsonSchema.description ?? "A function available to call.",
732
+ input_schema: jsonSchema
733
+ }];
734
+ outputParser = new AnthropicToolsOutputParser({
735
+ returnSingle: true,
736
+ keyName: functionName,
737
+ zodSchema: schema
738
+ });
739
+ } else {
740
+ let anthropicTools;
741
+ if (typeof schema.name === "string" && typeof schema.description === "string" && typeof schema.input_schema === "object" && schema.input_schema != null) {
742
+ anthropicTools = schema;
743
+ functionName = schema.name;
744
+ } else anthropicTools = {
745
+ name: functionName,
746
+ description: schema.description ?? "",
747
+ input_schema: schema
748
+ };
749
+ tools = [anthropicTools];
750
+ outputParser = new AnthropicToolsOutputParser({
751
+ returnSingle: true,
752
+ keyName: functionName
753
+ });
754
+ }
755
+ let llm;
756
+ if (this.thinking?.type === "enabled") {
757
+ const thinkingAdmonition = "Anthropic structured output relies on forced tool calling, which is not supported when `thinking` is enabled. This method will raise OutputParserException if tool calls are not generated. Consider disabling `thinking` or adjust your prompt to ensure the tool is called.";
758
+ console.warn(thinkingAdmonition);
759
+ llm = this.withConfig({
760
+ tools,
761
+ ls_structured_output_format: {
762
+ kwargs: { method: "functionCalling" },
763
+ schema: toJsonSchema(schema)
764
+ }
765
+ });
766
+ const raiseIfNoToolCalls = (message) => {
767
+ if (!message.tool_calls || message.tool_calls.length === 0) throw new Error(thinkingAdmonition);
768
+ return message;
769
+ };
770
+ llm = llm.pipe(raiseIfNoToolCalls);
771
+ } else llm = this.withConfig({
772
+ tools,
773
+ tool_choice: {
774
+ type: "tool",
775
+ name: functionName
776
+ },
777
+ ls_structured_output_format: {
778
+ kwargs: { method: "functionCalling" },
779
+ schema: toJsonSchema(schema)
780
+ }
781
+ });
782
+ if (!includeRaw) return llm.pipe(outputParser).withConfig({ runName: "ChatAnthropicStructuredOutput" });
783
+ const parserAssign = RunnablePassthrough.assign({ parsed: (input, config$1) => outputParser.invoke(input.raw, config$1) });
784
+ const parserNone = RunnablePassthrough.assign({ parsed: () => null });
785
+ const parsedWithFallback = parserAssign.withFallbacks({ fallbacks: [parserNone] });
786
+ return RunnableSequence.from([{ raw: llm }, parsedWithFallback]).withConfig({ runName: "StructuredOutputRunnable" });
787
+ }
788
+ };
789
+ var ChatAnthropic = class extends ChatAnthropicMessages {};
790
+
791
+ //#endregion
792
+ export { ChatAnthropic, ChatAnthropicMessages };
793
+ //# sourceMappingURL=chat_models.js.map