<script>
async function getChatGPTResponse(prompt) {
const apiKey = 'YOUR_OPENAI_API_KEY'; // Replace with your OpenAI API key
const apiUrl = 'https://api.openai.com/v1/chat/completions';
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'gpt-4', // Replace with the appropriate model
messages: [{ role: 'user', content: prompt }]
})
});
const data = await response.json();
return data.choices[0].message.content;
}
// Function to handle form submission
async function handleChatSubmit(event) {
event.preventDefault();
const prompt = document.getElementById('chatPrompt').value;
const response = await getChatGPTResponse(prompt);
document.getElementById('chatResponse').innerText = response;
}
</script>