@langchain/anthropic 0.3.22 → 0.3.23
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 +21 -0
- package/dist/chat_models.d.ts +16 -10
- package/dist/chat_models.js +21 -0
- package/dist/experimental/utils/tool_calling.cjs +3 -3
- package/dist/load/import_map.cjs +17 -7
- package/dist/load/index.cjs +19 -9
- package/dist/output_parsers.cjs +12 -2
- package/dist/output_parsers.js +10 -0
- package/dist/types.cjs +1 -2
- package/dist/types.d.ts +5 -0
- package/dist/utils/errors.cjs +2 -3
- package/dist/utils/message_inputs.cjs +19 -9
- package/dist/utils/message_inputs.js +17 -6
- package/dist/utils/message_outputs.cjs +23 -5
- package/dist/utils/message_outputs.js +21 -2
- package/dist/utils/prompts.cjs +1 -2
- package/dist/utils/tools.cjs +1 -2
- package/dist/utils/tools.d.ts +2 -2
- package/package.json +5 -5
package/dist/chat_models.cjs
CHANGED
|
@@ -43,6 +43,16 @@ function _thinkingInParams(params) {
|
|
|
43
43
|
function isAnthropicTool(tool) {
|
|
44
44
|
return "input_schema" in tool;
|
|
45
45
|
}
|
|
46
|
+
function isBuiltinTool(tool) {
|
|
47
|
+
const builtinTools = ["web_search"];
|
|
48
|
+
return (typeof tool === "object" &&
|
|
49
|
+
tool !== null &&
|
|
50
|
+
"type" in tool &&
|
|
51
|
+
"name" in tool &&
|
|
52
|
+
typeof tool.type === "string" &&
|
|
53
|
+
typeof tool.name === "string" &&
|
|
54
|
+
builtinTools.includes(tool.name));
|
|
55
|
+
}
|
|
46
56
|
function extractToken(chunk) {
|
|
47
57
|
if (typeof chunk.content === "string") {
|
|
48
58
|
return chunk.content;
|
|
@@ -626,6 +636,9 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
626
636
|
return undefined;
|
|
627
637
|
}
|
|
628
638
|
return tools.map((tool) => {
|
|
639
|
+
if (isBuiltinTool(tool)) {
|
|
640
|
+
return tool;
|
|
641
|
+
}
|
|
629
642
|
if (isAnthropicTool(tool)) {
|
|
630
643
|
return tool;
|
|
631
644
|
}
|
|
@@ -926,6 +939,10 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
926
939
|
console.warn(thinkingAdmonition);
|
|
927
940
|
llm = this.withConfig({
|
|
928
941
|
tools,
|
|
942
|
+
ls_structured_output_format: {
|
|
943
|
+
kwargs: { method: "functionCalling" },
|
|
944
|
+
schema: (0, json_schema_1.toJsonSchema)(schema),
|
|
945
|
+
},
|
|
929
946
|
});
|
|
930
947
|
const raiseIfNoToolCalls = (message) => {
|
|
931
948
|
if (!message.tool_calls || message.tool_calls.length === 0) {
|
|
@@ -942,6 +959,10 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
942
959
|
type: "tool",
|
|
943
960
|
name: functionName,
|
|
944
961
|
},
|
|
962
|
+
ls_structured_output_format: {
|
|
963
|
+
kwargs: { method: "functionCalling" },
|
|
964
|
+
schema: (0, json_schema_1.toJsonSchema)(schema),
|
|
965
|
+
},
|
|
945
966
|
});
|
|
946
967
|
}
|
|
947
968
|
if (!includeRaw) {
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -21,6 +21,10 @@ export interface ChatAnthropicCallOptions extends BaseChatModelCallOptions, Pick
|
|
|
21
21
|
*/
|
|
22
22
|
headers?: Record<string, string>;
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* @see https://docs.anthropic.com/claude/docs/models-overview
|
|
26
|
+
*/
|
|
27
|
+
export type AnthropicMessagesModelId = Anthropic.Model | (string & NonNullable<unknown>);
|
|
24
28
|
/**
|
|
25
29
|
* Input to AnthropicChat class.
|
|
26
30
|
*/
|
|
@@ -66,9 +70,9 @@ export interface AnthropicInput {
|
|
|
66
70
|
/** Anthropic API URL */
|
|
67
71
|
anthropicApiUrl?: string;
|
|
68
72
|
/** @deprecated Use "model" instead */
|
|
69
|
-
modelName?:
|
|
73
|
+
modelName?: AnthropicMessagesModelId;
|
|
70
74
|
/** Model name to use */
|
|
71
|
-
model?:
|
|
75
|
+
model?: AnthropicMessagesModelId;
|
|
72
76
|
/** Overridable Anthropic ClientOptions */
|
|
73
77
|
clientOptions?: ClientOptions;
|
|
74
78
|
/** Holds any additional parameters that are valid to pass to {@link
|
|
@@ -511,7 +515,7 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
|
|
|
511
515
|
* @param {ChatAnthropicCallOptions["tools"]} tools The tools to format
|
|
512
516
|
* @returns {AnthropicTool[] | undefined} The formatted tools, or undefined if none are passed.
|
|
513
517
|
*/
|
|
514
|
-
formatStructuredToolToAnthropic(tools: ChatAnthropicCallOptions["tools"]): Anthropic.Messages.
|
|
518
|
+
formatStructuredToolToAnthropic(tools: ChatAnthropicCallOptions["tools"]): Anthropic.Messages.ToolUnion[] | undefined;
|
|
515
519
|
bindTools(tools: ChatAnthropicToolType[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, CallOptions>;
|
|
516
520
|
/**
|
|
517
521
|
* Get the parameters used to invoke the model
|
|
@@ -519,16 +523,17 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
|
|
|
519
523
|
invocationParams(options?: this["ParsedCallOptions"]): Omit<AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams, "messages"> & Kwargs;
|
|
520
524
|
/** @ignore */
|
|
521
525
|
_identifyingParams(): {
|
|
522
|
-
system?: string | Anthropic.Messages.TextBlockParam
|
|
526
|
+
system?: (string | Array<Anthropic.Messages.TextBlockParam>) | undefined;
|
|
523
527
|
thinking?: Anthropic.Messages.ThinkingConfigParam | undefined;
|
|
524
528
|
metadata?: Anthropic.Messages.Metadata | undefined;
|
|
525
529
|
model: Anthropic.Messages.Model;
|
|
526
530
|
max_tokens: number;
|
|
527
|
-
tools?: Anthropic.Messages.ToolUnion
|
|
531
|
+
tools?: Array<Anthropic.Messages.ToolUnion> | undefined;
|
|
528
532
|
tool_choice?: Anthropic.Messages.ToolChoice | undefined;
|
|
529
533
|
temperature?: number | undefined;
|
|
530
534
|
stream?: boolean | undefined;
|
|
531
|
-
|
|
535
|
+
service_tier?: "auto" | "standard_only" | undefined;
|
|
536
|
+
stop_sequences?: Array<string> | undefined;
|
|
532
537
|
top_k?: number | undefined;
|
|
533
538
|
top_p?: number | undefined;
|
|
534
539
|
model_name: string;
|
|
@@ -537,16 +542,17 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
|
|
|
537
542
|
* Get the identifying parameters for the model
|
|
538
543
|
*/
|
|
539
544
|
identifyingParams(): {
|
|
540
|
-
system?: string | Anthropic.Messages.TextBlockParam
|
|
545
|
+
system?: (string | Array<Anthropic.Messages.TextBlockParam>) | undefined;
|
|
541
546
|
thinking?: Anthropic.Messages.ThinkingConfigParam | undefined;
|
|
542
547
|
metadata?: Anthropic.Messages.Metadata | undefined;
|
|
543
548
|
model: Anthropic.Messages.Model;
|
|
544
549
|
max_tokens: number;
|
|
545
|
-
tools?: Anthropic.Messages.ToolUnion
|
|
550
|
+
tools?: Array<Anthropic.Messages.ToolUnion> | undefined;
|
|
546
551
|
tool_choice?: Anthropic.Messages.ToolChoice | undefined;
|
|
547
552
|
temperature?: number | undefined;
|
|
548
553
|
stream?: boolean | undefined;
|
|
549
|
-
|
|
554
|
+
service_tier?: "auto" | "standard_only" | undefined;
|
|
555
|
+
stop_sequences?: Array<string> | undefined;
|
|
550
556
|
top_k?: number | undefined;
|
|
551
557
|
top_p?: number | undefined;
|
|
552
558
|
model_name: string;
|
|
@@ -558,7 +564,7 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
|
|
|
558
564
|
llmOutput: {
|
|
559
565
|
id: string;
|
|
560
566
|
model: Anthropic.Messages.Model;
|
|
561
|
-
stop_reason:
|
|
567
|
+
stop_reason: Anthropic.Messages.StopReason | null;
|
|
562
568
|
stop_sequence: string | null;
|
|
563
569
|
usage: Anthropic.Messages.Usage;
|
|
564
570
|
};
|
package/dist/chat_models.js
CHANGED
|
@@ -40,6 +40,16 @@ function _thinkingInParams(params) {
|
|
|
40
40
|
function isAnthropicTool(tool) {
|
|
41
41
|
return "input_schema" in tool;
|
|
42
42
|
}
|
|
43
|
+
function isBuiltinTool(tool) {
|
|
44
|
+
const builtinTools = ["web_search"];
|
|
45
|
+
return (typeof tool === "object" &&
|
|
46
|
+
tool !== null &&
|
|
47
|
+
"type" in tool &&
|
|
48
|
+
"name" in tool &&
|
|
49
|
+
typeof tool.type === "string" &&
|
|
50
|
+
typeof tool.name === "string" &&
|
|
51
|
+
builtinTools.includes(tool.name));
|
|
52
|
+
}
|
|
43
53
|
function extractToken(chunk) {
|
|
44
54
|
if (typeof chunk.content === "string") {
|
|
45
55
|
return chunk.content;
|
|
@@ -623,6 +633,9 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
623
633
|
return undefined;
|
|
624
634
|
}
|
|
625
635
|
return tools.map((tool) => {
|
|
636
|
+
if (isBuiltinTool(tool)) {
|
|
637
|
+
return tool;
|
|
638
|
+
}
|
|
626
639
|
if (isAnthropicTool(tool)) {
|
|
627
640
|
return tool;
|
|
628
641
|
}
|
|
@@ -923,6 +936,10 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
923
936
|
console.warn(thinkingAdmonition);
|
|
924
937
|
llm = this.withConfig({
|
|
925
938
|
tools,
|
|
939
|
+
ls_structured_output_format: {
|
|
940
|
+
kwargs: { method: "functionCalling" },
|
|
941
|
+
schema: toJsonSchema(schema),
|
|
942
|
+
},
|
|
926
943
|
});
|
|
927
944
|
const raiseIfNoToolCalls = (message) => {
|
|
928
945
|
if (!message.tool_calls || message.tool_calls.length === 0) {
|
|
@@ -939,6 +956,10 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
939
956
|
type: "tool",
|
|
940
957
|
name: functionName,
|
|
941
958
|
},
|
|
959
|
+
ls_structured_output_format: {
|
|
960
|
+
kwargs: { method: "functionCalling" },
|
|
961
|
+
schema: toJsonSchema(schema),
|
|
962
|
+
},
|
|
942
963
|
});
|
|
943
964
|
}
|
|
944
965
|
if (!includeRaw) {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.DEFAULT_TOOL_SYSTEM_PROMPT = void 0;
|
|
4
|
+
exports.formatAsXMLRepresentation = formatAsXMLRepresentation;
|
|
5
|
+
exports.fixArrayXMLParameters = fixArrayXMLParameters;
|
|
4
6
|
const fast_xml_parser_1 = require("fast-xml-parser");
|
|
5
7
|
const prompts_1 = require("@langchain/core/prompts");
|
|
6
8
|
exports.DEFAULT_TOOL_SYSTEM_PROMPT =
|
|
@@ -51,7 +53,6 @@ ${parameterXml}
|
|
|
51
53
|
</parameters>
|
|
52
54
|
</tool_description>`;
|
|
53
55
|
}
|
|
54
|
-
exports.formatAsXMLRepresentation = formatAsXMLRepresentation;
|
|
55
56
|
function fixArrayXMLParameters(schema,
|
|
56
57
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
57
58
|
xmlParameters
|
|
@@ -103,4 +104,3 @@ xmlParameters
|
|
|
103
104
|
}
|
|
104
105
|
return fixedParameters;
|
|
105
106
|
}
|
|
106
|
-
exports.fixArrayXMLParameters = fixArrayXMLParameters;
|
package/dist/load/import_map.cjs
CHANGED
|
@@ -16,13 +16,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
16
16
|
}) : function(o, v) {
|
|
17
17
|
o["default"] = v;
|
|
18
18
|
});
|
|
19
|
-
var __importStar = (this && this.__importStar) || function (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
};
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
26
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
37
|
exports.experimental = exports.index = void 0;
|
|
28
38
|
exports.index = __importStar(require("../index.cjs"));
|
package/dist/load/index.cjs
CHANGED
|
@@ -15,15 +15,26 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
36
|
+
exports.importMap = exports.optionalImportEntrypoints = void 0;
|
|
37
|
+
exports.load = load;
|
|
27
38
|
const load_1 = require("@langchain/core/load");
|
|
28
39
|
const importMap = __importStar(require("./import_map.cjs"));
|
|
29
40
|
exports.importMap = importMap;
|
|
@@ -50,4 +61,3 @@ optionalImportsMap = {}) {
|
|
|
50
61
|
importMap,
|
|
51
62
|
});
|
|
52
63
|
}
|
|
53
|
-
exports.load = load;
|
package/dist/output_parsers.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.AnthropicToolsOutputParser = void 0;
|
|
4
|
+
exports.extractToolCalls = extractToolCalls;
|
|
4
5
|
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
5
6
|
const types_1 = require("@langchain/core/utils/types");
|
|
6
7
|
class AnthropicToolsOutputParser extends output_parsers_1.BaseLLMOutputParser {
|
|
@@ -100,7 +101,16 @@ function extractToolCalls(content) {
|
|
|
100
101
|
type: "tool_call",
|
|
101
102
|
});
|
|
102
103
|
}
|
|
104
|
+
else if (block.type === "server_tool_use" &&
|
|
105
|
+
block.name === "web_search") {
|
|
106
|
+
// Handle Anthropic built-in web search tool
|
|
107
|
+
toolCalls.push({
|
|
108
|
+
name: block.name,
|
|
109
|
+
args: block.input,
|
|
110
|
+
id: block.id,
|
|
111
|
+
type: "tool_call",
|
|
112
|
+
});
|
|
113
|
+
}
|
|
103
114
|
}
|
|
104
115
|
return toolCalls;
|
|
105
116
|
}
|
|
106
|
-
exports.extractToolCalls = extractToolCalls;
|
package/dist/output_parsers.js
CHANGED
|
@@ -96,6 +96,16 @@ export function extractToolCalls(content) {
|
|
|
96
96
|
type: "tool_call",
|
|
97
97
|
});
|
|
98
98
|
}
|
|
99
|
+
else if (block.type === "server_tool_use" &&
|
|
100
|
+
block.name === "web_search") {
|
|
101
|
+
// Handle Anthropic built-in web search tool
|
|
102
|
+
toolCalls.push({
|
|
103
|
+
name: block.name,
|
|
104
|
+
args: block.input,
|
|
105
|
+
id: block.id,
|
|
106
|
+
type: "tool_call",
|
|
107
|
+
});
|
|
108
|
+
}
|
|
99
109
|
}
|
|
100
110
|
return toolCalls;
|
|
101
111
|
}
|
package/dist/types.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isAnthropicImageBlockParam =
|
|
3
|
+
exports.isAnthropicImageBlockParam = isAnthropicImageBlockParam;
|
|
4
4
|
function isAnthropicImageBlockParam(block) {
|
|
5
5
|
if (block == null) {
|
|
6
6
|
return false;
|
|
@@ -46,4 +46,3 @@ function isAnthropicImageBlockParam(block) {
|
|
|
46
46
|
}
|
|
47
47
|
return false;
|
|
48
48
|
}
|
|
49
|
-
exports.isAnthropicImageBlockParam = isAnthropicImageBlockParam;
|
package/dist/types.d.ts
CHANGED
|
@@ -25,4 +25,9 @@ export type AnthropicToolResultBlockParam = Anthropic.Messages.ToolResultBlockPa
|
|
|
25
25
|
export type AnthropicDocumentBlockParam = Anthropic.Messages.DocumentBlockParam;
|
|
26
26
|
export type AnthropicThinkingBlockParam = Anthropic.Messages.ThinkingBlockParam;
|
|
27
27
|
export type AnthropicRedactedThinkingBlockParam = Anthropic.Messages.RedactedThinkingBlockParam;
|
|
28
|
+
export type AnthropicServerToolUseBlockParam = Anthropic.Messages.ServerToolUseBlockParam;
|
|
29
|
+
export type AnthropicWebSearchToolResultBlockParam = Anthropic.Messages.WebSearchToolResultBlockParam;
|
|
30
|
+
export type AnthropicWebSearchResultBlockParam = Anthropic.Messages.WebSearchResultBlockParam;
|
|
31
|
+
export type AnthropicContentBlock = AnthropicTextBlockParam | AnthropicImageBlockParam | AnthropicToolUseBlockParam | AnthropicToolResultBlockParam | AnthropicDocumentBlockParam | AnthropicThinkingBlockParam | AnthropicRedactedThinkingBlockParam | AnthropicServerToolUseBlockParam | AnthropicWebSearchToolResultBlockParam | AnthropicWebSearchResultBlockParam;
|
|
28
32
|
export declare function isAnthropicImageBlockParam(block: unknown): block is AnthropicImageBlockParam;
|
|
33
|
+
export type AnthropicBuiltInToolUnion = Exclude<Anthropic.Messages.ToolUnion, Anthropic.Messages.Tool>;
|
package/dist/utils/errors.cjs
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
3
|
/* eslint-disable no-param-reassign */
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.
|
|
5
|
+
exports.addLangChainErrorFields = addLangChainErrorFields;
|
|
6
|
+
exports.wrapAnthropicClientError = wrapAnthropicClientError;
|
|
6
7
|
function addLangChainErrorFields(error, lc_error_code) {
|
|
7
8
|
error.lc_error_code = lc_error_code;
|
|
8
9
|
error.message = `${error.message}\n\nTroubleshooting URL: https://js.langchain.com/docs/troubleshooting/errors/${lc_error_code}/\n`;
|
|
9
10
|
return error;
|
|
10
11
|
}
|
|
11
|
-
exports.addLangChainErrorFields = addLangChainErrorFields;
|
|
12
12
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
13
|
function wrapAnthropicClientError(e) {
|
|
14
14
|
let error;
|
|
@@ -29,4 +29,3 @@ function wrapAnthropicClientError(e) {
|
|
|
29
29
|
}
|
|
30
30
|
return error;
|
|
31
31
|
}
|
|
32
|
-
exports.wrapAnthropicClientError = wrapAnthropicClientError;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports._convertLangChainToolCallToAnthropic = _convertLangChainToolCallToAnthropic;
|
|
4
|
+
exports._convertMessagesToAnthropicPayload = _convertMessagesToAnthropicPayload;
|
|
4
5
|
/**
|
|
5
6
|
* This util file contains functions for converting LangChain messages to Anthropic messages.
|
|
6
7
|
*/
|
|
@@ -101,7 +102,6 @@ function _convertLangChainToolCallToAnthropic(toolCall) {
|
|
|
101
102
|
input: toolCall.args,
|
|
102
103
|
};
|
|
103
104
|
}
|
|
104
|
-
exports._convertLangChainToolCallToAnthropic = _convertLangChainToolCallToAnthropic;
|
|
105
105
|
const standardContentBlockConverter = {
|
|
106
106
|
providerName: "anthropic",
|
|
107
107
|
fromStandardTextBlock(block) {
|
|
@@ -285,7 +285,14 @@ const standardContentBlockConverter = {
|
|
|
285
285
|
},
|
|
286
286
|
};
|
|
287
287
|
function _formatContent(content) {
|
|
288
|
-
const toolTypes = [
|
|
288
|
+
const toolTypes = [
|
|
289
|
+
"tool_use",
|
|
290
|
+
"tool_result",
|
|
291
|
+
"input_json_delta",
|
|
292
|
+
"server_tool_use",
|
|
293
|
+
"web_search_tool_result",
|
|
294
|
+
"web_search_result",
|
|
295
|
+
];
|
|
289
296
|
const textTypes = ["text", "text_delta"];
|
|
290
297
|
if (typeof content === "string") {
|
|
291
298
|
return content;
|
|
@@ -305,7 +312,7 @@ function _formatContent(content) {
|
|
|
305
312
|
source = _formatImage(contentPart.image_url.url);
|
|
306
313
|
}
|
|
307
314
|
return {
|
|
308
|
-
type: "image",
|
|
315
|
+
type: "image", // Explicitly setting the type as "image"
|
|
309
316
|
source,
|
|
310
317
|
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
311
318
|
};
|
|
@@ -322,7 +329,7 @@ function _formatContent(content) {
|
|
|
322
329
|
}
|
|
323
330
|
else if (contentPart.type === "thinking") {
|
|
324
331
|
const block = {
|
|
325
|
-
type: "thinking",
|
|
332
|
+
type: "thinking", // Explicitly setting the type as "thinking"
|
|
326
333
|
thinking: contentPart.thinking,
|
|
327
334
|
signature: contentPart.signature,
|
|
328
335
|
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
@@ -331,7 +338,7 @@ function _formatContent(content) {
|
|
|
331
338
|
}
|
|
332
339
|
else if (contentPart.type === "redacted_thinking") {
|
|
333
340
|
const block = {
|
|
334
|
-
type: "redacted_thinking",
|
|
341
|
+
type: "redacted_thinking", // Explicitly setting the type as "redacted_thinking"
|
|
335
342
|
data: contentPart.data,
|
|
336
343
|
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
337
344
|
};
|
|
@@ -341,9 +348,12 @@ function _formatContent(content) {
|
|
|
341
348
|
"text" in contentPart) {
|
|
342
349
|
// Assuming contentPart is of type MessageContentText here
|
|
343
350
|
return {
|
|
344
|
-
type: "text",
|
|
351
|
+
type: "text", // Explicitly setting the type as "text"
|
|
345
352
|
text: contentPart.text,
|
|
346
353
|
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
354
|
+
...("citations" in contentPart && contentPart.citations
|
|
355
|
+
? { citations: contentPart.citations }
|
|
356
|
+
: {}),
|
|
347
357
|
};
|
|
348
358
|
}
|
|
349
359
|
else if (toolTypes.find((t) => t === contentPart.type)) {
|
|
@@ -433,7 +443,8 @@ function _convertMessagesToAnthropicPayload(messages) {
|
|
|
433
443
|
else {
|
|
434
444
|
const { content } = message;
|
|
435
445
|
const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) => content.find((contentPart) => (contentPart.type === "tool_use" ||
|
|
436
|
-
contentPart.type === "input_json_delta"
|
|
446
|
+
contentPart.type === "input_json_delta" ||
|
|
447
|
+
contentPart.type === "server_tool_use") &&
|
|
437
448
|
contentPart.id === toolCall.id));
|
|
438
449
|
if (hasMismatchedToolCalls) {
|
|
439
450
|
console.warn(`The "tool_calls" field on a message is only respected if content is a string.`);
|
|
@@ -456,7 +467,6 @@ function _convertMessagesToAnthropicPayload(messages) {
|
|
|
456
467
|
system,
|
|
457
468
|
};
|
|
458
469
|
}
|
|
459
|
-
exports._convertMessagesToAnthropicPayload = _convertMessagesToAnthropicPayload;
|
|
460
470
|
function mergeMessages(messages) {
|
|
461
471
|
if (!messages || messages.length <= 1) {
|
|
462
472
|
return messages;
|
|
@@ -281,7 +281,14 @@ const standardContentBlockConverter = {
|
|
|
281
281
|
},
|
|
282
282
|
};
|
|
283
283
|
function _formatContent(content) {
|
|
284
|
-
const toolTypes = [
|
|
284
|
+
const toolTypes = [
|
|
285
|
+
"tool_use",
|
|
286
|
+
"tool_result",
|
|
287
|
+
"input_json_delta",
|
|
288
|
+
"server_tool_use",
|
|
289
|
+
"web_search_tool_result",
|
|
290
|
+
"web_search_result",
|
|
291
|
+
];
|
|
285
292
|
const textTypes = ["text", "text_delta"];
|
|
286
293
|
if (typeof content === "string") {
|
|
287
294
|
return content;
|
|
@@ -301,7 +308,7 @@ function _formatContent(content) {
|
|
|
301
308
|
source = _formatImage(contentPart.image_url.url);
|
|
302
309
|
}
|
|
303
310
|
return {
|
|
304
|
-
type: "image",
|
|
311
|
+
type: "image", // Explicitly setting the type as "image"
|
|
305
312
|
source,
|
|
306
313
|
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
307
314
|
};
|
|
@@ -318,7 +325,7 @@ function _formatContent(content) {
|
|
|
318
325
|
}
|
|
319
326
|
else if (contentPart.type === "thinking") {
|
|
320
327
|
const block = {
|
|
321
|
-
type: "thinking",
|
|
328
|
+
type: "thinking", // Explicitly setting the type as "thinking"
|
|
322
329
|
thinking: contentPart.thinking,
|
|
323
330
|
signature: contentPart.signature,
|
|
324
331
|
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
@@ -327,7 +334,7 @@ function _formatContent(content) {
|
|
|
327
334
|
}
|
|
328
335
|
else if (contentPart.type === "redacted_thinking") {
|
|
329
336
|
const block = {
|
|
330
|
-
type: "redacted_thinking",
|
|
337
|
+
type: "redacted_thinking", // Explicitly setting the type as "redacted_thinking"
|
|
331
338
|
data: contentPart.data,
|
|
332
339
|
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
333
340
|
};
|
|
@@ -337,9 +344,12 @@ function _formatContent(content) {
|
|
|
337
344
|
"text" in contentPart) {
|
|
338
345
|
// Assuming contentPart is of type MessageContentText here
|
|
339
346
|
return {
|
|
340
|
-
type: "text",
|
|
347
|
+
type: "text", // Explicitly setting the type as "text"
|
|
341
348
|
text: contentPart.text,
|
|
342
349
|
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
350
|
+
...("citations" in contentPart && contentPart.citations
|
|
351
|
+
? { citations: contentPart.citations }
|
|
352
|
+
: {}),
|
|
343
353
|
};
|
|
344
354
|
}
|
|
345
355
|
else if (toolTypes.find((t) => t === contentPart.type)) {
|
|
@@ -429,7 +439,8 @@ export function _convertMessagesToAnthropicPayload(messages) {
|
|
|
429
439
|
else {
|
|
430
440
|
const { content } = message;
|
|
431
441
|
const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) => content.find((contentPart) => (contentPart.type === "tool_use" ||
|
|
432
|
-
contentPart.type === "input_json_delta"
|
|
442
|
+
contentPart.type === "input_json_delta" ||
|
|
443
|
+
contentPart.type === "server_tool_use") &&
|
|
433
444
|
contentPart.id === toolCall.id));
|
|
434
445
|
if (hasMismatchedToolCalls) {
|
|
435
446
|
console.warn(`The "tool_calls" field on a message is only respected if content is a string.`);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports._makeMessageChunkFromAnthropicEvent = _makeMessageChunkFromAnthropicEvent;
|
|
4
|
+
exports.anthropicResponseToChatMessages = anthropicResponseToChatMessages;
|
|
4
5
|
const messages_1 = require("@langchain/core/messages");
|
|
5
6
|
const output_parsers_js_1 = require("../output_parsers.cjs");
|
|
6
7
|
function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
@@ -60,7 +61,12 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
60
61
|
};
|
|
61
62
|
}
|
|
62
63
|
else if (data.type === "content_block_start" &&
|
|
63
|
-
[
|
|
64
|
+
[
|
|
65
|
+
"tool_use",
|
|
66
|
+
"document",
|
|
67
|
+
"server_tool_use",
|
|
68
|
+
"web_search_tool_result",
|
|
69
|
+
].includes(data.content_block.type)) {
|
|
64
70
|
const contentBlock = data.content_block;
|
|
65
71
|
let toolCallChunks;
|
|
66
72
|
if (contentBlock.type === "tool_use") {
|
|
@@ -73,6 +79,17 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
73
79
|
},
|
|
74
80
|
];
|
|
75
81
|
}
|
|
82
|
+
else if (contentBlock.type === "server_tool_use") {
|
|
83
|
+
// Handle anthropic built-in server tool use (like web search)
|
|
84
|
+
toolCallChunks = [
|
|
85
|
+
{
|
|
86
|
+
id: contentBlock.id,
|
|
87
|
+
index: data.index,
|
|
88
|
+
name: contentBlock.name,
|
|
89
|
+
args: "",
|
|
90
|
+
},
|
|
91
|
+
];
|
|
92
|
+
}
|
|
76
93
|
else {
|
|
77
94
|
toolCallChunks = [];
|
|
78
95
|
}
|
|
@@ -84,7 +101,10 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
84
101
|
{
|
|
85
102
|
index: data.index,
|
|
86
103
|
...data.content_block,
|
|
87
|
-
input: ""
|
|
104
|
+
input: contentBlock.type === "server_tool_use" ||
|
|
105
|
+
contentBlock.type === "tool_use"
|
|
106
|
+
? ""
|
|
107
|
+
: undefined,
|
|
88
108
|
},
|
|
89
109
|
],
|
|
90
110
|
additional_kwargs: {},
|
|
@@ -193,7 +213,6 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
193
213
|
}
|
|
194
214
|
return null;
|
|
195
215
|
}
|
|
196
|
-
exports._makeMessageChunkFromAnthropicEvent = _makeMessageChunkFromAnthropicEvent;
|
|
197
216
|
function anthropicResponseToChatMessages(messages, additionalKwargs) {
|
|
198
217
|
const usage = additionalKwargs.usage;
|
|
199
218
|
const usageMetadata = usage != null
|
|
@@ -240,4 +259,3 @@ function anthropicResponseToChatMessages(messages, additionalKwargs) {
|
|
|
240
259
|
return generations;
|
|
241
260
|
}
|
|
242
261
|
}
|
|
243
|
-
exports.anthropicResponseToChatMessages = anthropicResponseToChatMessages;
|
|
@@ -57,7 +57,12 @@ export function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
57
57
|
};
|
|
58
58
|
}
|
|
59
59
|
else if (data.type === "content_block_start" &&
|
|
60
|
-
[
|
|
60
|
+
[
|
|
61
|
+
"tool_use",
|
|
62
|
+
"document",
|
|
63
|
+
"server_tool_use",
|
|
64
|
+
"web_search_tool_result",
|
|
65
|
+
].includes(data.content_block.type)) {
|
|
61
66
|
const contentBlock = data.content_block;
|
|
62
67
|
let toolCallChunks;
|
|
63
68
|
if (contentBlock.type === "tool_use") {
|
|
@@ -70,6 +75,17 @@ export function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
70
75
|
},
|
|
71
76
|
];
|
|
72
77
|
}
|
|
78
|
+
else if (contentBlock.type === "server_tool_use") {
|
|
79
|
+
// Handle anthropic built-in server tool use (like web search)
|
|
80
|
+
toolCallChunks = [
|
|
81
|
+
{
|
|
82
|
+
id: contentBlock.id,
|
|
83
|
+
index: data.index,
|
|
84
|
+
name: contentBlock.name,
|
|
85
|
+
args: "",
|
|
86
|
+
},
|
|
87
|
+
];
|
|
88
|
+
}
|
|
73
89
|
else {
|
|
74
90
|
toolCallChunks = [];
|
|
75
91
|
}
|
|
@@ -81,7 +97,10 @@ export function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
81
97
|
{
|
|
82
98
|
index: data.index,
|
|
83
99
|
...data.content_block,
|
|
84
|
-
input: ""
|
|
100
|
+
input: contentBlock.type === "server_tool_use" ||
|
|
101
|
+
contentBlock.type === "tool_use"
|
|
102
|
+
? ""
|
|
103
|
+
: undefined,
|
|
85
104
|
},
|
|
86
105
|
],
|
|
87
106
|
additional_kwargs: {},
|
package/dist/utils/prompts.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertPromptToAnthropic =
|
|
3
|
+
exports.convertPromptToAnthropic = convertPromptToAnthropic;
|
|
4
4
|
const message_inputs_js_1 = require("./message_inputs.cjs");
|
|
5
5
|
/**
|
|
6
6
|
* Convert a formatted LangChain prompt (e.g. pulled from the hub) into
|
|
@@ -46,4 +46,3 @@ function convertPromptToAnthropic(formattedPrompt) {
|
|
|
46
46
|
}
|
|
47
47
|
return anthropicBody;
|
|
48
48
|
}
|
|
49
|
-
exports.convertPromptToAnthropic = convertPromptToAnthropic;
|
package/dist/utils/tools.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.handleToolChoice =
|
|
3
|
+
exports.handleToolChoice = handleToolChoice;
|
|
4
4
|
function handleToolChoice(toolChoice) {
|
|
5
5
|
if (!toolChoice) {
|
|
6
6
|
return undefined;
|
|
@@ -25,4 +25,3 @@ function handleToolChoice(toolChoice) {
|
|
|
25
25
|
return toolChoice;
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
exports.handleToolChoice = handleToolChoice;
|
package/dist/utils/tools.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Anthropic } from "@anthropic-ai/sdk";
|
|
2
2
|
import { AnthropicToolChoice } from "../types.js";
|
|
3
|
-
export declare function handleToolChoice(toolChoice?: AnthropicToolChoice):
|
|
3
|
+
export declare function handleToolChoice(toolChoice?: AnthropicToolChoice): Anthropic.Messages.ToolChoiceAuto | Anthropic.Messages.ToolChoiceAny | Anthropic.Messages.ToolChoiceTool | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/anthropic",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.23",
|
|
4
4
|
"description": "Anthropic integrations for LangChain.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"build": "yarn turbo:command build:internal --filter=@langchain/anthropic",
|
|
18
18
|
"build:internal": "yarn lc_build --create-entrypoints --pre --tree-shaking --gen-maps",
|
|
19
19
|
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
|
|
20
|
-
"lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
|
|
20
|
+
"lint:dpdm": "dpdm --skip-dynamic-imports circular --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
|
|
21
21
|
"lint": "yarn lint:eslint && yarn lint:dpdm",
|
|
22
22
|
"lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
|
|
23
23
|
"clean": "rm -rf .turbo dist/",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"author": "LangChain",
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@anthropic-ai/sdk": "^0.
|
|
38
|
+
"@anthropic-ai/sdk": "^0.52.0",
|
|
39
39
|
"fast-xml-parser": "^4.4.1"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"@langchain/standard-tests": "0.0.0",
|
|
50
50
|
"@swc/core": "^1.3.90",
|
|
51
51
|
"@swc/jest": "^0.2.29",
|
|
52
|
-
"dpdm": "^3.
|
|
52
|
+
"dpdm": "^3.14.0",
|
|
53
53
|
"eslint": "^8.33.0",
|
|
54
54
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
55
55
|
"eslint-config-prettier": "^8.6.0",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"release-it": "^18.1.2",
|
|
64
64
|
"rimraf": "^5.0.1",
|
|
65
65
|
"ts-jest": "^29.1.0",
|
|
66
|
-
"typescript": "~5.
|
|
66
|
+
"typescript": "~5.8.3",
|
|
67
67
|
"zod": "^3.25.32"
|
|
68
68
|
},
|
|
69
69
|
"publishConfig": {
|