Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

a php based chat between two LLM's using ollama.

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.0-flash-001’s answer.
  1. User

    a php based chat between two LLM's using ollama.

  2. Assistant

    Creating a PHP-based chat interface between two large language models (LLMs) using Ollama involves setting up a backend that sends prompts to each model and relays their responses back and forth. Ollama provides a local API to interact with models, so you can call it from PHP using HTTP requests.

    Below is a simple example demonstrating how to create a PHP script that simulates a chat between two LLMs using Ollama's API.


    Prerequisites

    1. Ollama installed and running locally with the models you want to use (e.g., llama2, gpt4all, or any other Ollama-supported model).
    2. PHP environment with curl enabled.
    3. Ollama API endpoint is usually http://localhost:11434 (default).

    Step 1: Basic PHP function to call Ollama API

    <?php
    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)) {
            echo 'Error:' . curl_error($ch);
            return null;
        }
        curl_close($ch);
    
        $responseData = json_decode($response, true);
        return $responseData['choices'][0]['message']['content'] ?? null;
    }
    ?>
    

    Step 2: Simulate chat between two models

    <?php
    include 'ollama_api.php'; // Assuming the function above is saved here
    
    $model1 = "llama2";  // Replace with your first model name
    $model2 = "gpt4all"; // Replace with your second model name
    
    // Initial prompt to start the conversation
    $prompt1 = "Hello, how are you today?";
    
    // Chat loop
    for ($i = 0; $i < 5; $i++) {
        // Model 1 responds
        $response1 = callOllamaModel($model1, $prompt1);
        echo "Model 1: " . $response1 . "\n";
    
        // Model 2 responds to Model 1
        $response2 = callOllamaModel($model2, $response1);
        echo "Model 2: " . $response2 . "\n";
    
        // Next prompt for Model 1 is Model 2's response
        $prompt1 = $response2;
    }
    ?>
    

    Explanation

    • The callOllamaModel function sends a chat message to the specified Ollama model and returns the model's response.
    • The script starts with an initial prompt to Model 1.
    • Model 1 responds, then Model 2 responds to Model 1's output.
    • This loop continues for 5 iterations, simulating a back-and-forth chat.

    Notes

    • Adjust model names to match those installed in your Ollama environment.
    • You can expand this by maintaining conversation history in the messages array for better context.
    • Ollama API might have authentication or different endpoints depending on your setup; adjust accordingly.
    • For production, add error handling and possibly asynchronous calls.

    If you want, I can help you build a more advanced version with conversation history or a web interface! Would you like that?

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