Create openwebui like ai interference. There is only one model so no selection. Api is…
- User
Create openwebui like ai interference. There is only one model so no selection. Api is localhost:4000/completion with only messages list
- Assistant
Create openwebui like ai interference. There is only one model so no selection. Api is localhost:4000/completion with only messages list
Creating a custom web-based interface similar to OpenWebUI that interacts with your AI model via the specified API (http://localhost:4000/completion) can be achieved using various web technologies. Below, I'll provide a step-by-step guide to building a simple yet functional chat interface using HTML, CSS, and JavaScript. This interface will allow users to send messages, display the conversation history, and communicate with your AI model through the provided API.
Create a project directory with the following structure:
/openwebui-like-interface
│
├── index.html
├── styles.css
└── script.js
Create an index.html file with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AI Chat Interface</title>
<!-- Link to the CSS file -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="chat-container">
<div class="chat-header">
<h2>AI Chat Interface</h2>
</div>
<div class="chat-window" id="chat-window">
<!-- Chat messages will appear here -->
</div>
<div class="chat-input">
<textarea id="message-input" placeholder="Type your message here..." rows="2"></textarea>
<button id="send-button">Send</button>
</div>
</div>
<!-- Link to the JavaScript file -->
<script src="script.js"></script>
</body>
</html>
Create a styles.css file to style the chat interface:
/* styles.css */
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
}
.chat-container {
width: 100%;
max-width: 800px;
margin: 50px auto;
background-color: #ffffff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.chat-header {
padding: 20px;
background-color: #007bff;
color: #ffffff;
border-top-left-radius: 8px;
border-top-right-radius: 8px;
}
.chat-header h2 {
margin: 0;
}
.chat-window {
height: 500px;
padding: 20px;
overflow-y: auto;
background-color: #f9f9f9;
}
.message {
margin-bottom: 15px;
display: flex;
}
.message.user .message-content {
background-color: #007bff;
color: #ffffff;
align-self: flex-end;
}
.message.ai .message-content {
background-color: #e4e6eb;
color: #000000;
align-self: flex-start;
}
.message-content {
max-width: 70%;
padding: 10px 15px;
border-radius: 20px;
word-wrap: break-word;
}
.chat-input {
display: flex;
padding: 20px;
border-top: 1px solid #dddddd;
}
#message-input {
flex: 1;
padding: 10px;
border: 1px solid #cccccc;
border-radius: 20px;
resize: none;
font-size: 16px;
}
#send-button {
margin-left: 10px;
padding: 10px 20px;
background-color: #007bff;
color: #ffffff;
border: none;
border-radius: 20px;
cursor: pointer;
font-size: 16px;
}
#send-button:hover {
background-color: #0056b3;
}
Create a script.js file to handle user interactions and API communication:
// script.js
document.addEventListener('DOMContentLoaded', () => {
const sendButton = document.getElementById('send-button');
const messageInput = document.getElementById('message-input');
const chatWindow = document.getElementById('chat-window');
// Function to append messages to the chat window
function appendMessage(sender, text) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message', sender);
const messageContent = document.createElement('div');
messageContent.classList.add('message-content');
messageContent.textContent = text;
messageDiv.appendChild(messageContent);
chatWindow.appendChild(messageDiv);
// Scroll to the bottom
chatWindow.scrollTop = chatWindow.scrollHeight;
}
// Function to handle sending messages
async function sendMessage() {
const userMessage = messageInput.value.trim();
if (userMessage === '') return;
// Append user message
appendMessage('user', userMessage);
messageInput.value = '';
// Prepare the payload
const payload = {
messages: [
{ role: 'user', content: userMessage }
]
};
try {
// Send POST request to the API
const response = await fetch('http://localhost:4000/completion', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (!response.ok) {
throw new Error(`Server error: ${response.statusText}`);
}
const data = await response.json();
// Assuming the API returns a JSON object with the AI's response
const aiMessage = data.response || 'No response from AI.';
appendMessage('ai', aiMessage);
} catch (error) {
console.error('Error:', error);
appendMessage('ai', 'Sorry, there was an error communicating with the AI.');
}
}
// Event listeners
sendButton.addEventListener('click', sendMessage);
messageInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});
});
Notes on the JavaScript Code:
Appending Messages: The appendMessage function creates message elements dynamically and appends them to the chat window. Messages from the user and AI are styled differently based on their sender.
Sending Messages: When the user sends a message, it's sent to the API as a POST request with the messages list containing the user's message. The AI's response is then appended to the chat window.
Error Handling: If there's an error communicating with the API, an error message is displayed in the chat window.
Enter Key Handling: Users can send messages by pressing the "Enter" key. If they press "Shift + Enter," a new line is added to the textarea instead.
Ensure API is Running: Make sure your AI model's API is running and accessible at http://localhost:4000/completion. The API should accept POST requests with a JSON payload containing a messages array.
Serve the Frontend:
To view the interface in your browser, you need to serve the index.html file. You can use a simple HTTP server. Here are a few options:
Using Python (if installed):
# For Python 3.x
python -m http.server 8000
Navigate to http://localhost:8000 in your browser.
Using Node.js (with http-server):
First, install http-server globally if you haven't:
npm install -g http-server
Then, navigate to your project directory and start the server:
http-server -p 8000
Navigate to http://localhost:8000 in your browser.
Interact with the Chat Interface:
Open your browser and go to http://localhost:8000. You should see the AI Chat Interface where you can type messages and receive responses from your AI model.
To make your chat interface more robust and feature-rich, consider the following enhancements:
Conversation History:
Loading Indicators:
Error Notifications:
Responsive Design:
Advanced Styling:
Security Considerations:
Using Frontend Frameworks:
Backend Integration:
For the frontend to work seamlessly, ensure that your API at http://localhost:4000/completion returns a JSON response in the following format:
{
"response": "Hello! How can I assist you today?"
}
Note: Adjust the JavaScript code if your API's response structure differs.
By following this guide, you should have a functional web-based chat interface that communicates with your AI model via the specified API. Feel free to customize and expand upon this foundation to better suit your project's needs.
Not yet included in a published release.