@langchain/anthropic 0.3.26 → 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 -1000
  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 -206
  8. package/dist/chat_models.d.ts.map +1 -0
  9. package/dist/chat_models.js +766 -991
  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,318 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ChatAnthropicTools = void 0;
4
- const fast_xml_parser_1 = require("fast-xml-parser");
5
- const messages_1 = require("@langchain/core/messages");
6
- const chat_models_1 = require("@langchain/core/language_models/chat_models");
7
- const runnables_1 = require("@langchain/core/runnables");
8
- const openai_tools_1 = require("@langchain/core/output_parsers/openai_tools");
9
- const json_schema_1 = require("@langchain/core/utils/json_schema");
10
- const types_1 = require("@langchain/core/utils/types");
11
- const chat_models_js_1 = require("../chat_models.cjs");
12
- const tool_calling_js_1 = require("./utils/tool_calling.cjs");
13
- /**
14
- * Experimental wrapper over Anthropic chat models that adds support for
15
- * a function calling interface.
16
- * @deprecated Prefer traditional tool use through ChatAnthropic.
17
- */
18
- class ChatAnthropicTools extends chat_models_1.BaseChatModel {
19
- static lc_name() {
20
- return "ChatAnthropicTools";
21
- }
22
- constructor(fields) {
23
- if (fields?.cache !== undefined) {
24
- throw new Error("Caching is not supported for this model.");
25
- }
26
- super(fields ?? {});
27
- Object.defineProperty(this, "llm", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: void 0
32
- });
33
- Object.defineProperty(this, "stopSequences", {
34
- enumerable: true,
35
- configurable: true,
36
- writable: true,
37
- value: void 0
38
- });
39
- Object.defineProperty(this, "systemPromptTemplate", {
40
- enumerable: true,
41
- configurable: true,
42
- writable: true,
43
- value: void 0
44
- });
45
- Object.defineProperty(this, "lc_namespace", {
46
- enumerable: true,
47
- configurable: true,
48
- writable: true,
49
- value: ["langchain", "experimental", "chat_models"]
50
- });
51
- this.llm = fields?.llm ?? new chat_models_js_1.ChatAnthropic(fields);
52
- this.systemPromptTemplate =
53
- fields?.systemPromptTemplate ?? tool_calling_js_1.DEFAULT_TOOL_SYSTEM_PROMPT;
54
- this.stopSequences =
55
- fields?.stopSequences ?? this.llm.stopSequences;
56
- }
57
- invocationParams() {
58
- return this.llm.invocationParams();
59
- }
60
- /** @ignore */
61
- _identifyingParams() {
62
- return this.llm._identifyingParams();
63
- }
64
- async *_streamResponseChunks(messages, options, runManager) {
65
- yield* this.llm._streamResponseChunks(messages, options, runManager);
66
- }
67
- async _prepareAndParseToolCall({ messages, options, systemPromptTemplate = tool_calling_js_1.DEFAULT_TOOL_SYSTEM_PROMPT, stopSequences, }) {
68
- let promptMessages = messages;
69
- let forced = false;
70
- let toolCall;
71
- const tools = options.tools === undefined ? [] : [...options.tools];
72
- if (options.tools !== undefined && options.tools.length > 0) {
73
- const content = await systemPromptTemplate.format({
74
- tools: `<tools>\n${options.tools
75
- .map(tool_calling_js_1.formatAsXMLRepresentation)
76
- .join("\n\n")}</tools>`,
77
- });
78
- if (promptMessages.length && promptMessages[0]._getType() !== "system") {
79
- const systemMessage = new messages_1.SystemMessage({ content });
80
- promptMessages = [systemMessage].concat(promptMessages);
81
- }
82
- else {
83
- const systemMessage = new messages_1.SystemMessage({
84
- content: `${content}\n\n${promptMessages[0].content}`,
85
- });
86
- promptMessages = [systemMessage].concat(promptMessages.slice(1));
87
- }
88
- // eslint-disable-next-line no-param-reassign
89
- options.stop = stopSequences.concat(["</function_calls>"]);
90
- if (options.tool_choice && options.tool_choice !== "auto") {
91
- toolCall = options.tool_choice.function.name;
92
- forced = true;
93
- const matchingFunction = options.tools.find(
94
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
- (tool) => tool.function.name === toolCall);
96
- if (!matchingFunction) {
97
- throw new Error(`No matching function found for passed "tool_choice"`);
98
- }
99
- promptMessages = promptMessages.concat([
100
- new messages_1.AIMessage({
101
- content: `<function_calls>\n<invoke><tool_name>${toolCall}</tool_name>`,
102
- }),
103
- ]);
104
- // eslint-disable-next-line no-param-reassign
105
- delete options.tool_choice;
106
- }
107
- // eslint-disable-next-line no-param-reassign
108
- delete options.tools;
109
- }
110
- else if (options.tool_choice !== undefined) {
111
- throw new Error(`If "tool_choice" is provided, "tools" must also be.`);
112
- }
113
- const chatResult = await this.llm
114
- .withConfig({ runName: "ChatAnthropicTools" })
115
- .invoke(promptMessages, options);
116
- const chatGenerationContent = chatResult.content;
117
- if (typeof chatGenerationContent !== "string") {
118
- throw new Error("AnthropicFunctions does not support non-string output.");
119
- }
120
- if (forced) {
121
- const parser = new fast_xml_parser_1.XMLParser();
122
- const result = parser.parse(`<function_calls>\n<invoke><tool_name>${toolCall}</tool_name>${chatGenerationContent}</function_calls>`);
123
- if (toolCall === undefined) {
124
- throw new Error(`Could not parse called function from model output.`);
125
- }
126
- const invocations = Array.isArray(result.function_calls?.invoke ?? [])
127
- ? result.function_calls.invoke
128
- : [result.function_calls.invoke];
129
- const responseMessageWithFunctions = new messages_1.AIMessage({
130
- content: "",
131
- additional_kwargs: {
132
- tool_calls: invocations.map((toolInvocation, i) => {
133
- const calledTool = tools.find((tool) => tool.function.name === toolCall);
134
- if (calledTool === undefined) {
135
- throw new Error(`Called tool "${toolCall}" did not match an existing tool.`);
136
- }
137
- return {
138
- id: i.toString(),
139
- type: "function",
140
- function: {
141
- name: toolInvocation.tool_name,
142
- arguments: JSON.stringify((0, tool_calling_js_1.fixArrayXMLParameters)(calledTool.function.parameters, toolInvocation.parameters)),
143
- },
144
- };
145
- }),
146
- },
147
- });
148
- return {
149
- generations: [{ message: responseMessageWithFunctions, text: "" }],
150
- };
151
- }
152
- else if (chatGenerationContent.includes("<function_calls>")) {
153
- const parser = new fast_xml_parser_1.XMLParser();
154
- const result = parser.parse(`${chatGenerationContent}</function_calls>`);
155
- const invocations = Array.isArray(result.function_calls?.invoke ?? [])
156
- ? result.function_calls.invoke
157
- : [result.function_calls.invoke];
158
- const responseMessageWithFunctions = new messages_1.AIMessage({
159
- content: chatGenerationContent.split("<function_calls>")[0],
160
- additional_kwargs: {
161
- tool_calls: invocations.map((toolInvocation, i) => {
162
- const calledTool = tools.find((tool) => tool.function.name === toolInvocation.tool_name);
163
- if (calledTool === undefined) {
164
- throw new Error(`Called tool "${toolCall}" did not match an existing tool.`);
165
- }
166
- return {
167
- id: i.toString(),
168
- type: "function",
169
- function: {
170
- name: toolInvocation.tool_name,
171
- arguments: JSON.stringify((0, tool_calling_js_1.fixArrayXMLParameters)(calledTool.function.parameters, toolInvocation.parameters)),
172
- },
173
- };
174
- }),
175
- },
176
- });
177
- return {
178
- generations: [{ message: responseMessageWithFunctions, text: "" }],
179
- };
180
- }
181
- return { generations: [{ message: chatResult, text: "" }] };
182
- }
183
- async generate(messages, parsedOptions, callbacks) {
184
- const baseMessages = messages.map((messageList) => messageList.map(messages_1.coerceMessageLikeToMessage));
185
- // generate results
186
- const chatResults = await Promise.all(baseMessages.map((messageList) => this._prepareAndParseToolCall({
187
- messages: messageList,
188
- options: { callbacks, ...parsedOptions },
189
- systemPromptTemplate: this.systemPromptTemplate,
190
- stopSequences: this.stopSequences ?? [],
191
- })));
192
- // create combined output
193
- const output = {
194
- generations: chatResults.map((chatResult) => chatResult.generations),
195
- };
196
- return output;
197
- }
198
- async _generate(_messages, _options, _runManager) {
199
- throw new Error("Unused.");
200
- }
201
- _llmType() {
202
- return "anthropic_tool_calling";
203
- }
204
- withStructuredOutput(outputSchema, config) {
205
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
206
- let schema;
207
- let name;
208
- let method;
209
- let includeRaw;
210
- let force;
211
- if (isStructuredOutputMethodParams(outputSchema)) {
212
- schema = outputSchema.schema;
213
- name = outputSchema.name;
214
- method = outputSchema.method;
215
- includeRaw = outputSchema.includeRaw;
216
- }
217
- else {
218
- schema = outputSchema;
219
- name = config?.name;
220
- method = config?.method;
221
- includeRaw = config?.includeRaw;
222
- force = config?.force ?? false;
223
- }
224
- if (method === "jsonMode") {
225
- throw new Error(`Anthropic only supports "functionCalling" as a method.`);
226
- }
227
- let functionName = name ?? "extract";
228
- let outputParser;
229
- let tools;
230
- if ((0, types_1.isInteropZodSchema)(schema)) {
231
- const jsonSchema = (0, json_schema_1.toJsonSchema)(schema);
232
- tools = [
233
- {
234
- type: "function",
235
- function: {
236
- name: functionName,
237
- description: jsonSchema.description,
238
- parameters: jsonSchema,
239
- },
240
- },
241
- ];
242
- outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
243
- returnSingle: true,
244
- keyName: functionName,
245
- zodSchema: schema,
246
- });
247
- }
248
- else {
249
- let openAIFunctionDefinition;
250
- if (typeof schema.name === "string" &&
251
- typeof schema.parameters === "object" &&
252
- schema.parameters != null) {
253
- openAIFunctionDefinition = schema;
254
- functionName = schema.name;
255
- }
256
- else {
257
- openAIFunctionDefinition = {
258
- name: functionName,
259
- description: schema.description ?? "",
260
- parameters: schema,
261
- };
262
- }
263
- tools = [
264
- {
265
- type: "function",
266
- function: openAIFunctionDefinition,
267
- },
268
- ];
269
- outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
270
- returnSingle: true,
271
- keyName: functionName,
272
- });
273
- }
274
- const llm = this.withConfig({
275
- tools,
276
- tool_choice: force
277
- ? {
278
- type: "function",
279
- function: {
280
- name: functionName,
281
- },
282
- }
283
- : "auto",
284
- });
285
- if (!includeRaw) {
286
- return llm.pipe(outputParser).withConfig({
287
- runName: "ChatAnthropicStructuredOutput",
288
- });
289
- }
290
- const parserAssign = runnables_1.RunnablePassthrough.assign({
291
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
292
- parsed: (input, config) => outputParser.invoke(input.raw, config),
293
- });
294
- const parserNone = runnables_1.RunnablePassthrough.assign({
295
- parsed: () => null,
296
- });
297
- const parsedWithFallback = parserAssign.withFallbacks({
298
- fallbacks: [parserNone],
299
- });
300
- return runnables_1.RunnableSequence.from([
301
- {
302
- raw: llm,
303
- },
304
- parsedWithFallback,
305
- ]).withConfig({
306
- runName: "StructuredOutputRunnable",
307
- });
308
- }
309
- }
310
- exports.ChatAnthropicTools = ChatAnthropicTools;
311
- function isStructuredOutputMethodParams(x
312
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
313
- ) {
314
- return (x !== undefined &&
315
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
316
- typeof x.schema ===
317
- "object");
318
- }
@@ -1,57 +0,0 @@
1
- import { BaseMessage, BaseMessageLike } from "@langchain/core/messages";
2
- import type { ChatGenerationChunk, ChatResult, LLMResult } from "@langchain/core/outputs";
3
- import { BaseChatModel, BaseChatModelParams } from "@langchain/core/language_models/chat_models";
4
- import { CallbackManagerForLLMRun, Callbacks } from "@langchain/core/callbacks/manager";
5
- import { BasePromptTemplate } from "@langchain/core/prompts";
6
- import type { BaseLanguageModelCallOptions, BaseLanguageModelInput, StructuredOutputMethodParams, StructuredOutputMethodOptions, ToolDefinition } from "@langchain/core/language_models/base";
7
- import { Runnable } from "@langchain/core/runnables";
8
- import { InteropZodType } from "@langchain/core/utils/types";
9
- import { type AnthropicInput } from "../chat_models.js";
10
- export interface ChatAnthropicToolsCallOptions extends BaseLanguageModelCallOptions {
11
- tools?: ToolDefinition[];
12
- tool_choice?: "auto" | {
13
- function: {
14
- name: string;
15
- };
16
- type: "function";
17
- };
18
- }
19
- export type ChatAnthropicToolsInput = Partial<AnthropicInput> & BaseChatModelParams & {
20
- llm?: BaseChatModel;
21
- systemPromptTemplate?: BasePromptTemplate;
22
- };
23
- /**
24
- * Experimental wrapper over Anthropic chat models that adds support for
25
- * a function calling interface.
26
- * @deprecated Prefer traditional tool use through ChatAnthropic.
27
- */
28
- export declare class ChatAnthropicTools extends BaseChatModel<ChatAnthropicToolsCallOptions> {
29
- llm: BaseChatModel;
30
- stopSequences?: string[];
31
- systemPromptTemplate: BasePromptTemplate;
32
- lc_namespace: string[];
33
- static lc_name(): string;
34
- constructor(fields?: ChatAnthropicToolsInput);
35
- invocationParams(): any;
36
- /** @ignore */
37
- _identifyingParams(): Record<string, any>;
38
- _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
39
- _prepareAndParseToolCall({ messages, options, systemPromptTemplate, stopSequences, }: {
40
- messages: BaseMessage[];
41
- options: ChatAnthropicToolsCallOptions;
42
- systemPromptTemplate?: BasePromptTemplate;
43
- stopSequences: string[];
44
- }): Promise<ChatResult>;
45
- generate(messages: BaseMessageLike[][], parsedOptions?: ChatAnthropicToolsCallOptions, callbacks?: Callbacks): Promise<LLMResult>;
46
- _generate(_messages: BaseMessage[], _options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun | undefined): Promise<ChatResult>;
47
- _llmType(): string;
48
- withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, false> | InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false> & {
49
- force?: boolean;
50
- }): Runnable<BaseLanguageModelInput, RunOutput>;
51
- withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, true> | InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true> & {
52
- force?: boolean;
53
- }): Runnable<BaseLanguageModelInput, {
54
- raw: BaseMessage;
55
- parsed: RunOutput;
56
- }>;
57
- }
@@ -1,314 +0,0 @@
1
- import { XMLParser } from "fast-xml-parser";
2
- import { AIMessage, SystemMessage, coerceMessageLikeToMessage, } from "@langchain/core/messages";
3
- import { BaseChatModel, } from "@langchain/core/language_models/chat_models";
4
- import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
5
- import { JsonOutputKeyToolsParser } from "@langchain/core/output_parsers/openai_tools";
6
- import { toJsonSchema, } from "@langchain/core/utils/json_schema";
7
- import { isInteropZodSchema, } from "@langchain/core/utils/types";
8
- import { ChatAnthropic } from "../chat_models.js";
9
- import { DEFAULT_TOOL_SYSTEM_PROMPT, formatAsXMLRepresentation, fixArrayXMLParameters, } from "./utils/tool_calling.js";
10
- /**
11
- * Experimental wrapper over Anthropic chat models that adds support for
12
- * a function calling interface.
13
- * @deprecated Prefer traditional tool use through ChatAnthropic.
14
- */
15
- export class ChatAnthropicTools extends BaseChatModel {
16
- static lc_name() {
17
- return "ChatAnthropicTools";
18
- }
19
- constructor(fields) {
20
- if (fields?.cache !== undefined) {
21
- throw new Error("Caching is not supported for this model.");
22
- }
23
- super(fields ?? {});
24
- Object.defineProperty(this, "llm", {
25
- enumerable: true,
26
- configurable: true,
27
- writable: true,
28
- value: void 0
29
- });
30
- Object.defineProperty(this, "stopSequences", {
31
- enumerable: true,
32
- configurable: true,
33
- writable: true,
34
- value: void 0
35
- });
36
- Object.defineProperty(this, "systemPromptTemplate", {
37
- enumerable: true,
38
- configurable: true,
39
- writable: true,
40
- value: void 0
41
- });
42
- Object.defineProperty(this, "lc_namespace", {
43
- enumerable: true,
44
- configurable: true,
45
- writable: true,
46
- value: ["langchain", "experimental", "chat_models"]
47
- });
48
- this.llm = fields?.llm ?? new ChatAnthropic(fields);
49
- this.systemPromptTemplate =
50
- fields?.systemPromptTemplate ?? DEFAULT_TOOL_SYSTEM_PROMPT;
51
- this.stopSequences =
52
- fields?.stopSequences ?? this.llm.stopSequences;
53
- }
54
- invocationParams() {
55
- return this.llm.invocationParams();
56
- }
57
- /** @ignore */
58
- _identifyingParams() {
59
- return this.llm._identifyingParams();
60
- }
61
- async *_streamResponseChunks(messages, options, runManager) {
62
- yield* this.llm._streamResponseChunks(messages, options, runManager);
63
- }
64
- async _prepareAndParseToolCall({ messages, options, systemPromptTemplate = DEFAULT_TOOL_SYSTEM_PROMPT, stopSequences, }) {
65
- let promptMessages = messages;
66
- let forced = false;
67
- let toolCall;
68
- const tools = options.tools === undefined ? [] : [...options.tools];
69
- if (options.tools !== undefined && options.tools.length > 0) {
70
- const content = await systemPromptTemplate.format({
71
- tools: `<tools>\n${options.tools
72
- .map(formatAsXMLRepresentation)
73
- .join("\n\n")}</tools>`,
74
- });
75
- if (promptMessages.length && promptMessages[0]._getType() !== "system") {
76
- const systemMessage = new SystemMessage({ content });
77
- promptMessages = [systemMessage].concat(promptMessages);
78
- }
79
- else {
80
- const systemMessage = new SystemMessage({
81
- content: `${content}\n\n${promptMessages[0].content}`,
82
- });
83
- promptMessages = [systemMessage].concat(promptMessages.slice(1));
84
- }
85
- // eslint-disable-next-line no-param-reassign
86
- options.stop = stopSequences.concat(["</function_calls>"]);
87
- if (options.tool_choice && options.tool_choice !== "auto") {
88
- toolCall = options.tool_choice.function.name;
89
- forced = true;
90
- const matchingFunction = options.tools.find(
91
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
92
- (tool) => tool.function.name === toolCall);
93
- if (!matchingFunction) {
94
- throw new Error(`No matching function found for passed "tool_choice"`);
95
- }
96
- promptMessages = promptMessages.concat([
97
- new AIMessage({
98
- content: `<function_calls>\n<invoke><tool_name>${toolCall}</tool_name>`,
99
- }),
100
- ]);
101
- // eslint-disable-next-line no-param-reassign
102
- delete options.tool_choice;
103
- }
104
- // eslint-disable-next-line no-param-reassign
105
- delete options.tools;
106
- }
107
- else if (options.tool_choice !== undefined) {
108
- throw new Error(`If "tool_choice" is provided, "tools" must also be.`);
109
- }
110
- const chatResult = await this.llm
111
- .withConfig({ runName: "ChatAnthropicTools" })
112
- .invoke(promptMessages, options);
113
- const chatGenerationContent = chatResult.content;
114
- if (typeof chatGenerationContent !== "string") {
115
- throw new Error("AnthropicFunctions does not support non-string output.");
116
- }
117
- if (forced) {
118
- const parser = new XMLParser();
119
- const result = parser.parse(`<function_calls>\n<invoke><tool_name>${toolCall}</tool_name>${chatGenerationContent}</function_calls>`);
120
- if (toolCall === undefined) {
121
- throw new Error(`Could not parse called function from model output.`);
122
- }
123
- const invocations = Array.isArray(result.function_calls?.invoke ?? [])
124
- ? result.function_calls.invoke
125
- : [result.function_calls.invoke];
126
- const responseMessageWithFunctions = new AIMessage({
127
- content: "",
128
- additional_kwargs: {
129
- tool_calls: invocations.map((toolInvocation, i) => {
130
- const calledTool = tools.find((tool) => tool.function.name === toolCall);
131
- if (calledTool === undefined) {
132
- throw new Error(`Called tool "${toolCall}" did not match an existing tool.`);
133
- }
134
- return {
135
- id: i.toString(),
136
- type: "function",
137
- function: {
138
- name: toolInvocation.tool_name,
139
- arguments: JSON.stringify(fixArrayXMLParameters(calledTool.function.parameters, toolInvocation.parameters)),
140
- },
141
- };
142
- }),
143
- },
144
- });
145
- return {
146
- generations: [{ message: responseMessageWithFunctions, text: "" }],
147
- };
148
- }
149
- else if (chatGenerationContent.includes("<function_calls>")) {
150
- const parser = new XMLParser();
151
- const result = parser.parse(`${chatGenerationContent}</function_calls>`);
152
- const invocations = Array.isArray(result.function_calls?.invoke ?? [])
153
- ? result.function_calls.invoke
154
- : [result.function_calls.invoke];
155
- const responseMessageWithFunctions = new AIMessage({
156
- content: chatGenerationContent.split("<function_calls>")[0],
157
- additional_kwargs: {
158
- tool_calls: invocations.map((toolInvocation, i) => {
159
- const calledTool = tools.find((tool) => tool.function.name === toolInvocation.tool_name);
160
- if (calledTool === undefined) {
161
- throw new Error(`Called tool "${toolCall}" did not match an existing tool.`);
162
- }
163
- return {
164
- id: i.toString(),
165
- type: "function",
166
- function: {
167
- name: toolInvocation.tool_name,
168
- arguments: JSON.stringify(fixArrayXMLParameters(calledTool.function.parameters, toolInvocation.parameters)),
169
- },
170
- };
171
- }),
172
- },
173
- });
174
- return {
175
- generations: [{ message: responseMessageWithFunctions, text: "" }],
176
- };
177
- }
178
- return { generations: [{ message: chatResult, text: "" }] };
179
- }
180
- async generate(messages, parsedOptions, callbacks) {
181
- const baseMessages = messages.map((messageList) => messageList.map(coerceMessageLikeToMessage));
182
- // generate results
183
- const chatResults = await Promise.all(baseMessages.map((messageList) => this._prepareAndParseToolCall({
184
- messages: messageList,
185
- options: { callbacks, ...parsedOptions },
186
- systemPromptTemplate: this.systemPromptTemplate,
187
- stopSequences: this.stopSequences ?? [],
188
- })));
189
- // create combined output
190
- const output = {
191
- generations: chatResults.map((chatResult) => chatResult.generations),
192
- };
193
- return output;
194
- }
195
- async _generate(_messages, _options, _runManager) {
196
- throw new Error("Unused.");
197
- }
198
- _llmType() {
199
- return "anthropic_tool_calling";
200
- }
201
- withStructuredOutput(outputSchema, config) {
202
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
203
- let schema;
204
- let name;
205
- let method;
206
- let includeRaw;
207
- let force;
208
- if (isStructuredOutputMethodParams(outputSchema)) {
209
- schema = outputSchema.schema;
210
- name = outputSchema.name;
211
- method = outputSchema.method;
212
- includeRaw = outputSchema.includeRaw;
213
- }
214
- else {
215
- schema = outputSchema;
216
- name = config?.name;
217
- method = config?.method;
218
- includeRaw = config?.includeRaw;
219
- force = config?.force ?? false;
220
- }
221
- if (method === "jsonMode") {
222
- throw new Error(`Anthropic only supports "functionCalling" as a method.`);
223
- }
224
- let functionName = name ?? "extract";
225
- let outputParser;
226
- let tools;
227
- if (isInteropZodSchema(schema)) {
228
- const jsonSchema = toJsonSchema(schema);
229
- tools = [
230
- {
231
- type: "function",
232
- function: {
233
- name: functionName,
234
- description: jsonSchema.description,
235
- parameters: jsonSchema,
236
- },
237
- },
238
- ];
239
- outputParser = new JsonOutputKeyToolsParser({
240
- returnSingle: true,
241
- keyName: functionName,
242
- zodSchema: schema,
243
- });
244
- }
245
- else {
246
- let openAIFunctionDefinition;
247
- if (typeof schema.name === "string" &&
248
- typeof schema.parameters === "object" &&
249
- schema.parameters != null) {
250
- openAIFunctionDefinition = schema;
251
- functionName = schema.name;
252
- }
253
- else {
254
- openAIFunctionDefinition = {
255
- name: functionName,
256
- description: schema.description ?? "",
257
- parameters: schema,
258
- };
259
- }
260
- tools = [
261
- {
262
- type: "function",
263
- function: openAIFunctionDefinition,
264
- },
265
- ];
266
- outputParser = new JsonOutputKeyToolsParser({
267
- returnSingle: true,
268
- keyName: functionName,
269
- });
270
- }
271
- const llm = this.withConfig({
272
- tools,
273
- tool_choice: force
274
- ? {
275
- type: "function",
276
- function: {
277
- name: functionName,
278
- },
279
- }
280
- : "auto",
281
- });
282
- if (!includeRaw) {
283
- return llm.pipe(outputParser).withConfig({
284
- runName: "ChatAnthropicStructuredOutput",
285
- });
286
- }
287
- const parserAssign = RunnablePassthrough.assign({
288
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
289
- parsed: (input, config) => outputParser.invoke(input.raw, config),
290
- });
291
- const parserNone = RunnablePassthrough.assign({
292
- parsed: () => null,
293
- });
294
- const parsedWithFallback = parserAssign.withFallbacks({
295
- fallbacks: [parserNone],
296
- });
297
- return RunnableSequence.from([
298
- {
299
- raw: llm,
300
- },
301
- parsedWithFallback,
302
- ]).withConfig({
303
- runName: "StructuredOutputRunnable",
304
- });
305
- }
306
- }
307
- function isStructuredOutputMethodParams(x
308
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
309
- ) {
310
- return (x !== undefined &&
311
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
312
- typeof x.schema ===
313
- "object");
314
- }