tenqz / ollama
Php library for working with Ollama server.
v0.4.0
2025-07-24 20:58 UTC
Requires
- php: ^7.2|^8.0
- ext-curl: *
- ext-json: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.19
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^8.5
- squizlabs/php_codesniffer: ^3.7
README
Ollama PHP Client Library
Documentation for version v0.4.0
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 clientsTenqz\Ollama\Transport\Domain\Response\ResponseInterface
- Interface for API responsesTenqz\Ollama\Transport\Infrastructure\Http\Client\CurlTransportClient
- cURL implementation
Generation Layer
Tenqz\Ollama\Generation\Application\DTO\Request\GenerationRequest
- Request DTO for text generationTenqz\Ollama\Generation\Application\DTO\Response\GenerationResponse
- Response DTO for generated textTenqz\Ollama\Generation\Domain\Client\GenerationClientInterface
- Client interface for generation operationsTenqz\Ollama\Generation\Infrastructure\Client\OllamaGenerationClient
- Implementation of generation client
Shared Layer
Tenqz\Ollama\Shared\Infrastructure\Config\OllamaServerConfig
- Configuration for Ollama server connectionTenqz\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.