Ollama PHP Client Library

repository·master·Indexed 42 days ago

https://github.com/ardagnsrn/ollama-php

A structured PHP 8.1+ interface for interacting with the Ollama API. It enables developers to run LLMs locally, manage model lifecycles (list, show, create, copy, delete, pull, push), generate text completions, handle multi-turn chat sessions with tool calling support, and generate vector embeddings.

Tokens
6.4K
Snippets
25
Records
28
Agent score
52%

What's inside Ollama PHP

  1. Install and initialize the Ollama PHP client

    master

    To use this library, you must have PHP 8.1+ and Ollama installed on your system.

    1. Install the package via Composer:
    composer require ardagnsrn/ollama-php
    1. Initialize the client. You can use the default base URL or provide a custom one (e.g., if Ollama is running on a different host or port):
    // Using default base URL
    $client = \ArdaGnsrn\Ollama\Ollama::client();
    
    // Using a custom base URL
    $client = \ArdaGnsrn\Ollama\Ollama::client('http://localhost:11434');
  2. Initialize the OllamaClient

    master

    To interact with the Ollama API, instantiate the OllamaClient class. You can specify the host address, an optional API key for authentication, and a connection timeout.

    By default, it connects to http://localhost:11434 with a 10-second timeout.

    use ArdaGnsrn\
    Ollama\OllamaClient;
    
    $client = new OllamaClient(
        host: 'http://localhost:11434',
        apiKey: 'your-api-key',
        timeout: 30
    );
  3. Manage chat sessions with the Chat resource

    master

    The Chat resource handles multi-turn conversations using a list of messages with specific roles (system, user, assistant).

    Key Features:

    • create(): Generates a response. Supports the tools parameter for function calling.
    • createStreamed(): Streams the chat response. Note: The tools parameter cannot be used with createStreamed.
    • Tool Calling: When using tools, the response may contain toolCalls which include the function name and arguments.
    // Standard Chat with Tool Calling
    $response = $client->chat()->create([
        'model' => 'llama3.1',
        'messages' => [
            ['role' => 'user', 'content' => 'What is the weather today in Paris?'],
        ],
        'tools' => [
            [
                'type' => 'function',
                'function' => [
                    'name' => 'get_current_weather',
                    'description' => 'Get the current weather',
                    'parameters' => [
                        'type' => 'object',
                        'properties' => [
                            'location' => ['type' => 'string'],
                            'format' => ['type' => 'string', 'enum' => ['celsius', 'fahrenheit']],
                        ],
                        'required' => ['location', 'format'],
                    ],
                ],
            ]
        ]
    ]);
    
    $toolCall = $response->message->toolCalls[0];
    echo $toolCall->function->name;
  4. Manage blobs with the Blobs resource

    master

    The Blobs resource allows you to interact with the underlying data blobs used by Ollama models.

    // Check if a blob exists
    $exists = $client->blobs()->exists('blobname');
    
    // Create a new blob
    $success = $client->blobs()->create('blobname');
  5. Generate embeddings with the Embed resource

    master

    Use the Embed resource to generate vector embeddings for input text using a specified model.

    $response = $client->embed()->create([
        'model' => 'llama3.1',
        'input' => [
            "Why is the sky blue?",
        ]
    ]);
    
    $embedding = $response->embedding; // Array of floats
  6. Manage models with the Models resource

    master

    The Models resource provides full lifecycle management for LLMs within Ollama.

    Available Methods:

    • list(): Returns all available models.
    • show($name): Shows details (modelfile, parameters, template) of a specific model.
    • create([...]): Creates a new model from a modelfile.
    • copy($source, $target): Copies an existing model.
    • delete($name): Deletes a model.
    • pull($name): Downloads a model from the Ollama server.
    • push($name): Uploads a model to the Ollama server.
    • runningList(): Lists models currently running in memory.
    // List models
    $response = $client->models()->list();
    
    // Pull a model
    $response = $client->models()->pull('llama3.1');
    
    // Create a custom model
    $client->models()->create([
        'name' => 'mario',
        'modelfile' => "FROM llama3.1\nSYSTEM You are mario from Super Mario Bros."
    ]);
  7. Generate text completions with the Completions resource

    master

    The Completions resource allows you to generate a single response for a given prompt using a specific model.

    Use create() for a standard blocking request, or createStreamed() to iterate over chunks of the response as they are generated.

    // Standard completion
    $completions = $client->completions()->create([
        'model' => 'llama3.1',
        'prompt' => 'Once upon a time',
    ]);
    echo $completions->response;
    
    // Streamed completion
    $completions = $client->completions()->createStreamed([
        'model' => 'llama3.1',
        'prompt' => 'Once upon a time',
    ]);
    
    foreach ($completions as $completion) {
        echo $completion->response;
    }
  8. Make POST requests

    master

    The post() method performs a POST request to a specified endpoint. This is the primary method for interacting with generation and chat endpoints.

    • $endpoint: The API endpoint path.
    • $parameters: An associative array of parameters to be sent in the JSON body.
    • $stream: A boolean indicating if streaming should be enabled. If true, the method returns the raw ResponseInterface to allow for stream processing.
    • $parseJson: If true (default) and $stream is false, the method returns the decoded JSON as an associative array. If false or if $stream is true, it returns the raw ResponseInterface.
    // Standard non-streaming POST request
    $result = $client->post('api/generate', [
        'model' => 'llama3',
        'prompt' => 'Why is the sky blue?'
    ]);
    
    // Streaming POST request (returns ResponseInterface)
    $streamResponse = $client->post('api/generate', [
        'model' => 'llama3',
        'prompt' => 'Tell me a story'
    ], stream: true);
  9. Check if Ollama is running

    master

    Use the isRunning() method to verify if the Ollama service is active and reachable. It returns true if the service responds with 'Ollama is running', and false otherwise (including on connection errors).

    if ($client->isRunning()) {
        echo 'Ollama is ready!';
    }
  10. Handle the ListModelsResponse object

    master

    The ListModelsResponse object is returned when requesting a list of available models from the Ollama API. It contains a collection of ListModelsModelResponse objects. You can access the list of models via the $models property or convert the entire response back into a plain associative array using the toArray() method.

    // Assuming $response is an instance of ListModelsResponse
    foreach ($response->models as $model) {
        // $model is an instance of ListModelsModelResponse
        echo $model->name;
    }
    
    // Convert back to array
    $array = $response->toArray();
  11. Parse chat messages with ChatMessageResponse::from()

    master

    The ChatMessageResponse class represents an individual message within a chat response. You can instantiate it from an associative array (typically parsed from an Ollama API response) using the from() static method.

    The method expects the following keys in the $attributes array:

    • role (string): The role of the message (e.g., 'assistant', 'user', 'system').
    • content (string): The text content of the message.
    • tool_calls (array, optional): An array of tool call data. If provided, these are automatically converted into ChatMessageToolCallResponse objects.
    $message = ChatMessageResponse::from([
        'role' => 'assistant',
        'content' => 'Hello! How can I help you today?',
        'tool_calls' => []
    ]);
    
    echo $message->role;    // 'assistant'
    echo $message->content; // 'Hello! How can I help you today?'
  12. Convert ChatMessageResponse to an array with toArray()

    master

    To convert a ChatMessageResponse instance back into a plain associative array, use the toArray() method. This is useful for serialization or passing data back to other systems. The resulting array includes the role, content, and tool_calls keys.

    $array = $chatMessageResponse->toArray();
    // Returns:
    // [
    //   'role' => '...',
    //   'content' => '...',
    //   'tool_calls' => [...]
    // ]