@langchain/anthropic 0.3.21 → 0.3.22
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 +5 -5
- package/dist/chat_models.d.ts +3 -3
- package/dist/chat_models.js +6 -6
- package/dist/experimental/tool_calling.cjs +4 -9
- package/dist/experimental/tool_calling.d.ts +3 -3
- package/dist/experimental/tool_calling.js +4 -9
- package/dist/experimental/utils/tool_calling.d.ts +1 -1
- package/dist/output_parsers.cjs +3 -2
- package/dist/output_parsers.d.ts +4 -4
- package/dist/output_parsers.js +3 -2
- package/package.json +5 -6
package/dist/chat_models.cjs
CHANGED
|
@@ -7,7 +7,7 @@ const outputs_1 = require("@langchain/core/outputs");
|
|
|
7
7
|
const env_1 = require("@langchain/core/utils/env");
|
|
8
8
|
const chat_models_1 = require("@langchain/core/language_models/chat_models");
|
|
9
9
|
const base_1 = require("@langchain/core/language_models/base");
|
|
10
|
-
const
|
|
10
|
+
const json_schema_1 = require("@langchain/core/utils/json_schema");
|
|
11
11
|
const runnables_1 = require("@langchain/core/runnables");
|
|
12
12
|
const types_1 = require("@langchain/core/utils/types");
|
|
13
13
|
const function_calling_1 = require("@langchain/core/utils/function_calling");
|
|
@@ -641,8 +641,8 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
641
641
|
return {
|
|
642
642
|
name: tool.name,
|
|
643
643
|
description: tool.description,
|
|
644
|
-
input_schema: ((0, types_1.
|
|
645
|
-
? (0,
|
|
644
|
+
input_schema: ((0, types_1.isInteropZodSchema)(tool.schema)
|
|
645
|
+
? (0, json_schema_1.toJsonSchema)(tool.schema)
|
|
646
646
|
: tool.schema),
|
|
647
647
|
};
|
|
648
648
|
}
|
|
@@ -879,8 +879,8 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
879
879
|
let functionName = name ?? "extract";
|
|
880
880
|
let outputParser;
|
|
881
881
|
let tools;
|
|
882
|
-
if ((0, types_1.
|
|
883
|
-
const jsonSchema = (0,
|
|
882
|
+
if ((0, types_1.isInteropZodSchema)(schema)) {
|
|
883
|
+
const jsonSchema = (0, json_schema_1.toJsonSchema)(schema);
|
|
884
884
|
tools = [
|
|
885
885
|
{
|
|
886
886
|
name: functionName,
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { ChatGenerationChunk, type ChatResult } from "@langchain/core/outputs";
|
|
|
6
6
|
import { BaseChatModel, BaseChatModelCallOptions, LangSmithParams, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
|
|
7
7
|
import { type StructuredOutputMethodOptions, type BaseLanguageModelInput } from "@langchain/core/language_models/base";
|
|
8
8
|
import { Runnable } from "@langchain/core/runnables";
|
|
9
|
-
import {
|
|
9
|
+
import { InteropZodType } from "@langchain/core/utils/types";
|
|
10
10
|
import { AnthropicMessageCreateParams, AnthropicMessageStreamEvent, AnthropicRequestOptions, AnthropicStreamingMessageCreateParams, AnthropicThinkingConfigParam, AnthropicToolChoice, ChatAnthropicToolType } from "./types.js";
|
|
11
11
|
export interface ChatAnthropicCallOptions extends BaseChatModelCallOptions, Pick<AnthropicInput, "streamUsage"> {
|
|
12
12
|
tools?: ChatAnthropicToolType[];
|
|
@@ -575,8 +575,8 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
|
|
|
575
575
|
/** @ignore */
|
|
576
576
|
protected completionWithRetry(request: AnthropicMessageCreateParams & Kwargs, options: AnthropicRequestOptions): Promise<Anthropic.Message>;
|
|
577
577
|
_llmType(): string;
|
|
578
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema:
|
|
579
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema:
|
|
578
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
579
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
580
580
|
raw: BaseMessage;
|
|
581
581
|
parsed: RunOutput;
|
|
582
582
|
}>;
|
package/dist/chat_models.js
CHANGED
|
@@ -4,9 +4,9 @@ import { ChatGenerationChunk } from "@langchain/core/outputs";
|
|
|
4
4
|
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
5
5
|
import { BaseChatModel, } from "@langchain/core/language_models/chat_models";
|
|
6
6
|
import { isOpenAITool, } from "@langchain/core/language_models/base";
|
|
7
|
-
import {
|
|
7
|
+
import { toJsonSchema } from "@langchain/core/utils/json_schema";
|
|
8
8
|
import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
|
|
9
|
-
import {
|
|
9
|
+
import { isInteropZodSchema, } from "@langchain/core/utils/types";
|
|
10
10
|
import { isLangChainTool } from "@langchain/core/utils/function_calling";
|
|
11
11
|
import { AnthropicToolsOutputParser } from "./output_parsers.js";
|
|
12
12
|
import { handleToolChoice } from "./utils/tools.js";
|
|
@@ -638,8 +638,8 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
638
638
|
return {
|
|
639
639
|
name: tool.name,
|
|
640
640
|
description: tool.description,
|
|
641
|
-
input_schema: (
|
|
642
|
-
?
|
|
641
|
+
input_schema: (isInteropZodSchema(tool.schema)
|
|
642
|
+
? toJsonSchema(tool.schema)
|
|
643
643
|
: tool.schema),
|
|
644
644
|
};
|
|
645
645
|
}
|
|
@@ -876,8 +876,8 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
876
876
|
let functionName = name ?? "extract";
|
|
877
877
|
let outputParser;
|
|
878
878
|
let tools;
|
|
879
|
-
if (
|
|
880
|
-
const jsonSchema =
|
|
879
|
+
if (isInteropZodSchema(schema)) {
|
|
880
|
+
const jsonSchema = toJsonSchema(schema);
|
|
881
881
|
tools = [
|
|
882
882
|
{
|
|
883
883
|
name: functionName,
|
|
@@ -6,7 +6,8 @@ const messages_1 = require("@langchain/core/messages");
|
|
|
6
6
|
const chat_models_1 = require("@langchain/core/language_models/chat_models");
|
|
7
7
|
const runnables_1 = require("@langchain/core/runnables");
|
|
8
8
|
const openai_tools_1 = require("@langchain/core/output_parsers/openai_tools");
|
|
9
|
-
const
|
|
9
|
+
const json_schema_1 = require("@langchain/core/utils/json_schema");
|
|
10
|
+
const types_1 = require("@langchain/core/utils/types");
|
|
10
11
|
const chat_models_js_1 = require("../chat_models.cjs");
|
|
11
12
|
const tool_calling_js_1 = require("./utils/tool_calling.cjs");
|
|
12
13
|
/**
|
|
@@ -226,8 +227,8 @@ class ChatAnthropicTools extends chat_models_1.BaseChatModel {
|
|
|
226
227
|
let functionName = name ?? "extract";
|
|
227
228
|
let outputParser;
|
|
228
229
|
let tools;
|
|
229
|
-
if (
|
|
230
|
-
const jsonSchema = (0,
|
|
230
|
+
if ((0, types_1.isInteropZodSchema)(schema)) {
|
|
231
|
+
const jsonSchema = (0, json_schema_1.toJsonSchema)(schema);
|
|
231
232
|
tools = [
|
|
232
233
|
{
|
|
233
234
|
type: "function",
|
|
@@ -307,12 +308,6 @@ class ChatAnthropicTools extends chat_models_1.BaseChatModel {
|
|
|
307
308
|
}
|
|
308
309
|
}
|
|
309
310
|
exports.ChatAnthropicTools = ChatAnthropicTools;
|
|
310
|
-
function isZodSchema(
|
|
311
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
312
|
-
input) {
|
|
313
|
-
// Check for a characteristic method of Zod schemas
|
|
314
|
-
return typeof input?.parse === "function";
|
|
315
|
-
}
|
|
316
311
|
function isStructuredOutputMethodParams(x
|
|
317
312
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
318
313
|
) {
|
|
@@ -5,7 +5,7 @@ import { CallbackManagerForLLMRun, Callbacks } from "@langchain/core/callbacks/m
|
|
|
5
5
|
import { BasePromptTemplate } from "@langchain/core/prompts";
|
|
6
6
|
import type { BaseLanguageModelCallOptions, BaseLanguageModelInput, StructuredOutputMethodParams, StructuredOutputMethodOptions, ToolDefinition } from "@langchain/core/language_models/base";
|
|
7
7
|
import { Runnable } from "@langchain/core/runnables";
|
|
8
|
-
import {
|
|
8
|
+
import { InteropZodType } from "@langchain/core/utils/types";
|
|
9
9
|
import { type AnthropicInput } from "../chat_models.js";
|
|
10
10
|
export interface ChatAnthropicToolsCallOptions extends BaseLanguageModelCallOptions {
|
|
11
11
|
tools?: ToolDefinition[];
|
|
@@ -45,10 +45,10 @@ export declare class ChatAnthropicTools extends BaseChatModel<ChatAnthropicTools
|
|
|
45
45
|
generate(messages: BaseMessageLike[][], parsedOptions?: ChatAnthropicToolsCallOptions, callbacks?: Callbacks): Promise<LLMResult>;
|
|
46
46
|
_generate(_messages: BaseMessage[], _options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun | undefined): Promise<ChatResult>;
|
|
47
47
|
_llmType(): string;
|
|
48
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, false> |
|
|
48
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, false> | InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false> & {
|
|
49
49
|
force?: boolean;
|
|
50
50
|
}): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
51
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, true> |
|
|
51
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, true> | InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true> & {
|
|
52
52
|
force?: boolean;
|
|
53
53
|
}): Runnable<BaseLanguageModelInput, {
|
|
54
54
|
raw: BaseMessage;
|
|
@@ -3,7 +3,8 @@ import { AIMessage, SystemMessage, coerceMessageLikeToMessage, } from "@langchai
|
|
|
3
3
|
import { BaseChatModel, } from "@langchain/core/language_models/chat_models";
|
|
4
4
|
import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
|
|
5
5
|
import { JsonOutputKeyToolsParser } from "@langchain/core/output_parsers/openai_tools";
|
|
6
|
-
import {
|
|
6
|
+
import { toJsonSchema, } from "@langchain/core/utils/json_schema";
|
|
7
|
+
import { isInteropZodSchema, } from "@langchain/core/utils/types";
|
|
7
8
|
import { ChatAnthropic } from "../chat_models.js";
|
|
8
9
|
import { DEFAULT_TOOL_SYSTEM_PROMPT, formatAsXMLRepresentation, fixArrayXMLParameters, } from "./utils/tool_calling.js";
|
|
9
10
|
/**
|
|
@@ -223,8 +224,8 @@ export class ChatAnthropicTools extends BaseChatModel {
|
|
|
223
224
|
let functionName = name ?? "extract";
|
|
224
225
|
let outputParser;
|
|
225
226
|
let tools;
|
|
226
|
-
if (
|
|
227
|
-
const jsonSchema =
|
|
227
|
+
if (isInteropZodSchema(schema)) {
|
|
228
|
+
const jsonSchema = toJsonSchema(schema);
|
|
228
229
|
tools = [
|
|
229
230
|
{
|
|
230
231
|
type: "function",
|
|
@@ -303,12 +304,6 @@ export class ChatAnthropicTools extends BaseChatModel {
|
|
|
303
304
|
});
|
|
304
305
|
}
|
|
305
306
|
}
|
|
306
|
-
function isZodSchema(
|
|
307
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
308
|
-
input) {
|
|
309
|
-
// Check for a characteristic method of Zod schemas
|
|
310
|
-
return typeof input?.parse === "function";
|
|
311
|
-
}
|
|
312
307
|
function isStructuredOutputMethodParams(x
|
|
313
308
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
314
309
|
) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonSchema7ObjectType } from "
|
|
1
|
+
import { type JsonSchema7ObjectType } from "@langchain/core/utils/json_schema";
|
|
2
2
|
import { PromptTemplate } from "@langchain/core/prompts";
|
|
3
3
|
import { ToolDefinition } from "@langchain/core/language_models/base";
|
|
4
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>;
|
package/dist/output_parsers.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.extractToolCalls = exports.AnthropicToolsOutputParser = void 0;
|
|
4
4
|
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
5
|
+
const types_1 = require("@langchain/core/utils/types");
|
|
5
6
|
class AnthropicToolsOutputParser extends output_parsers_1.BaseLLMOutputParser {
|
|
6
7
|
static lc_name() {
|
|
7
8
|
return "AnthropicToolsOutputParser";
|
|
@@ -61,12 +62,12 @@ class AnthropicToolsOutputParser extends output_parsers_1.BaseLLMOutputParser {
|
|
|
61
62
|
if (this.zodSchema === undefined) {
|
|
62
63
|
return parsedResult;
|
|
63
64
|
}
|
|
64
|
-
const zodParsedResult = await this.zodSchema
|
|
65
|
+
const zodParsedResult = await (0, types_1.interopSafeParseAsync)(this.zodSchema, parsedResult);
|
|
65
66
|
if (zodParsedResult.success) {
|
|
66
67
|
return zodParsedResult.data;
|
|
67
68
|
}
|
|
68
69
|
else {
|
|
69
|
-
throw new output_parsers_1.OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error.
|
|
70
|
+
throw new output_parsers_1.OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error.issues)}`, JSON.stringify(parsedResult, null, 2));
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
73
|
async parseResult(generations) {
|
package/dist/output_parsers.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
1
|
import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
|
|
3
|
-
import {
|
|
2
|
+
import { JsonOutputKeyToolsParserParamsInterop } from "@langchain/core/output_parsers/openai_tools";
|
|
4
3
|
import { ChatGeneration } from "@langchain/core/outputs";
|
|
5
4
|
import { ToolCall } from "@langchain/core/messages/tool";
|
|
6
|
-
|
|
5
|
+
import { InteropZodType } from "@langchain/core/utils/types";
|
|
6
|
+
interface AnthropicToolsOutputParserParams<T extends Record<string, any>> extends JsonOutputKeyToolsParserParamsInterop<T> {
|
|
7
7
|
}
|
|
8
8
|
export declare class AnthropicToolsOutputParser<T extends Record<string, any> = Record<string, any>> extends BaseLLMOutputParser<T> {
|
|
9
9
|
static lc_name(): string;
|
|
@@ -13,7 +13,7 @@ export declare class AnthropicToolsOutputParser<T extends Record<string, any> =
|
|
|
13
13
|
keyName: string;
|
|
14
14
|
/** Whether to return only the first tool call. */
|
|
15
15
|
returnSingle: boolean;
|
|
16
|
-
zodSchema?:
|
|
16
|
+
zodSchema?: InteropZodType<T>;
|
|
17
17
|
constructor(params: AnthropicToolsOutputParserParams<T>);
|
|
18
18
|
protected _validateResult(result: unknown): Promise<T>;
|
|
19
19
|
parseResult(generations: ChatGeneration[]): Promise<T>;
|
package/dist/output_parsers.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BaseLLMOutputParser, OutputParserException, } from "@langchain/core/output_parsers";
|
|
2
|
+
import { interopSafeParseAsync, } from "@langchain/core/utils/types";
|
|
2
3
|
export class AnthropicToolsOutputParser extends BaseLLMOutputParser {
|
|
3
4
|
static lc_name() {
|
|
4
5
|
return "AnthropicToolsOutputParser";
|
|
@@ -58,12 +59,12 @@ export class AnthropicToolsOutputParser extends BaseLLMOutputParser {
|
|
|
58
59
|
if (this.zodSchema === undefined) {
|
|
59
60
|
return parsedResult;
|
|
60
61
|
}
|
|
61
|
-
const zodParsedResult = await this.zodSchema
|
|
62
|
+
const zodParsedResult = await interopSafeParseAsync(this.zodSchema, parsedResult);
|
|
62
63
|
if (zodParsedResult.success) {
|
|
63
64
|
return zodParsedResult.data;
|
|
64
65
|
}
|
|
65
66
|
else {
|
|
66
|
-
throw new OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error.
|
|
67
|
+
throw new OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error.issues)}`, JSON.stringify(parsedResult, null, 2));
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
async parseResult(generations) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/anthropic",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.22",
|
|
4
4
|
"description": "Anthropic integrations for LangChain.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -36,12 +36,10 @@
|
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@anthropic-ai/sdk": "^0.39.0",
|
|
39
|
-
"fast-xml-parser": "^4.4.1"
|
|
40
|
-
"zod": "^3.22.4",
|
|
41
|
-
"zod-to-json-schema": "^3.22.4"
|
|
39
|
+
"fast-xml-parser": "^4.4.1"
|
|
42
40
|
},
|
|
43
41
|
"peerDependencies": {
|
|
44
|
-
"@langchain/core": ">=0.3.
|
|
42
|
+
"@langchain/core": ">=0.3.58 <0.4.0"
|
|
45
43
|
},
|
|
46
44
|
"devDependencies": {
|
|
47
45
|
"@anthropic-ai/vertex-sdk": "^0.4.1",
|
|
@@ -65,7 +63,8 @@
|
|
|
65
63
|
"release-it": "^18.1.2",
|
|
66
64
|
"rimraf": "^5.0.1",
|
|
67
65
|
"ts-jest": "^29.1.0",
|
|
68
|
-
"typescript": "~5.1.6"
|
|
66
|
+
"typescript": "~5.1.6",
|
|
67
|
+
"zod": "^3.25.32"
|
|
69
68
|
},
|
|
70
69
|
"publishConfig": {
|
|
71
70
|
"access": "public"
|