@langchain/anthropic 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chat_models.cjs +13 -1
- package/dist/chat_models.js +13 -1
- package/dist/experimental/tool_calling.cjs +17 -7
- package/dist/experimental/tool_calling.d.ts +1 -1
- package/dist/experimental/tool_calling.js +17 -7
- package/dist/experimental/utils/tool_calling.cjs +9 -2
- package/dist/experimental/utils/tool_calling.d.ts +1 -1
- package/dist/experimental/utils/tool_calling.js +9 -2
- package/package.json +1 -1
package/dist/chat_models.cjs
CHANGED
|
@@ -203,6 +203,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
203
203
|
...this.formatMessagesForAnthropic(messages),
|
|
204
204
|
stream: true,
|
|
205
205
|
});
|
|
206
|
+
let usageData = { input_tokens: 0, output_tokens: 0 };
|
|
206
207
|
for await (const data of stream) {
|
|
207
208
|
if (options.signal?.aborted) {
|
|
208
209
|
stream.controller.abort();
|
|
@@ -210,7 +211,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
210
211
|
}
|
|
211
212
|
if (data.type === "message_start") {
|
|
212
213
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
213
|
-
const { content, ...additionalKwargs } = data.message;
|
|
214
|
+
const { content, usage, ...additionalKwargs } = data.message;
|
|
214
215
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
215
216
|
const filteredAdditionalKwargs = {};
|
|
216
217
|
for (const [key, value] of Object.entries(additionalKwargs)) {
|
|
@@ -218,6 +219,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
218
219
|
filteredAdditionalKwargs[key] = value;
|
|
219
220
|
}
|
|
220
221
|
}
|
|
222
|
+
usageData = usage;
|
|
221
223
|
yield new outputs_1.ChatGenerationChunk({
|
|
222
224
|
message: new messages_1.AIMessageChunk({
|
|
223
225
|
content: "",
|
|
@@ -234,6 +236,9 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
234
236
|
}),
|
|
235
237
|
text: "",
|
|
236
238
|
});
|
|
239
|
+
if (data?.usage !== undefined) {
|
|
240
|
+
usageData.output_tokens += data.usage.output_tokens;
|
|
241
|
+
}
|
|
237
242
|
}
|
|
238
243
|
else if (data.type === "content_block_delta") {
|
|
239
244
|
const content = data.delta?.text;
|
|
@@ -249,6 +254,13 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
249
254
|
}
|
|
250
255
|
}
|
|
251
256
|
}
|
|
257
|
+
yield new outputs_1.ChatGenerationChunk({
|
|
258
|
+
message: new messages_1.AIMessageChunk({
|
|
259
|
+
content: "",
|
|
260
|
+
additional_kwargs: { usage: usageData },
|
|
261
|
+
}),
|
|
262
|
+
text: "",
|
|
263
|
+
});
|
|
252
264
|
}
|
|
253
265
|
/**
|
|
254
266
|
* Formats messages as a prompt for the model.
|
package/dist/chat_models.js
CHANGED
|
@@ -200,6 +200,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
200
200
|
...this.formatMessagesForAnthropic(messages),
|
|
201
201
|
stream: true,
|
|
202
202
|
});
|
|
203
|
+
let usageData = { input_tokens: 0, output_tokens: 0 };
|
|
203
204
|
for await (const data of stream) {
|
|
204
205
|
if (options.signal?.aborted) {
|
|
205
206
|
stream.controller.abort();
|
|
@@ -207,7 +208,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
207
208
|
}
|
|
208
209
|
if (data.type === "message_start") {
|
|
209
210
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
210
|
-
const { content, ...additionalKwargs } = data.message;
|
|
211
|
+
const { content, usage, ...additionalKwargs } = data.message;
|
|
211
212
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
212
213
|
const filteredAdditionalKwargs = {};
|
|
213
214
|
for (const [key, value] of Object.entries(additionalKwargs)) {
|
|
@@ -215,6 +216,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
215
216
|
filteredAdditionalKwargs[key] = value;
|
|
216
217
|
}
|
|
217
218
|
}
|
|
219
|
+
usageData = usage;
|
|
218
220
|
yield new ChatGenerationChunk({
|
|
219
221
|
message: new AIMessageChunk({
|
|
220
222
|
content: "",
|
|
@@ -231,6 +233,9 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
231
233
|
}),
|
|
232
234
|
text: "",
|
|
233
235
|
});
|
|
236
|
+
if (data?.usage !== undefined) {
|
|
237
|
+
usageData.output_tokens += data.usage.output_tokens;
|
|
238
|
+
}
|
|
234
239
|
}
|
|
235
240
|
else if (data.type === "content_block_delta") {
|
|
236
241
|
const content = data.delta?.text;
|
|
@@ -246,6 +251,13 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
246
251
|
}
|
|
247
252
|
}
|
|
248
253
|
}
|
|
254
|
+
yield new ChatGenerationChunk({
|
|
255
|
+
message: new AIMessageChunk({
|
|
256
|
+
content: "",
|
|
257
|
+
additional_kwargs: { usage: usageData },
|
|
258
|
+
}),
|
|
259
|
+
text: "",
|
|
260
|
+
});
|
|
249
261
|
}
|
|
250
262
|
/**
|
|
251
263
|
* Formats messages as a prompt for the model.
|
|
@@ -217,12 +217,12 @@ class ChatAnthropicTools extends chat_models_1.BaseChatModel {
|
|
|
217
217
|
name = config?.name;
|
|
218
218
|
method = config?.method;
|
|
219
219
|
includeRaw = config?.includeRaw;
|
|
220
|
-
force = config?.force ??
|
|
220
|
+
force = config?.force ?? false;
|
|
221
221
|
}
|
|
222
222
|
if (method === "jsonMode") {
|
|
223
223
|
throw new Error(`Anthropic only supports "functionCalling" as a method.`);
|
|
224
224
|
}
|
|
225
|
-
|
|
225
|
+
let functionName = name ?? "extract";
|
|
226
226
|
let outputParser;
|
|
227
227
|
let tools;
|
|
228
228
|
if (isZodSchema(schema)) {
|
|
@@ -244,14 +244,24 @@ class ChatAnthropicTools extends chat_models_1.BaseChatModel {
|
|
|
244
244
|
});
|
|
245
245
|
}
|
|
246
246
|
else {
|
|
247
|
+
let openAIFunctionDefinition;
|
|
248
|
+
if (typeof schema.name === "string" &&
|
|
249
|
+
typeof schema.parameters === "object" &&
|
|
250
|
+
schema.parameters != null) {
|
|
251
|
+
openAIFunctionDefinition = schema;
|
|
252
|
+
functionName = schema.name;
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
openAIFunctionDefinition = {
|
|
256
|
+
name: functionName,
|
|
257
|
+
description: schema.description ?? "",
|
|
258
|
+
parameters: schema,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
247
261
|
tools = [
|
|
248
262
|
{
|
|
249
263
|
type: "function",
|
|
250
|
-
function:
|
|
251
|
-
name: functionName,
|
|
252
|
-
description: schema.description,
|
|
253
|
-
parameters: schema,
|
|
254
|
-
},
|
|
264
|
+
function: openAIFunctionDefinition,
|
|
255
265
|
},
|
|
256
266
|
];
|
|
257
267
|
outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
|
|
@@ -3,7 +3,7 @@ import type { ChatGenerationChunk, ChatResult, LLMResult } from "@langchain/core
|
|
|
3
3
|
import { BaseChatModel, BaseChatModelParams } from "@langchain/core/language_models/chat_models";
|
|
4
4
|
import { CallbackManagerForLLMRun, Callbacks } from "@langchain/core/callbacks/manager";
|
|
5
5
|
import { BasePromptTemplate } from "@langchain/core/prompts";
|
|
6
|
-
import { BaseLanguageModelCallOptions, BaseLanguageModelInput, StructuredOutputMethodParams, StructuredOutputMethodOptions, ToolDefinition } from "@langchain/core/language_models/base";
|
|
6
|
+
import type { BaseLanguageModelCallOptions, BaseLanguageModelInput, StructuredOutputMethodParams, StructuredOutputMethodOptions, ToolDefinition } from "@langchain/core/language_models/base";
|
|
7
7
|
import { Runnable } from "@langchain/core/runnables";
|
|
8
8
|
import { z } from "zod";
|
|
9
9
|
import { type AnthropicInput } from "../chat_models.js";
|
|
@@ -214,12 +214,12 @@ export class ChatAnthropicTools extends BaseChatModel {
|
|
|
214
214
|
name = config?.name;
|
|
215
215
|
method = config?.method;
|
|
216
216
|
includeRaw = config?.includeRaw;
|
|
217
|
-
force = config?.force ??
|
|
217
|
+
force = config?.force ?? false;
|
|
218
218
|
}
|
|
219
219
|
if (method === "jsonMode") {
|
|
220
220
|
throw new Error(`Anthropic only supports "functionCalling" as a method.`);
|
|
221
221
|
}
|
|
222
|
-
|
|
222
|
+
let functionName = name ?? "extract";
|
|
223
223
|
let outputParser;
|
|
224
224
|
let tools;
|
|
225
225
|
if (isZodSchema(schema)) {
|
|
@@ -241,14 +241,24 @@ export class ChatAnthropicTools extends BaseChatModel {
|
|
|
241
241
|
});
|
|
242
242
|
}
|
|
243
243
|
else {
|
|
244
|
+
let openAIFunctionDefinition;
|
|
245
|
+
if (typeof schema.name === "string" &&
|
|
246
|
+
typeof schema.parameters === "object" &&
|
|
247
|
+
schema.parameters != null) {
|
|
248
|
+
openAIFunctionDefinition = schema;
|
|
249
|
+
functionName = schema.name;
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
openAIFunctionDefinition = {
|
|
253
|
+
name: functionName,
|
|
254
|
+
description: schema.description ?? "",
|
|
255
|
+
parameters: schema,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
244
258
|
tools = [
|
|
245
259
|
{
|
|
246
260
|
type: "function",
|
|
247
|
-
function:
|
|
248
|
-
name: functionName,
|
|
249
|
-
description: schema.description,
|
|
250
|
-
parameters: schema,
|
|
251
|
-
},
|
|
261
|
+
function: openAIFunctionDefinition,
|
|
252
262
|
},
|
|
253
263
|
];
|
|
254
264
|
outputParser = new JsonOutputKeyToolsParser({
|
|
@@ -18,7 +18,9 @@ You may call them like this:
|
|
|
18
18
|
</function_calls>
|
|
19
19
|
|
|
20
20
|
Here are the tools available:
|
|
21
|
-
{tools}
|
|
21
|
+
{tools}
|
|
22
|
+
|
|
23
|
+
If the schema above contains a property typed as an enum, you must only return values matching an allowed value for that enum.`);
|
|
22
24
|
function formatAsXMLRepresentation(tool) {
|
|
23
25
|
const builder = new fast_xml_parser_1.XMLBuilder();
|
|
24
26
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -88,7 +90,12 @@ xmlParameters
|
|
|
88
90
|
}
|
|
89
91
|
else if (typeof xmlParameters[key] === "object" &&
|
|
90
92
|
xmlParameters[key] !== null) {
|
|
91
|
-
fixedParameters[key] = fixArrayXMLParameters(
|
|
93
|
+
fixedParameters[key] = fixArrayXMLParameters({
|
|
94
|
+
...schema.properties[key],
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
96
|
+
definitions: schema.definitions,
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
98
|
+
}, xmlParameters[key]);
|
|
92
99
|
}
|
|
93
100
|
else {
|
|
94
101
|
fixedParameters[key] = xmlParameters[key];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { JsonSchema7ObjectType } from "zod-to-json-schema";
|
|
2
2
|
import { PromptTemplate } from "@langchain/core/prompts";
|
|
3
3
|
import { ToolDefinition } from "@langchain/core/language_models/base";
|
|
4
|
-
export declare const DEFAULT_TOOL_SYSTEM_PROMPT: PromptTemplate<import("@langchain/core/prompts").ParamsFromFString<"In this environment you have access to a set of tools you can use to answer the user's question.\n\nYou may call them like this:\n<function_calls>\n<invoke>\n<tool_name>$TOOL_NAME</tool_name>\n<parameters>\n<$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>\n...\n</parameters>\n</invoke>\n</function_calls>\n\nHere are the tools available:\n{tools}">, any>;
|
|
4
|
+
export declare const DEFAULT_TOOL_SYSTEM_PROMPT: PromptTemplate<import("@langchain/core/prompts").ParamsFromFString<"In this environment you have access to a set of tools you can use to answer the user's question.\n\nYou may call them like this:\n<function_calls>\n<invoke>\n<tool_name>$TOOL_NAME</tool_name>\n<parameters>\n<$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>\n...\n</parameters>\n</invoke>\n</function_calls>\n\nHere are the tools available:\n{tools}\n\nIf the schema above contains a property typed as an enum, you must only return values matching an allowed value for that enum.">, any>;
|
|
5
5
|
export type ToolInvocation = {
|
|
6
6
|
tool_name: string;
|
|
7
7
|
parameters: Record<string, unknown>;
|
|
@@ -15,7 +15,9 @@ You may call them like this:
|
|
|
15
15
|
</function_calls>
|
|
16
16
|
|
|
17
17
|
Here are the tools available:
|
|
18
|
-
{tools}
|
|
18
|
+
{tools}
|
|
19
|
+
|
|
20
|
+
If the schema above contains a property typed as an enum, you must only return values matching an allowed value for that enum.`);
|
|
19
21
|
export function formatAsXMLRepresentation(tool) {
|
|
20
22
|
const builder = new XMLBuilder();
|
|
21
23
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -84,7 +86,12 @@ xmlParameters
|
|
|
84
86
|
}
|
|
85
87
|
else if (typeof xmlParameters[key] === "object" &&
|
|
86
88
|
xmlParameters[key] !== null) {
|
|
87
|
-
fixedParameters[key] = fixArrayXMLParameters(
|
|
89
|
+
fixedParameters[key] = fixArrayXMLParameters({
|
|
90
|
+
...schema.properties[key],
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
92
|
+
definitions: schema.definitions,
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
+
}, xmlParameters[key]);
|
|
88
95
|
}
|
|
89
96
|
else {
|
|
90
97
|
fixedParameters[key] = xmlParameters[key];
|