create a simple llm chat ui
- User
create a simple llm chat ui
Assistant
Okay, here is a very simple LLM chat UI using just HTML, CSS, and JavaScript.
This example simulates an LLM response by just echoing the user's message. To connect it to a real LLM, you would replace the simulation logic with an API call.
1.
index.html(The Structure)<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Simple LLM Chat UI</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="chat-container"> <div id="chat-display"> <!-- Messages will appear here --> <div class="message llm-message">Welcome to the simple chat! Type your message below.</div> </div> <div id="input-area"> <input type="text" id="user-input" placeholder="Type your message..."> <button id="send-button">Send</button> </div> </div> <script src="script.js"></script> </body> </html>2.
style.css(The Styling)body { font-family: sans-serif; margin: 0; padding: 0; background-color: #f0f0f0; display: flex; justify-content: center; align-items: center; min-height: 100vh; } #chat-container { width: 90%; max-width: 600px; /* Max width for larger screens */ height: 80vh; /* Use 80% of viewport height */ max-height: 700px; /* Max height */ background-color: #fff; border-radius: 8px; box-shadow: 0 0 15px rgba(0, 0, 0, 0.1); display: flex; flex-direction: column; /* Stack display and input */ overflow: hidden; /* Keep contents inside rounded corners */ } #chat-display { flex-grow: 1; /* Take up all available space */ padding: 15px; overflow-y: auto; /* Make it scrollable */ display: flex; /* Use flexbox for messages */ flex-direction: column; /* Stack messages */ } .message { margin-bottom: 10px; padding: 10px 15px; border-radius: 18px; /* More rounded corners */ max-width: 80%; /* Don't let messages take full width */ word-wrap: break-word; /* Prevent long words overflowing */ } .user-message { background-color: #007bff; /* Blue background for user */ color: white; align-self: flex-end; /* Align to the right */ margin-left: auto; /* Push to the right */ } .llm-message { background-color: #e9e9eb; /* Light grey background for LLM */ color: #333; align-self: flex-start; /* Align to the left */ margin-right: auto; /* Push to the left */ } #input-area { display: flex; /* Arrange input and button side-by-side */ padding: 15px; background-color: #f9f9f9; border-top: 1px solid #eee; } #user-input { flex-grow: 1; /* Take up available space */ padding: 10px; border: 1px solid #ccc; border-radius: 20px; /* Rounded input field */ margin-right: 10px; font-size: 1rem; outline: none; /* Remove default outline */ } #user-input:focus { border-color: #007bff; /* Highlight on focus */ box-shadow: 0 0 5px rgba(0, 123, 255, 0.25); } #send-button { padding: 10px 20px; background-color: #28a745; /* Green send button */ color: white; border: none; border-radius: 20px; /* Rounded button */ cursor: pointer; font-size: 1rem; transition: background-color 0.2s ease; } #send-button:hover { background-color: #218838; /* Darker green on hover */ } #send-button:active { background-color: #1e7e34; /* Even darker when clicked */ }3.
script.js(The Logic)document.addEventListener('DOMContentLoaded', () => { const userInput = document.getElementById('user-input'); const sendButton = document.getElementById('send-button'); const chatDisplay = document.getElementById('chat-display'); // Function to add a message to the chat display function addMessage(text, sender) { const messageDiv = document.createElement('div'); messageDiv.classList.add('message'); messageDiv.classList.add(`${sender}-message`); // Adds 'user-message' or 'llm-message' messageDiv.textContent = text; chatDisplay.appendChild(messageDiv); // Auto-scroll to the bottom chatDisplay.scrollTop = chatDisplay.scrollHeight; } // Function to simulate LLM response (replace with actual API call) function getLLMResponse(userMessage) { // --- This is where you would integrate with an LLM API --- // For this simple example, we just echo the message const simulatedResponse = `You said: "${userMessage}". (This is a simulated LLM response)`; // Simulate a delay for realism setTimeout(() => { addMessage(simulatedResponse, 'llm'); }, 500); // Wait 500 milliseconds // ----------------------------------------------------------- } // Function to handle sending a message function sendMessage() { const messageText = userInput.value.trim(); // Get text and remove whitespace if (messageText === '') { return; // Don't send empty messages } // 1. Display user message addMessage(messageText, 'user'); // 2. Get and display LLM response (simulated here) getLLMResponse(messageText); // 3. Clear the input field userInput.value = ''; } // Event listener for the Send button click sendButton.addEventListener('click', sendMessage); // Event listener for pressing Enter key in the input field userInput.addEventListener('keypress', (event) => { // Check if the key pressed was 'Enter' (key code 13) if (event.key === 'Enter') { event.preventDefault(); // Prevent default form submission (if input was in a form) sendMessage(); } }); });How to Use:
- Save the three code blocks above into three separate files:
index.html,style.css, andscript.js. - Make sure all three files are in the same folder.
- Open the
index.htmlfile in your web browser.
You will see a simple chat interface. Type into the box, click "Send" or press Enter, and your message will appear on the right (blue), followed by a simulated response from the "LLM" on the left (grey).
To integrate with a real LLM:
- Sign up for an LLM API (e.g., OpenAI, Google AI, Anthropic, etc.).
- Get your API key.
- In the
getLLMResponsefunction inscript.js, replace the simulation logic (const simulatedResponse = ...; setTimeout(...)) with code that makes an HTTP request to the LLM provider's API, sending theuserMessage. - Handle the API response (which will be the LLM's text) and pass it to
addMessage(llmResponseText, 'llm');. - Important: For a client-side UI like this, you should generally not put your API key directly in the client-side JavaScript. A more secure approach is to have a small backend server (using Node.js, Python/Flask, etc.) that receives the user message from the frontend, makes the API call securely using your key, and then sends the LLM response back to the frontend.
- Save the three code blocks above into three separate files:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.