CohereGateway
class CohereGateway implements EmbeddingGateway, RerankingGateway, StepTextGateway
Traits
Methods
Add structured output requirements to the instructions.
Build the request body for the Cohere Chat API.
Create an HTTP client with the provider's request options.
Select a boot-registered connection without retaining dynamic provider names.
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.
Process a Cohere Chat API event stream.
Emit a reasoning end event if a reasoning block is open.
Generate a lowercase UUID v7 for use as a stream event ID.
Map the given Hypervel attachments to Chat Completions content parts.
Map the given Hypervel messages to Chat Completions messages format.
Map a user message to Chat Completions format.
Map an assistant message to Chat Completions format.
Map a tool result message to Chat Completions format.
Serialize a tool call DTO to Chat Completions array format.
Map a tool call or result ID to the provider's wire format.
Map a provider tool to a Chat Completions tool definition.
Normalize the embeddings of a Cohere response into a list of vectors.
Parse an SSE stream body into decoded JSON data objects.
Read one line without waiting for a larger body buffer to fill.
Decode a structured output payload, tolerating markdown code fences.
Join reasoning blocks, separating each block with a blank line.
Parse the Cohere response data into a single step response.
Extract usage data from a Cohere usage object.
Map a Cohere finish reason to the finish reason enum.
Generate text for a single step in a conversation.
Stream text for a single step in a conversation.
Generate embedding vectors representing the given inputs.
Rerank the given documents based on their relevance to the query.
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 22
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 Cohere Chat API.
in
BuildsTextRequests at line 91
protected string|null
mapCohereToolChoice(ToolChoice $choice)
Map a tool choice to the Cohere tool_choice value.
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
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.
in
HandlesTextStreaming at line 33
protected Generator
processTextStream(string $invocationId, Provider $provider, string $model, StreamInterface $streamBody)
Process a Cohere Chat API event stream.
in
HandlesTextStreaming at line 201
protected Generator
endReasoning(string $invocationId, string|null $reasoningId)
Emit a reasoning end event if a reasoning block is open.
in
HandlesTextStreaming at line 219
protected string
generateEventId()
Generate a lowercase UUID v7 for use as a stream event ID.
in
MapsAttachments at line 23
protected array
mapAttachments(Collection $attachments)
Map the given Hypervel attachments to Chat Completions content parts.
in
MapsAttachments at line 63
protected bool
isImage(UploadedFile $attachment)
Determine if the given uploaded file is an image.
in
MapsChatCompletionMessages at line 19
protected array
mapMessagesToChat(array $messages, string|null $instructions = null)
Map the given Hypervel messages to Chat Completions messages format.
in
MapsChatCompletionMessages at line 46
protected void
mapUserMessage(UserMessage|Message $message, array $chatMessages)
Map a user message to Chat Completions format.
in
MapsChatCompletionMessages at line 69
protected void
mapAssistantMessage(AssistantMessage|Message $message, array $chatMessages)
Map an assistant message to Chat Completions format.
in
MapsChatCompletionMessages at line 89
protected void
mapToolResultMessage(ToolResultMessage|Message $message, array $chatMessages)
Map a tool result message to Chat Completions format.
in
MapsChatCompletionMessages at line 107
protected array
serializeToolCallToChat(ToolCall $toolCall)
Serialize a tool call DTO to Chat Completions array format.
in
MapsChatCompletionMessages at line 122
protected string
mapToolCallId(string $id)
Map a tool call or result ID to the provider's wire format.
in
MapsChatCompletionTools at line 22
protected array
mapTools(array $tools, Provider $provider)
Map the given tools to Chat Completions function definitions.
in
MapsChatCompletionTools at line 40
protected array
mapProviderTool(ProviderTool $tool, Provider $provider)
Map a provider tool to a Chat Completions tool definition.
in
MapsChatCompletionTools at line 48
protected array
mapTool(Tool $tool)
Map a regular tool to a Chat Completions function definition.
in
MapsChatCompletionTools at line 76
protected string|array
mapToolChoice(ToolChoice $choice)
Map a tool choice to the Chat Completions tool_choice shape.
in
ParsesEmbeddings at line 18
protected array
parseCohereEmbeddings(mixed $embeddings)
Normalize the embeddings of a Cohere response into a list of vectors.
in
ParsesServerSentEvents at line 24
protected Generator
parseServerSentEvents(StreamInterface $streamBody)
Parse an SSE stream body into decoded JSON data objects.
in
ParsesServerSentEvents at line 52
protected string
readLine(StreamInterface $streamBody)
Read one line without waiting for a larger body buffer to fill.
in
DecodesStructuredOutput at line 12
protected array
decodeStructuredOutput(string|null $text)
Decode a structured output payload, tolerating markdown code fences.
in
JoinsReasoning at line 16
static protected string
joinReasoning(iterable $blocks)
Join reasoning blocks, separating each block with a blank line.
in
ParsesTextResponses at line 27
protected void
validateTextResponse(mixed $data)
Validate the Cohere response data.
in
ParsesTextResponses at line 40
protected StepResponse
parseTextResponse(array $data, Provider $provider, string $model, bool $structured)
Parse the Cohere response data into a single step response.
in
ParsesTextResponses at line 80
protected TextUsage
extractUsage(array $usage)
Extract usage data from a Cohere usage object.
in
ParsesTextResponses at line 92
protected FinishReason
extractFinishReason(string|null $finishReason)
Map a Cohere finish reason to the finish reason enum.
at line 50
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 step in a conversation.
at line 79
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 step in a conversation.
at line 112
EmbeddingsResponse
generateEmbeddings(EmbeddingProvider $provider, string $model, array $inputs, int $dimensions, int $timeout = 30, array $providerOptions = [])
Generate embedding vectors representing the given inputs.
at line 154
RerankingResponse
rerank(RerankingProvider $provider, string $model, array $documents, string $query, int|null $limit = null, int $timeout = 30, array $providerOptions = [])
Rerank the given documents based on their relevance to the query.
at line 195
protected PendingRequest
client(Provider $provider, int $timeout = 30)
Get an HTTP client for the Cohere API.