ChatGPT 3.5 Turbo 在线聊天 - 使用 OpenAI API 构建
<!DOCTYPE html>
<html>
<head>
<title>Chat with GPT-3.5 Turbo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
box-sizing: border-box;
}
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
width: 80%;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 2px 5px rgba(0,0,0,0.3);
}
h1 {
text-align: center;
margin-top: 0;
}
form {
display: flex;
flex-direction: column;
align-items: center;
}
input[type="text"],
input[type="submit"] {
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
border: none;
font-size: 16px;
font-weight: bold;
color: #fff;
cursor: pointer;
}
input[type="text"] {
background-color: #f0f0f0;
color: #333;
width: 100%;
max-width: 600px;
height: 50px;
font-size: 18px;
padding-left: 10px;
border: 2px solid #ccc;
border-radius: 5px;
}
input[type="submit"] {
background-color: #007bff;
transition: background-color 0.3s ease;
}
input[type="submit"]:hover {
background-color: #0069d9;
}
.chatbox {
width: 100%;
max-width: 600px;
height: 400px;
margin-top: 20px;
background-color: #f0f0f0;
overflow-y: scroll;
padding: 10px;
border: 2px solid #ccc;
border-radius: 5px;
font-size: 18px;
line-height: 1.5;
}
.chatbox p {
margin: 0;
}
.loading {
display: none;
margin-top: 10px;
font-size: 18px;
font-weight: bold;
color: #333;
text-align: center;
}
.loading::after {
content: "⠋";
animation: spin 0.5s infinite linear;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div class="container">
<h1>Chat with GPT-3.5 Turbo</h1>
<form id="chat-form" onsubmit="return false;">
<input id="chat-input" type="text" placeholder="Type your message here...">
<input type="submit" value="Send">
</form>
<div class="chatbox" id="chatbox"></div>
<div class="loading" id="loading">Loading...</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/@openai/sdk@1.0.0-beta.6/dist/openai.min.js"></script>
<script>
// Set up OpenAI API client
const openai = new OpenAI('YOUR_API_KEY');
<pre><code> // Get DOM elements
const chatForm = document.getElementById('chat-form');
const chatInput = document.getElementById('chat-input');
const chatbox = document.getElementById('chatbox');
const loading = document.getElementById('loading');
// Initialize conversation
let conversationID = null;
// Handle form submission
chatForm.addEventListener('submit', async (event) => {
event.preventDefault();
// Get user input
const userInput = chatInput.value.trim();
// Clear input field
chatInput.value = '';
// Add user's message to chatbox
addMessage('User', userInput);
// Show loading indicator
loading.style.display = 'block';
try {
// Call OpenAI API to generate response
const response = await openai.complete({
engine: 'text-davinci-002',
prompt: `${conversationID ? 'Conversation ID: ' + conversationID + '\n' : ''}User: ${userInput}\nGPT-3.5 Turbo:`,
temperature: 0.7,
maxTokens: 150,
n: 1,
stream: conversationID ? conversationID : undefined
});
// Hide loading indicator
loading.style.display = 'none';
// Get response text
const responseData = response.data[0].text.trim();
// Update conversation ID
conversationID = response.data[0].conversation_id;
// Add GPT-3.5 Turbo's message to chatbox
addMessage('GPT-3.5 Turbo', responseData);
} catch (error) {
console.error(error);
}
});
// Function to add message to chatbox
function addMessage(sender, message) {
const messageElement = document.createElement('p');
messageElement.innerHTML = `<strong>${sender}: </strong>${message}`;
chatbox.appendChild(messageElement);
chatbox.scrollTop = chatbox.scrollHeight;
}
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lBpR 著作权归作者所有。请勿转载和采集!