create a simple llm chat ui
- User
create a simple llm chat ui
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
create a simple llm chat ui
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is an example of a simple LLM chat UI using plain HTML, CSS, and JavaScript. This example creates a chat container that displays the conversation between a user and a simulated LLM. The LLM "responds" by echoing the user input after a short delay. You can later modify the LLM response logic to integrate with an API or your backend.
Save the code below as index.html and open it in your browser:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple LLM Chat UI</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f5f5f5;
margin: 0;
font-family: Arial, sans-serif;
}
.chat-container {
width: 400px;
max-width: 100%;
background-color: #fff;
border: 1px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
display: flex;
flex-direction: column;
height: 500px;
}
.messages {
flex: 1;
padding: 10px;
overflow-y: auto;
border-bottom: 1px solid #ddd;
}
.message {
margin-bottom: 10px;
padding: 8px 12px;
border-radius: 18px;
max-width: 75%;
clear: both;
}
.message.user {
background-color: #007BFF;
color: #fff;
margin-left: auto;
text-align: right;
}
.message.llm {
background-color: #e9ecef;
color: #333;
margin-right: auto;
text-align: left;
}
.input-area {
display: flex;
border-top: 1px solid #ddd;
}
.input-area input {
flex: 1;
padding: 12px;
border: none;
outline: none;
font-size: 16px;
}
.input-area button {
padding: 0 16px;
border: none;
background-color: #007BFF;
color: white;
font-size: 16px;
cursor: pointer;
transition: background-color 0.2s ease-in-out;
}
.input-area button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="chat-container">
<!-- Conversations will be rendered here -->
<div id="messages" class="messages"></div>
<!-- Input area -->
<div class="input-area">
<input type="text" id="userInput" placeholder="Type your message..." />
<button id="sendButton">Send</button>
</div>
</div>
<script>
const messagesDiv = document.getElementById('messages');
const userInput = document.getElementById('userInput');
const sendButton = document.getElementById('sendButton');
// Function to append a message in the chat area.
function addMessage(text, sender) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message', sender);
messageDiv.textContent = text;
messagesDiv.appendChild(messageDiv);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
// Function to handle sending user messages.
function sendUserMessage() {
const text = userInput.value.trim();
if (!text) return;
// Append the user's message.
addMessage(text, 'user');
userInput.value = '';
// Simulate an LLM response after a short delay.
setTimeout(() => {
// In a real scenario, replace this with a call to your LLM API/backend.
const llmResponse = "Echo: " + text;
addMessage(llmResponse, 'llm');
}, 500);
}
// Event listeners for the send button and pressing "Enter".
sendButton.addEventListener('click', sendUserMessage);
userInput.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
sendUserMessage();
}
});
</script>
</body>
</html>
HTML Structure:
.chat-container holds the entire chat UI..messages is the scrollable container where messages (both user and LLM) are displayed..input-area contains an input field and a "Send" button.CSS Styling:
JavaScript Functionality:
addMessage function creates a new message element and appends it to the chat history.sendUserMessage function gets the text from the input field, appends it to the chat, and then simulates an LLM response after a 500ms delay.You now have a basic UI that you can build upon and enhance. To integrate with a real LLM, replace the simulated response with an API call to your model backend.