Creating a simple web-based version of an AI chatbot similar to GPT-4 requires access to a backend AI service. Below is an HTML template that uses JavaScript to simulate an interaction with the chatbot. Since we can't call OpenAI's GPT-4 directly in this example without an API key and backend server, the following code simply showcases a basic structure for a chat interface.
To create a functional chatbot, the below code would need to be connected to an appropriate backend that can handle requests to an AI model. For demonstration purposes, we will simulate responses.
Here's an HTML/JavaScript example for a "GPT-4o Mini" interface:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GPT-4o Mini</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f2f2f2;
}
#chatContainer {
width: 100%;
max-width: 600px;
margin: auto;
background-color: white;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 20px;
overflow: hidden;
height: 400px;
display: flex;
flex-direction: column;
}
#messages {
flex-grow: 1;
overflow-y: auto;
padding: 10px;
border: 1px solid #e0e0e0;
border-radius: 5px;
margin-bottom: 10px;
}
.userMessage {
text-align: right;
color: blue;
margin: 5px;
}
.botMessage {
text-align: left;
color: green;
margin: 5px;
}
#inputContainer {
display: flex;
}
#userInput {
flex-grow: 1;
padding: 10px;
border: 1px solid #e0e0e0;
border-radius: 5px;
}
#sendButton {
padding: 10px;
background-color: #0284c7;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
#sendButton:hover {
background-color: #0272a6;
}
</style>
</head>
<body>
<div id="chatContainer">
<div id="messages"></div>
<div id="inputContainer">
<input type="text" id="userInput" placeholder="Type your message here..." />
<button id="sendButton">Send</button>
</div>
</div>
<script>
const messagesContainer = document.getElementById('messages');
const userInput = document.getElementById('userInput');
const sendButton = document.getElementById('sendButton');
sendButton.addEventListener('click', sendMessage);
userInput.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
sendMessage();
}
});
function sendMessage() {
const userMessage = userInput.value.trim();
if (userMessage === '') return;
addMessage('user', userMessage);
userInput.value = '';
// Simulate a bot response (Not real AI processing)
setTimeout(() => {
const botResponse = generateBotResponse(userMessage);
addMessage('bot', botResponse);
}, 1000);
}
function addMessage(sender, text) {
const messageDiv = document.createElement('div');
messageDiv.textContent = text;
messageDiv.className = sender === 'user' ? 'userMessage' : 'botMessage';
messagesContainer.appendChild(messageDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight; // Auto-scroll
}
function generateBotResponse(userMessage) {
// Basic simulated responses based on user input
if (userMessage.toLowerCase() === 'hello') {
return 'Hello! How can I assist you today?';
} else if (userMessage.toLowerCase() === 'what is your name?') {
return 'I am GPT-4o Mini, your virtual assistant!';
} else {
return 'Sorry, I do not understand that. Can you please rephrase?';
}
}
</script>
</body>
</html>
How to Use This Code:
- Create an HTML file: Copy the code above into a text editor and save it as
chatbot.html.
- Open in a Browser: Double-click the saved file to open it in a web browser.
- Interact with the Chatbot: Type messages into the input field and hit "Send", or press "Enter". The bot will respond with pre-defined messages.
Note:
This is a simple simulation of a chatbot and not a real implementation of GPT-4. For actual AI interaction, you'd need to implement server-side code to communicate with a real AI service (like OpenAI's API) and handle responses accordingly. Make sure to secure any API keys and respect usage limits based on the service you choose to use.