-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
100 lines (83 loc) · 3.11 KB
/
run.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
// Can be copy pasted into browser, but consumes tokens every time you close the alert so beware
let apiKey = localStorage.getItem('apiKey');
if (!apiKey) {
apiKey = prompt('Enter your OpenAI API key');
localStorage.setItem('apiKey', apiKey);
}
let systemPrompt = `
Your output will be sent to the JavaScript \`eval\` function.
This means you must only output JavaScript code.
Specifically, you are responding as \`fetchLLMResponse\` in this JavaScript code:
\`\`\`
const history = [{ role: 'system', content: systemPrompt }];
const goal = prompt('What do you want, human?\n');
history.push({ role: 'assistant', content: 'What do you want, human?' });
history.push({ role: 'user', content: goal });
while (true) {
let response = await fetchLLMResponse(systemPrompt, history);
response = response.replace(/\`\`\`javascript|\`\`\`/g, '');
console.log(response);
alert('Press enter to execute above code...');
console.log('-----------------');
history.push({ role: 'assistant', content: response });
let out;
try {
out = eval(response);
console.log(out);
} catch (e) {
console.error(e);
out = e.toString();
}
alert('Press enter to let agent continue...');
console.log('-----------------');
history.push({ role: 'system', content: out });
}
\`\`\`
This means you can:
- ask for input from the user with prompt('...')
- define and access previously defined variables
- append to the history
- call the \`fetchLLMResponse\` function
- access the internet
If something goes wrong, write some comments in the code to think step by step and then try a different approach.
`;
async function fetchLLMResponse(history) {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'gpt-4o',
messages: history,
})
});
const data = await response.json();
return data.choices[0].message.content;
}
(async () => {
const history = [{ role: 'system', content: systemPrompt }];
const goal = prompt('What do you want, human?\n');
history.push({ role: 'assistant', content: 'What do you want, human?' });
history.push({ role: 'user', content: goal });
while (true) {
let response = await fetchLLMResponse(history);
response = response.replace(/```javascript|```/g, '');
console.log(response);
alert('Press enter to execute above code...');
console.log('-----------------');
history.push({ role: 'assistant', content: response });
let out;
try {
out = eval(response);
console.log(out);
} catch (e) {
console.error(e);
out = e.toString();
}
alert('Press enter to let agent continue...');
console.log('-----------------');
history.push({ role: 'system', content: out });
}
})();