OllamaGateway
class OllamaGateway implements EmbeddingGateway, StepTextGateway
Traits
Methods
Add structured output requirements to the instructions.
Build the request body for the current text generation step.
Build the request body for the Ollama Chat API.
Build a request body from pre-mapped chat messages.
Build the response format schema for structured output.
Create an HTTP client with the provider's request options.
Select a boot-registered connection without retaining dynamic provider names.
Process an Ollama NDJSON streaming response and yield Hypervel stream events.
Parse an Ollama NDJSON stream, yielding each parsed JSON object.
Generate a lowercase UUID v7 for use as a stream event ID.
Map the given Hypervel attachments to Ollama base64 image strings.
Map the given Hypervel messages to Ollama Chat API messages format.
Map a user message to Ollama Chat API format.
Map an assistant message to Ollama Chat API format.
Map a tool result message to Ollama Chat API format.
Serialize a tool call DTO to Ollama Chat API array format.
Decode a structured output payload, tolerating markdown code fences.
Parse the Ollama response data into a single step response.
Parse tool call arguments, handling both array and JSON string formats.
Extract usage data from the Ollama response.
Extract and map the finish reason from the Ollama response.
Execute a callback with failoverable error handling.
Get the short Retry-After delay of a rate limited or overloaded response, if it has one.
Determine if the response is a 402, or a 429 insufficient credits or quota error rather than a rate limit.
Get the status codes that indicate a provider is transiently unavailable and the request should fail over.
Get the patterns used to detect insufficient credits or quota errors.
Get the error codes or types that indicate insufficient credits or quota on a 429 response.
Generate text for a single Ollama Chat API step.
Stream text for a single Ollama Chat API step.
Generate embeddings for the given inputs.
Details
in
ComposesSchemaInstructions at line 14
protected string|null
composeInstructions(string|null $instructions, array|null $schema)
Add structured output requirements to the instructions.
in
BuildsTextRequests at line 26
protected array
buildStepBody(TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, StepContext $stepContext)
Build the request body for the current text generation step.
in
BuildsTextRequests at line 42
protected array
buildTextRequestBody(Provider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options)
Build the request body for the Ollama Chat API.
in
BuildsTextRequests at line 64
protected array
buildChatRequestBody(Provider $provider, string $model, array $chatMessages, array $tools, array|null $schema, TextGenerationOptions|null $options, bool $stream = false)
Build a request body from pre-mapped chat messages.
in
BuildsTextRequests at line 126
protected array
buildResponseFormat(array $schema)
Build the response format schema for structured output.
in
CreatesClient at line 21
protected PendingRequest
createClient(string $baseUrl, array $headers = [], array $configuredHeaders = [], int|null $timeout = 60, bool $throw = true)
Create an HTTP client with the provider's request options.
in
CreatesClient at line 43
protected string
httpConnection(Provider $provider)
Select a boot-registered connection without retaining dynamic provider names.
in
CreatesOllamaClient at line 18
protected PendingRequest
client(Provider $provider, int|null $timeout = null)
Get an HTTP client for the Ollama API.
in
CreatesOllamaClient at line 33
protected string
baseUrl(Provider $provider)
Get the base URL for the Ollama API.
in
HandlesTextStreaming at line 35
protected Generator
processTextStream(string $invocationId, Provider $provider, string $model, StreamInterface $streamBody)
Process an Ollama NDJSON streaming response and yield Hypervel stream events.
in
HandlesTextStreaming at line 256
protected Generator
parseNdjsonStream(StreamInterface $streamBody)
Parse an Ollama NDJSON stream, yielding each parsed JSON object.
in
HandlesTextStreaming at line 276
protected string
generateEventId()
Generate a lowercase UUID v7 for use as a stream event ID.
in
MapsAttachments at line 22
protected array
mapAttachments(Collection $attachments)
Map the given Hypervel attachments to Ollama base64 image strings.
in
MapsAttachments at line 47
protected bool
isImage(UploadedFile $attachment)
Determine if the given uploaded file is an image.
in
MapsMessages at line 19
protected array
mapMessagesToChat(array $messages, string|null $instructions = null)
Map the given Hypervel messages to Ollama Chat API messages format.
in
MapsMessages at line 46
protected void
mapUserMessage(UserMessage|Message $message, array $chatMessages)
Map a user message to Ollama Chat API format.
in
MapsMessages at line 67
protected void
mapAssistantMessage(AssistantMessage|Message $message, array $chatMessages)
Map an assistant message to Ollama Chat API format.
in
MapsMessages at line 86
protected void
mapToolResultMessage(ToolResultMessage|Message $message, array $chatMessages)
Map a tool result message to Ollama Chat API format.
in
MapsMessages at line 108
protected array
serializeToolCallToChat(ToolCall $toolCall)
Serialize a tool call DTO to Ollama Chat API array format.
Ollama's /api/chat message history shape only uses the function object for assistant tool calls — no top-level type key.
That key belongs on the /api/chat tools definitions, not the message history.
in
MapsTools at line 19
protected array
mapTools(array $tools)
Map the given tools to Ollama function definitions.
in
MapsTools at line 39
protected array
mapTool(Tool $tool)
Map a regular tool to an Ollama function definition.
in
DecodesStructuredOutput at line 12
protected array
decodeStructuredOutput(string|null $text)
Decode a structured output payload, tolerating markdown code fences.
in
ParsesTextResponses at line 26
protected void
validateTextResponse(array $data)
Validate the Ollama response data.
in
ParsesTextResponses at line 39
protected StepResponse
parseTextResponse(array $data, Provider $provider, bool $structured)
Parse the Ollama response data into a single step response.
in
ParsesTextResponses at line 82
protected array
parseToolArguments(mixed $arguments)
Parse tool call arguments, handling both array and JSON string formats.
in
ParsesTextResponses at line 94
protected TextUsage
extractUsage(array $data)
Extract usage data from the Ollama response.
in
ParsesTextResponses at line 106
protected FinishReason
extractFinishReason(array $data)
Extract and map the finish reason from the Ollama response.
in
HandlesFailoverErrors at line 26
protected mixed
withErrorHandling(string $providerName, Closure $callback)
Execute a callback with failoverable error handling.
in
HandlesFailoverErrors at line 91
protected int|null
retryAfterSeconds(Throwable $exception)
Get the short Retry-After delay of a rate limited or overloaded response, if it has one.
in
HandlesFailoverErrors at line 115
protected bool
isInsufficientCredits(RequestException $exception)
Determine if the response is a 402, or a 429 insufficient credits or quota error rather than a rate limit.
in
HandlesFailoverErrors at line 138
protected array
overloadedStatusCodes()
Get the status codes that indicate a provider is transiently unavailable and the request should fail over.
in
HandlesFailoverErrors at line 148
protected array
insufficientCreditPatterns()
Get the patterns used to detect insufficient credits or quota errors.
in
HandlesFailoverErrors at line 158
protected array
insufficientCreditCodes()
Get the error codes or types that indicate insufficient credits or quota on a 429 response.
at line 44
__construct(Dispatcher $events)
Create an Ollama gateway instance.
at line 51
StepResponse
generateTextStep(TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, int|null $timeout, StepContext $stepContext)
Generate text for a single Ollama Chat API step.
at line 80
Generator
generateStreamStep(string $invocationId, TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, int|null $timeout, StepContext $stepContext)
Stream text for a single Ollama Chat API step.
at line 109
EmbeddingsResponse
generateEmbeddings(EmbeddingProvider $provider, string $model, array $inputs, int $dimensions, int $timeout = 30, array $providerOptions = [])
Generate embeddings for the given inputs.