-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_chatgpt.js
29 lines (29 loc) · 1.12 KB
/
api_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
const CHATGPT_KEY = ""; // Insira entre as aspas sua OPEN AI KEY
const consultaChat = async () => {
let question = document.getElementById('question').value;
document.getElementById('pergunta').innerHTML = question;
await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${CHATGPT_KEY}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo-1106",
messages: [{ role: "user", content: question }],
max_tokens: 1024,
temperature: 0.7
}),
})
.then((response) => response.json())
.then((data) => {
if (data.choices && data.choices.length > 0) {
document.getElementById('resposta').innerHTML = data.choices[0].message.content
} else {
document.getElementById('resposta').innerHTML = "Nenhuma resposta encontrada";
}
})
.catch((error) => {
document.getElementById('resposta').innerHTML = "reformule a pergunta";
});
}