Nous.DecisionModel.Backend.LlamaCpp (nous v0.21.1)

Copy Markdown View Source

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 through LlamaCppEx.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. Default 4096, or :n_ctx when only that is given.
  • :n_ctx - context size. Default: :n_batch.
  • :n_gpu_layers - default -1 (all).
  • :decision_opts - extra LlamaCppEx.Decision.new/2 tuning options (:n_threads, :flash_attn, ...).
  • :shutdown - the child spec's shutdown, in ms: how long the supervisor waits before escalating to :kill. Default 30_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. Default 30_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. Raise n_batch, or chunk the state (max_state_tokens/1 is 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_dead and 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

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

count_tokens(dm, text)

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.

max_state_tokens(dm)

The request budget: the process's n_batch (Clef evaluates state plus questions in one batch).

Exits if the process is not running.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts the process that owns one decision model. See "Start options".