É um problema no código? Ou eu não posso ultilizar a api em meu repositório publico, a api no caso é o chatgpt, vi em alguns lugares dizendo que eu deixar a chave em uma variavel de ambiente, porém já está e mesmo assim está acontecendo a mesma coisa.
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 = "chave de acesso";
result.disabled = true
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, // tamanho da resposta
temperature: 0.9, // criatividade na resposta
}),
})
.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 += "Informações encontradas: " + 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 += `Sua pergunta: ${sQuestion}`;
inputQuestion.value = "Carregando...";
inputQuestion.disabled = true;
result.disabled = true
result.scrollTop = result.scrollHeight;
}