@langchain/anthropic 0.3.25 → 1.0.0-alpha.1

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