Back to packages list

Vals using @anthropic-ai/sdk

Description from the NPM package:
The official TypeScript library for the Anthropic API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Anthropic from "npm:@anthropic-ai/sdk";
const anthropic = new Anthropic({
// apiKey: 'my_api_key', // defaults to process.env["ANTHROPIC_API_KEY"]
});
// Define a mapping for model shortcuts
const modelMap = {
opus: "claude-3-opus-20240229",
sonnet: "claude-3-sonnet-20240229",
haiku: "claude-3-haiku-20240307",
};
export async function prompt(
text,
{ mode = "text", model = "opus", max_tokens = 1024, messages = [] } = {},
) {
const modelId = modelMap[model] || model;
console.log('modelId: ', modelId);
messages.push({ role: "user", content: text });
let res = await anthropic.messages.create({
model: modelId,
max_tokens,
messages,
});
if (mode == "text") return res.content?.[0].text;
// return {...res.content?.[0].text, model: modelId};
return {...res, model: modelId};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
export let npmExample = (async () => {
const { HUMAN_PROMPT, AI_PROMPT, Client } = await import(
"npm:@anthropic-ai/sdk"
); // The Lodash library exported as ES modules.
const client = new Client(
"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"
);
const prompt = `${HUMAN_PROMPT}
当你的女朋友说“滚, 不要拿AI跟我对话.”。你应该如何安慰她?
${AI_PROMPT}`;
console.log("size of prompt", prompt.length);
const result = await client.complete({
prompt,
stop_sequences: [HUMAN_PROMPT],
max_tokens_to_sample: 8000,
model: "claude-v1.3-100k",
});
console.log(result.completion);
return result;
})();
1
Next