-
Notifications
You must be signed in to change notification settings - Fork 0
/
jarvis.js
51 lines (45 loc) · 1.33 KB
/
jarvis.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
(function(ext) {
var queryResult = null;
ext.askJarvis = function(question, callback) {
var xhr = new XMLHttpRequest();
var url = "https://api.openai.com/v1/engines/davinci-codex/completions";
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
queryResult = JSON.parse(xhr.responseText);
callback();
}
};
var data = JSON.stringify({
prompt: "Q: " + question + "\nA:",
max_tokens: 60,
n: 1,
stop: "\n"
});
xhr.send(data);
};
ext.getJarvisAnswer = function() {
if (queryResult) {
var answer = queryResult.choices[0].text.trim();
queryResult = null;
return answer;
}
return "";
};
// Cleanup function when the extension is unloaded
ext._shutdown = function() {};
// Status reporting code
// Use this to report missing hardware, plugin or unsupported browser
ext._getStatus = function() {
return {status: 2, msg: 'Ready'};
};
// Register the extension
var descriptor = {
blocks: [
['w', 'Ask Jarvis %s', 'askJarvis', ''],
['r', 'Jarvis answer', 'getJarvisAnswer']
]
};
ScratchExtensions.register('Ask Jarvis', descriptor, ext);
})({});