写一个GPT小程序的聊天页面
时间: 2023-12-19 09:06:42 浏览: 102
以下是一个简单的 GPT 聊天页面的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>GPT Chat</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
.chatbox {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
margin: 20px;
background-color: #fff;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
border-radius: 10px;
max-width: 500px;
min-height: 500px;
}
.chatbox h1 {
margin: 0;
padding: 10px;
font-size: 24px;
text-align: center;
}
.chat {
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: flex-start;
margin: 20px 0;
height: 100%;
overflow-y: scroll;
max-height: 400px;
width: 100%;
}
.chat .message {
display: flex;
flex-direction: column;
align-items: flex-start;
margin-bottom: 10px;
max-width: 80%;
word-wrap: break-word;
padding: 10px;
border-radius: 10px;
background-color: #eee;
}
.chat .message .text {
margin: 0;
font-size: 16px;
line-height: 1.5;
}
.chat .message .time {
margin: 5px 0 0;
font-size: 12px;
color: #999;
}
.form {
display: flex;
flex-direction: column;
align-items: stretch;
width: 100%;
margin-top: auto;
}
.form input[type="text"] {
padding: 10px;
margin: 0;
border: none;
border-radius: 5px;
background-color: #f2f2f2;
font-size: 16px;
line-height: 1.5;
resize: none;
outline: none;
}
.form input[type="submit"] {
padding: 10px;
margin: 10px 0 0;
border: none;
border-radius: 5px;
background-color: #4caf50;
color: #fff;
font-size: 16px;
line-height: 1.5;
cursor: pointer;
outline: none;
}
.form input[type="submit"]:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<div class="chatbox">
<h1>GPT Chat</h1>
<div class="chat">
<div class="message">
<p class="text">Welcome to GPT chat!</p>
<p class="time">12:00 PM</p>
</div>
</div>
<form class="form" onsubmit="sendMessage(event)">
<input type="text" placeholder="Type your message here..." id="messageInput">
<input type="submit" value="Send">
</form>
</div>
<script>
function sendMessage(event) {
event.preventDefault();
var messageInput = document.getElementById('messageInput');
var message = messageInput.value;
if (message.trim()) {
addMessage(message, true);
messageInput.value = '';
sendToGPT(message);
}
}
function addMessage(message, isUser) {
var chat = document.querySelector('.chat');
var messageElement = document.createElement('div');
messageElement.className = 'message';
var textElement = document.createElement('p');
textElement.className = 'text';
textElement.textContent = message;
messageElement.appendChild(textElement);
var timeElement = document.createElement('p');
timeElement.className = 'time';
timeElement.textContent = getTime();
messageElement.appendChild(timeElement);
if (isUser) {
messageElement.classList.add('user');
}
chat.appendChild(messageElement);
chat.scrollTop = chat.scrollHeight;
}
function sendToGPT(message) {
// TODO: Send message to GPT and get response
// Replace this with your GPT API call
var response = 'This is a response from GPT.';
addMessage(response, false);
}
function getTime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var time = hours + ':' + minutes + ' ' + ampm;
return time;
}
</script>
</body>
</html>
```
这个页面包括一个聊天框,一个输入框和一个发送按钮。当用户在输入框中输入消息并点击发送按钮时,页面将调用 `sendMessage` 函数来添加用户的消息到聊天框中,并调用 `sendToGPT` 函数与 GPT 进行交互,获取并添加 GPT 的响应到聊天框中。其中,`addMessage` 函数用于将消息添加到聊天框中,`getTime` 函数用于获取当前时间。需要注意的是,`sendToGPT` 函数需要替换为你的 GPT API 调用代码。
阅读全文