Skip to content
TheChatArchiveThe Chat Archive
o3-mini· OpenAIHugging Face LMArena Human Preference 140K

add interface

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with chatgpt-4o-latest-20250326’s answer.
  1. User

    add interface

  2. Assistant

    Below is an example of a self-contained PHP application that both calls Ollama’s API and provides a very simple web interface. In this example, the chat is simulated between two LLMs (“llama2” and “gpt4all”). The conversation is maintained in a PHP session, and the page displays the conversation history in a scrollable chat area. You can start a new chat by providing an initial prompt, then press “Next Chat Round” to continue the back‐and‐forth.

    For this example, ensure that:

    1. Ollama is running locally (the API is assumed to be at http://localhost:11434/api/chat).
    2. The selected models (here "llama2" and "gpt4all") are installed in your Ollama environment.
    3. PHP has session and cURL enabled.

    Save the following code as (for example) index.php in your web server’s document root.


    <?php
    session_start();
    
    // Define the two models to be used
    $model1 = "llama2";   // Adjust the model name as needed
    $model2 = "gpt4all";  // Adjust the model name as needed
    
    // Function that sends a message to the Ollama API and returns the model response
    function callOllamaModel($model, $prompt) {
        $url = "http://localhost:11434/api/chat";
        $data = [
            "model" => $model,
            "messages" => [
                [ "role" => "user", "content" => $prompt ]
            ]
        ];
      
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        $response = curl_exec($ch);
        if(curl_errno($ch)) {
            $error_msg = curl_error($ch);
            curl_close($ch);
            return "Error: " . $error_msg;
        }
        curl_close($ch);
        $responseData = json_decode($response, true);
        return $responseData['choices'][0]['message']['content'] ?? "No response";
    }
    
    // Initialize conversation history if not already set
    if (!isset($_SESSION['chat_history'])) {
        $_SESSION['chat_history'] = [];
    }
    
    // Process POST requests
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        // Start a new chat if the initial prompt is provided  
        if (isset($_POST['start']) && !empty($_POST['initial_prompt'])) {
            // Clear any previous conversation
            $_SESSION['chat_history'] = [];
            $initialPrompt = trim($_POST['initial_prompt']);
            $_SESSION['chat_history'][] = "<strong>User:</strong> " . htmlspecialchars($initialPrompt);
            
            // First round: Model 1 responds to the user's prompt
            $response1 = callOllamaModel($model1, $initialPrompt);
            $_SESSION['chat_history'][] = "<strong>{$model1}:</strong> " . htmlspecialchars($response1);
            
            // Then Model 2 responds to Model 1
            $response2 = callOllamaModel($model2, $response1);
            $_SESSION['chat_history'][] = "<strong>{$model2}:</strong> " . htmlspecialchars($response2);
          
            // Save last prompt so that the next round starts with Model 2's message
            $_SESSION['last_prompt'] = $response2;
        } 
        // If "Next Chat Round" button is pressed, continue the conversation
        else if (isset($_POST['next_round'])) {
            if (isset($_SESSION['last_prompt'])) {
                $lastPrompt = $_SESSION['last_prompt'];
          
                // Model 1 responds to the last prompt from Model 2
                $response1 = callOllamaModel($model1, $lastPrompt);
                $_SESSION['chat_history'][] = "<strong>{$model1}:</strong> " . htmlspecialchars($response1);
          
                // Model 2 responds to Model 1's reply
                $response2 = callOllamaModel($model2, $response1);
                $_SESSION['chat_history'][] = "<strong>{$model2}:</strong> " . htmlspecialchars($response2);
          
                // Update the last prompt for subsequent rounds
                $_SESSION['last_prompt'] = $response2;
            }
        } 
        // Reset chat if requested
        else if (isset($_POST['reset'])) {
            session_destroy();
            header("Location: index.php");
            exit();
        }
    }
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>LLM Chat Interface with Ollama</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 20px;
                background: #f0f0f0;
            }
            .chat-container {
                width: 80%;
                max-width: 800px;
                margin: 20px auto;
                background: white;
                padding: 15px;
                border: 1px solid #ccc;
                border-radius: 4px;
            }
            .chat-box {
                height: 400px;
                overflow-y: scroll;
                border: 1px solid #ddd;
                padding: 10px;
                background: #fafafa;
                margin-bottom: 20px;
            }
            .chat-entry {
                margin-bottom: 10px;
            }
            .controls {
                text-align: center;
            }
            input[type="text"] {
                width: 90%;
                padding: 8px;
                font-size: 16px;
                box-sizing: border-box;
            }
            input[type="submit"] {
                padding: 10px 20px;
                margin-top: 10px;
                font-size: 16px;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
    <div class="chat-container">
        <h2>LLM Chat Interface</h2>
        <div class="chat-box">
            <?php 
            if (isset($_SESSION['chat_history'])) {
                foreach ($_SESSION['chat_history'] as $entry) {
                    echo "<div class='chat-entry'>" . $entry . "</div>";
                }
            } 
            ?>
        </div>
        <div class="controls">
            <?php if (!isset($_SESSION['last_prompt'])) { ?>
                <!-- Display initial prompt form if chat has not started -->
                <form method="post">
                    <input type="text" name="initial_prompt" placeholder="Enter your initial prompt..." required>
                    <br><br>
                    <input type="submit" name="start" value="Start Chat">
                </form>
            <?php } else { ?>
                <!-- Display controls for continuing or resetting the conversation -->
                <form method="post">
                    <input type="submit" name="next_round" value="Next Chat Round">
                    <input type="submit" name="reset" value="Reset Chat">
                </form>
            <?php } ?>
        </div>
    </div>
    </body>
    </html>
    

    How It Works

    1. Initialization and Session Storage:
      The script uses PHP sessions to store the conversation history and the prompt from the last round. When the page loads for the first time, the user sees a text box to enter an initial prompt.

    2. Calling Ollama’s API:
      The function callOllamaModel() uses PHP cURL to POST a JSON payload to Ollama’s API endpoint. It sends the prompt along with the specified model's name and returns the response.

    3. Interface Workflow:

      • Starting the Chat: When the user submits the initial prompt, the script calls Model 1 to respond. Then Model 2 replies to Model 1’s response, and both messages are added to the conversation history.
      • Continuing the Chat: Clicking the “Next Chat Round” button uses the last Model 2 response as the prompt to continue the conversation in a back-and-forth manner.
      • Resetting the Chat: Clicking “Reset Chat” destroys the session and reloads the page, allowing a new conversation to be started.
    4. Chat Display:
      The conversation history is rendered inside a scrollable div so you can see the complete back-and-forth.


    This should serve as a good starting point for building an interactive web interface where two LLMs chat with each other using Ollama. You can expand on this by adding robust error handling, asynchronous AJAX calls (to avoid full-page refreshes), or by maintaining a more detailed conversation history (e.g., including roles or timestamps).

    Feel free to adjust model names, the API endpoint, or the interface design as needed. Enjoy chatting with your LLMs!

  3. User

    the models should be configurable in the interface

  4. Assistant

    Below is an enhanced version of the PHP chat interface that lets you configure the two model names directly from the web interface. In this version, the user can enter the initial prompt as well as the names of the two models. The selected model names are stored in the session along with the conversation history so that subsequent Next Chat Round actions use the configured models.

    Save this file as (for example) index.php in your web server’s document root. Make sure your PHP installation has sessions and cURL enabled, and that Ollama is running locally with your chosen models.


    <?php
    session_start();
    
    /**
     * Function to call the Ollama API with the given model and prompt.
     *
     * @param string $model The name of the model to query.
     * @param string $prompt The chat prompt to send.
     * @return string The model response or an error message.
     */
    function callOllamaModel($model, $prompt) {
        $url = "http://localhost:11434/api/chat";
        $data = [
            "model" => $model,
            "messages" => [
                [ "role" => "user", "content" => $prompt ]
            ]
        ];
      
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        $response = curl_exec($ch);
        if(curl_errno($ch)) {
            $error_msg = curl_error($ch);
            curl_close($ch);
            return "Error: " . $error_msg;
        }
        curl_close($ch);
        $responseData = json_decode($response, true);
        return $responseData['choices'][0]['message']['content'] ?? "No response";
    }
    
    // If no chat history is set, initialize it.
    if (!isset($_SESSION['chat_history'])) {
        $_SESSION['chat_history'] = [];
    }
    
    // Process POST requests
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        // If the user clicked "Start Chat" with an initial prompt and model names.
        if (isset($_POST['start'])) {
            // Clear any previous conversation and configuration.
            $_SESSION = [];
            session_destroy();
            session_start();
    
            // Retrieve initial prompt and model names
            $initialPrompt = trim($_POST['initial_prompt'] ?? '');
            $model1 = trim($_POST['model1'] ?? 'llama2');
            $model2 = trim($_POST['model2'] ?? 'gpt4all');
    
            // Save the selected model names in the session
            $_SESSION['model1'] = $model1;
            $_SESSION['model2'] = $model2;
    
            // Initialize conversation history and add the user's prompt
            $_SESSION['chat_history'] = [];
            $_SESSION['chat_history'][] = "<strong>User:</strong> " . htmlspecialchars($initialPrompt);
          
            // Model 1 responds to the user's initial prompt.
            $response1 = callOllamaModel($model1, $initialPrompt);
            $_SESSION['chat_history'][] = "<strong>{$model1}:</strong> " . htmlspecialchars($response1);
          
            // Model 2 responds to Model 1's reply.
            $response2 = callOllamaModel($model2, $response1);
            $_SESSION['chat_history'][] = "<strong>{$model2}:</strong> " . htmlspecialchars($response2);
          
            // Save the last prompt from Model 2 (used in next rounds).
            $_SESSION['last_prompt'] = $response2;
        } 
        // If "Next Chat Round" button is pressed
        else if (isset($_POST['next_round'])) {
            // Retrieve models from the session.
            $model1 = $_SESSION['model1'] ?? 'llama2';
            $model2 = $_SESSION['model2'] ?? 'gpt4all';
          
            if (isset($_SESSION['last_prompt'])) {
                // Model 1 responds to the last prompt from Model 2.
                $response1 = callOllamaModel($model1, $_SESSION['last_prompt']);
                $_SESSION['chat_history'][] = "<strong>{$model1}:</strong> " . htmlspecialchars($response1);
          
                // Model 2 responds to Model 1's reply.
                $response2 = callOllamaModel($model2, $response1);
                $_SESSION['chat_history'][] = "<strong>{$model2}:</strong> " . htmlspecialchars($response2);
          
                // Update the last prompt for subsequent rounds.
                $_SESSION['last_prompt'] = $response2;
            }
        } 
        // If the reset button is clicked, clear the session and reload the page.
        else if (isset($_POST['reset'])) {
            session_destroy();
            header("Location: index.php");
            exit();
        }
    }
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Configurable LLM Chat Interface with Ollama</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 20px;
                background: #f0f0f0;
            }
            .chat-container {
                width: 80%;
                max-width: 800px;
                margin: 20px auto;
                background: white;
                padding: 15px;
                border: 1px solid #ccc;
                border-radius: 4px;
            }
            .chat-box {
                height: 400px;
                overflow-y: scroll;
                border: 1px solid #ddd;
                padding: 10px;
                background: #fafafa;
                margin-bottom: 20px;
            }
            .chat-entry {
                margin-bottom: 10px;
            }
            .controls {
                text-align: center;
            }
            input[type="text"] {
                width: 90%;
                padding: 8px;
                font-size: 16px;
                box-sizing: border-box;
            }
            input[type="submit"] {
                padding: 10px 20px;
                margin-top: 10px;
                font-size: 16px;
                cursor: pointer;
            }
            .config-group {
                margin-bottom: 10px;
            }
            label {
                font-weight: bold;
            }
        </style>
    </head>
    <body>
    <div class="chat-container">
        <h2>LLM Chat Interface</h2>
        <div class="chat-box">
            <?php 
            if (isset($_SESSION['chat_history'])) {
                foreach ($_SESSION['chat_history'] as $entry) {
                    echo "<div class='chat-entry'>" . $entry . "</div>";
                }
            } 
            ?>
        </div>
        <div class="controls">
            <?php if (!isset($_SESSION['last_prompt'])) { ?>
                <!-- Initial configuration form for starting a new chat session -->
                <form method="post">
                    <div class="config-group">
                        <label for="initial_prompt">Initial Prompt:</label><br>
                        <input type="text" id="initial_prompt" name="initial_prompt" placeholder="Enter your initial prompt..." required>
                    </div>
                    <div class="config-group">
                        <label for="model1">Model 1:</label><br>
                        <input type="text" id="model1" name="model1" placeholder="e.g., llama2" value="llama2" required>
                    </div>
                    <div class="config-group">
                        <label for="model2">Model 2:</label><br>
                        <input type="text" id="model2" name="model2" placeholder="e.g., gpt4all" value="gpt4all" required>
                    </div>
                    <input type="submit" name="start" value="Start Chat">
                </form>
            <?php } else { ?>
                <!-- Controls for continuing or resetting the conversation -->
                <form method="post">
                    <input type="submit" name="next_round" value="Next Chat Round">
                    <input type="submit" name="reset" value="Reset Chat">
                </form>
            <?php } ?>
        </div>
    </div>
    </body>
    </html>
    

    How It Works

    1. Configuration in the Interface:

      • The initial form now includes text fields for the Initial Prompt, Model 1, and Model 2.
      • The default values (llama2 and gpt4all) are pre-populated in the respective fields, but you can change them.
    2. Session Setup:

      • When the user starts the chat (by clicking Start Chat), the selected model names are saved to the session along with the conversation history.
      • The first exchange is initiated by sending the user's prompt to Model 1 and then having Model 2 reply to Model 1's answer.
    3. Continuing the Chat:

      • Each time the Next Chat Round button is pressed, the script uses the saved model names and conversation history.
      • Model 1 responds to the last message from Model 2, and then Model 2 responds to Model 1.
    4. Resetting the Chat:

      • The Reset Chat button destroys the session and reloads the page so you can begin a new conversation with new model configurations if desired.

    This interface provides flexibility to change the LLMs via a simple web form while maintaining conversation history during the session. Enjoy experimenting with different models and conversations!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.