llm.md 5.1 KB


title: llm section: llm

order: 180

The llm library connects to any OpenAI-compatible or Anthropic endpoint configured by an admin in System Settings > AI Integration > AI Model (the settings tab and its /system/aimodel/... routes kept their original "AI Model" name; only the requirelib identifier changed from aimodel to llm). Per-model pricing and an optional token/cost quota are also defined there. The wire-protocol logic (OpenAI / Anthropic request building and response parsing) lives in the standalone mod/aiservers/llm Go package.

llm.chat(prompt, options) → string

Sends a single-turn text prompt and returns the assistant's reply. options is an optional object (see Options below).

requirelib("llm");
var reply = llm.chat("What is the capital of France?");
sendResp(reply);

With a system prompt and model override:

requirelib("llm");
var reply = llm.chat("Summarise this in one sentence.", {
    system: "You are a concise summariser.",
    model:  "gpt-4o-mini"
});
sendResp(reply);

llm.chatWithFile(prompt, files, options) → string

Like llm.chat() but attaches one or more virtual-path files to the message. Images are sent as base64 vision parts; text files are inlined as labelled text. files may be a single vpath string or an array.

requirelib("llm");
var reply = llm.chatWithFile(
    "Describe what you see in this image.",
    "user:/Photos/holiday.jpg"
);
sendResp(reply);

llm.request(messages, options) → object

Low-level call. Accepts the full OpenAI-style messages array and returns the raw response object (including usage and choices).

requirelib("llm");
var resp = llm.request([
    { role: "system",    content: "You are helpful." },
    { role: "user",      content: "Hi!" },
    { role: "assistant", content: "Hello! How can I help?" },
    { role: "user",      content: "Tell me a joke." }
]);
sendResp(resp.choices[0].message.content);

When the model exposes its chain-of-thought, choices[0].message.reasoning_content carries that "thinking" text separately from the answer (content). It is populated from DeepSeek's reasoning_content, OpenRouter's reasoning or Anthropic thinking content blocks, and is an empty string for models that do not return reasoning.

llm.streamRequest(messages, options, onDelta) → object

Like llm.request() but streams the completion: onDelta is called for every incremental chunk as the model produces it, and the fully assembled response (same shape as llm.request, including usage) is returned when the stream ends. Each delta is { content, reasoning } — content is new answer text and reasoning is new chain-of-thought text; either may be empty for a given chunk.

The callback runs on the script's own goroutine, so it is safe to relay chunks straight to a browser over websocket.send():

requirelib("llm");
requirelib("websocket");
websocket.upgrade(300);

var resp = llm.streamRequest([{ role: "user", content: "Explain gravity" }], {}, function(d){
    if (d.reasoning != "") websocket.send(JSON.stringify({ type: "reasoning", content: d.reasoning }));
    if (d.content   != "") websocket.send(JSON.stringify({ type: "delta",     content: d.content }));
});
websocket.send(JSON.stringify({ type: "done", usage: resp.usage }));

Token usage is recorded against the metrics/quota board exactly as for a non-streaming call.

llm.usage() → object

Returns accumulated token / cost metrics across all models.

requirelib("llm");
var u = llm.usage();
sendJSONResp(u);
// { totalTokens, totalCost, totalRequests, perModel: { ... }, currency, ... }

llm.models() → object

Returns the configured default model name and a list of models that have pricing entries defined in System Settings.

requirelib("llm");
var m = llm.models();
sendJSONResp(m);
// { default: "gpt-4o", models: ["gpt-4o", "gpt-4o-mini", ...] }

llm.listModels() → object

Queries the live endpoint for available models (does not consume tokens).

requirelib("llm");
var m = llm.listModels();
sendJSONResp(m.models);

llm.fileParts(files) → object[]

Converts virtual-path file(s) into OpenAI-style content parts that can be embedded in a messages array for llm.request(). Images become image_url data-URI parts; text files become text parts.

requirelib("llm");
var parts = llm.fileParts(["user:/report.txt"]);
var resp  = llm.request([
    { role: "user", content: parts }
]);
sendResp(resp.choices[0].message.content);

Options object

All functions that accept options support the following fields (all optional):

Field Type Description
model string Override the configured default model
system string System prompt (prepended as a system role message)
endpoint string Override the global endpoint URL
apikey string Override the global API key
apiFormat string Wire format: "openai" (default) or "anthropic"
temperature number Sampling temperature
max_tokens number Maximum tokens to generate