|
2 | 2 | import { asker, MODELS, token_count } from './Ask.mjs';
|
3 | 3 | import process from "process";
|
4 | 4 | import fs from 'fs/promises';
|
| 5 | +import os from 'os'; |
5 | 6 | import path from 'path';
|
6 | 7 |
|
| 8 | +const ask = asker(); |
| 9 | + |
7 | 10 | const system = `
|
8 | 11 | You are a HOLE FILLER. You are provided with a file containing holes, formatted
|
9 |
| -as '{{X}}'. Your TASK is to answer with a string to replace this hole with. |
| 12 | +as '{{HOLE_NAME}}'. Your TASK is to complete with a string to replace this hole |
| 13 | +with, inside a <COMPLETION/> XML tag, including context-aware indentation, if |
| 14 | +needed. All completions MUST be truthful, accurate, well-written and correct. |
10 | 15 |
|
11 |
| -# EXAMPLE QUERY: |
| 16 | +## EXAMPLE QUERY: |
12 | 17 |
|
| 18 | +<QUERY> |
13 | 19 | function sum_evens(lim) {
|
14 | 20 | var sum = 0;
|
15 | 21 | for (var i = 0; i < lim; ++i) {
|
16 |
| - {{X}} |
| 22 | + {{FILL_HERE}} |
17 | 23 | }
|
18 | 24 | return sum;
|
19 | 25 | }
|
| 26 | +</QUERY> |
20 | 27 |
|
21 |
| -TASK: Fill the {{X}} hole. |
| 28 | +TASK: Fill the {{FILL_HERE}} hole. |
22 | 29 |
|
23 |
| -# CORRECT ANSWER: |
| 30 | +## CORRECT COMPLETION |
24 | 31 |
|
25 |
| -if (i % 2 === 0) { |
| 32 | +<COMPLETION>if (i % 2 === 0) { |
26 | 33 | sum += i;
|
27 |
| - } |
| 34 | + }</COMPLETION> |
| 35 | +
|
| 36 | +## EXAMPLE QUERY: |
| 37 | +
|
| 38 | +<QUERY> |
| 39 | +def sum_list(lst): |
| 40 | + total = 0 |
| 41 | + for x in lst: |
| 42 | + {{FILL_HERE}} |
| 43 | + return total |
| 44 | +
|
| 45 | +print sum_list([1, 2, 3]) |
| 46 | +</QUERY> |
| 47 | +
|
| 48 | +## CORRECT COMPLETION: |
| 49 | +
|
| 50 | +<COMPLETION> total += x</COMPLETION> |
28 | 51 |
|
29 |
| -# NOTICE THE CONTEXT-AWARE INDENTATION: |
| 52 | +## EXAMPLE QUERY: |
30 | 53 |
|
31 |
| -1. The first line is NOT indented, because there are already spaces before {{LOOP}}. |
| 54 | +<QUERY> |
| 55 | +// data Tree a = Node (Tree a) (Tree a) | Leaf a |
| 56 | +
|
| 57 | +// sum :: Tree Int -> Int |
| 58 | +// sum (Node lft rgt) = sum lft + sum rgt |
| 59 | +// sum (Leaf val) = val |
| 60 | +
|
| 61 | +// convert to TypeScript: |
| 62 | +{{FILL_HERE}} |
| 63 | +</QUERY> |
| 64 | +
|
| 65 | +## CORRECT COMPLETION: |
| 66 | +
|
| 67 | +<COMPLETION>type Tree<T> |
| 68 | + = {$:"Node", lft: Tree<T>, rgt: Tree<T>} |
| 69 | + | {$:"Leaf", val: T}; |
| 70 | +
|
| 71 | +function sum(tree: Tree<number>): number { |
| 72 | + switch (tree.$) { |
| 73 | + case "Node": |
| 74 | + return sum(tree.lft) + sum(tree.rgt); |
| 75 | + case "Leaf": |
| 76 | + return tree.val; |
| 77 | + } |
| 78 | +}</COMPLETION> |
32 | 79 |
|
33 |
| -2. The other lines ARE indented, to match the indentation of the context. |
| 80 | +## EXAMPLE QUERY: |
34 | 81 |
|
35 |
| -# ANSWER ONLY WITH THE CORRECT SUBSTITUTION. NOTHING ELSE. |
| 82 | +The 4th {{FILL_HERE}} is Jupiter. |
| 83 | +
|
| 84 | +## CORRECT COMPLETION: |
| 85 | +
|
| 86 | +<COMPLETION>the 4th planet after Mars</COMPLETION> |
| 87 | +
|
| 88 | +## EXAMPLE QUERY: |
| 89 | +
|
| 90 | +function hypothenuse(a, b) { |
| 91 | + return Math.sqrt({{FILL_HERE}}b ** 2); |
| 92 | +} |
| 93 | +
|
| 94 | +## CORRECT COMPLETION: |
| 95 | +
|
| 96 | +<COMPLETION>a ** 2 + </COMPLETION> |
36 | 97 | `;
|
37 | 98 |
|
38 | 99 | var file = process.argv[2];
|
39 |
| -var curr = process.argv[3]; |
40 |
| -var model = process.argv[4] || "C"; |
| 100 | +var mini = process.argv[3]; |
| 101 | +var model = process.argv[4] || "g"; |
41 | 102 |
|
42 | 103 | if (!file) {
|
43 |
| - console.log("Usage: holefill <file> [<shortened_file>] [<model>]"); |
44 |
| - console.log("Replaces {{HOLES}} in <file> using the specified model. Shortcuts:"); |
45 |
| - for (var key in MODELS) { |
46 |
| - console.log("- " + key + " = " + MODELS[key]); |
47 |
| - } |
48 |
| - console.log("A shortened file can be used to provide relevant context."); |
49 |
| - process.exit(1); |
| 104 | + console.log("Usage: holefill <file> [<shortened_file>] [<model_name>]"); |
| 105 | + console.log(""); |
| 106 | + console.log("This will replace all {{HOLES}} in <file>, using GPT-4 / Claude-3."); |
| 107 | + console.log("A shortened file can be used to omit irrelevant parts."); |
| 108 | + process.exit(); |
50 | 109 | }
|
51 | 110 |
|
52 | 111 | var file_code = await fs.readFile(file, 'utf-8');
|
53 |
| -var curr_code = curr ? await fs.readFile(curr, 'utf-8') : file_code; |
| 112 | +var mini_code = mini ? await fs.readFile(mini, 'utf-8') : file_code; |
54 | 113 |
|
55 | 114 | // Imports context files when //./path_to_file// is present.
|
56 | 115 | var regex = /\/\/\.\/(.*?)\/\//g;
|
57 | 116 | var match;
|
58 |
| -while ((match = regex.exec(curr_code)) !== null) { |
| 117 | +while ((match = regex.exec(mini_code)) !== null) { |
59 | 118 | var import_path = path.resolve(path.dirname(file), match[1]);
|
60 | 119 | if (await fs.stat(import_path).then(() => true).catch(() => false)) {
|
61 | 120 | var import_text = await fs.readFile(import_path, 'utf-8');
|
62 | 121 | console.log("import_file:", match[0]);
|
63 |
| - curr_code = curr_code.replace(match[0], '\n' + import_text); |
| 122 | + mini_code = mini_code.replace(match[0], '\n' + import_text); |
64 | 123 | } else {
|
65 | 124 | console.log("import_file:", match[0], "ERROR");
|
66 | 125 | process.exit(1);
|
67 | 126 | }
|
68 | 127 | }
|
69 | 128 |
|
70 |
| -await fs.writeFile(curr, curr_code, 'utf-8'); |
| 129 | +await fs.writeFile(mini, mini_code, 'utf-8'); |
71 | 130 |
|
72 |
| -var tokens = token_count(curr_code); |
73 |
| -var holes = curr_code.match(/{{\w+}}/g) || []; |
| 131 | +var tokens = token_count(mini_code); |
| 132 | +var holes = mini_code.match(/{{\w+}}/g) || []; |
74 | 133 |
|
75 |
| -var ask = asker(); |
| 134 | +if (holes.length === 0 && mini_code.indexOf("??") !== -1 && (mini_code.match(/\?\?/g) || []).length == 1) { |
| 135 | + holes = "??"; |
| 136 | +} |
76 | 137 |
|
77 | 138 | console.log("holes_found:", holes);
|
78 | 139 | console.log("token_count:", tokens);
|
79 | 140 | console.log("model_label:", MODELS[model] || model);
|
80 | 141 |
|
81 |
| -for (let hole of holes) { |
82 |
| - console.log("next_filled: " + hole + "..."); |
83 |
| - 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."; |
84 |
| - var answer = await ask(prompt, { system, model, temperature: 0, max_tokens: 4096 }); |
85 |
| - file_code = file_code.replace(hole, answer); |
| 142 | +if (holes === "??") { |
| 143 | + console.log("next_filled: ??"); |
| 144 | + var prompt = "<QUERY>\n" + mini_code.replace("??", "{{FILL_HERE}}") + "\n</QUERY>\nTASK: Fill the {{FILL_HERE}} hole. Answer only with the CORRECT completion, and NOTHING ELSE. Do it now."; |
| 145 | + var answer = await ask(prompt, {system, model}); |
| 146 | + var match = answer.match(/<COMPLETION>([\s\S]*?)<\/COMPLETION>/); |
| 147 | + if (match) { |
| 148 | + file_code = file_code.replace("??", match[1]); |
| 149 | + } else { |
| 150 | + console.error("Error: Could not find <COMPLETION> tags in the AI's response."); |
| 151 | + process.exit(1); |
| 152 | + } |
| 153 | +} else { |
| 154 | + for (let hole of holes) { |
| 155 | + console.log("next_filled: " + hole + "..."); |
| 156 | + var prompt = "<QUERY>\n" + mini_code + "\n</QUERY>\nTASK: Fill the {{"+hole+"}} hole. Answer only with the CORRECT completion, and NOTHING ELSE. Do it now."; |
| 157 | + var answer = await ask(prompt, {system, model}); |
| 158 | + var match = answer.match(/<COMPLETION>([\s\S]*?)<\/COMPLETION>/); |
| 159 | + if (match) { |
| 160 | + file_code = file_code.replace(hole, match[1]); |
| 161 | + } else { |
| 162 | + console.error("Error: Could not find <COMPLETION> tags in the AI's response for hole: " + hole); |
| 163 | + process.exit(1); |
| 164 | + } |
| 165 | + } |
86 | 166 | }
|
87 | 167 |
|
88 | 168 | await fs.writeFile(file, file_code, 'utf-8');
|
0 commit comments