Functions

assistant-message

fn (content: Str): Message

Create an assistant message, typically for multi-turn conversation history.

Example

messages [
    ::qwen::chat/user-message("My name is Alice."),
    ::qwen::chat/assistant-message("Nice to meet you, Alice!"),
    ::qwen::chat/user-message("What is my name?")
]

chat

fn (model: Str, message: Str): Str
fn (model: Str, message: Str, opts: Map): Str
fn (model: Str, message: Str, system: Str): Str
fn (model: Str, message: Str, system: Str, opts: Map): Str
fn (model: Str, message: Str, system: Str?, max-tokens: Int?): Str
fn (model: Str, message: Str, system: Str?, max-tokens: Int?, opts: Map): Str

Simple chat — send a message and get the assistant's reply as a string.

Accepts an optional system prompt and max token limit. This is a convenience wrapper around complete that returns the extracted text directly.

Example

answer ::qwen::chat/chat("qwen3.7-plus", "What is the capital of France?")
// => "The capital of France is Paris."

// With system instructions
answer ::qwen::chat/chat("qwen3.8-flash", "What is 2+2?", "Respond with just the number.")
// => "4"

complete

fn (request: ChatCompletionRequest): ChatCompletionResponse | ::openai::api/HttpError
fn (request: ChatCompletionRequest, opts: Map): ChatCompletionResponse | ::openai::api/HttpError

Create a chat completion (non-streaming).

Returns a ChatCompletionResponse with id, choices, model, and usage fields.

Example — simple

response ::qwen::chat/complete(::qwen::chat/ChatCompletionRequest({
    model: "qwen3.7-plus",
    messages: [{role: "user", content: "Say 'Hello, Hot!' and nothing else."}],
    max_tokens: 50
}))

response.id                                // => "chatcmpl-..."
content response.choices[0].message.content
response.usage.total_tokens                // => 42

Example — with a system message

response ::qwen::chat/complete(::qwen::chat/ChatCompletionRequest({
    model: "qwen3.7-plus",
    messages: [
        {role: "system", content: "You are a helpful assistant that responds in JSON."},
        {role: "user", content: "What is 2+2? Reply with {\"answer\": <number>}"}
    ],
    response_format: {type: "json_object"},
    max_tokens: 100
}))

Example — thinking mode, without streaming

Hybrid models return their reasoning in choices[0].message.reasoning_content. Some open-weight models only allow thinking when streaming; use complete-stream for those.

response ::qwen::chat/complete(::qwen::chat/ChatCompletionRequest({
    model: "qwen3.7-plus",
    messages: [{role: "user", content: "Is 89 prime? Show your reasoning."}],
    enable_thinking: true,
    thinking_budget: 2048
}))
reasoning response.choices[0].message.reasoning_content

complete-stream

fn (request: ChatCompletionRequest): StreamingChatResponse | ::openai::api/HttpError
fn (request: ChatCompletionRequest, opts: Map): StreamingChatResponse | ::openai::api/HttpError

Create a streaming chat completion. Returns a StreamingChatResponse with status, headers, and a body iterator that yields SSE events.

Each event has {event, data} where data is a ChatCompletionChunk with choices[0].delta.content (and, when thinking mode is enabled, choices[0].delta.reasoning_content).

Example

response ::qwen::chat/complete-stream(::qwen::chat/ChatCompletionRequest({
    model: "qwen3.7-plus",
    messages: [{role: "user", content: "Count from 1 to 3."}],
    max_tokens: 50
}))

response.status // => 200

for-each(response.body, (event) {
    text ::qwen::chat/extract-chunk-text(event)
    cond {
        gt(length(text), 0) => { print(text) }
    }
})

extract-chunk-reasoning

fn (event): Str

Extract the chain-of-thought (reasoning_content) from a streaming SSE event. Qwen models in thinking mode stream their reasoning separately from the final answer. Returns empty string if the event has no reasoning content.

Example

response ::qwen::chat/complete-stream(::qwen::chat/ChatCompletionRequest({
    model: "qwen3.7-plus",
    messages: [{role: "user", content: "Is 89 prime?"}],
    enable_thinking: true
}))

for-each(response.body, (event) {
    thought ::qwen::chat/extract-chunk-reasoning(event)
    answer  ::qwen::chat/extract-chunk-text(event)
    cond {
        gt(length(thought), 0) => { print(`[thinking] ${thought}`) }
        gt(length(answer), 0)  => { print(answer) }
    }
})

extract-chunk-text

fn (event): Str

Extract the delta text content from a streaming SSE event. Returns empty string if the event has no content (for example, role-only deltas, or reasoning-only deltas when thinking mode is enabled — use extract-chunk-reasoning for the latter).

Example

response ::qwen::chat/complete-stream(::qwen::chat/ChatCompletionRequest({
    model: "qwen3.8-flash",
    messages: [::qwen::chat/user-message("Hello")]
}))

