<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ChatGPT AI</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
<pre><code>    .container {
        max-width: 800px;
        margin: 0 auto;
        padding: 20px;
    }
    
    .chat-container {
        background-color: #f5f5f5;
        border-radius: 10px;
        padding: 20px;
        margin-bottom: 20px;
    }
    
    .user-message {
        background-color: #eaf6ff;
        border-radius: 10px;
        padding: 10px;
        margin-bottom: 10px;
    }
    
    .bot-message {
        background-color: #efffef;
        border-radius: 10px;
        padding: 10px;
        margin-bottom: 10px;
    }
    
    .input-container {
        display: flex;
    }
    
    .input-container input {
        flex: 1;
        padding: 10px;
        border-radius: 5px;
        border: none;
        margin-right: 10px;
    }
    
    .input-container button {
        padding: 10px 20px;
        background-color: #4CAF50;
        border: none;
        color: white;
        border-radius: 5px;
        cursor: pointer;
    }
    
    @media (max-width: 600px) {
        .container {
            max-width: 100%;
            padding: 10px;
        }
    }
&lt;/style&gt;
</code></pre>
</head>
<body>
    <div class="container">
        <h1>ChatGPT AI</h1>
        <div class="chat-container" id="chatContainer"></div>
        <div class="input-container">
            <input type="text" id="userInput" placeholder="请输入消息">
            <button onclick="sendMessage()">发送</button>
        </div>
    </div>
<pre><code>&lt;script&gt;
    function appendUserMessage(message) {
        const chatContainer = document.getElementById('chatContainer');
        const userMessageElement = document.createElement('div');
        userMessageElement.classList.add('user-message');
        userMessageElement.innerText = message;
        chatContainer.appendChild(userMessageElement);
    }

    function appendBotMessage(message) {
        const chatContainer = document.getElementById('chatContainer');
        const botMessageElement = document.createElement('div');
        botMessageElement.classList.add('bot-message');
        botMessageElement.innerText = message;
        chatContainer.appendChild(botMessageElement);
    }

    function sendMessage() {
        const userInput = document.getElementById('userInput');
        const message = userInput.value.trim();

        if (message) {
            appendUserMessage(message);
            userInput.value = '';

            // 调用ChatGPT AI的API发送用户消息并获取回复
            const apiUrl = 'https://api.openai.com/v1/chat/completions';
            const apiKey = 'YOUR_API_KEY';
            
            fetch(apiUrl, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${apiKey}`
                },
                body: JSON.stringify({
                    &quot;model&quot;: &quot;gpt-3.5-turbo&quot;,
                    &quot;messages&quot;: [
                        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are a helpful assistant.&quot;},
                        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: message}
                    ]
                })
            })
            .then(response =&gt; response.json())
            .then(data =&gt; {
                const botMessage = data.choices[0].message.content;
                appendBotMessage(botMessage);
            })
            .catch(error =&gt; {
                console.error('Error:', error);
            });
        }
    }
&lt;/script&gt;
</code></pre>
</body>
</html>
中文版 ChatGPT AI 网站源码:移动端自适应 | 免费在线聊天机器人

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

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