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.

The Ollama class lives in the root AiSdk\ namespace (defined in src/Root/Ollama.php) and acts as a static facade over OllamaProvider. It manages a process-level singleton provider instance, meaning you configure the provider once at application bootstrap and all subsequent calls to Ollama::model(), Ollama::availableModels(), and Ollama::inspectModel() share that same instance automatically.

Namespace and Import

use AiSdk\Ollama;

Methods

Ollama::create()

public static function create(array $config = []): OllamaProvider
Creates a new OllamaProvider from the given configuration array and stores it as the process-level default. All subsequent calls to Ollama::model(), Ollama::availableModels(), and Ollama::inspectModel() will use this instance until Ollama::reset() is called. Internally, this passes the config array to OllamaOptions::fromArray(), which reads values from the array and falls back to environment variables (OLLAMA_API_KEY, OLLAMA_BASE_URL, OLLAMA_NATIVE_BASE_URL) when array keys are absent.
config
array
default:"[]"
Optional configuration array. Supported keys:
KeyTypeDefaultDescription
baseUrlstringhttp://localhost:11434/v1The OpenAI-compatible API base URL. Also read from OLLAMA_BASE_URL. Trailing slashes are stripped automatically.
nativeBaseUrlstringDerived from baseUrl by stripping /v1The native Ollama API base URL used for /api/tags and /api/show. Also read from OLLAMA_NATIVE_BASE_URL.
apiKeystring|nullnullBearer token sent in the Authorization header. Also read from OLLAMA_API_KEY. Pass null or omit for unauthenticated local installs.
apistring|OllamaApi'chat_completions'Which Ollama API to use. Accepts 'chat_completions' or 'responses'.
headersarray<string, string>[]Additional HTTP headers merged into every request.
sdkSdk|nullnullAn explicit AiSdk\Support\Sdk instance providing the HTTP client and PSR-17 factories. Falls back to the global Generate::sdk() singleton.
return
OllamaProvider
The newly created and stored OllamaProvider instance.

Ollama::model()

public static function model(string $modelId): Model
Creates a model reference for the given model ID string and returns it as an AiSdk\Contracts\Model instance. The model ID is treated as an opaque value — any string the Ollama server accepts is valid.
modelId
string
required
The model identifier to pass through to the Ollama server. Examples: 'llama3.2', 'acme/private-model:latest', 'llava', 'nomic-embed-text'.
return
Model
A model reference implementing AiSdk\Contracts\Model. Pass this directly to Generate::text()->model() or other generation builders.

Ollama::availableModels()

public static function availableModels(): array
Calls the Ollama server’s /api/tags endpoint and returns an array of ModelDefinition objects representing every model currently installed on that server. The HTTP request is issued synchronously using the configured SDK HTTP client.
return
array<int, ModelDefinition>
An array of AiSdk\ModelDefinition objects. Each object exposes:
PropertyTypeDescription
idstringThe model identifier (e.g. 'private/model:latest').
metadataarrayRaw metadata from the Ollama response. Contains modified_at, size, digest, and details keys when present on the server response.

Ollama::inspectModel()

public static function inspectModel(string $modelId): ModelDefinition
Calls the Ollama server’s /api/show endpoint for the specified model and returns a ModelDefinition with mapped SDK capabilities and full metadata. Capability mapping translates Ollama’s native capability strings (completion, vision, audio, tools, thinking, image, image_generation, embedding) into typed AiSdk\Capability enum values. See OllamaProvider for the full mapping table.
modelId
string
required
The model identifier to inspect (e.g. 'llava', 'llama3.2').
return
ModelDefinition
An AiSdk\ModelDefinition object. Key properties:
PropertyTypeDescription
idstringThe queried model identifier.
capabilitiesarray<int, Capability>Mapped SDK Capability enum values (e.g. TextGeneration, ImageInput, ToolCalling, Reasoning).
adaptedCapabilitiesarrayContains structured_output strategy info when the model supports completion.
metadataarrayRaw Ollama metadata: modified_at, details, model_info, and ollama_capabilities (the original string array from the server).

Ollama::default()

public static function default(): OllamaProvider
Returns the current singleton OllamaProvider. If no provider has been configured yet, it lazily creates one by calling Ollama::create() with an empty config array (which applies defaults and reads from environment variables).
return
OllamaProvider
The active singleton OllamaProvider instance.

Ollama::reset()

public static function reset(): void
Clears the singleton provider by setting the static $default property to null. The next call to any static method that requires a provider will lazily re-initialize it. This method is essential in test suites to prevent state leaking between test cases.
return
void

Usage Examples

// Configure once at bootstrap
Ollama::create([
    'baseUrl' => 'http://localhost:11434/v1',
    'apiKey'  => 'optional-token',
]);

// Create a model reference
$model = Ollama::model('llama3.2');

// List installed models
$models = Ollama::availableModels();
foreach ($models as $model) {
    echo $model->id . PHP_EOL;
}

// Inspect a model's capabilities
$def = Ollama::inspectModel('llava');
echo implode(', ', $def->capabilityNames());
Test teardown — always call Ollama::reset() in afterEach hooks alongside Generate::reset() to keep tests isolated:
afterEach(function () {
    Generate::reset();
    Ollama::reset();
});
Using the Responses API — switch the entire provider to Ollama’s /v1/responses endpoint at creation time:
Ollama::create(['api' => 'responses']);

$result = Generate::text('Summarize this document.')
    ->model(Ollama::model('llama3.2'))
    ->run();
Per-request API override — keep the default chat_completions API but route a single request through the Responses API using provider options:
Ollama::create(); // defaults to chat_completions

Generate::text('Hi')
    ->model(Ollama::model('llama3.2'))
    ->providerOptions('ollama', ['api' => 'responses'])
    ->run();
Ollama::create() should be called before any other facade methods in production code. Without it, the provider is lazily initialized with defaults — pointing at a local Ollama instance on http://localhost:11434/v1 with no API key. In long-running processes or frameworks with a service container, call Ollama::create() once during the bootstrap phase to ensure your custom baseUrl, apiKey, and headers are in effect for every request.

Build docs developers (and LLMs) love