mirror of
https://github.com/searxng/searxng
synced 2024-01-01 19:24:07 +01:00
Create gpt.js
This commit is contained in:
parent
c24913c3f7
commit
e487f6d733
1 changed files with 44 additions and 0 deletions
44
searx/static/themes/simple/js/gpt.js
Normal file
44
searx/static/themes/simple/js/gpt.js
Normal 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;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue