add interface
- User
add interface
- Assistant
Great! Let's build a simple web-based chat interface in PHP that lets you see a conversation between two LLMs (via Ollama) in real time.
🧠 What You’ll Get:
- A PHP web page showing a conversation between two models (LLM1 & LLM2).
- A clean HTML/CSS frontend displaying each message.
- PHP & JavaScript wiring it all together.
- Backend uses Ollama's HTTP API to simulate two models chatting.
- Messages are stored in memory for the session using
$_SESSION.
✅ Requirements:
- Ollama installed and running models locally.
- PHP server (e.g., using
php -S localhost:8000). llama2andgpt4allmodels available in Ollama (or replace with models you have).
📁 File Structure:
/chat ├── index.php # Web UI and controller ├── chat.php # Handles AJAX requests ├── style.css # Basic CSS styles
🔧 Step-by-Step Setup
index.php– Main Interface<?php session_start(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>LLM vs LLM Chat</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>🤖 LLM vs LLM Chat</h1> <div id="chat-box"> <?php if (!isset($_SESSION['chat'])) { $_SESSION['chat'] = []; } foreach ($_SESSION['chat'] as $entry) { echo "<div class='message {$entry['sender']}'> <strong>{$entry['sender']}:</strong> {$entry['text']} </div>"; } ?> </div> <form id="chat-form" method="POST" action="chat.php"> <input type="text" name="user_message" id="user_message" placeholder="Start with a message..." required> <button>Start</button> </form> <script> const form = document.getElementById('chat-form'); const messageInput = document.getElementById('user_message'); const chatBox = document.getElementById('chat-box'); form.addEventListener('submit', async function (e) { e.preventDefault(); const message = messageInput.value; const response = await fetch('chat.php', { method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, body: 'user_message=' + encodeURIComponent(message) }); const html = await response.text(); chatBox.innerHTML = html; messageInput.value = ''; }); </script> </body> </html>
chat.php– PHP Chat Logic<?php session_start(); function callOllama($model, $prompt) { $url = "http://localhost:11434/api/chat"; $data = [ "model" => $model, "messages" => [ ["role" => "user", "content" => $prompt] ] ]; $options = [ 'http' => [ 'header' => "Content-type: application/json\r\n", 'method' => 'POST', 'content' => json_encode($data), ], ]; $context = stream_context_create($options); $result = file_get_contents($url, false, $context); if ($result === FALSE) { return "Error connecting to model."; } $response = json_decode($result, true); return $response['choices'][0]['message']['content'] ?? "No response."; } if (!isset($_SESSION['chat'])) { $_SESSION['chat'] = []; } if (isset($_POST['user_message'])) { $prompt = trim($_POST['user_message']); $model1 = "llama2"; $model2 = "gpt4all"; $reply1 = callOllama($model1, $prompt); $_SESSION['chat'][] = ['sender' => $model1, 'text' => $reply1]; $reply2 = callOllama($model2, $reply1); $_SESSION['chat'][] = ['sender' => $model2, 'text' => $reply2]; } // Output updated chat foreach ($_SESSION['chat'] as $entry) { echo "<div class='message {$entry['sender']}'> <strong>{$entry['sender']}:</strong> {$entry['text']} </div>"; }
style.css– Simple Stylingbody { font-family: Arial, sans-serif; background: #f4f4f4; margin: 0; padding: 20px; } h1 { text-align: center; } #chat-box { width: 90%; max-width: 800px; margin: 0 auto 20px; background: #fff; border: 1px solid #ccc; padding: 15px; height: 400px; overflow-y: scroll; } .message { margin: 10px 0; padding: 10px; border-radius: 6px; } .llama2 { background-color: #e0f7fa; text-align: left; } .gpt4all { background-color: #f1f8e9; text-align: right; } form { display: flex; justify-content: center; gap: 10px; margin-bottom: 20px; } input[type="text"] { padding: 10px; width: 50%; font-size: 16px; } button { padding: 10px 20px; background-color: #2b90d9; color: white; border: none; font-size: 16px; cursor: pointer; }
🚀 How to Run
-
Make sure Ollama is running:
ollama run llama2 ollama run gpt4all -
Start PHP server in project folder:
php -S localhost:8000 -
Open browser:
http://localhost:8000 -
Enter a message. Watch the LLMs respond to each other!
✅ Bonus Features You Can Add Later
- Save conversation history to a file.
- Display model names differently (like avatars).
- Use streaming API (Ollama supports
/api/chat?stream=true) for real-time typing effect. - Allow choosing models from a dropdown.
Would you like an extended version with JSON-based session storage, or more dynamic streaming output?
- User
the models should be configurable in the interface
- Assistant
Excellent! Let’s enhance your LLM chat interface by allowing the user to select two different models from the UI before starting the conversation.
✨ What’s New in This Version:
- Adds a form interface to pick two LLM models (e.g.
llama2,mistral, etc.). - The selected models are saved in the PHP session.
- All message exchanges are handled by those selected models.
- Can easily be extended to populate the model list dynamically via Ollama’s
/api/tags.
✅ Updated File Structure:
/chat ├── index.php # Main UI and model selection ├── chat.php # Handles chat logic and Ajax requests ├── style.css # Styles └── utils.php # (Optional) Helper functions like listing models (WIP)
🔧 Step-by-Step Setup
🔁
index.php– Add model selection<?php session_start(); // Set default models if not set yet if (!isset($_SESSION['model1'])) $_SESSION['model1'] = 'llama2'; if (!isset($_SESSION['model2'])) $_SESSION['model2'] = 'mistral'; // Capture model selection if (isset($_POST['model1']) && isset($_POST['model2'])) { $_SESSION['model1'] = $_POST['model1']; $_SESSION['model2'] = $_POST['model2']; $_SESSION['chat'] = []; // reset conversation header("Location: index.php"); // Refresh to avoid reposting form exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>LLM vs LLM Configurable Chat</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>🤖 Chat: LLM vs LLM</h1> <form method="POST" class="model-selector"> <label> Model 1: <input type="text" name="model1" value="<?= htmlspecialchars($_SESSION['model1']) ?>" placeholder="e.g. llama2" required> </label> <label> Model 2: <input type="text" name="model2" value="<?= htmlspecialchars($_SESSION['model2']) ?>" placeholder="e.g. mistral" required> </label> <button type="submit">Set Models</button> </form> <div class="info"> Current Models: <strong><?= htmlspecialchars($_SESSION['model1']) ?></strong> vs <strong><?= htmlspecialchars($_SESSION['model2']) ?></strong> </div> <div id="chat-box"> <?php if (!isset($_SESSION['chat'])) { $_SESSION['chat'] = []; } foreach ($_SESSION['chat'] as $entry) { echo "<div class='message {$entry['sender']}'> <strong>{$entry['sender']}:</strong> {$entry['text']} </div>"; } ?> </div> <form id="chat-form" method="POST" action="chat.php"> <input type="text" name="user_message" id="user_message" placeholder="Start with a message..." required> <button>Send</button> </form> <script> const form = document.getElementById('chat-form'); const messageInput = document.getElementById('user_message'); const chatBox = document.getElementById('chat-box'); form.addEventListener('submit', async function (e) { e.preventDefault(); const message = messageInput.value; const response = await fetch('chat.php', { method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, body: 'user_message=' + encodeURIComponent(message) }); const html = await response.text(); chatBox.innerHTML = html; messageInput.value = ''; }); </script> </body> </html>
⚙️
chat.php– Use model settings from session<?php session_start(); // Function to call Ollama API function callOllama($model, $prompt) { $url = "http://localhost:11434/api/chat"; $messageHistory = [ ["role" => "user", "content" => $prompt] ]; $data = [ "model" => $model, "messages" => $messageHistory ]; $options = [ 'http' => [ 'header' => "Content-type: application/json\r\n", 'method' => 'POST', 'content' => json_encode($data), ], ]; $context = stream_context_create($options); $result = file_get_contents($url, false, $context); if ($result === FALSE) return "Error calling model: $model"; $response = json_decode($result, true); return $response['choices'][0]['message']['content'] ?? "No response."; } if (!isset($_SESSION['chat'])) { $_SESSION['chat'] = []; } if (isset($_POST['user_message'])) { $prompt = trim($_POST['user_message']); $model1 = $_SESSION['model1'] ?? 'llama2'; $model2 = $_SESSION['model2'] ?? 'mistral'; $reply1 = callOllama($model1, $prompt); $_SESSION['chat'][] = ['sender' => $model1, 'text' => $reply1]; $reply2 = callOllama($model2, $reply1); $_SESSION['chat'][] = ['sender' => $model2, 'text' => $reply2]; } // Display updated chat window foreach ($_SESSION['chat'] as $entry) { echo "<div class='message {$entry['sender']}'> <strong>{$entry['sender']}:</strong> {$entry['text']} </div>"; }
🎨
style.css– Add styles for model selection toobody { font-family: Arial, sans-serif; background: #f4f4f4; margin: 0; padding: 20px; } h1 { text-align: center; } #chat-box { width: 90%; max-width: 800px; margin: 20px auto; background: #fff; border: 1px solid #ccc; padding: 15px; height: 400px; overflow-y: auto; } .message { margin: 10px 0; padding: 10px; border-radius: 6px; } .model-selector { display: flex; justify-content: center; gap: 20px; margin-top: 10px; padding: 10px; background: #eeeeee; } .model-selector input[type="text"] { padding: 8px; width: 120px; } .info { text-align: center; margin: 10px 0; font-style: italic; color: #666; } form { display: flex; justify-content: center; gap: 10px; margin-bottom: 20px; } input[type="text"] { padding: 10px; width: 50%; font-size: 16px; } button { padding: 10px 20px; background-color: #2b90d9; color: white; border: none; font-size: 16px; cursor: pointer; }
🚀 Try It Out
-
Start Ollama with your preferred models:
ollama run llama2 ollama run mistral -
Launch PHP built-in server:
php -S localhost:8000 -
Open browser:
http://localhost:8000
✅ Bonus (Optional)
Use Ollama’s
/api/tagsto list installed models:In a helper
utils.phpor inline:function getInstalledModels() { $json = file_get_contents("http://localhost:11434/api/tags"); $data = json_decode($json, true); return array_column($data['models'], 'name'); }Replace static input with
<select>for models!
Would you like me to help turn the model inputs into
<select>dropdowns automatically populated from the Ollama API? - Adds a form interface to pick two LLM models (e.g.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.