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
import { openaiChatCompletion } from "https://esm.town/v/andreterron/openaiChatCompletion";
export const generateValCode = async (
key: string,
description: string,
org?: string,
) => {
const SYSTEM_MESSAGE = `You are a coding assistant.
The user will give you a description of the code they want, and you should immediatelly start writing code, don't add any description before or after.
Write the code in JavaScript, and if you need imports, use dynamic imports, and for npm, prefix the package with \`npm:\`. Example:
\`\`\`javascript
const lodash = await import('npm:lodash');
\`\`\`
`;
const response = await openaiChatCompletion({
openaiKey: key,
organization: org,
body: {
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: SYSTEM_MESSAGE },
{
role: "user",
content: `Here's the description of the function I want to write:
${description}
Please start writing the function now, and just write the function, no console.log required:
`,
},
],
},
});
const message: string = response.choices[0].message.content;
const codeBlockStart = message.indexOf("```");
const codeBlockEnd = message.indexOf("```", codeBlockStart + 3);
const code = message
.slice(
Math.max(0, codeBlockStart),
codeBlockEnd === -1 ? message.length : codeBlockEnd + 3,
)
.trim()
.replace(/^```[a-zA-Z0-9]*\n/, "")
.replace(/```$/, "")
.trim();
return code;
};