Skip to content

nimgent/providers/openai_chat

This page is generated from the module’s exported API and ## documentation comments.

Chat Completions wire format (OpenRouter, Hyper, OpenAI-compat).

How an OpenAI-compatible Chat host wants replayed assistant thinking.

ChatReasoning = enum
crReasoning, ## `reasoning` plus `reasoning_details` (OpenRouter style).
crReasoningContent ## `reasoning_content`, required back by thinking-mode hosts.

View source

Encode an image as an OpenAI-compatible chat content part.

proc openAiImagePart(mimeType, data: string): JsonNode {.raises: [], tags: [],
forbids: [].}

Returns: JsonNode.

Name Type Default
mimeType string
data string

View source

Build an OpenAI-compatible Chat Completions request body.

proc buildChatBody(request: ProviderRequest; stream: bool;
includeConversationId = false; applyCache = false;
maxTokensField = "max_completion_tokens";
promptCacheKey = ""; reasoning = crReasoning): JsonNode {.
raises: [ProviderError, KeyError], tags: [ReadIOEffect, WriteIOEffect],
forbids: [].}

Returns: JsonNode.

Name Type Default
request ProviderRequest
stream bool
includeConversationId inferred false
applyCache inferred false
maxTokensField inferred "max_completion_tokens"
promptCacheKey inferred ""
reasoning inferred crReasoning

View source

Build Chat Completions options for native JSON schema output.

proc chatObjectOptions(name, description: string; schema: JsonNode): JsonNode {.
raises: [], tags: [], forbids: [].}

Returns: JsonNode.

Name Type Default
name string
description string
schema JsonNode

View source

Parse an OpenAI-compatible Chat Completions response.

proc parseChatOutput(data: JsonNode; failPrefix: string): ProviderResponse {.
raises: [KeyError, ProviderError], tags: [ReadIOEffect, WriteIOEffect],
forbids: [].}

Returns: ProviderResponse.

Name Type Default
data JsonNode
failPrefix string

View source

Consume one Chat Completions SSE event.

proc handleChatEvent(acc: var StreamAcc; response: var ProviderResponse;
data: JsonNode; onEvent: StreamCallback): SseAction {.
raises: [KeyError, Exception], tags: [RootEffect], forbids: [].}

Returns: SseAction.

Name Type Default
acc var StreamAcc
response var ProviderResponse
data JsonNode
onEvent StreamCallback

View source