-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathClaude.mjs
33 lines (31 loc) · 1.12 KB
/
Claude.mjs
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
31
32
33
import { Anthropic } from '@anthropic-ai/sdk';
import fs from 'fs/promises';
import path from 'path';
async function getAnthropicKey() {
const keyPath = path.join(process.env.HOME, '.config', 'anthropic.token');
return (await fs.readFile(keyPath, 'utf8')).trim();
}
export async function ask({ system, prompt, max_tokens, model = 'claude-3-opus-20240229', temperature = 1, debug = true }) {
const anthropic = new Anthropic({ apiKey: await getAnthropicKey() });
if (debug) {
const stream = anthropic.messages.stream({
model,
messages: [{ role: 'user', content: prompt }],
max_tokens: max_tokens || 4096,
temperature,
...(system && { system }),
}).on('text', (text) => process.stdout.write(text));
const message = await stream.finalMessage();
console.log(); // Add a newline at the end
return message.content[0].text;
} else {
const message = await anthropic.messages.create({
model,
messages: [{ role: 'user', content: prompt }],
max_tokens: max_tokens || 4096,
temperature,
...(system && { system }),
});
return message.content[0].text;
}
}