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.

This page walks you through getting a complete, working response from a local Ollama model using the PHP AI SDK — from a clean project to printed output. By the end you will have installed the package, pulled a model, sent a generation request, and seen how to extend that into a streaming response.

Steps

1

Install the package

Add aisdk/ollama to your project with Composer:
composer require aisdk/ollama
This installs the Ollama provider together with aisdk/core and aisdk/openai-compatible. A PSR-18 HTTP client (Guzzle, Symfony HTTP Client, or any other compliant library) must also be present for the SDK to make HTTP requests.
2

Pull a model

Download a model onto your local Ollama server. This example uses Llama 3.2:
ollama pull llama3.2
If Ollama is not yet installed, visit ollama.com for platform-specific instructions. Run ollama serve to start the server if it is not already running.
3

Generate your first response

Create a PHP file with the following code and run it:
use AiSdk\Generate;
use AiSdk\Ollama;

$result = Generate::text()
    ->model(Ollama::model('llama3.2'))
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;
You should see the model’s response printed to your terminal within a few seconds.
4

Explore further

Now that you have a working request, head over to the Guides section to learn about streaming, embeddings, image generation, tool calling, and more.

What Just Happened

Each line in the example above maps to a distinct SDK concept:
CodeWhat it does
Ollama::model('llama3.2')Creates a model reference for llama3.2 on the default Ollama server (http://localhost:11434). The package does not validate the model ID locally — it is sent to the server as-is.
Generate::text()Starts building a text generation request using the fluent request builder provided by aisdk/core.
->prompt('...')Sets the user prompt for this request.
->run()Sends the request to Ollama’s Chat Completions endpoint and blocks until the full response is received.
$result->textA string containing the model’s complete response.
No API key or environment variable configuration is required for a local Ollama server. The provider connects to http://localhost:11434/v1 by default.

Streaming Responses

Instead of waiting for the full response, you can stream tokens as they are generated. Swap ->run() for ->stream()->chunks() and iterate:
use AiSdk\Generate;
use AiSdk\Ollama;

foreach (Generate::text('Tell me a story.')
    ->model(Ollama::model('llama3.2'))
    ->stream()
    ->chunks() as $chunk) {
    echo $chunk;
}
Each iteration of the loop yields the next piece of text from the model. This is ideal for CLI tools, server-sent events, or any interface where you want output to appear progressively rather than after a full round-trip.

Next Steps

Text Generation

Deep-dive into prompts, system messages, structured output, vision inputs, and tool calling with Generate::text().

Streaming

Learn how streaming works under the hood and how to wire chunks into HTTP responses for real-time UIs.

Embeddings

Generate single or batched text embeddings using Ollama’s native /api/embed endpoint via Generate::embedding().

Configuration

Customise base URLs, switch between the Chat Completions and Responses API surfaces, and set per-request provider options.

Build docs developers (and LLMs) love