<!DOCTYPE html>
<html>
<head>
	<title>Chat with GPT</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<style>
		body {
			font-family: Arial, sans-serif;
			margin: 0;
			padding: 0;
			background-color: #f1f1f1;
		}
		h1 {
			text-align: center;
			padding: 20px;
			color: #333;
		}
		.container {
			max-width: 800px;
			margin: 0 auto;
			padding: 20px;
			background-color: #fff;
			box-shadow: 0 0 10px rgba(0,0,0,0.1);
		}
		.message {
			display: flex;
			margin-bottom: 10px;
		}
		.message .user {
			flex: 0 0 100px;
			background-color: #eee;
			padding: 10px;
			border-radius: 10px;
			margin-right: 10px;
			text-align: center;
			font-weight: bold;
			color: #333;
			align-self: flex-start;
			word-wrap: break-word;
			word-break: break-all;
		}
		.message .bot {
			flex: 1;
			background-color: #fff;
			padding: 10px;
			border-radius: 10px;
			box-shadow: 0 0 10px rgba(0,0,0,0.1);
			align-self: flex-start;
			word-wrap: break-word;
			word-break: break-all;
		}
		.input-box {
			display: flex;
			margin-top: 20px;
		}
		.input-box input[type="text"] {
			flex: 1;
			padding: 10px;
			border: none;
			border-radius: 5px;
			box-shadow: 0 0 10px rgba(0,0,0,0.1);
			font-size: 16px;
			color: #333;
		}
		.input-box input[type="submit"] {
			flex: 0 0 100px;
			background-color: #333;
			color: #fff;
			border: none;
			border-radius: 5px;
			padding: 10px;
			font-size: 16px;
			cursor: pointer;
			margin-left: 10px;
		}
		.input-box input[type="submit"]:hover {
			background-color: #555;
		}
	</style>
</head>
<body>
	<h1>Chat with GPT</h1>
	<div class="container">
		<div id="chat-log"></div>
		<form id="chat-form">
			<div class="input-box">
				<input type="text" id="chat-input" placeholder="Type your message here...">
				<input type="submit" value="Send">
			</div>
		</form>
	</div>
	<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
	<script>
		const chatLog = document.getElementById('chat-log');
		const chatForm = document.getElementById('chat-form');
		const chatInput = document.getElementById('chat-input');
<pre><code>	// Function to add message to chat log
	function addMessage(user, message) {
		const messageBox = document.createElement('div');
		messageBox.classList.add('message');
		messageBox.innerHTML = `
			&lt;div class=&quot;user&quot;&gt;${user}&lt;/div&gt;
			&lt;div class=&quot;bot&quot;&gt;${message}&lt;/div&gt;
		`;
		chatLog.appendChild(messageBox);
		chatLog.scrollTop = chatLog.scrollHeight;
	}

	// Function to send message to GPT
	async function sendMessage(message) {
		try {
			const response = await axios.post('https://api.openai.com/v1/engine/davinci-codex/completions', {
				prompt: `User: ${message}\nBot:`,
				max_tokens: 100,
				n: 1,
				stop: '\n',
				model: 'davinci-codex',
				engine: 'davinci-codex'
			}, {
				headers: {
					'Content-Type': 'application/json',
					'Authorization': 'Bearer API_KEY' // Replace API_KEY with your OpenAI API key
				}
			});
			const botMessage = response.data.choices[0].text.trim();
			addMessage('Bot', botMessage);
		} catch (error) {
			console.error(error);
			addMessage('Bot', 'Oops! Something went wrong.');
		}
	}

	// Event listener for chat form submission
	chatForm.addEventListener('submit', event =&gt; {
		event.preventDefault();
		const userMessage = chatInput.value.trim();
		if (userMessage) {
			addMessage('User', userMessage);
			sendMessage(userMessage);
			chatInput.value = '';
		}
	});
&lt;/script&gt;
</code></pre>
</body>
</html>
编写一个网页页面,实现调用openai接口实现chatgpt功能

原文地址: https://www.cveoy.top/t/topic/e3U 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录