-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathaskOpenAI.js
61 lines (57 loc) · 2.09 KB
/
askOpenAI.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
const askOpenAI = async function (prompt, role, modelChoice = 'gpt-3.5-turbo', tokens=5000, temp=0.85) {
let now = new Date();
// let model = 'gpt-3.5-turbo' // //gpt-4-0314
let roleContent = "You are an ChatGPT-powered chat bot."
if (role == 'machine') {
roleContent = "You are a computer program attempting to comply with the user's wishes."
}
if (role == 'writer') {
roleContent = "You are a professional fiction writer who is a best-selling author. You use all of the rhetorical devices you know to write a compelling book."
}
return new Promise(function(resolve, reject) {
fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
'headers': {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.API_KEY}`,
},
'body': JSON.stringify({
'model': modelChoice,
"messages": [
{"role": "system", "content": roleContent},
{"role": "user", "content": prompt},
],
"max_tokens": tokens,
"temperature": temp,
})
}).then(async response => {
const data = await response.text();
try {
if (JSON.parse(data) && !!data) {
resolve (JSON.parse(data));
return JSON.parse(data);
} else {
resolve ('error')
return 'error';
}
} catch(e) {
console.log(data);
console.log(e);
}
})
.then(async data => {
// console.log(data);
try {
let elapsed = new Date() - now;
console.log('\nOpenAI response time: ' + elapsed + 'ms\n')
resolve (data)
return data;
} catch(e) {
console.log(e);
resolve (e);
return e;
}
})
});
}
exports.askOpenAI = askOpenAI;