async function sendMessage() { const userInput = document.getElementById("userInput").value.trim(); if (!userInput) return;
const chatBox = document.getElementById("chatBox"); chatBox.innerHTML += `
`; document.getElementById("userInput").value = "";
try { const response = await fetch("https://api.openai.com/v1/chat/completions", { method: "POST", headers: { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY_HERE" }, body: JSON.stringify({ model: "gpt-3.5-turbo", messages: [{ role: "user", content: userInput }] }) });
if (!response.ok) { throw new Error(`API Error: ${response.status}`); }
const data = await response.json(); const botMessage = data.choices?.[0]?.message?.content || "Sorry, I couldn't process your request.";
chatBox.innerHTML += `
`; chatBox.scrollTop = chatBox.scrollHeight; } catch (error) { chatBox.innerHTML += `
`;
}
}