In-process decision backend on LlamaCppEx.Decision (llama.cpp's
/v1/systemone, ported into the NIF): Clef / Clef-Flash, OpenJev, Laya and
the other decision GGUFs, with no server.
Requires the optional dep {:llama_cpp_ex, "~> 0.8.63"}.
The process owns the model
A %LlamaCppEx.Decision{} drives one llama.cpp context and is not safe for
concurrent use, so each loaded model lives in its own process, which
serialises the calls. Start it under your supervision tree:
children = [
{Nous.DecisionModel.Backend.LlamaCpp,
name: :clef, source: {:path, "/models/Clef-Flash-Q8_0.gguf"}}
]
dm = Nous.DecisionModel.parse("llamacpp:clef")
{:ok, result} = Nous.DecisionModel.decide(dm, state, questions)"llamacpp:<name>" resolves <name> with String.to_existing_atom/1
and then only to a process this module started: each process registers
its name in a registry this module owns, and a name that is not there
(any other registered process included) is :unavailable, never called.
Pass server: (a pid or any GenServer.server/0) to
Nous.DecisionModel.new/2 or parse/2 for another name form; it is
checked the same way and must be a process of this module on this node.
Start options
:name- required; the registered name.:source- required;{:path, path}, or{:hub, repo, file}to download from Hugging Face throughLlamaCppEx.Hub(only when given explicitly).:n_batch- tokens per batch. Clef evaluates the whole prompt (state plus questions) in one batch, so this caps the request size. Default4096, or:n_ctxwhen only that is given.:n_ctx- context size. Default::n_batch.:n_gpu_layers- default-1(all).:decision_opts- extraLlamaCppEx.Decision.new/2tuning options (:n_threads,:flash_attn, ...).:shutdown- the child spec's shutdown, in ms: how long the supervisor waits before escalating to:kill. Default30_000. The process does not trap exits, so it stops as soon as an in-flight decode returns; the model is freed with the process either way.
:n_ctx and :n_batch are capped at 32,768: above that, the macOS GPU
watchdog kills the single command buffer and poisons the context. A
larger value raises ArgumentError at start.
The model loads in handle_continue/2, so start-up does not block the
supervisor; calls made while it loads wait for it. The loaded model is
published in the process's registry entry, which goes away with the
process however it exits, so the model's memory is freed with it.
Special tokens
Before tokenizing, every <...>, [...] or |||...||| sequence in the
state that the loaded vocabulary parses as one token (control and
user-defined tokens, such as Clef's <tts_pad> and </think>) is broken
with Nous.DecisionModel.Defang.defang_tokens/2, on top of the generic
Nous.DecisionModel.Defang pass. count_tokens/2 counts the same text.
Decide options
:timeout- how long to wait for the serialised call, in ms. Default30_000. Calls queue behind each other: one Clef-Flash check takes ~0.5–1 s on an M1 Max. A request whose timeout has passed, or whose caller has exited, by the time the process reaches it is not decoded.
Errors
- a prompt that does not fit
n_batch/n_ctx→:oversize. Raisen_batch, or chunk the state (max_state_tokens/1is the budget). "decision decode failed with code: -3"(macOS GPU watchdog) and other negative decode codes →:backend_dead; the context is recreated before the next call. If recreating it fails, that call gets:backend_deadand the process exits, so its supervisor restarts it with a fresh model.- the call timing out →
:timeout; the process not running, or the name not belonging to this module →:unavailable.
Summary
Functions
Returns a specification to start this module under a supervisor.
Exact token count of text with the model's tokenizer
(add_special: false, parse_special: false), after breaking the
vocabulary's special tokens (see "Special tokens").
The request budget: the process's n_batch (Clef evaluates state plus
questions in one batch).
Starts the process that owns one decision model. See "Start options".
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
Exact token count of text with the model's tokenizer
(add_special: false, parse_special: false), after breaking the
vocabulary's special tokens (see "Special tokens").
Tokenizes in the caller once the model is loaded; before that it waits for the load. Exits if the process is not running.
The request budget: the process's n_batch (Clef evaluates state plus
questions in one batch).
Exits if the process is not running.
@spec start_link(keyword()) :: GenServer.on_start()
Starts the process that owns one decision model. See "Start options".