DiscordPHP-NHA Documentation

OllamaClient
in package

FinalYes

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 native POST /api/chat, which lets num_ctx / think be set explicitly;
  • a URL ending in /v1 (http://host:11434/v1, exactly what OpenCode's @ai-sdk/openai-compatible provider uses) uses OpenAI-compatible POST /v1/chat/completions.

The transport is injectable so the class can be unit-tested without a network; the default wraps Browser.

Tags
link
https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion
https://github.com/ollama/ollama/blob/main/docs/openai.md
since
3.0.0

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

$openai read-only

Whether the base URL points at an OpenAI-compatible `/v1` endpoint.

private bool $openai

$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:11434 for Ollama-native, or http://host:11434/v1 for the OpenAI-compatible endpoint (as in OpenCode's config).

$model : string

Model tag, e.g. gemma3:27b or a custom ollama create name.

$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

false disables 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: array}

openaiRequest()

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
Return values
array{0: string, 1: array}
On this page

Search results