Chat Completions against a configured OpenAI-compatible provider. Request and message shapes are OpenAI's (::openai::chat/Message, roles system, developer, user, assistant, tool); the request is a Map so provider-specific fields pass straight through.

Every function that calls the API takes a trailing opts Map, applied over the provider's endpoint for that call (see ::openai::compatible/resolve): api-key, base-url, or endpoint.

Functions

chat

fn (p: ::openai::compatible/CompatibleProvider, model: Str, message: Str): Str
fn (p: ::openai::compatible/CompatibleProvider, model: Str, message: Str, opts: Map): Str
fn (p: ::openai::compatible/CompatibleProvider, model: Str, message: Str, system: Str?): Str
fn (p: ::openai::compatible/CompatibleProvider, model: Str, message: Str, system: Str?, opts: Map): Str

Send one message and return the reply text.

Example

::openai-compatible::chat/chat(::openai-compatible/ollama(), "llama3.2", "Say hi")
::openai-compatible::chat/chat(::openai-compatible/groq(), "llama-3.3-70b-versatile", "2+2?", "Reply with just the number.")

complete

fn (p: ::openai::compatible/CompatibleProvider, request: Map): Map
fn (p: ::openai::compatible/CompatibleProvider, request: Map, opts: Map): Map

POST /chat/completions on the provider. Returns the decoded body (choices, usage, ...) as a Map, or an Err carrying ::openai::api/HttpError on a non-2xx response.

Example

::chat ::openai-compatible::chat
::oai ::openai::chat

response ::chat/complete(::openai-compatible/groq(), {
    model: "llama-3.3-70b-versatile",
    messages: [
        ::oai/system-message("You are terse."),
        ::oai/user-message("Why is the sky blue?")
    ]
})
response.choices[0].message.content

complete-stream

fn (p: ::openai::compatible/CompatibleProvider, request: Map): ::openai::chat/StreamingChatResponse
fn (p: ::openai::compatible/CompatibleProvider, request: Map, opts: Map): ::openai::chat/StreamingChatResponse

POST /chat/completions with stream: true. Returns a StreamingChatResponse whose body iterates SSE events, or an Err carrying ::openai::api/HttpError when the provider answers with an error status.

Example

::chat ::openai-compatible::chat
::oai ::openai::chat

stream ::chat/complete-stream(::openai-compatible/ollama(), {
    model: "llama3.2",
    messages: [::oai/user-message("Count to five.")]
})
for-each(stream.body, (event) { print(::oai/extract-chunk-text(event)) })

prepare-request

fn (p: ::openai::compatible/CompatibleProvider, request: Map): Map

Prepare a chat request for p: strips the routing prefix from model, merges the provider's extra-body, and sets the provider's output-ceiling field when the request has none. Pure; exposed for testing.

Example

p ::openai-compatible/configure(::openai-compatible/GROQ, "gsk_test")
req ::openai-compatible::chat/prepare-request(p, {model: "groq/llama-3.3-70b-versatile", messages: []})
req.model                  // => "llama-3.3-70b-versatile"
req.max_completion_tokens  // => 4096

reply-text

fn (response: Map): Str

First-choice message text from a chat response body, or "".

Example

::openai-compatible::chat/reply-text({choices: [{message: {content: "Hi"}}]})  // "Hi"
::openai-compatible::chat/reply-text({choices: []})  // ""