פרטי השיעור
שלב 4: יצירת ממשק דמוי ChatGPT
-
צירת תבנית HTML:
בתוך תיקיית
chat/templates/chat
, צור קובץ בשםchat.html
עם התוכן הבא:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>ChatGPT Clone</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #f4f4f4; } .chat-container { width: 100%; max-width: 800px; margin: 50px auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .chat-box { height: 400px; overflow-y: scroll; padding: 10px; border: 1px solid #ddd; border-radius: 4px; margin-bottom: 20px; background-color: #f9f9f9; } .chat-message { margin-bottom: 15px; } .chat-message.user { text-align: right; } .chat-message.bot { text-align: left; } input[type="text"] { width: calc(100% - 100px); padding: 10px; border: 1px solid #ddd; border-radius: 4px; margin-right: 10px; } button { padding: 10px 20px; border: none; border-radius: 4px; background-color: #007bff; color: white; cursor: pointer; } button:hover { background-color: #0056b3; } </style> </head> <body> <div class="chat-container"> <div class="chat-box" id="chat-box"> <!-- הודעות הצ'אט יוכנסו כאן --> </div> <input type="text" id="user-input" placeholder="Type your message..." /> <button onclick="sendMessage()">Send</button> </div> <script> function appendMessage(content, className) { const chatBox = document.getElementById("chat-box"); const messageElement = document.createElement("div"); messageElement.className = "chat-message " + className; messageElement.innerText = content; chatBox.appendChild(messageElement); chatBox.scrollTop = chatBox.scrollHeight; } async function sendMessage() { const userInput = document.getElementById("user-input").value; if (!userInput) return; appendMessage(userInput, "user"); document.getElementById("user-input").value = ""; try { const response = await fetch("/generate_response/", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", }, body: new URLSearchParams({ "prompt": userInput }) }); const data = await response.json(); appendMessage(data.response, "bot"); } catch (error) { appendMessage("Error: Could not retrieve response.", "bot"); } } </script> </body> </html>