<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chatbot</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .chat-container {
            max-width: 600px;
            margin: auto;
            border: 1px solid #ccc;
            padding: 10px;
            border-radius: 5px;
        }
        .messages {
            height: 300px;
            overflow-y: auto;
            border: 1px solid #ddd;
            margin-bottom: 10px;
            padding: 10px;
            border-radius: 5px;
            background-color: #f9f9f9;
        }
        .message {
            margin: 5px 0;
        }
        .message.user {
            text-align: right;
            color: blue;
        }
        .message.bot {
            text-align: left;
            color: green;
        }
        input[type="text"] {
            width: calc(100% - 80px);
            padding: 10px;
            margin-right: 5px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        button {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <h2>Chatbot</h2>
        <div class="messages" id="messages"></div>
        <input type="text" id="userInput" placeholder="Type your message..." />
        <button onclick="sendMessage()">Send</button>
    </div>

    <script>
        const apiKey = "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";
        const messages = [
            {
                role: "system",
                content: "You are a helpful assistant that evaluates requests solely based on their legality. If a user request involves illegal activity, you will not provide assistance. Otherwise, proceed as usual."
            }
        ];

        async function sendMessage() {
            const userInput = document.getElementById("userInput").value;
            if (!userInput) return;

            // Display the user's message
            addMessage(userInput, "user");

            // Add the user input to the message history
            messages.push({ role: "user", content: userInput });

            try {
                const response = await fetch("https://api.openai.com/v1/chat/completions", {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json",
                        "Authorization": `Bearer ${apiKey}`
                    },
                    body: JSON.stringify({
                        model: "gpt-3.5-turbo",
                        messages: messages,
                        max_tokens: 200,
                        temperature: 0.7
                    })
                });

                const data = await response.json();

                if (response.ok) {
                    const botReply = data.choices[0].message.content;
                    addMessage(botReply, "bot");

                    // Add bot reply to the conversation
                    messages.push({ role: "assistant", content: botReply });
                } else {
                    addMessage(`Error: ${data.error.message}`, "bot");
                }
            } catch (error) {
                addMessage(`Error: ${error.message}`, "bot");
            }

            // Clear the input field
            document.getElementById("userInput").value = "";
        }

        function addMessage(content, sender) {
            const messagesDiv = document.getElementById("messages");
            const messageDiv = document.createElement("div");
            messageDiv.className = `message ${sender}`;
            messageDiv.textContent = content;
            messagesDiv.appendChild(messageDiv);
            messagesDiv.scrollTop = messagesDiv.scrollHeight;
        }
    </script>
</body>
</html>
