create a beautiful html webpage with chat interface on the left and code block interface…
- User
create a beautiful html webpage with chat interface on the left and code block interface on the right. the chatbot uses the below api details for understanding and outputting user responses. make sure to properly format the chat bubbles. API details are POST /api/query_responder Generates complete responses to queries across various topics, mimicking a chosen LLM model's output. Overview: This API accepts diverse queries and produces comprehensive responses, including code outputs that can be rendered in HTML format. Input: This API accepts both structured parameters and free-form text. The system will interpret your input contextually. { "query": "How do I create a responsive webpage?", "context": "User is looking for front-end development help." } Output: The API returns a JSON object tailored to your input and the WebSim context. Here's a sample output: { "response": "Here’s a simple responsive webpage example:", "code": "\n\n\n \n \n Responsive Webpage\n \n body { font-family: Arial, sans-serif; } \n .container { max-width: 1200px; margin: auto; padding: 20px; } \n @media (max-width: 600px) { \n .container { padding: 10px; } \n }\n \n\n\n \n Hello World!\n This is a responsive webpage example.\n \n\n" } This API adapts to your input and the WebSim context, providing relevant JSON responses. AI Prompt Note: This API endpoint (/api/query_responder) uses advanced techniques to understand and respond to queries. It's aware of the WebSim context and can handle both structured and unstructured input. Additional Context: If the output involves code, it will be provided in a codebox, with options for code and preview modes.
the chat should retain context from the previous code output, and when asked to enhance code (if asked), it should include changes in relevant parts of code instead of completely rewriting the code. within the main page, also implement dark mode and light mode buttons. give appropriate icons where requried.
- Assistant
Certainly! Below is a complete HTML page that features a beautiful chat interface on the left and a code block interface on the right. It includes dark mode and light mode toggles with appropriate icons. The chatbot interacts with the provided
/api/query_responderAPI to handle user queries and display responses, retaining context for code enhancements.Features:
- Responsive Layout: Utilizes Flexbox to create a responsive two-column layout.
- Chat Interface: Styled chat bubbles for both user and bot messages.
- Code Block Interface: Displays code with syntax highlighting using Highlight.js.
- Theme Toggle: Switch between dark and light modes with icons.
- Context Retention: Maintains conversation history to provide context-aware responses.
Dependencies:
- Font Awesome: For icons.
- Highlight.js: For syntax highlighting in code blocks.
Instructions:
- Ensure that the
/api/query_responderendpoint is correctly set up to handle POST requests as described. - Place this HTML file in your project directory and open it in a web browser.
- Interact with the chatbot on the left, and view or modify code on the right.
Here's the complete HTML code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Chat & Code Interface</title> <!-- Font Awesome for Icons --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <!-- Highlight.js for Code Syntax Highlighting --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/default.min.css"> <style> /* Root Variables for Themes */ :root { --bg-color: #ffffff; --text-color: #000000; --chat-bg: #f1f1f1; --user-color: #0084ff; --bot-color: #e5e5ea; --input-bg: #ffffff; --button-bg: #0084ff; --button-color: #ffffff; } [data-theme="dark"] { --bg-color: #121212; --text-color: #ffffff; --chat-bg: #1e1e1e; --user-color: #0a84ff; --bot-color: #2c2c2e; --input-bg: #2c2c2e; --button-bg: #0a84ff; --button-color: #ffffff; } body { margin: 0; font-family: Arial, sans-serif; background-color: var(--bg-color); color: var(--text-color); height: 100vh; display: flex; flex-direction: column; } header { padding: 10px 20px; background-color: var(--chat-bg); display: flex; justify-content: space-between; align-items: center; } header h1 { font-size: 1.5rem; margin: 0; } .theme-toggle { cursor: pointer; font-size: 1.2rem; } main { flex: 1; display: flex; overflow: hidden; } .chat-container, .code-container { padding: 20px; height: 100%; overflow-y: auto; } .chat-container { width: 40%; background-color: var(--chat-bg); display: flex; flex-direction: column; } .messages { flex: 1; margin-bottom: 10px; } .message { max-width: 80%; margin-bottom: 10px; padding: 10px 15px; border-radius: 15px; position: relative; word-wrap: break-word; } .message.user { background-color: var(--user-color); color: #fff; align-self: flex-end; border-bottom-right-radius: 0; } .message.bot { background-color: var(--bot-color); color: #000; align-self: flex-start; border-bottom-left-radius: 0; } .input-container { display: flex; } .input-container input { flex: 1; padding: 10px; border: none; border-radius: 20px; background-color: var(--input-bg); color: var(--text-color); outline: none; } .input-container button { margin-left: 10px; padding: 10px 15px; border: none; border-radius: 50%; background-color: var(--button-bg); color: var(--button-color); cursor: pointer; font-size: 1rem; } .code-container { width: 60%; background-color: var(--chat-bg); display: flex; flex-direction: column; } .code-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; } .code-header h2 { margin: 0; font-size: 1.2rem; } .code-actions button { margin-left: 5px; padding: 5px 10px; border: none; border-radius: 5px; background-color: var(--button-bg); color: var(--button-color); cursor: pointer; font-size: 0.9rem; } pre { background-color: #2d2d2d; color: #ccc; padding: 15px; border-radius: 5px; overflow-x: auto; flex: 1; } /* Scrollbar Styling */ ::-webkit-scrollbar { width: 8px; } ::-webkit-scrollbar-track { background: var(--chat-bg); } ::-webkit-scrollbar-thumb { background-color: var(--user-color); border-radius: 4px; } /* Responsive Adjustments */ @media (max-width: 768px) { main { flex-direction: column; } .chat-container, .code-container { width: 100%; height: 50%; } } </style> </head> <body data-theme="light"> <header> <h1>Chat & Code Interface</h1> <div class="theme-toggle" id="theme-toggle" title="Toggle Dark/Light Mode"> <i class="fas fa-moon"></i> </div> </header> <main> <!-- Chat Interface --> <div class="chat-container"> <div class="messages" id="messages"> <!-- Messages will appear here --> </div> <div class="input-container"> <input type="text" id="user-input" placeholder="Type your message here..."> <button id="send-button" title="Send Message"><i class="fas fa-paper-plane"></i></button> </div> </div> <!-- Code Block Interface --> <div class="code-container"> <div class="code-header"> <h2>Code Preview</h2> <div class="code-actions"> <button id="copy-button" title="Copy Code"><i class="fas fa-copy"></i></button> <button id="refresh-button" title="Refresh Preview"><i class="fas fa-sync"></i></button> </div> </div> <pre><code id="code-block" class="html"></code></pre> </div> </main> <!-- Highlight.js Library --> <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script> <script> // Initialize Highlight.js hljs.highlightAll(); // Theme Toggle const themeToggle = document.getElementById('theme-toggle'); const body = document.body; themeToggle.addEventListener('click', () => { if (body.getAttribute('data-theme') === 'light') { body.setAttribute('data-theme', 'dark'); themeToggle.innerHTML = '<i class="fas fa-sun"></i>'; } else { body.setAttribute('data-theme', 'light'); themeToggle.innerHTML = '<i class="fas fa-moon"></i>'; } }); // Chat Functionality const sendButton = document.getElementById('send-button'); const userInput = document.getElementById('user-input'); const messagesContainer = document.getElementById('messages'); const codeBlock = document.getElementById('code-block'); const copyButton = document.getElementById('copy-button'); const refreshButton = document.getElementById('refresh-button'); let conversation = []; sendButton.addEventListener('click', sendMessage); userInput.addEventListener('keypress', function(e) { if (e.key === 'Enter') { sendMessage(); } }); function appendMessage(content, sender) { const messageDiv = document.createElement('div'); messageDiv.classList.add('message', sender); messageDiv.innerHTML = content; messagesContainer.appendChild(messageDiv); messagesContainer.scrollTop = messagesContainer.scrollHeight; } async function sendMessage() { const query = userInput.value.trim(); if (query === "") return; appendMessage(query, 'user'); conversation.push({ role: 'user', content: query }); userInput.value = ''; // Prepare payload with context const payload = { query: query, context: generateContext() }; try { const response = await fetch('/api/query_responder', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (!response.ok) { throw new Error('Network response was not ok'); } const data = await response.json(); const botResponse = data.response; const botCode = data.code; appendMessage(botResponse, 'bot'); conversation.push({ role: 'bot', content: botResponse }); if (botCode) { // Display code in code block codeBlock.textContent = botCode; hljs.highlightElement(codeBlock); } } catch (error) { appendMessage('Sorry, there was an error processing your request.', 'bot'); console.error('Error:', error); } } function generateContext() { // Generate context from the conversation history // You can customize how much context to send return conversation.slice(-5).map(msg => `${msg.role}: ${msg.content}`).join('\n'); } // Copy Code to Clipboard copyButton.addEventListener('click', () => { const code = codeBlock.textContent; navigator.clipboard.writeText(code).then(() => { alert('Code copied to clipboard!'); }).catch(err => { alert('Failed to copy code.'); console.error('Error:', err); }); }); // Refresh Code Preview refreshButton.addEventListener('click', () => { // For demonstration, we'll just re-highlight the code hljs.highlightElement(codeBlock); alert('Code preview refreshed!'); }); // Initial Theme Icon Setup window.onload = () => { if (body.getAttribute('data-theme') === 'dark') { themeToggle.innerHTML = '<i class="fas fa-sun"></i>'; } else { themeToggle.innerHTML = '<i class="fas fa-moon"></i>'; } }; </script> </body> </html>Explanation of Key Components:
-
Theme Toggle:
- Uses JavaScript to switch between light and dark themes by toggling the
data-themeattribute on the<body>. - The toggle button updates its icon based on the current theme.
- Uses JavaScript to switch between light and dark themes by toggling the
-
Chat Interface:
- Messages are appended to the
.messagescontainer with distinct styles for user and bot. - User input is captured from the input field and sent to the
/api/query_responderAPI via a POST request. - The conversation history is maintained in the
conversationarray to retain context.
- Messages are appended to the
-
API Interaction:
- Sends the user's query along with the recent context to the API.
- Upon receiving a response, it displays the bot's response and any accompanying code.
- Code received from the API is displayed in the code block area with syntax highlighting.
-
Code Block Interface:
- Displays the latest code snippet returned by the API.
- Includes buttons to copy the code to the clipboard and refresh the code preview.
- Utilizes Highlight.js for enhanced readability of code.
-
Responsive Design:
- The layout adjusts for smaller screens, stacking the chat and code interfaces vertically.
-
Accessibility & UX:
- Buttons include tooltips for better user understanding.
- The interface is designed to be intuitive and user-friendly.
Notes:
- API Endpoint: Ensure that the
/api/query_responderendpoint is accessible from the frontend. If it's hosted on a different domain, you might need to handle CORS or adjust the fetch URL accordingly. - Security: Implement necessary security measures, such as sanitizing user inputs and handling API responses securely, especially if deploying in a production environment.
- Customization: Feel free to adjust the styling, enhance features, or integrate additional functionalities as per your requirements.
This setup provides a seamless experience for interacting with the chatbot and viewing or modifying code in real-time. Enjoy building your responsive and interactive web page!