Documentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/ollama/llms.txt
Use this file to discover all available pages before exploring further.
aisdk/ollama is the official Ollama provider for the PHP AI SDK. It connects your PHP application to an Ollama server — local or remote — and exposes text generation, streaming, native embeddings, and Ollama’s experimental OpenAI-compatible image generation endpoint. Because it is built on aisdk/core, it is entirely framework-agnostic: it works in plain PHP scripts, Laravel, Symfony, or any other context without additional adapters. No API key is required when running Ollama locally, making it the fastest way to get a fully offline LLM pipeline into a PHP project. The provider supports both the Chat Completions endpoint (the default) and the Responses endpoint, giving you flexibility as Ollama’s API surface evolves.
Project Structure
The package is distributed via Composer asaisdk/ollama and targets PHP ^8.3. It depends on two other SDK packages:
| Dependency | Role |
|---|---|
aisdk/core | Shared contracts, Generate entry point, model definitions |
aisdk/openai-compatible | OpenAI-compatible HTTP layer reused for Chat Completions and Responses |
Feature Overview
Text Generation
Generate text from any installed Ollama model using
Generate::text(). Supports prompts, system messages, structured output, tool calling, and vision inputs.Streaming
Stream tokens in real time with
->stream()->chunks(). Works with Chat Completions and the Responses endpoint for interactive output.Embeddings
Produce single or batched embeddings using Ollama’s native
/api/embed endpoint. Pass truncate, keep_alive, and custom dimensions via provider options.Image Generation
Generate images via Ollama’s experimental OpenAI-compatible image endpoint using
Generate::image(). Experimental — model support varies.Model Discovery
List all models installed on the server with
Ollama::availableModels(), or inspect capabilities — vision, tools, embeddings, reasoning — with Ollama::inspectModel().Configuration
Customise base URLs, API key, default API surface, and per-request headers. Supports environment variables and programmatic configuration.
The Ollama Facade
The public entry point for every interaction is the Ollama class in the AiSdk\ namespace, located at src/Root/Ollama.php. It is a static facade over OllamaProvider and exposes six static methods:
Ollama::create(array $config = [])
Instantiates a new OllamaProvider with the given configuration and registers it as the default provider for the current process. Call this once at bootstrap time when you need non-default settings:
create() is never called, the facade lazily initialises a provider using environment variables and built-in defaults the first time any other method is invoked.
Ollama::model(string $modelId)
Returns an AiSdk\Contracts\Model reference for the given model ID. Pass the result directly to Generate::text(), Generate::embedding(), or Generate::image(). The model ID is any name installed on the configured Ollama server — the package does not maintain a model inventory.
Ollama::availableModels()
Calls Ollama’s native /api/tags endpoint and returns an array of ModelDefinition objects, each carrying the model’s id and server-reported metadata such as modified_at, size, and details.
Ollama::inspectModel(string $modelId)
Calls Ollama’s /api/show endpoint for a specific model and returns a ModelDefinition populated with the capabilities reported by the server — such as image_input (vision), tools, embedding, and thinking (reasoning). This is informational only; it does not change adapter behaviour, and ordinary generation calls do not trigger hidden discovery requests.
Ollama::default()
Returns the currently registered OllamaProvider instance, lazily creating one with default settings if create() has not yet been called. This is called internally by model(), availableModels(), and inspectModel(), but you can also call it directly when you need a reference to the underlying provider:
Ollama::reset()
Clears the registered OllamaProvider instance, setting it back to null. The next call to any facade method will trigger lazy re-initialisation. This is primarily useful in tests where you want a clean provider state between test cases:
Two API Surfaces
Chat Completions (Default)
The default API surface is Ollama’s OpenAI-compatible Chat Completions endpoint (/v1/chat/completions). It provides the broadest feature coverage: vision inputs, function tools, reasoning effort controls, and structured output via JSON schema passed through the response format field.
Responses
Ollama also exposes a Responses endpoint, mirroring the OpenAI Responses API. Select it globally or per request:api option are chat_completions and responses.
No API key is required when running Ollama locally.
OLLAMA_API_KEY and the apiKey config option exist only if your Ollama server is protected by a bearer token — for example, when hosted remotely or behind an authenticating proxy.