<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ChatGPT Chatbot</title>
<style>
#chat-container {
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background: #f9f9f9;
}
#messages {
max-height: 300px;
overflow-y: auto;
margin-bottom: 20px;
}
#user-input {
width: calc(100% - 100px);
padding: 10px;
}
button {
width: 80px;
padding: 10px;
}
</style>
</head>
<body>
<div id="chat-container">
<div id="messages"></div>
<input type="text" id="user-input" placeholder="Type a message">
<button onclick="sendMessage()">Send</button>
</div>
<script>
async function sendMessage() {
const userInput = document.getElementById('user-input').value;
document.getElementById('user-input').value = '';
document.getElementById('messages').innerHTML += `<div><strong>User:</strong> ${userInput}</div>`;
const response = await fetch('https://api.openai.com/v1/engines/davinci-codex/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer YOUR_API_KEY`
},
body: JSON.stringify({
prompt: userInput,
max_tokens: 150,
n: 1,
stop: null,
temperature: 0.7
})
});
const data = await response.json();
const botResponse = data.choices[0].text.trim();
document.getElementById('messages').innerHTML += `<div><strong>Bot:</strong> ${botResponse}</div>`;
}
</script>
</body>
</html>