@langchain/anthropic 0.3.2 → 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/index.cjs +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.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/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";
|
|
@@ -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
|
+
}
|