@langchain/anthropic 0.3.2-rc.0 → 0.3.3
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 +2 -2
- package/dist/chat_models.d.ts +0 -9
- package/dist/chat_models.js +3 -3
- package/dist/index.cjs +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/utils/message_inputs.cjs +3 -3
- package/dist/utils/message_inputs.d.ts +1 -1
- package/dist/utils/message_inputs.js +1 -1
- package/dist/utils/prompts.cjs +49 -0
- package/dist/utils/prompts.d.ts +39 -0
- package/dist/utils/prompts.js +45 -0
- package/package.json +1 -1
package/dist/chat_models.cjs
CHANGED
|
@@ -661,7 +661,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
661
661
|
}
|
|
662
662
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
663
663
|
const params = this.invocationParams(options);
|
|
664
|
-
const formattedMessages = (0, message_inputs_js_1.
|
|
664
|
+
const formattedMessages = (0, message_inputs_js_1._convertMessagesToAnthropicPayload)(messages);
|
|
665
665
|
const coerceContentToString = !_toolsInParams({
|
|
666
666
|
...params,
|
|
667
667
|
...formattedMessages,
|
|
@@ -712,7 +712,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
712
712
|
const response = await this.completionWithRetry({
|
|
713
713
|
...params,
|
|
714
714
|
stream: false,
|
|
715
|
-
...(0, message_inputs_js_1.
|
|
715
|
+
...(0, message_inputs_js_1._convertMessagesToAnthropicPayload)(messages),
|
|
716
716
|
}, requestOptions);
|
|
717
717
|
const { content, ...additionalKwargs } = response;
|
|
718
718
|
const generations = (0, message_outputs_js_1.anthropicResponseToChatMessages)(content, additionalKwargs);
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -552,17 +552,8 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
|
|
|
552
552
|
generations: import("@langchain/core/outputs").ChatGeneration[];
|
|
553
553
|
llmOutput: {
|
|
554
554
|
id: string;
|
|
555
|
-
/** Does nucleus sampling, in which we compute the
|
|
556
|
-
* cumulative distribution over all the options for each
|
|
557
|
-
* subsequent token in decreasing probability order and
|
|
558
|
-
* cut it off once it reaches a particular probability
|
|
559
|
-
* specified by top_p. Defaults to -1, which disables it.
|
|
560
|
-
* Note that you should either alter temperature or top_p,
|
|
561
|
-
* but not both.
|
|
562
|
-
*/
|
|
563
555
|
model: Anthropic.Messages.Model;
|
|
564
556
|
stop_reason: "tool_use" | "stop_sequence" | "end_turn" | "max_tokens" | null;
|
|
565
|
-
/** Anthropic API URL */
|
|
566
557
|
stop_sequence: string | null;
|
|
567
558
|
usage: Anthropic.Messages.Usage;
|
|
568
559
|
};
|
package/dist/chat_models.js
CHANGED
|
@@ -10,7 +10,7 @@ import { isZodSchema } 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 { extractToolCallChunk, handleToolChoice } from "./utils/tools.js";
|
|
13
|
-
import {
|
|
13
|
+
import { _convertMessagesToAnthropicPayload } from "./utils/message_inputs.js";
|
|
14
14
|
import { _makeMessageChunkFromAnthropicEvent, anthropicResponseToChatMessages, } from "./utils/message_outputs.js";
|
|
15
15
|
function _toolsInParams(params) {
|
|
16
16
|
return !!(params.tools && params.tools.length > 0);
|
|
@@ -658,7 +658,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
658
658
|
}
|
|
659
659
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
660
660
|
const params = this.invocationParams(options);
|
|
661
|
-
const formattedMessages =
|
|
661
|
+
const formattedMessages = _convertMessagesToAnthropicPayload(messages);
|
|
662
662
|
const coerceContentToString = !_toolsInParams({
|
|
663
663
|
...params,
|
|
664
664
|
...formattedMessages,
|
|
@@ -709,7 +709,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
709
709
|
const response = await this.completionWithRetry({
|
|
710
710
|
...params,
|
|
711
711
|
stream: false,
|
|
712
|
-
...
|
|
712
|
+
..._convertMessagesToAnthropicPayload(messages),
|
|
713
713
|
}, requestOptions);
|
|
714
714
|
const { content, ...additionalKwargs } = response;
|
|
715
715
|
const generations = anthropicResponseToChatMessages(content, additionalKwargs);
|
package/dist/index.cjs
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
17
|
+
exports.convertPromptToAnthropic = void 0;
|
|
18
18
|
__exportStar(require("./chat_models.cjs"), exports);
|
|
19
|
-
var
|
|
20
|
-
Object.defineProperty(exports, "
|
|
19
|
+
var prompts_js_1 = require("./utils/prompts.cjs");
|
|
20
|
+
Object.defineProperty(exports, "convertPromptToAnthropic", { enumerable: true, get: function () { return prompts_js_1.convertPromptToAnthropic; } });
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from "./chat_models.js";
|
|
2
|
-
export {
|
|
2
|
+
export { convertPromptToAnthropic } from "./utils/prompts.js";
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from "./chat_models.js";
|
|
2
|
-
export {
|
|
2
|
+
export { convertPromptToAnthropic } from "./utils/prompts.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports._convertMessagesToAnthropicPayload = exports._convertLangChainToolCallToAnthropic = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* This util file contains functions for converting LangChain messages to Anthropic messages.
|
|
6
6
|
*/
|
|
@@ -167,7 +167,7 @@ function _formatContent(content) {
|
|
|
167
167
|
* @param messages The base messages to format as a prompt.
|
|
168
168
|
* @returns The formatted prompt.
|
|
169
169
|
*/
|
|
170
|
-
function
|
|
170
|
+
function _convertMessagesToAnthropicPayload(messages) {
|
|
171
171
|
const mergedMessages = _mergeMessages(messages);
|
|
172
172
|
let system;
|
|
173
173
|
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === "system") {
|
|
@@ -235,4 +235,4 @@ function _convertMessagesToAnthropic(messages) {
|
|
|
235
235
|
system,
|
|
236
236
|
};
|
|
237
237
|
}
|
|
238
|
-
exports.
|
|
238
|
+
exports._convertMessagesToAnthropicPayload = _convertMessagesToAnthropicPayload;
|
|
@@ -11,4 +11,4 @@ export declare function _convertLangChainToolCallToAnthropic(toolCall: ToolCall)
|
|
|
11
11
|
* @param messages The base messages to format as a prompt.
|
|
12
12
|
* @returns The formatted prompt.
|
|
13
13
|
*/
|
|
14
|
-
export declare function
|
|
14
|
+
export declare function _convertMessagesToAnthropicPayload(messages: BaseMessage[]): AnthropicMessageCreateParams;
|
|
@@ -163,7 +163,7 @@ function _formatContent(content) {
|
|
|
163
163
|
* @param messages The base messages to format as a prompt.
|
|
164
164
|
* @returns The formatted prompt.
|
|
165
165
|
*/
|
|
166
|
-
export function
|
|
166
|
+
export function _convertMessagesToAnthropicPayload(messages) {
|
|
167
167
|
const mergedMessages = _mergeMessages(messages);
|
|
168
168
|
let system;
|
|
169
169
|
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === "system") {
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertPromptToAnthropic = void 0;
|
|
4
|
+
const message_inputs_js_1 = require("./message_inputs.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Convert a formatted LangChain prompt (e.g. pulled from the hub) into
|
|
7
|
+
* a format expected by Anthropic's JS SDK.
|
|
8
|
+
*
|
|
9
|
+
* Requires the "@langchain/anthropic" package to be installed in addition
|
|
10
|
+
* to the Anthropic SDK.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { convertPromptToAnthropic } from "langsmith/utils/hub/anthropic";
|
|
15
|
+
* import { pull } from "langchain/hub";
|
|
16
|
+
*
|
|
17
|
+
* import Anthropic from '@anthropic-ai/sdk';
|
|
18
|
+
*
|
|
19
|
+
* const prompt = await pull("jacob/joke-generator");
|
|
20
|
+
* const formattedPrompt = await prompt.invoke({
|
|
21
|
+
* topic: "cats",
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* const { system, messages } = convertPromptToAnthropic(formattedPrompt);
|
|
25
|
+
*
|
|
26
|
+
* const anthropicClient = new Anthropic({
|
|
27
|
+
* apiKey: 'your_api_key',
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* const anthropicResponse = await anthropicClient.messages.create({
|
|
31
|
+
* model: "claude-3-5-sonnet-20240620",
|
|
32
|
+
* max_tokens: 1024,
|
|
33
|
+
* stream: false,
|
|
34
|
+
* system,
|
|
35
|
+
* messages,
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
* @param formattedPrompt
|
|
39
|
+
* @returns A partial Anthropic payload.
|
|
40
|
+
*/
|
|
41
|
+
function convertPromptToAnthropic(formattedPrompt) {
|
|
42
|
+
const messages = formattedPrompt.toChatMessages();
|
|
43
|
+
const anthropicBody = (0, message_inputs_js_1._convertMessagesToAnthropicPayload)(messages);
|
|
44
|
+
if (anthropicBody.messages === undefined) {
|
|
45
|
+
anthropicBody.messages = [];
|
|
46
|
+
}
|
|
47
|
+
return anthropicBody;
|
|
48
|
+
}
|
|
49
|
+
exports.convertPromptToAnthropic = convertPromptToAnthropic;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { BasePromptValue } from "@langchain/core/prompt_values";
|
|
2
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
3
|
+
/**
|
|
4
|
+
* Convert a formatted LangChain prompt (e.g. pulled from the hub) into
|
|
5
|
+
* a format expected by Anthropic's JS SDK.
|
|
6
|
+
*
|
|
7
|
+
* Requires the "@langchain/anthropic" package to be installed in addition
|
|
8
|
+
* to the Anthropic SDK.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { convertPromptToAnthropic } from "langsmith/utils/hub/anthropic";
|
|
13
|
+
* import { pull } from "langchain/hub";
|
|
14
|
+
*
|
|
15
|
+
* import Anthropic from '@anthropic-ai/sdk';
|
|
16
|
+
*
|
|
17
|
+
* const prompt = await pull("jacob/joke-generator");
|
|
18
|
+
* const formattedPrompt = await prompt.invoke({
|
|
19
|
+
* topic: "cats",
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* const { system, messages } = convertPromptToAnthropic(formattedPrompt);
|
|
23
|
+
*
|
|
24
|
+
* const anthropicClient = new Anthropic({
|
|
25
|
+
* apiKey: 'your_api_key',
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* const anthropicResponse = await anthropicClient.messages.create({
|
|
29
|
+
* model: "claude-3-5-sonnet-20240620",
|
|
30
|
+
* max_tokens: 1024,
|
|
31
|
+
* stream: false,
|
|
32
|
+
* system,
|
|
33
|
+
* messages,
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
* @param formattedPrompt
|
|
37
|
+
* @returns A partial Anthropic payload.
|
|
38
|
+
*/
|
|
39
|
+
export declare function convertPromptToAnthropic(formattedPrompt: BasePromptValue): Anthropic.Messages.MessageCreateParams;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { _convertMessagesToAnthropicPayload } from "./message_inputs.js";
|
|
2
|
+
/**
|
|
3
|
+
* Convert a formatted LangChain prompt (e.g. pulled from the hub) into
|
|
4
|
+
* a format expected by Anthropic's JS SDK.
|
|
5
|
+
*
|
|
6
|
+
* Requires the "@langchain/anthropic" package to be installed in addition
|
|
7
|
+
* to the Anthropic SDK.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { convertPromptToAnthropic } from "langsmith/utils/hub/anthropic";
|
|
12
|
+
* import { pull } from "langchain/hub";
|
|
13
|
+
*
|
|
14
|
+
* import Anthropic from '@anthropic-ai/sdk';
|
|
15
|
+
*
|
|
16
|
+
* const prompt = await pull("jacob/joke-generator");
|
|
17
|
+
* const formattedPrompt = await prompt.invoke({
|
|
18
|
+
* topic: "cats",
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* const { system, messages } = convertPromptToAnthropic(formattedPrompt);
|
|
22
|
+
*
|
|
23
|
+
* const anthropicClient = new Anthropic({
|
|
24
|
+
* apiKey: 'your_api_key',
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* const anthropicResponse = await anthropicClient.messages.create({
|
|
28
|
+
* model: "claude-3-5-sonnet-20240620",
|
|
29
|
+
* max_tokens: 1024,
|
|
30
|
+
* stream: false,
|
|
31
|
+
* system,
|
|
32
|
+
* messages,
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
* @param formattedPrompt
|
|
36
|
+
* @returns A partial Anthropic payload.
|
|
37
|
+
*/
|
|
38
|
+
export function convertPromptToAnthropic(formattedPrompt) {
|
|
39
|
+
const messages = formattedPrompt.toChatMessages();
|
|
40
|
+
const anthropicBody = _convertMessagesToAnthropicPayload(messages);
|
|
41
|
+
if (anthropicBody.messages === undefined) {
|
|
42
|
+
anthropicBody.messages = [];
|
|
43
|
+
}
|
|
44
|
+
return anthropicBody;
|
|
45
|
+
}
|