Create gpt.js

This commit is contained in:
RecentRichRail 2023-03-28 12:37:56 -04:00 committed by GitHub
parent c24913c3f7
commit e487f6d733
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,44 @@
document.addEventListener('DOMContentLoaded', () => {
const chatgptApiKey = '{{ chatgpt_api_key_var }}';
const query = '{{ q }}';
const gptResultsContent = document.getElementById('gptResultsContent');
if (chatgptApiKey && query && gptResultsContent) {
fetchGPTResults(chatgptApiKey, query).then((results) => {
if (results) {
gptResultsContent.innerHTML = results;
}
});
}
});
async function fetchGPTResults(apiKey, query) {
const apiEndpoint = 'https://api.openai.com/v1/engines/davinci-codex/completions';
const prompt = `Generate a brief summary for the following search query: ${query}`;
try {
const response = await fetch(apiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify({
prompt: prompt,
max_tokens: 50,
n: 1,
stop: null,
temperature: 0.5,
}),
});
const data = await response.json();
if (data.choices && data.choices.length > 0) {
return data.choices[0].text.trim();
}
} catch (error) {
console.error('Error fetching GPT-3.5 Turbo results:', error);
}
return null;
}