-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.js
430 lines (370 loc) · 10.4 KB
/
base.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import process from "node:process";
import fs from "node:fs/promises";
import path from "node:path";
import { config } from "dotenv";
import meow from "meow";
import { Configuration, OpenAIApi } from "openai";
import ora from "ora";
import { globby } from "globby";
import prettier from "prettier";
const pkg = JSON.parse(await fs.readFile("package.json"));
config();
const { flags } = meow(
`
Usage
$ node <starter file>
Options
-p, --prompt Set the main prompt of the generated code. Default is "extend the code".
-g, --generations Set the number of generations for the generated code. Default is 1.
-P, --persona Set the persona of the generated code. Default is "JavaScript expert, performance expert".
-t, --temperature Set the temperature for the generated code. Default is 0.2.
-c, --clean Set to true if you want to remove any previously generated code. Default is false.
-m, --model Set the model to use for generating the code. Default is "gpt-3.5-turbo".
-n, --negativePrompt Set the negative prompt. Default is "".
-s, --seed Set the seed. Default is -1.
Examples
$ node base-default.js -p "matrix code" -g 3
$ node base-art.js -p "flow field" -g 10 -c -s 123456789
$ node base-game.js -p "arcade game asteroids" -g 5 -n "audio files, images, alert" -P "JavaScript expert, game developer, retro lover"
`,
{
importMeta: import.meta,
flags: {
prompt: {
type: "string",
alias: "p",
default: "extend the code",
},
generations: {
type: "number",
alias: "g",
default: 1,
},
persona: {
type: "string",
alias: "P",
default: "JavaScript expert, performance expert",
},
negativePrompt: {
type: "string",
alias: "n",
default: "",
},
temperature: {
type: "number",
alias: "t",
default: 0.2,
},
seed: {
type: "number",
alias: "s",
default: -1,
},
clean: {
type: "boolean",
alias: "c",
default: false,
},
model: {
type: "string",
alias: "m",
default: "gpt-3.5-turbo",
},
},
}
);
const spinner = ora("Evolving");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
export const openai = new OpenAIApi(configuration);
// Seed is pseudo but can help for less predictive result without adjusting th temperature
const seed = flags.seed < 0 ? Math.round(Math.random() * 100000000) : flags.seed;
const instructions = `
GOAL: ${flags.prompt}${
flags.negativePrompt
? `, ${flags.negativePrompt
.split(",")
.map(negativePrompt => `no ${negativePrompt.trim()}`)
.join(", ")}`
: ""
}
SEED: ${seed}
RULES:
COMPLETE the GOAL
ALWAYS EXTEND or FIX the code
INCREMENT "const generation" ONCE per generation
NEVER use external apis with key
NEVER explain anything
NEVER output markdown
NEVER add imports
EXCLUSIVELY output JavaScript
EXCLUSIVELY use one file
VERY IMPORTANT: the entire answer has to be valid JavaScript
`;
const starter = path.parse(process.argv[1]);
const { base, name } = starter;
const history = [];
export const generations = flags.generations;
let run = 0;
/**
* Generates a new population for the specified generation.
*
* @param {number} generation - The number of the current generation.
* @returns {Promise<void>} - A Promise that resolves once the new generation has been generated.
*/
export async function evolve(generation) {
if (flags.help) {
return;
}
const nextGeneration = generation + 1;
spinner.start(`Generation ${pad(nextGeneration)}`);
try {
const filename = generation === 0 ? base : buildFilename(generation);
const code = await fs.readFile(filename, "utf-8");
// Reduce history length
history.shift();
history.shift();
if (run === 0) {
if (flags.clean) {
// Remove all older generations and prompts
const files = (await globby(["generation-*", "base-*.md", "!base-*.js"])).filter(
file => file > filename
);
await Promise.all(files.map(async file => await fs.unlink(file)));
}
const promptFilename = buildPromptFilename(
generation === 0 ? name : `generation-${pad(generation)}`
);
await fs.writeFile(promptFilename, buildPrompt(base));
// Imply previous message history. Guides te AI and provides the base code
history.push(
{
role: "user",
content: generation === 0 ? "build the initial code" : "continue the code",
},
{
role: "assistant",
content: minify(code),
}
);
}
// Increment the run cycle to allow safe usage of this flag
run++;
// Push the new request
history.push({
role: "user",
content: "continue the code",
});
const completion = await openai.createChatCompletion({
model: flags.model,
messages: [
{
role: "system",
content: `You are a ${flags.persona}. You strictly follow these instructions: ${instructions}`,
},
...history,
],
max_tokens: 2048,
temperature: flags.temperature,
});
const { content } = completion.data.choices[0].message;
// Clean GPT output (might return code block)
const cleanContent = minify(content)
.replace("```javascript", "")
.replace("```js", "")
.replace("```", "")
.trim();
// Test for valid JavaScript
if (isValidJS(cleanContent)) {
history.push({
role: "assistant",
content: cleanContent,
});
const nextFilename = buildFilename(nextGeneration);
await fs.writeFile(nextFilename, prettify(cleanContent));
spinner.succeed(`Generation ${pad(nextGeneration)}`);
await import(`./${nextFilename}`);
} else {
spinner.fail(`Generation ${pad(nextGeneration)}`);
await writeError(nextGeneration, cleanContent);
await handleError(new Error("NOT_JAVASCRIPT"));
}
} catch (error) {
spinner.fail(`Generation ${pad(nextGeneration)}`);
await handleError(error);
}
}
/**
* Pads the given number or string with zeros to a length of 3 characters.
*
* @param {number} n - The input number or string to be padded.
* @returns {string} - The padded string.
*/
export function pad(n) {
return n.toString().padStart(4, "0");
}
/**
* Builds a filename string for the given generation number.
*
* @param {number} currentGeneration - The input generation number.
* @returns {string} - The generated filename string.
*/
export function buildFilename(currentGeneration) {
return path.join(".", `generation-${pad(currentGeneration)}.js`);
}
/**
* Builds an error filename string for the given generation number.
*
* @param {number} currentGeneration - The input generation number.
* @returns {string} - The generated filename string.
*/
export function buildErrorFilename(currentGeneration) {
return path.join(".", `generation-${pad(currentGeneration)}.log`);
}
/**
* Builds a prompt filename string for the base of the generation request.
*
* @param {string} name - name of the file.
* @returns {string} - The generated prompt filename string.
*/
export function buildPromptFilename(name) {
return path.join(".", `${name}.md`);
}
/**
* Builds a formatted string combining the given prompt and optional negative prompt,
* as well as other configuration options such as persona, model, and temperature.
*
* @returns {string} - The formatted string combining the prompts and configuration options.
*/
export function buildPrompt(base) {
return `# Configuration
> Generated by [fail2@${pkg.version}](https://github.com/failfa-st/fail2)
## Command
\`\`\`
node ${base} ${Object.entries(flags)
.map(([key, value]) => {
if (typeof value === "boolean") {
return value ? `--${key}` : "";
}
if (typeof value === "number") {
return `--${key} ${value}`;
}
if (value) {
return `--${key} "${value}"`;
}
})
.join(" ")}
\`\`\`
### Prompt
\`\`\`
${flags.prompt}
\`\`\`
### Negative Prompt
\`\`\`
${flags.negativePrompt}
\`\`\`
### Persona
\`\`\`
${flags.persona}
\`\`\`
### Model
\`\`\`
${flags.model}
\`\`\`
### Temperature
\`\`\`
${flags.temperature}
\`\`\`
### Seed
\`\`\`
${seed}
\`\`\`
`;
}
/**
* Minifies the given code string by removing leading whitespace.
*
* @param {string} code - The input code to be minified.
* @returns {string} - The minified code.
*/
export function minify(code) {
return code.replace(/^\s+/g, "");
}
/**
* Prettifies the given code string using Prettier.
*
* @param {string} code - The input code to be prettified.
* @returns {string} - The prettified code.
*/
export function prettify(code) {
return prettier.format(code, { semi: false, parser: "babel" });
}
/**
* Writes error content to a file.
*
* @param {number} generation - The generation number for the error file.
* @param {string} content - The content to write to the error file.
* @returns {Promise<void>} - A promise that resolves when the write operation is complete.
*/
export async function writeError(generation, content) {
const filename = buildErrorFilename(generation);
await fs.writeFile(filename, content);
}
/**
* Handles errors that occur during the code generation process.
*
* @param {Error} error - The error object containing information about the error.
* @returns {Promise<void>} - A promise that resolves when the error is handled.
*/
export async function handleError(error) {
const message = (
error.response?.data?.error?.message ??
error.message ??
"unknown error"
).trim();
const code = error.response?.status ?? error.code ?? "UNKNOWN_CODE";
if (code === "ERR_MODULE_NOT_FOUND") {
console.error(message);
return;
}
// Errors in the API
if (error.response && code !== 200) {
console.error(`${code}: ${message}`);
if (code === 401) {
console.error(
"Please make sure to use a valid API key and that you have set OPENAI_SECRET in .env"
);
}
return;
}
if (message === "NOT_JAVASCRIPT") {
console.error("The API returned a message that is not valid JavaScript");
return;
}
console.error(error);
throw error;
}
/**
* Writes the given code string to a file, prettifying it before writing.
*
* @param {string} code - The input code to be written to the file.
* @returns {Promise<void>} - A promise that resolves when the file is successfully written.
*/
export async function write(code) {
await fs.writeFile("./project/src/index.js", prettify(code));
}
/**
* Determines whether the input code is valid JavaScript by attempting to format it using Prettier.
*
* @param {string} code - The code to check for validity.
* @returns {boolean} - True if the code is valid JavaScript, false otherwise.
*/
export function isValidJS(code) {
try {
prettier.format(code, { parser: "babel" });
return true;
} catch {
return false;
}
}