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.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.
Steps
Install the package
Add This installs the Ollama provider together with
aisdk/ollama to your project with Composer: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.Pull a model
Download a model onto your local Ollama server. This example uses Llama 3.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.Generate your first response
Create a PHP file with the following code and run it:You should see the model’s response printed to your terminal within a few seconds.
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:| Code | What 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->text | A string containing the model’s complete response. |
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:
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.
