-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
58 lines (53 loc) · 2.04 KB
/
Copy pathmain.js
File metadata and controls
58 lines (53 loc) · 2.04 KB
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
const getCompletion = async (messages, model = 'gpt-4', temperature = 1, topProbability = 1) => {
const key = "";
const url = 'https://api.openai.com/v1/chat/completions';
const data = {
messages: messages,
model: model,
temperature: temperature,
top_p: topProbability,
stream: false
};
const completionResponse = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${key}`,
},
body: JSON.stringify(data),
});
const completion = await completionResponse.json();
return completion.choices[0].message.content;
}
document.addEventListener("keydown", async (event) => {
if (event.shiftKey && event.altKey && event.code === "Enter") {
var s = "";
var cells = window.colab.global.notebook.cells;
for (var i = 0; i < cells.length; i++) {
var cell = cells[i];
if (cell.getText().includes("#Collectme")) {
s += "\n\n" + cell.getText();
}
if (cell == window.colab.global.notebook.focusedCell) {
break;
}
}
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("'''Retrieving data...'''");
var messages = [
{
'role': 'system',
'content': text
}
];
var output = (await getCompletion(messages)).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());
}
});