Public
Script
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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};
}
April 14, 2024