File size: 2,000 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
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
import type { Tool } from './tools/tool';

function toolDescription(tool: Tool<any, any>) {
	let prompt = `	name: ${tool.name} \n	description: ${tool.description}`;

	const examples = tool.examples.slice(0, 1).map((example) => {
		return `	prompt: ${example.prompt} \n	command generated: \`${example.command}\``;
	});

	prompt += `\n` + examples.join('\n');

	return prompt;
}

export function generatePrompt(
	prompt: string,
	tools: Tool<any, any>[],
	image?: boolean,
	audio?: boolean
) {
	if (tools.length === 0) {
		throw new Error('no tools selected');
	}

	let params = '';

	if (image) {
		params += `image`;
	}
	if (audio) {
		params += params ? ',' : '';
		params += `audio`;
	}

	// describe all the tools
	const fullPrompt = `
Create a function that does the following: ${prompt}. 

Examples:
For the prompt: "Caption the image and give me the caption read out loud."
\`\`\`js
async function generate(image) {
	const caption = await imageToText(image);
	message("First we caption the image", caption);
	const output = await textToSpeech(caption);
	message("Then we read the caption out loud", output);
	return output;
};
\`\`\`

For the prompt "Display an image of a yellow dog wearing a top hat"
\`\`\`js
async function generate() {
	const output = await textToImage("yellow dog wearing a top hat");
	message("We generate the dog picture", output);
	return output;
}
\`\`\`

For the prompt "transcribe the attached audio"

\`\`\`js
async function generate(audio) {
	const output = await speechToText(audio)
	message("We read the text", output);
	return output;
}
\`\`\`

In order to help in answering the above prompt, the function has access to the following methods to generate outputs.
${tools.map((tool) => toolDescription(tool)).join('\n\n ')}

Use the above methods and only the above methods to answer the prompt: ${prompt}.

It must match the following signature:
\`\`\`js
async function generate(${params}}) {
// your code here
return output;
};
\`\`\``;

	return fullPrompt;
}