OllamaClient
in package
Minimal async chat client for a local LLM server, driven by the bot's ReactPHP event loop.
Two wire protocols are supported, chosen from the base URL:
- a bare origin (
http://host:11434) uses Ollama's nativePOST /api/chat, which letsnum_ctx/thinkbe set explicitly; - a URL ending in
/v1(http://host:11434/v1, exactly what OpenCode's@ai-sdk/openai-compatibleprovider uses) uses OpenAI-compatiblePOST /v1/chat/completions.
The transport is injectable so the class can be unit-tested without a network; the default wraps Browser.
Tags
Table of Contents
Properties
- $baseUrl : string
- $model : string
- $numCtx : int
- $openai : bool
- Whether the base URL points at an OpenAI-compatible `/v1` endpoint.
- $think : bool|null
- $timeout : float
- $transport : callable(string, string, array<string, string>, string): PromiseInterface<string|int, string>
Methods
- __construct() : mixed
- chat() : PromiseInterface<string|int, string>
- Sends a non-streaming chat completion and resolves with the assistant message text.
- browserTransport() : callable(string, string, array<string, string>, string): PromiseInterface<string|int, string>
-
ollamaRequest()
: array{0: string, 1: array
} -
openaiRequest()
: array{0: string, 1: array
}
Properties
$baseUrl read-only
private
string
$baseUrl
$model read-only
private
string
$model
$numCtx read-only
private
int
$numCtx
= 32768
$openai read-only
Whether the base URL points at an OpenAI-compatible `/v1` endpoint.
private
bool
$openai
$think read-only
private
bool|null
$think
= null
$timeout read-only
private
float
$timeout
= 120.0
$transport
private
callable(string, string, array<string, string>, string): PromiseInterface<string|int, string>
$transport
Methods
__construct()
public
__construct(string $baseUrl, string $model[, callable|null $transport = null ][, float $timeout = 120.0 ][, int $numCtx = 32768 ][, LoopInterface|null $loop = null ][, bool|null $think = null ]) : mixed
Parameters
- $baseUrl : string
-
http://host:11434for Ollama-native, orhttp://host:11434/v1for the OpenAI-compatible endpoint (as in OpenCode's config). - $model : string
-
Model tag, e.g.
gemma3:27bor a customollama createname. - $transport : callable|null = null
-
fn(string $method, string $url, array $headers, string $body): PromiseInterface<string>resolving with the raw response body. Defaults to a Browser. - $timeout : float = 120.0
-
Per-request timeout in seconds (LLM replies are slow).
- $numCtx : int = 32768
-
Context window to request, in tokens (Ollama-native mode only).
- $loop : LoopInterface|null = null
-
Only used to build the default transport.
- $think : bool|null = null
-
falsedisables a thinking model's reasoning pass (faster);null(default) leaves it unset (Ollama-native mode only).
chat()
Sends a non-streaming chat completion and resolves with the assistant message text.
public
chat(array<int, array{role: string, content: string}> $messages[, string|array<string|int, mixed>|null $format = 'json' ][, float $temperature = 0.4 ]) : PromiseInterface<string|int, string>
Parameters
- $messages : array<int, array{role: string, content: string}>
- $format : string|array<string|int, mixed>|null = 'json'
-
"json", a JSON schema, or null for free text. - $temperature : float = 0.4
-
Sampling temperature.
Return values
PromiseInterface<string|int, string>browserTransport()
private
static browserTransport(float $timeout, LoopInterface|null $loop) : callable(string, string, array<string, string>, string): PromiseInterface<string|int, string>
Parameters
- $timeout : float
- $loop : LoopInterface|null
Return values
callable(string, string, array<string, string>, string): PromiseInterface<string|int, string>ollamaRequest()
private
ollamaRequest(array<int, array{role: string, content: string}> $messages, string|array<string|int, mixed>|null $format, float $temperature) : array{0: string, 1: array}
Parameters
- $messages : array<int, array{role: string, content: string}>
- $format : string|array<string|int, mixed>|null
- $temperature : float
Return values
array{0: string, 1: arrayopenaiRequest()
private
openaiRequest(array<int, array{role: string, content: string}> $messages, string|array<string|int, mixed>|null $format, float $temperature) : array{0: string, 1: array}
Parameters
- $messages : array<int, array{role: string, content: string}>
- $format : string|array<string|int, mixed>|null
- $temperature : float