-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassitente_script.js
More file actions
50 lines (45 loc) · 1.5 KB
/
assitente_script.js
File metadata and controls
50 lines (45 loc) · 1.5 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
const inputQuestion = document.getElementById("inputQuestion");
const result = document.getElementById("result");
inputQuestion.addEventListener("keypress", (e) => {
if (inputQuestion.value && e.key === "Enter") SendQuestion();
});
const OPENAI_API_KEY = "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";
function SendQuestion() {
var sQuestion = inputQuestion.value;
fetch("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer " + OPENAI_API_KEY,
},
body: JSON.stringify({
model: "text-davinci-003",
prompt: sQuestion,
max_tokens: 2048,
temperature: 0.5,
}),
})
.then((response) => response.json())
.then((json) => {
if (result.value) result.value += "\n";
if (json.error?.message) {
result.value += `Error : ${json.error.message} `;
} else if (json.choices?.[0].text) {
var text = json.choices[0].text || "Sem resposta";
result.value += "Chat GPT: " + text;
}
result.scrollTop = result.scrollHeight;
})
.catch((error) => console.error("Error:", error))
.finally(() => {
inputQuestion.value = "";
inputQuestion.disabled = false;
inputQuestion.focus();
});
if (result.value) result.value += "\n \n \n";
result.value += `EU:${sQuestion} `;
inputQuestion.value = "Carregando...";
inputQuestion.disabled = true;
result.scrollTop = result.scrollHeight;
}