Other AI models: the openai mod
mods/openai.luma asks the models that speak OpenAI’s Chat Completions API: OpenAI’s own, and nearly every other provider and local server, such as Ollama, llama-server (llama.cpp), LM Studio, vLLM, OpenRouter, Groq, Mistral, DeepSeek and Gemini (through its OpenAI endpoint). It does what the claude mod does, with the same methods: ask, stream, extract JSON that fits a schema, tools, conversations. Copy it next to your program with mods/llm.luma, which it uses, and import it.
import "openai"
ai: openai.client = openai.connect() // OpenAI, the key from OPENAI_API_KEY
ai.model("gpt-5.2")
print(ai.ask("In one sentence: what is a monad?"))
local: openai.client = openai.ollama("qwen2.5:7b") // Ollama on this computer, no key
print(local.ask("Hello"))Where it connects
| Function | Connects to |
|---|---|
openai.connect(), openai.connect(key) | OpenAI’s API; with no key it reads OPENAI_API_KEY. Name a model with model(name) |
openai.ollama(name), openai.ollama(name, url) | Ollama, on this computer (http://localhost:11434) or at url, with a model it has pulled (ollama pull qwen2.5:7b) |
openai.local(), openai.local(url) | a server on this computer that speaks the API, as llama-server does on port 8080 |
openai.serve(file, args) | starts llama-server with a GGUF file (see below) |
ai.url(base) | another server: its address up to /chat/completions, such as https://openrouter.ai/api/v1, https://api.groq.com/openai/v1 or https://generativelanguage.googleapis.com/v1beta/openai |
ai: openai.client = openai.connect(env.get("OPENROUTER_API_KEY"))
ai.url("https://openrouter.ai/api/v1")
ai.model("meta-llama/llama-4-scout")Asking
| Method | What it does |
|---|---|
ai.ask(text), ai.ask(text, files) | asks one question and gives the answer; files: pictures (png, jpg, gif, webp), PDFs and text files |
ai.stream(text, part) | asks, and gives each piece of the answer to part as it comes; the whole answer at the end |
ai.extract(text, schema) | the answer as a json that fits the schema, ready to convert |
ai.tool(name, about, schema, run) | adds a tool the model may call; ai.approve(check) asks before each call |
ai.chat() | starts a conversation, an llm.talk: c.say(text), c.flow(text, part) |
ai.models() | the names of the models the server has, a [str] |
ai.usage() | the tokens used so far: input, output and read (from the server’s cache) |
ai.close() | stops the llama-server serve started |
These work as in the claude mod; its examples run here as they are. What a server can do depends on the server and the model: pictures need a model that sees, PDFs work with OpenAI’s models only, and tools need a model made for them (a small local model may answer instead of calling one).
Settings
| Method | Sets | If not set |
|---|---|---|
ai.model(name) | the model, as the server names it | none for OpenAI (an error asks for it); the name given to ollama; anything for llama-server |
ai.system(text) | the system prompt | none |
ai.effort(level) | how hard a reasoning model thinks (reasoning_effort): "minimal", "low", "medium", "high", as the model takes them | not sent |
ai.tokens(n) | the most tokens of an answer | not sent: the server’s own limit |
ai.turns(n) | the most rounds of tool calls in one question | 20 |
Extract
extract asks for JSON that fits a schema (response_format with json_schema, strict), and works as in the claude mod: schema(T) of an obj gives it, convert reads the answer, and date-times with a zone are put on the local clock. OpenAI’s strict mode wants every field asked for, so a field with a default is asked for as “this, or null”, and a null in the answer is left out, so convert gives the field its default. Ollama and llama-server keep to the schema too (llama-server makes a grammar of it).
Any model: llm.model
The clients of this mod and of the claude mod are both an llm.model, a trait of mods/llm.luma, and their conversations an llm.talk. A function written for llm.model works with any of them, so a program can change models, or use several:
import "llm"
import "claude"
import "openai"
fn summary(ai: llm.model, text: str): str {
ai.system("Summarize in one sentence.")
return ai.ask(text)
}
models: [llm.model] = [claude.connect(), openai.ollama("qwen2.5:7b")]
models.walk(m) {
print(summary(m, report))
}llm.model has system, ask, stream, extract, tool, approve and chat. What only one mod has (model, effort, usage, Claude’s count and cache) is used on its own type, or after if m is openai.client.
Running a GGUF file: llama-server
openai.serve(file("model.gguf")) runs a model file with llama-server, from llama.cpp (its releases have it built for Linux, macOS and Windows), and gives a client of it:
import "openai"
ai: openai.client = openai.serve(file("models/qwen2.5-7b-instruct-q4_k_m.gguf"), ["-c", "8192"])
print(ai.ask("Hello"))
ai.close()- llama-server is looked for in the folders of
PATH, or whereLLAMA_SERVERsays. - It listens on a free port of this computer only (
127.0.0.1), with--jinja(the model’s own chat template, which tools need) and--log-disable. The arguments given are added:["-c", "8192"]for a longer context,["-ngl", "99"]to put the model on a GPU. servewaits until the model is loaded (up to 30 minutes), then gives the client. If llama-server stops before that, the error says how to run it yourself to see why.- The server stops with
ai.close(), and when the program ends.
For a model in Ollama, there is nothing to start: openai.ollama(name). The llama mod runs a GGUF file itself, with nothing installed (slower than llama.cpp, on one thread); the gguf mod reads its metadata and tokenizer.
Errors
The same codes as the claude mod’s, so one check works for both:
| Code | When |
|---|---|
openai.AUTH (100) | no key or a wrong one |
openai.LIMIT (101) | a rate limit after the retries, or a used-up quota |
openai.REFUSED (102) | the model declined, or the content filter stopped the answer |
openai.CUT (103) | the answer reached tokens(n) (or the server’s limit) |
openai.FAILED (104) | any other error the server gives (its message: a model it doesn’t have, …), a server that isn’t there, no model named, a schema extract can’t use, a llama-server that didn’t start |
A rate limit or a server error (429, 500, 502, 503) is tried again up to 3 times, after the time the server asks for (retry-after), else 1, 2 and 4 seconds. A used-up quota is not tried again.
How it was checked
Against a fake API (in every mode), against Ollama with a small model (qwen2.5:0.5b: questions, streaming, extract, tools, conversations) and against llama-server (llama.cpp b12864) running a GGUF file. OpenAI’s Responses API, and each provider’s own extras, are not used: the Chat Completions API is the one they all share.