Skip to main content

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.

OllamaProvider is the concrete provider class created by Ollama::create(). It extends AiSdk\Contracts\BaseProvider and implements multiple provider interfaces from aisdk/core, covering text generation, embeddings, image generation, model listing, and model inspection. Most applications interact with OllamaProvider indirectly through the Ollama facade — direct instantiation is an advanced pattern for cases where multiple provider instances with different configurations must coexist in the same process.

Namespace

use AiSdk\Ollama\OllamaProvider;

Implemented Interfaces

OllamaProvider implements the following contracts from aisdk/core:
InterfaceDescription
TextProviderInterfaceEnables text generation. The protected textModel() method returns an OllamaTextModel instance, which is resolved automatically by BaseProvider when $provider->model($id) is called with a text context.
EmbeddingProviderInterfaceEnables embedding generation. The protected embeddingModel() method returns an OllamaEmbeddingModel instance.
ImageProviderInterfaceEnables image generation. The protected imageModel() method returns an OllamaImageModel instance.
AvailableModelsProviderInterfaceExposes the availableModels() method, which fetches the list of locally installed models from the Ollama server’s /api/tags endpoint.
ModelInspectionProviderInterfaceExposes the inspectModel() method, which fetches detailed capabilities and metadata for a specific model from /api/show.

Constructor

public function __construct(public readonly OllamaOptions $options) {}
Takes a fully configured OllamaOptions instance. The options property is publicly accessible (read-only) after construction, allowing inspection of the active configuration. The recommended way to build an OllamaOptions instance is via its named static factory:
$options = OllamaOptions::fromArray($config);
options
OllamaOptions
required
A configured OllamaOptions value object. Carries the baseUrl, nativeBaseUrl, apiKey, api, headers, and optional sdk instance. Build with OllamaOptions::fromArray().

Methods

name()

public function name(): string
Returns the provider’s canonical name string, used internally by the SDK to identify the provider in error messages and telemetry.
return
string
Always returns 'ollama' (the value of OllamaOptions::PROVIDER_NAME).

availableModels()

public function availableModels(): array
Issues a GET request to {nativeBaseUrl}/api/tags and parses the response into an array of ModelDefinition objects. Each model entry in the Ollama response is mapped to a ModelDefinition using the model field (falling back to name) as the id, and preserving modified_at, size, digest, and details in the metadata. Models with missing or empty id fields are silently skipped.
return
array<int, ModelDefinition>
An array of AiSdk\ModelDefinition objects, one per installed model. Each object exposes:
PropertyTypeDescription
idstringThe model identifier as reported by the Ollama server (e.g. 'llama3.2:latest', 'private/model:latest').
metadataarrayA filtered associative array. Present keys: modified_at (string), size (int), digest (string), details (array). Keys are omitted when the server does not return a value for them.

inspectModel()

public function inspectModel(string $modelId): ModelDefinition
Issues a POST request to {nativeBaseUrl}/api/show with body {"model": $modelId, "verbose": false} and returns a ModelDefinition with fully mapped capabilities. The Ollama-native capability strings are translated to typed AiSdk\Capability enum values according to this mapping:
Ollama capabilitySDK Capability values
completionTextGeneration, Streaming, TextInput
visionImageInput
audioAudioInput
toolsToolCalling
thinkingReasoning
image / image_generationImageGeneration
embeddingEmbedding
When completion is present, structured_output is also added to adaptedCapabilities with strategy 'JSON schema passed through the response format'.
modelId
string
required
The model identifier to inspect (e.g. 'llava', 'llama3.2', 'private/model:latest').
return
ModelDefinition
An AiSdk\ModelDefinition object with:
PropertyTypeDescription
idstringThe queried $modelId value.
capabilitiesarray<int, Capability>Mapped SDK Capability enum instances derived from the Ollama capability strings.
adaptedCapabilitiesarrayContains structured_output info when completion is in the native capabilities array; empty array otherwise.
metadataarrayContains modified_at, details, model_info, and ollama_capabilities (the original raw string array from Ollama). Keys are omitted when the server does not return values for them.

Direct Instantiation

For advanced use cases — such as running multiple Ollama providers pointing at different servers in the same process — you can bypass the facade and instantiate OllamaProvider directly:
use AiSdk\Ollama\OllamaOptions;
use AiSdk\Ollama\OllamaProvider;

$options = OllamaOptions::fromArray([
    'baseUrl' => 'http://localhost:11434/v1',
    'apiKey'  => 'optional-token',
]);

$provider = new OllamaProvider($options);
You can then use the provider instance directly with generation builders:
$model = $provider->model('llama3.2');

$result = Generate::text('Explain recursion.')
    ->model($model)
    ->run();

echo $result->text;
Or use it for model discovery without involving the facade:
// List installed models
$models = $provider->availableModels();
foreach ($models as $definition) {
    echo $definition->id . PHP_EOL;
}

// Inspect a specific model
$definition = $provider->inspectModel('llava');
echo implode(', ', $definition->capabilityNames());

The protected textModel(), embeddingModel(), and imageModel() methods are part of the BaseProvider contract and are called automatically by the SDK’s routing logic when $provider->model($id) is invoked in the appropriate context. You do not call them directly. They return OllamaTextModel, OllamaEmbeddingModel, and OllamaImageModel instances respectively, each initialized with the same OllamaOptions instance held by the provider.

Build docs developers (and LLMs) love