Skip to content

Commit 53fec29

Browse files
committed
use anthropic's sdk instead of gambiarra
1 parent 7f15792 commit 53fec29

File tree

4 files changed

+2671
-82
lines changed

4 files changed

+2671
-82
lines changed

Claude.mjs

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from 'node-fetch';
1+
import { Anthropic } from '@anthropic-ai/sdk';
22
import fs from 'fs/promises';
33
import path from 'path';
44

@@ -7,54 +7,27 @@ async function getAnthropicKey() {
77
return (await fs.readFile(keyPath, 'utf8')).trim();
88
}
99

10-
export async function ask({ system, prompt, model = 'claude-3-opus-20240229', temperature = 1, debug = true }) {
11-
const anthropicKey = await getAnthropicKey();
12-
const response = await fetch('https://api.anthropic.com/v1/messages', {
13-
method: 'POST',
14-
headers: {
15-
'Content-Type': 'application/json',
16-
'X-API-Key': anthropicKey,
17-
'anthropic-version': '2023-06-01',
18-
},
19-
body: JSON.stringify({
10+
export async function ask({ system, prompt, max_tokens, model = 'claude-3-opus-20240229', temperature = 1, debug = true }) {
11+
const anthropic = new Anthropic({ apiKey: await getAnthropicKey() });
12+
if (debug) {
13+
const stream = anthropic.messages.stream({
2014
model,
2115
messages: [{ role: 'user', content: prompt }],
22-
max_tokens: 1000,
16+
max_tokens: max_tokens || 4096,
2317
temperature,
24-
stream: debug,
2518
...(system && { system }),
26-
}),
27-
});
28-
29-
if (!response.ok) {
30-
const errorBody = await response.text();
31-
throw new Error(`Anthropic API request failed with status ${response.status}: ${errorBody}`);
32-
}
33-
34-
if (debug) {
35-
let result = '';
36-
for await (const chunk of response.body) {
37-
const textChunk = new TextDecoder().decode(chunk);
38-
const lines = textChunk.split('\n');
39-
for (const line of lines) {
40-
if (line.startsWith('data:')) {
41-
try {
42-
const data = JSON.parse(line.slice(5));
43-
if (data.type === 'content_block_delta' && data.delta.type === 'text_delta') {
44-
process.stdout.write(data.delta.text);
45-
result += data.delta.text;
46-
}
47-
} catch (error) {
48-
// Skip the line if JSON parsing fails
49-
console.error('Error parsing JSON:', error.message);
50-
}
51-
}
52-
}
53-
}
19+
}).on('text', (text) => process.stdout.write(text));
20+
const message = await stream.finalMessage();
5421
console.log(); // Add a newline at the end
55-
return result;
22+
return message.content;
5623
} else {
57-
const { content } = await response.json();
58-
return content[0].text;
24+
const message = await anthropic.messages.create({
25+
model,
26+
messages: [{ role: 'user', content: prompt }],
27+
max_tokens: max_tokens || 4096,
28+
temperature,
29+
...(system && { system }),
30+
});
31+
return message.content;
5932
}
6033
}

holefill.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ if (i % 2 === 0) {
3232
3333
1. The first line is NOT indented, because there are already spaces before {{LOOP}}.
3434
35-
2. The other lines ARE indented, to match the identation of the context.
35+
2. The other lines ARE indented, to match the indentation of the context.
3636
`;
3737

3838
var file = process.argv[2];
@@ -65,6 +65,8 @@ while ((match = regex.exec(curr_code)) !== null) {
6565
}
6666
}
6767

68+
await fs.writeFile(curr, curr_code, 'utf-8');
69+
6870
var tokens = GPT.token_count(curr_code);
6971
var holes = curr_code.match(/{{\w+}}/g) || [];
7072
var ask = model.startsWith("claude") ? Claude.ask : GPT.ask;

0 commit comments

Comments
 (0)