for-each(response.body, (event) {
    text ::qwen::chat/extract-chunk-text(event)
    cond { gt(length(text), 0) => { print(text) } }
})

extract-response-text

fn (response: ChatCompletionResponse): Str

Extract the assistant text from a ChatCompletionResponse. Returns the first choice's message.content, or empty string if unavailable.

Example

response ::qwen::chat/complete(::qwen::chat/ChatCompletionRequest({
    model: "qwen3.7-plus",
    messages: [{role: "user", content: "Say 'test'"}],
    max_tokens: 50
}))

text ::qwen::chat/extract-response-text(response)

is-stream-done

fn (event): Bool

Check whether a streaming SSE event indicates the stream is complete. Returns true when the event carries the [DONE] marker or a non-null finish_reason on the first choice.

Example

response ::qwen::chat/complete-stream(::qwen::chat/ChatCompletionRequest({
    model: "qwen3.8-flash",
    messages: [::qwen::chat/user-message("Hello")]
}))

for-each(response.body, (event) {
    cond { ::qwen::chat/is-stream-done(event) => { println("---done---") } }
})

request-body

fn (request: ChatCompletionRequest): Map

The JSON body for a ChatCompletionRequest: unset optional fields are omitted rather than sent as null. Pure; exposed for testing.

Example

body ::qwen::chat/request-body(::qwen::chat/ChatCompletionRequest({
    model: "qwen3.8-flash",
    messages: [{role: "user", content: "Hi"}],
    top_k: 20
}))
body.top_k  // 20
in("temperature", keys(body))  // false

system-message

fn (content: Str): Message

Create a system message for setting model behavior.

Example

msg ::qwen::chat/system-message("You are a helpful math tutor.")
msg.role // => "system"

user-message

fn (content: Str): Message

Create a user message for use in a chat completion request.

Example

msg ::qwen::chat/user-message("Hello")
msg.role    // => "user"
msg.content // => "Hello"

Types

ChatChoice

ChatChoice type {
    index: Int,
    message: Message,
    finish_reason: FinishReason?,
    logprobs: Any?
}

ChatCompletionChunk

ChatCompletionChunk type {
    id: Str,
    object: Str,
    created: Int,
    model: Str,
    choices: Vec<StreamChoice>,
    usage: Usage?
}

ChatCompletionRequest

ChatCompletionRequest type {
    model: Str,
    messages: Vec<Message>,
    max_tokens: Int?,
    max_completion_tokens: Int?,
    temperature: Dec?,
    top_p: Dec?,
    top_k: Int?,
    n: Int?,
    stream: Bool?,
    stream_options: Map?,
    stop: Any?,
    presence_penalty: Dec?,
    repetition_penalty: Dec?,
    seed: Int?,
    logprobs: Bool?,
    top_logprobs: Int?,
    user: Str?,
    tools: Vec<Tool>?,
    tool_choice: Any?,
    parallel_tool_calls: Bool?,
    tool_stream: Bool?,
    response_format: ResponseFormat?,
    enable_thinking: Bool?,
    thinking_budget: Int?,
    preserve_thinking: Bool?,
    reasoning_effort: Str?,
    enable_search: Bool?,
    search_options: Map?,
    enable_code_interpreter: Bool?,
    vl_high_resolution_images: Bool?
}

ChatCompletionResponse

ChatCompletionResponse type {
    id: Str,
    object: Str,
    created: Int,
    model: Str,
    choices: Vec<ChatChoice>,
    usage: Usage?,
    system_fingerprint: Str?
}

Delta

Delta type {
    role: Role?,
    content: Str?,
    reasoning_content: Str?,
    tool_calls: Vec<Map>?
}

FinishReason

FinishReason type "stop" | "length" | "tool_calls"

FunctionDefinition

FunctionDefinition type {
    name: Str,
    description: Str?,
    parameters: Any?
}

Message

Message type {
    role: Role,
    content: Any,
    name: Str?,
    tool_calls: Vec<Map>?,
    tool_call_id: Str?,
    reasoning_content: Str?
}

ResponseFormat

ResponseFormat type {
    type: Str,
    json_schema: Any?
}

Role

Role type "user" | "system" | "developer" | "assistant" | "tool"

StreamChoice

StreamChoice type {
    index: Int,
    delta: Delta,
    finish_reason: FinishReason?
}

StreamingChatResponse

StreamingChatResponse type {
    status: Int,
    headers: Map,
    body: Any
}

Tool

Tool type {
    type: Str,
    function: FunctionDefinition
}

Usage

Usage type {
    prompt_tokens: Int,
    completion_tokens: Int,
    total_tokens: Int,
    prompt_tokens_details: Map?,
    completion_tokens_details: Map?
}