tenqz/ollama

Php library for working with Ollama server.

v0.4.0 2025-07-24 20:58 UTC

This package is auto-updated.

Last update: 2025-08-24 21:07:54 UTC


README

Ollama PHP Client Library

Ollama PHP Client Library

Documentation for version v0.4.0

Build Status Total Downloads Latest Stable Version License

About

Ollama PHP Client Library is a robust, well-designed PHP client for interacting with the Ollama API. This library allows PHP developers to easily integrate large language models (LLMs) into their applications using the Ollama server.

Features

  • Clean, domain-driven architecture
  • Simple HTTP transport layer
  • Full support for Ollama API endpoints
  • Type-safe request and response handling
  • PSR standards compliance
  • Comprehensive test coverage

Installation

You can install the package via composer:

composer require tenqz/ollama

Usage

Using API Client Pattern

use Tenqz\Ollama\Generation\Application\DTO\Request\GenerationRequest;
use Tenqz\Ollama\Generation\Infrastructure\Client\OllamaGenerationClient;
use Tenqz\Ollama\Shared\Infrastructure\Config\OllamaServerConfig;
use Tenqz\Ollama\Transport\Infrastructure\Http\Client\CurlTransportClient;

// Configure the server connection
$config = new OllamaServerConfig('localhost', 11434);

// Initialize the transport client
$transportClient = new CurlTransportClient($config->getBaseUrl());

// Create the API client
$apiClient = new OllamaGenerationClient($transportClient);

// Create a generation request
$request = new GenerationRequest('llama3.2');
$request->setPrompt('What is artificial intelligence?');

// Generate text using the API client
$response = $apiClient->generate($request);

// Get the generated text
echo $response->getResponse();

// Access metadata if available
echo 'Model: ' . $response->getModel();
echo 'Created At: ' . $response->getCreatedAt();

Architecture

The library follows Domain-Driven Design principles with a clear separation of concerns:

Transport Layer

  • Tenqz\Ollama\Transport\Domain\Client\TransportClientInterface - Interface for HTTP clients
  • Tenqz\Ollama\Transport\Domain\Response\ResponseInterface - Interface for API responses
  • Tenqz\Ollama\Transport\Infrastructure\Http\Client\CurlTransportClient - cURL implementation

Generation Layer

  • Tenqz\Ollama\Generation\Application\DTO\Request\GenerationRequest - Request DTO for text generation
  • Tenqz\Ollama\Generation\Application\DTO\Response\GenerationResponse - Response DTO for generated text
  • Tenqz\Ollama\Generation\Domain\Client\GenerationClientInterface - Client interface for generation operations
  • Tenqz\Ollama\Generation\Infrastructure\Client\OllamaGenerationClient - Implementation of generation client

Shared Layer

  • Tenqz\Ollama\Shared\Infrastructure\Config\OllamaServerConfig - Configuration for Ollama server connection
  • Tenqz\Ollama\Shared\Infrastructure\Api\OllamaApiEndpoints - API endpoints constants
  • Contains cross-cutting concerns and components that are used by multiple domains

Requirements

  • PHP 7.2 or higher
  • cURL extension

License

The MIT License (MIT). Please see License File for more information.