Skip to content

Commit 7f15792

Browse files
committed
claude-3
1 parent 6f6899f commit 7f15792

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

Claude.mjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import fetch from 'node-fetch';
2+
import fs from 'fs/promises';
3+
import path from 'path';
4+
5+
async function getAnthropicKey() {
6+
const keyPath = path.join(process.env.HOME, '.config', 'anthropic.token');
7+
return (await fs.readFile(keyPath, 'utf8')).trim();
8+
}
9+
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({
20+
model,
21+
messages: [{ role: 'user', content: prompt }],
22+
max_tokens: 1000,
23+
temperature,
24+
stream: debug,
25+
...(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+
}
54+
console.log(); // Add a newline at the end
55+
return result;
56+
} else {
57+
const { content } = await response.json();
58+
return content[0].text;
59+
}
60+
}

holefill.mjs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
2-
32
import * as GPT from './GPT.mjs';
3+
import * as Claude from './Claude.mjs';
44
import process from "process";
55
import fs from 'fs/promises';
66
import os from 'os';
@@ -10,8 +10,6 @@ const system = `
1010
You are a HOLE FILLER. You are provided with a file containing holes, formatted
1111
as '{{HOLE}}'. Your TASK is to answer with a string to replace this hole with.
1212
13-
#################
14-
1513
## EXAMPLE QUERY:
1614
1715
function sum_evens(lim) {
@@ -30,20 +28,21 @@ if (i % 2 === 0) {
3028
sum += i;
3129
}
3230
33-
## NOTICE THE INDENTATION.
34-
## 1. The first line is NOT indented, because there are already spaces before {{LOOP}}.
35-
## 2. The other lines ARE indented, to match the identation of the context.
36-
## THIS IS VERY IMPORTANT.
31+
## NOTICE THE INDENTATION:
32+
33+
1. The first line is NOT indented, because there are already spaces before {{LOOP}}.
34+
35+
2. The other lines ARE indented, to match the identation of the context.
3736
`;
3837

3938
var file = process.argv[2];
4039
var curr = process.argv[3];
41-
var fast = process.argv[4] === "--fast";
40+
var model = process.argv[4] || "gpt-4-0125-preview";
4241

4342
if (!file) {
44-
console.log("Usage: holefill <file> [<shortened_file>]");
43+
console.log("Usage: holefill <file> [<shortened_file>] [<model_name>]");
4544
console.log("");
46-
console.log("This will replace all {{HOLES}} in <file>, using GPT-4.");
45+
console.log("This will replace all {{HOLES}} in <file>, using GPT-4 / Claude-3.");
4746
console.log("A shortened file can be used to omit irrelevant parts.");
4847
process.exit();
4948
}
@@ -68,7 +67,7 @@ while ((match = regex.exec(curr_code)) !== null) {
6867

6968
var tokens = GPT.token_count(curr_code);
7069
var holes = curr_code.match(/{{\w+}}/g) || [];
71-
var model = fast ? "gpt-4-0125-preview" : "gpt-4-32k-0314";
70+
var ask = model.startsWith("claude") ? Claude.ask : GPT.ask;
7271

7372
console.log("holes_found:", holes);
7473
console.log("token_count:", tokens);
@@ -77,7 +76,7 @@ console.log("model_label:", model);
7776
for (let hole of holes) {
7877
console.log("next_filled: " + hole + "...");
7978
var prompt = curr_code + "\nTASK: Fill the {{"+hole+"}} hole. Answer only with the EXACT completion to replace {{"+hole+"}} with. INDENT IT BASED ON THE CONTEXT. DO NOT USE BACKTICKS.";
80-
var answer = await GPT.ask({system, prompt, model});
79+
var answer = await ask({system, prompt, model});
8180
file_code = file_code.replace(hole, answer);
8281
}
8382

0 commit comments

Comments
 (0)