@langchain/anthropic 0.1.17 → 0.1.19

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.
@@ -427,6 +427,22 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
427
427
  * Get the parameters used to invoke the model
428
428
  */
429
429
  invocationParams(options) {
430
+ let tool_choice;
431
+ if (options?.tool_choice) {
432
+ if (options?.tool_choice === "any") {
433
+ tool_choice = {
434
+ type: "any",
435
+ };
436
+ }
437
+ else if (options?.tool_choice === "auto") {
438
+ tool_choice = {
439
+ type: "auto",
440
+ };
441
+ }
442
+ else {
443
+ tool_choice = options?.tool_choice;
444
+ }
445
+ }
430
446
  return {
431
447
  model: this.model,
432
448
  temperature: this.temperature,
@@ -436,6 +452,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
436
452
  stream: this.streaming,
437
453
  max_tokens: this.maxTokens,
438
454
  tools: this.formatStructuredToolToAnthropic(options?.tools),
455
+ tool_choice,
439
456
  ...this.invocationKwargs,
440
457
  };
441
458
  }
@@ -697,6 +714,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
697
714
  }
698
715
  const llm = this.bind({
699
716
  tools,
717
+ tool_choice: "any",
700
718
  });
701
719
  if (!includeRaw) {
702
720
  return llm.pipe(outputParser).withConfig({
@@ -22,8 +22,17 @@ type AnthropicMessageCreateParams = Anthropic.MessageCreateParamsNonStreaming;
22
22
  type AnthropicStreamingMessageCreateParams = Anthropic.MessageCreateParamsStreaming;
23
23
  type AnthropicMessageStreamEvent = Anthropic.MessageStreamEvent;
24
24
  type AnthropicRequestOptions = Anthropic.RequestOptions;
25
+ type AnthropicToolChoice = {
26
+ type: "tool";
27
+ name: string;
28
+ } | "any" | "auto";
25
29
  interface ChatAnthropicCallOptions extends BaseLanguageModelCallOptions {
26
30
  tools?: (StructuredToolInterface | AnthropicTool)[];
31
+ /**
32
+ * Whether or not to specify what tool the model should use
33
+ * @default "auto"
34
+ */
35
+ tool_choice?: AnthropicToolChoice;
27
36
  }
28
37
  /**
29
38
  * Input to AnthropicChat class.
@@ -183,7 +192,10 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
183
192
  model: string;
184
193
  stop_reason: "max_tokens" | "stop_sequence" | "end_turn" | null;
185
194
  stop_sequence: string | null;
186
- usage: Anthropic.Messages.Usage;
195
+ usage: Anthropic.Messages.Usage; /** A list of strings upon which to stop generating.
196
+ * You probably want `["\n\nHuman:"]`, as that's the cue for
197
+ * the next turn in the dialog agent.
198
+ */
187
199
  };
188
200
  }>;
189
201
  /** @ignore */
@@ -423,6 +423,22 @@ export class ChatAnthropicMessages extends BaseChatModel {
423
423
  * Get the parameters used to invoke the model
424
424
  */
425
425
  invocationParams(options) {
426
+ let tool_choice;
427
+ if (options?.tool_choice) {
428
+ if (options?.tool_choice === "any") {
429
+ tool_choice = {
430
+ type: "any",
431
+ };
432
+ }
433
+ else if (options?.tool_choice === "auto") {
434
+ tool_choice = {
435
+ type: "auto",
436
+ };
437
+ }
438
+ else {
439
+ tool_choice = options?.tool_choice;
440
+ }
441
+ }
426
442
  return {
427
443
  model: this.model,
428
444
  temperature: this.temperature,
@@ -432,6 +448,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
432
448
  stream: this.streaming,
433
449
  max_tokens: this.maxTokens,
434
450
  tools: this.formatStructuredToolToAnthropic(options?.tools),
451
+ tool_choice,
435
452
  ...this.invocationKwargs,
436
453
  };
437
454
  }
@@ -693,6 +710,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
693
710
  }
694
711
  const llm = this.bind({
695
712
  tools,
713
+ tool_choice: "any",
696
714
  });
697
715
  if (!includeRaw) {
698
716
  return llm.pipe(outputParser).withConfig({
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ // Auto-generated by `scripts/create-entrypoints.js`. Do not edit manually.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.optionalImportEntrypoints = void 0;
5
+ exports.optionalImportEntrypoints = [];
@@ -0,0 +1 @@
1
+ export declare const optionalImportEntrypoints: string[];
@@ -0,0 +1,2 @@
1
+ // Auto-generated by `scripts/create-entrypoints.js`. Do not edit manually.
2
+ export const optionalImportEntrypoints = [];
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // Auto-generated by `scripts/create-entrypoints.js`. Do not edit manually.
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.experimental = exports.index = void 0;
28
+ exports.index = __importStar(require("../index.cjs"));
29
+ exports.experimental = __importStar(require("../experimental/index.cjs"));
@@ -0,0 +1,2 @@
1
+ export * as index from "../index.js";
2
+ export * as experimental from "../experimental/index.js";
@@ -0,0 +1,3 @@
1
+ // Auto-generated by `scripts/create-entrypoints.js`. Do not edit manually.
2
+ export * as index from "../index.js";
3
+ export * as experimental from "../experimental/index.js";
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // Auto-generated by `scripts/create-entrypoints.js`. Do not edit manually.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ export interface OptionalImportMap {
2
+ }
3
+ export interface SecretMap {
4
+ ANTHROPIC_API_KEY?: string;
5
+ }
@@ -0,0 +1,2 @@
1
+ // Auto-generated by `scripts/create-entrypoints.js`. Do not edit manually.
2
+ export {};
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.load = exports.importMap = exports.optionalImportEntrypoints = void 0;
27
+ const load_1 = require("@langchain/core/load");
28
+ const importMap = __importStar(require("./import_map.cjs"));
29
+ exports.importMap = importMap;
30
+ const import_constants_js_1 = require("./import_constants.cjs");
31
+ Object.defineProperty(exports, "optionalImportEntrypoints", { enumerable: true, get: function () { return import_constants_js_1.optionalImportEntrypoints; } });
32
+ /**
33
+ * Load a LangChain module from a serialized text representation.
34
+ * NOTE: This functionality is currently in beta.
35
+ * Loaded classes may change independently of semver.
36
+ * @param text Serialized text representation of the module.
37
+ * @param secretsMap
38
+ * @param optionalImportsMap
39
+ * @returns A loaded instance of a LangChain module.
40
+ */
41
+ async function load(text,
42
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
+ secretsMap = {},
44
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
+ optionalImportsMap = {}) {
46
+ return (0, load_1.load)(text, {
47
+ secretsMap,
48
+ optionalImportsMap,
49
+ optionalImportEntrypoints: import_constants_js_1.optionalImportEntrypoints,
50
+ importMap,
51
+ });
52
+ }
53
+ exports.load = load;
@@ -0,0 +1,14 @@
1
+ import { type OptionalImportMap, type SecretMap } from "./import_type.js";
2
+ import * as importMap from "./import_map.js";
3
+ import { optionalImportEntrypoints } from "./import_constants.js";
4
+ export { optionalImportEntrypoints, importMap, type OptionalImportMap, type SecretMap, };
5
+ /**
6
+ * Load a LangChain module from a serialized text representation.
7
+ * NOTE: This functionality is currently in beta.
8
+ * Loaded classes may change independently of semver.
9
+ * @param text Serialized text representation of the module.
10
+ * @param secretsMap
11
+ * @param optionalImportsMap
12
+ * @returns A loaded instance of a LangChain module.
13
+ */
14
+ export declare function load<T>(text: string, secretsMap?: Record<string, any>, optionalImportsMap?: OptionalImportMap & Record<string, any>): Promise<T>;
@@ -0,0 +1,25 @@
1
+ import { load as coreLoad } from "@langchain/core/load";
2
+ import * as importMap from "./import_map.js";
3
+ import { optionalImportEntrypoints } from "./import_constants.js";
4
+ export { optionalImportEntrypoints, importMap, };
5
+ /**
6
+ * Load a LangChain module from a serialized text representation.
7
+ * NOTE: This functionality is currently in beta.
8
+ * Loaded classes may change independently of semver.
9
+ * @param text Serialized text representation of the module.
10
+ * @param secretsMap
11
+ * @param optionalImportsMap
12
+ * @returns A loaded instance of a LangChain module.
13
+ */
14
+ export async function load(text,
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ secretsMap = {},
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+ optionalImportsMap = {}) {
19
+ return coreLoad(text, {
20
+ secretsMap,
21
+ optionalImportsMap,
22
+ optionalImportEntrypoints,
23
+ importMap,
24
+ });
25
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export interface SerializedFields {
2
+ [key: string]: any;
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("@langchain/core/load/serializable"), exports);
@@ -0,0 +1 @@
1
+ export * from "@langchain/core/load/serializable";
@@ -0,0 +1 @@
1
+ export * from "@langchain/core/load/serializable";
@@ -216,3 +216,63 @@ test("withStructuredOutput JSON Schema only", async () => {
216
216
  }, "withStructuredOutput JSON Schema only");
217
217
  expect(typeof result.location).toBe("string");
218
218
  });
219
+ test("Can pass tool_choice", async () => {
220
+ const tool1 = {
221
+ name: "get_weather",
222
+ description: "Get the weather of a specific location and return the temperature in Celsius.",
223
+ input_schema: {
224
+ type: "object",
225
+ properties: {
226
+ location: {
227
+ type: "string",
228
+ description: "The name of city to get the weather for.",
229
+ },
230
+ },
231
+ required: ["location"],
232
+ },
233
+ };
234
+ const tool2 = {
235
+ name: "calculator",
236
+ description: "Calculate any math expression and return the result.",
237
+ input_schema: {
238
+ type: "object",
239
+ properties: {
240
+ expression: {
241
+ type: "string",
242
+ description: "The math expression to calculate.",
243
+ },
244
+ },
245
+ required: ["expression"],
246
+ },
247
+ };
248
+ const tools = [tool1, tool2];
249
+ const modelWithTools = model.bindTools(tools, {
250
+ tool_choice: {
251
+ type: "tool",
252
+ name: "get_weather",
253
+ },
254
+ });
255
+ const result = await modelWithTools.invoke("What is the sum of 272818 and 281818?");
256
+ console.log({
257
+ tool_calls: JSON.stringify(result.content, null, 2),
258
+ }, "Can bind & invoke StructuredTools");
259
+ expect(Array.isArray(result.content)).toBeTruthy();
260
+ if (!Array.isArray(result.content)) {
261
+ throw new Error("Content is not an array");
262
+ }
263
+ let toolCall;
264
+ result.content.forEach((item) => {
265
+ if (item.type === "tool_use") {
266
+ toolCall = item;
267
+ }
268
+ });
269
+ if (!toolCall) {
270
+ throw new Error("No tool call found");
271
+ }
272
+ expect(toolCall).toBeTruthy();
273
+ const { name, input } = toolCall;
274
+ expect(toolCall.input).toEqual(result.tool_calls?.[0].args);
275
+ expect(name).toBe("get_weather");
276
+ expect(input).toBeTruthy();
277
+ expect(input.location).toBeTruthy();
278
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/anthropic",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "description": "Anthropic integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -24,7 +24,7 @@
24
24
  "lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
25
25
  "lint": "yarn lint:eslint && yarn lint:dpdm",
26
26
  "lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
27
- "clean": "rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096 yarn lc-build --config ./langchain.config.js --create-entrypoints --pre",
27
+ "clean": "rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096 yarn lc-build --config ./langchain.config.js --create-entrypoints --pre --gen-maps",
28
28
  "prepack": "yarn build",
29
29
  "test": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
30
30
  "test:watch": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
@@ -39,15 +39,15 @@
39
39
  "author": "LangChain",
40
40
  "license": "MIT",
41
41
  "dependencies": {
42
- "@anthropic-ai/sdk": "^0.20.1",
43
- "@langchain/core": "~0.1.56",
42
+ "@anthropic-ai/sdk": "^0.21.0",
43
+ "@langchain/core": "<0.3.0 || >0.1.0",
44
44
  "fast-xml-parser": "^4.3.5",
45
45
  "zod": "^3.22.4",
46
46
  "zod-to-json-schema": "^3.22.4"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@jest/globals": "^29.5.0",
50
- "@langchain/community": "workspace:^",
50
+ "@langchain/community": "workspace:*",
51
51
  "@langchain/scripts": "~0.0",
52
52
  "@swc/core": "^1.3.90",
53
53
  "@swc/jest": "^0.2.29",