-
Notifications
You must be signed in to change notification settings - Fork 45
/
script.js
47 lines (43 loc) · 1.91 KB
/
script.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
const generate = async (prompt) => {
// Get your API key from storage
const key = "<YOUR OPENAI API KEY>";
const url = 'https://api.openai.com/v1/completions';
// Call completions endpoint
const completionResponse = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${key}`,
},
body: JSON.stringify({
model: 'code-davinci-002',
prompt: prompt,
max_tokens: 1000,
temperature: 0,
stop: ["Question:"]
}),
});
// Select the top choice and send back
const completion = await completionResponse.json();
return completion.choices.pop()["text"];
}
document.addEventListener("keydown", async (event) => {
if (event.shiftKey && event.altKey && event.code === "Enter") {
s = ""
for(var i=0; i<window.colab.global.notebook.cells.length; i++) {
if(window.colab.global.notebook.focusedCell == window.colab.global.notebook.cells[i]) break;
s = "\n\n"+window.colab.global.notebook.cells[i].getText()
}
var prompt = window.colab.global.notebook.focusedCell.getText();
var text = "A solution manual which has FAQs and answers without quotation marks in Python 3 for Google Colab notebooks\n\nQuestion: "+prompt +"\nAppend code to this:"+s+"\nAnswer:";
window.colab.global.notebook.focusedCell.setText("'''\n\nthis may take a few seconds...\n\nprompt: \""+prompt+"\"\n\n'''");
var output = (await generate(text)).trim();
var c = "\n";
var re = new RegExp("^[" + c + "]+|[" + c + "]+$", "g");
output = output.replace(re,"");
c = "`";
re = new RegExp("^[" + c + "]+|[" + c + "]+$", "g");
output = output.replace(re,"");
window.colab.global.notebook.focusedCell.setText(output.trim());
}
});