File size: 941 Bytes
57e16da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { Tool } from './tools/tool';

import { get } from 'svelte/store';
import { OPENAI_API_KEY } from '../store';
import { Configuration, OpenAIApi } from 'openai';
import { generatePrompt } from './promptGeneration';

export async function generateCode(
	prompt: string,
	tools: Tool<any, any>[],
	files: FileList | null
) {
	const fullprompt = generatePrompt(
		prompt,
		tools,
		!!files && files[0].type.startsWith('image'),
		!!files && files[0].type.startsWith('audio')
	);

	const openai = new OpenAIApi(new Configuration({ apiKey: get(OPENAI_API_KEY) }));
	const textAnswer =
		(
			await openai.createCompletion({
				model: 'text-davinci-003',
				prompt: fullprompt,
				max_tokens: 1000
			})
		).data.choices[0].text ?? '';

	const regex = /```(.*?)```/gs;
	const matches = [...textAnswer.matchAll(regex)];

	const codeBlocks = matches.map((match) => match[1]);
	return codeBlocks[0].replace('js\n', '') ?? 'nothing';
}