diff --git a/script.js b/script.js index bd478b3..6da98be 100644 --- a/script.js +++ b/script.js @@ -38,11 +38,23 @@ function appendMessage(sender, message) { async function getBotResponse(userInput) { try { - // Replace this URL with the actual API endpoint to fetch bot responses - var apiUrl = 'https://example.com/chatbot-api?q=' + encodeURIComponent(userInput); - var response = await fetch(apiUrl); + // Replace 'YOUR_API_KEY' with your actual OpenAI API key + var apiKey = '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'; + var apiUrl = 'https://api.openai.com/v1/completions'; + var response = await fetch(apiUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + apiKey + }, + body: JSON.stringify({ + model: 'text-davinci-003', // You can use other models as well + prompt: userInput, + max_tokens: 100 + }) + }); var data = await response.json(); - return data.response; + return data.choices[0].text.trim(); } catch (error) { console.error("Error fetching bot response:", error); return "Sorry, I couldn't understand that.";