@langchain/anthropic 0.1.9 → 0.1.10
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 +283 -93
- package/dist/chat_models.d.ts +38 -9
- package/dist/chat_models.js +284 -94
- package/dist/experimental/tool_calling.cjs +1 -0
- package/dist/experimental/tool_calling.d.ts +1 -0
- package/dist/experimental/tool_calling.js +1 -0
- package/dist/output_parsers.cjs +60 -0
- package/dist/output_parsers.d.ts +17 -0
- package/dist/output_parsers.js +56 -0
- package/dist/tests/chat_models.int.test.js +178 -0
- package/dist/types.cjs +2 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.js +1 -0
- package/package.json +2 -2
|
@@ -4,6 +4,9 @@ import { HumanMessage } from "@langchain/core/messages";
|
|
|
4
4
|
import { ChatPromptValue } from "@langchain/core/prompt_values";
|
|
5
5
|
import { PromptTemplate, ChatPromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, } from "@langchain/core/prompts";
|
|
6
6
|
import { CallbackManager } from "@langchain/core/callbacks/manager";
|
|
7
|
+
import { StructuredTool } from "@langchain/core/tools";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
7
10
|
import { ChatAnthropic } from "../chat_models.js";
|
|
8
11
|
test.skip("Test ChatAnthropic", async () => {
|
|
9
12
|
const chat = new ChatAnthropic({
|
|
@@ -272,3 +275,178 @@ test("Test ChatAnthropic multimodal", async () => {
|
|
|
272
275
|
]);
|
|
273
276
|
console.log(res);
|
|
274
277
|
});
|
|
278
|
+
describe("Tool calling", () => {
|
|
279
|
+
const zodSchema = z
|
|
280
|
+
.object({
|
|
281
|
+
location: z.string().describe("The name of city to get the weather for."),
|
|
282
|
+
})
|
|
283
|
+
.describe("Get the weather of a specific location and return the temperature in Celsius.");
|
|
284
|
+
class WeatherTool extends StructuredTool {
|
|
285
|
+
constructor() {
|
|
286
|
+
super(...arguments);
|
|
287
|
+
Object.defineProperty(this, "schema", {
|
|
288
|
+
enumerable: true,
|
|
289
|
+
configurable: true,
|
|
290
|
+
writable: true,
|
|
291
|
+
value: z.object({
|
|
292
|
+
location: z.string().describe("The name of city to get the weather for."),
|
|
293
|
+
})
|
|
294
|
+
});
|
|
295
|
+
Object.defineProperty(this, "description", {
|
|
296
|
+
enumerable: true,
|
|
297
|
+
configurable: true,
|
|
298
|
+
writable: true,
|
|
299
|
+
value: "Get the weather of a specific location and return the temperature in Celsius."
|
|
300
|
+
});
|
|
301
|
+
Object.defineProperty(this, "name", {
|
|
302
|
+
enumerable: true,
|
|
303
|
+
configurable: true,
|
|
304
|
+
writable: true,
|
|
305
|
+
value: "get_weather"
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
async _call(input) {
|
|
309
|
+
console.log(`WeatherTool called with input: ${input}`);
|
|
310
|
+
return `The weather in ${input.location} is 25°C`;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
const model = new ChatAnthropic({
|
|
314
|
+
modelName: "claude-3-sonnet-20240229",
|
|
315
|
+
temperature: 0,
|
|
316
|
+
});
|
|
317
|
+
const anthropicTool = {
|
|
318
|
+
name: "get_weather",
|
|
319
|
+
description: "Get the weather of a specific location and return the temperature in Celsius.",
|
|
320
|
+
input_schema: {
|
|
321
|
+
type: "object",
|
|
322
|
+
properties: {
|
|
323
|
+
location: {
|
|
324
|
+
type: "string",
|
|
325
|
+
description: "The name of city to get the weather for.",
|
|
326
|
+
},
|
|
327
|
+
},
|
|
328
|
+
required: ["location"],
|
|
329
|
+
},
|
|
330
|
+
};
|
|
331
|
+
test("Can bind & invoke StructuredTools", async () => {
|
|
332
|
+
const tools = [new WeatherTool()];
|
|
333
|
+
const modelWithTools = model.bind({
|
|
334
|
+
tools,
|
|
335
|
+
});
|
|
336
|
+
const result = await modelWithTools.invoke("What is the weather in London today?");
|
|
337
|
+
console.log({
|
|
338
|
+
tool_calls: JSON.stringify(result.content, null, 2),
|
|
339
|
+
}, "Can bind & invoke StructuredTools");
|
|
340
|
+
expect(Array.isArray(result.content)).toBeTruthy();
|
|
341
|
+
if (!Array.isArray(result.content)) {
|
|
342
|
+
throw new Error("Content is not an array");
|
|
343
|
+
}
|
|
344
|
+
let toolCall;
|
|
345
|
+
result.content.forEach((item) => {
|
|
346
|
+
if (item.type === "tool_use") {
|
|
347
|
+
toolCall = item;
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
if (!toolCall) {
|
|
351
|
+
throw new Error("No tool call found");
|
|
352
|
+
}
|
|
353
|
+
expect(toolCall).toBeTruthy();
|
|
354
|
+
const { name, input } = toolCall;
|
|
355
|
+
expect(name).toBe("get_weather");
|
|
356
|
+
expect(input).toBeTruthy();
|
|
357
|
+
expect(input.location).toBeTruthy();
|
|
358
|
+
});
|
|
359
|
+
test("Can bind & invoke AnthropicTools", async () => {
|
|
360
|
+
const modelWithTools = model.bind({
|
|
361
|
+
tools: [anthropicTool],
|
|
362
|
+
});
|
|
363
|
+
const result = await modelWithTools.invoke("What is the weather in London today?");
|
|
364
|
+
console.log({
|
|
365
|
+
tool_calls: JSON.stringify(result.content, null, 2),
|
|
366
|
+
}, "Can bind & invoke StructuredTools");
|
|
367
|
+
expect(Array.isArray(result.content)).toBeTruthy();
|
|
368
|
+
if (!Array.isArray(result.content)) {
|
|
369
|
+
throw new Error("Content is not an array");
|
|
370
|
+
}
|
|
371
|
+
let toolCall;
|
|
372
|
+
result.content.forEach((item) => {
|
|
373
|
+
if (item.type === "tool_use") {
|
|
374
|
+
toolCall = item;
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
if (!toolCall) {
|
|
378
|
+
throw new Error("No tool call found");
|
|
379
|
+
}
|
|
380
|
+
expect(toolCall).toBeTruthy();
|
|
381
|
+
const { name, input } = toolCall;
|
|
382
|
+
expect(name).toBe("get_weather");
|
|
383
|
+
expect(input).toBeTruthy();
|
|
384
|
+
expect(input.location).toBeTruthy();
|
|
385
|
+
});
|
|
386
|
+
test("Can bind & stream AnthropicTools", async () => {
|
|
387
|
+
const modelWithTools = model.bind({
|
|
388
|
+
tools: [anthropicTool],
|
|
389
|
+
});
|
|
390
|
+
const result = await modelWithTools.stream("What is the weather in London today?");
|
|
391
|
+
let finalMessage;
|
|
392
|
+
for await (const item of result) {
|
|
393
|
+
console.log("item", JSON.stringify(item, null, 2));
|
|
394
|
+
finalMessage = item;
|
|
395
|
+
}
|
|
396
|
+
if (!finalMessage) {
|
|
397
|
+
throw new Error("No final message returned");
|
|
398
|
+
}
|
|
399
|
+
console.log({
|
|
400
|
+
tool_calls: JSON.stringify(finalMessage.content, null, 2),
|
|
401
|
+
}, "Can bind & invoke StructuredTools");
|
|
402
|
+
expect(Array.isArray(finalMessage.content)).toBeTruthy();
|
|
403
|
+
if (!Array.isArray(finalMessage.content)) {
|
|
404
|
+
throw new Error("Content is not an array");
|
|
405
|
+
}
|
|
406
|
+
let toolCall;
|
|
407
|
+
finalMessage.content.forEach((item) => {
|
|
408
|
+
if (item.type === "tool_use") {
|
|
409
|
+
toolCall = item;
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
if (!toolCall) {
|
|
413
|
+
throw new Error("No tool call found");
|
|
414
|
+
}
|
|
415
|
+
expect(toolCall).toBeTruthy();
|
|
416
|
+
const { name, input } = toolCall;
|
|
417
|
+
expect(name).toBe("get_weather");
|
|
418
|
+
expect(input).toBeTruthy();
|
|
419
|
+
expect(input.location).toBeTruthy();
|
|
420
|
+
});
|
|
421
|
+
test("withStructuredOutput with zod schema", async () => {
|
|
422
|
+
const modelWithTools = model.withStructuredOutput(zodSchema, {
|
|
423
|
+
name: "get_weather",
|
|
424
|
+
});
|
|
425
|
+
const result = await modelWithTools.invoke("What is the weather in London today?");
|
|
426
|
+
console.log({
|
|
427
|
+
result,
|
|
428
|
+
}, "withStructuredOutput with zod schema");
|
|
429
|
+
expect(typeof result.location).toBe("string");
|
|
430
|
+
});
|
|
431
|
+
test("withStructuredOutput with AnthropicTool", async () => {
|
|
432
|
+
const modelWithTools = model.withStructuredOutput(anthropicTool, {
|
|
433
|
+
name: anthropicTool.name,
|
|
434
|
+
});
|
|
435
|
+
const result = await modelWithTools.invoke("What is the weather in London today?");
|
|
436
|
+
console.log({
|
|
437
|
+
result,
|
|
438
|
+
}, "withStructuredOutput with AnthropicTool");
|
|
439
|
+
expect(typeof result.location).toBe("string");
|
|
440
|
+
});
|
|
441
|
+
test("withStructuredOutput JSON Schema only", async () => {
|
|
442
|
+
const jsonSchema = zodToJsonSchema(zodSchema);
|
|
443
|
+
const modelWithTools = model.withStructuredOutput(jsonSchema, {
|
|
444
|
+
name: "get_weather",
|
|
445
|
+
});
|
|
446
|
+
const result = await modelWithTools.invoke("What is the weather in London today?");
|
|
447
|
+
console.log({
|
|
448
|
+
result,
|
|
449
|
+
}, "withStructuredOutput JSON Schema only");
|
|
450
|
+
expect(typeof result.location).toBe("string");
|
|
451
|
+
});
|
|
452
|
+
});
|
package/dist/types.cjs
ADDED
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/anthropic",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"description": "Anthropic integrations for LangChain.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"license": "MIT",
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@anthropic-ai/sdk": "^0.17.2",
|
|
43
|
-
"@langchain/core": "~0.1.
|
|
43
|
+
"@langchain/core": "~0.1.54",
|
|
44
44
|
"fast-xml-parser": "^4.3.5",
|
|
45
45
|
"zod": "^3.22.4",
|
|
46
46
|
"zod-to-json-schema": "^3.22.4"
|