Functions
assistant-message
fn (content: Str): Message
Create an assistant message, typically for multi-turn conversation history.
Example
messages [
::openai::chat/user-message("My name is Alice."),
::openai::chat/assistant-message("Nice to meet you, Alice!"),
::openai::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 a response string.
A convenience wrapper around complete that accepts a model, message, optional system prompt, and optional max tokens. Returns the assistant's reply as a plain Str.
Example - Basic
response ::openai::chat/chat("gpt-4o-mini", "What is 1+1?")
// => "2"
Example - With system prompt
response ::openai::chat/chat("gpt-4o-mini", "What is 2+2?", "You are a math tutor. Reply with just the number.")
// => "4"
Example - With max tokens
response ::openai::chat/chat("gpt-4o-mini", "Tell me a story.", "You are a storyteller.", 200)
Example - With a per-call key
response ::openai::chat/chat("gpt-4o-mini", "What is 1+1?", {api-key: tenant-key})
complete
fn (request: ChatCompletionRequest): ChatCompletionResponse
fn (request: ChatCompletionRequest, opts: Map): ChatCompletionResponse
Create a chat completion (non-streaming).
Returns a ChatCompletionResponse with id, choices, model, and usage fields.
Example - Simple
response ::openai::chat/complete(::openai::chat/ChatCompletionRequest({
model: "gpt-4o-mini",
max_completion_tokens: 50,
messages: [{role: "user", content: "Say 'Hello, Hot!' and nothing else."}]
}))
response.id // => "chatcmpl-abc123"
content response.choices[0].message.content
response.usage.total_tokens // => 42
Example - With system message
response ::openai::chat/complete(::openai::chat/ChatCompletionRequest({
model: "gpt-4o-mini",
max_completion_tokens: 100,
messages: [
{role: "system", content: "You are a helpful assistant that responds in JSON."},
{role: "user", content: "What is 2+2? Reply with {\"answer\": <number>}"}
]
}))
Example - With tools
response ::openai::chat/complete(::openai::chat/ChatCompletionRequest({
model: "gpt-4o-mini",
max_completion_tokens: 200,
tools: [
{
type: "function",
function: {
name: "get_weather",
description: "Get the current weather for a location",
parameters: {
type: "object",
properties: {
location: {type: "string", description: "The city and state"}
},
required: ["location"]
}
}
}
],
messages: [{role: "user", content: "What's the weather in San Francisco?"}]
}))
complete-stream
fn (request: ChatCompletionRequest): StreamingChatResponse
fn (request: ChatCompletionRequest, opts: Map): StreamingChatResponse
Create a streaming chat completion. Returns a StreamingChatResponse with status, headers, and a body iterator that yields SSE events.
Event Structure
Each event from the iterator has {event, data} where data contains:
id- Completion IDchoices[0].delta.content- Text chunk (may be null)choices[0].finish_reason- "stop" when complete
Example
response ::openai::chat/complete-stream(::openai::chat/ChatCompletionRequest({
model: "gpt-4o-mini",
max_completion_tokens: 50,
messages: [{role: "user", content: "Count from 1 to 3."}]
}))
response.status // => 200
events collect(response.body)
length(events) // => number of SSE chunks received
Example - Processing chunks
response ::openai::chat/complete-stream(::openai::chat/ChatCompletionRequest({
model: "gpt-4o-mini",
max_completion_tokens: 100,
messages: [{role: "user", content: "Hello!"}]
}))
for-each(response.body, (event) {
text ::openai::chat/extract-chunk-text(event)
cond {
not(is-empty(text)) => { print(text) }
}
})
complete-stream-with
fn (ep: ::openai::api/Endpoint, request: Map): StreamingChatResponse
complete-stream against an explicit ::openai::api/Endpoint. Forces
stream: true and returns a StreamingChatResponse whose body yields
SSE events, or an Err carrying HttpError on a non-2xx status.
Example
ep ::openai::api/endpoint("http://localhost:11434/v1")
response ::openai::chat/complete-stream-with(ep, {
model: "llama3.2",
messages: [{role: "user", content: "Count to 3."}]
})
for-each(response.body, (event) {
print(::openai::chat/extract-chunk-text(event))
})
complete-with
fn (ep: ::openai::api/Endpoint, request: Map): Map
complete against an explicit ::openai::api/Endpoint instead of the
ctx-configured OpenAI. Reads no ctx, so it works with keyless local
servers (Ollama, vLLM, LM Studio) and any OpenAI-compatible provider.
Returns the decoded response body as a Map (id, choices, usage,
...). It is not coerced to ChatCompletionResponse, because
compatible servers often omit or add fields. Non-2xx responses return
an Err carrying HttpError.
Example
ep ::openai::api/endpoint("http://localhost:11434/v1")
response ::openai::chat/complete-with(ep, {
model: "llama3.2",
messages: [{role: "user", content: "Say hi"}],
max_tokens: 50
})
response.choices[0].message.content
extract-chunk-text
fn (event): Str
Extract the text content from a streaming SSE event. Returns empty string if no content in the chunk.
Example
response ::openai::chat/complete-stream(::openai::chat/ChatCompletionRequest({
model: "gpt-4o-mini",
max_completion_tokens: 50,
messages: [{role: "user", content: "Hello!"}]
}))
for-each(response.body, (event) {
text ::openai::chat/extract-chunk-text(event)
// text is "" for empty chunks, or the content fragment
})
is-stream-done
fn (event): Bool
Check if a streaming event indicates the stream is complete. Returns true when the event contains a [DONE] marker or a non-null finish_reason.
Example
response ::openai::chat/complete-stream(::openai::chat/ChatCompletionRequest({
model: "gpt-4o-mini",
max_completion_tokens: 50,
messages: [{role: "user", content: "Hi"}]
}))
for-each(response.body, (event) {
done ::openai::chat/is-stream-done(event)
// done is true when stream is finished
})
system-message
fn (content: Str): Message
Create a system message for use in a chat completion request.
Example
msg ::openai::chat/system-message("You are a helpful assistant that responds in JSON.")
// => Message({role: "system", content: "You are a helpful assistant that responds in JSON."})
user-message
fn (content: Str): Message
Create a user message for use in a chat completion request.
Example
msg ::openai::chat/user-message("What is the capital of France?")
// => Message({role: "user", content: "What is the capital of France?"})
Types
ChatChoice
ChatChoice type {
index: Int,
message: Message,
finish_reason: FinishReason?
}
ChatCompletionChunk
ChatCompletionChunk type {
id: Str,
object: Str,
created: Int,
model: Str,
choices: Vec<StreamChoice>
}
ChatCompletionRequest
ChatCompletionRequest type {
model: Str,
messages: Vec<Message>,
max_completion_tokens: Int?,
reasoning_effort: Str?,
temperature: Dec?,
top_p: Dec?,
n: Int?,
stream: Bool?,
stop: Any?,
presence_penalty: Dec?,
frequency_penalty: Dec?,
logit_bias: Map?,
user: Str?,
tools: Vec<Map>?,
tool_choice: Any?,
response_format: Map?,
stream_options: Map?
}
ChatCompletionResponse
ChatCompletionResponse type {
id: Str,
object: Str,
created: Int,
model: Str,
choices: Vec<ChatChoice>,
usage: Usage?
}
Delta
Delta type {
role: Role?,
content: Str?,
tool_calls: Vec<Map>?
}
FinishReason
FinishReason type "stop" | "length" | "tool_calls" | "content_filter" | "function_call"
Message
Message type {
role: Role,
content: Any,
name: Str?,
tool_calls: Vec<Map>?,
tool_call_id: Str?
}
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
}
Usage
Usage type {
prompt_tokens: Int,
completion_tokens: Int,
total_tokens: Int
}