-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt.js
44 lines (34 loc) · 882 Bytes
/
chatgpt.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
'use strict'
let btn = document.getElementById('btn');
let output = document.getElementById('output');
let input = document.getElementById('input');
btn.onclick = e => {
console.log('click');
const text = input.value;
console.log('Ask for text', text);
const url = 'https://api.openai.com/v1/chat/completions';
const req =
{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": text}]
}
const headers = {
'Content-Type': "application/json",
'Authorization': `Bearer ${CKEY}`
};
fetch(
url,
{ method: 'POST',
headers: headers,
body: JSON.stringify(req)}
).then(
res => res.json()
)
.then(data => {
console.log(data)
console.log(data.choices[0])
console.log(data.choices[0].message.content);
var content = data.choices[0].message.content;
output.value = content;
});
};