@langchain/anthropic 0.1.7 → 0.1.8

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.
@@ -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 ?? true;
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
- const functionName = name ?? "extract";
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 ?? true;
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
- const functionName = name ?? "extract";
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(schema, xmlParameters[key]);
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(schema, xmlParameters[key]);
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];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/anthropic",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Anthropic integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {