Expand description
On-device models for Troupekit: one sync LocalModel trait,
a checked model catalog and cache, and llama.cpp behind the llama
feature. The default build compiles no C or C++ code.
Modules§
- llama
- llama.cpp behind the
llamafeature, through thetk_llamaC shim. Everyunsafeblock of the crate lives in this module; Rust input is checked before it crosses into C. - sha256
- SHA-256 as in FIPS 180-4, written by hand so the crate needs no
hashing dependency. Streaming
updateandfinish, plus one-shotdigestand lowercaseto_hex.
Structs§
- Chat
Message - One message given to
render_chat: a role and its text. Built withnew; read throughroleandcontent. The text is passed to the template unchanged. - GenParams
- Settings for one
generatecall. Defaults: 512 tokens, temperature 0.7, top-p 0.95, seed0x5452_4B54, no stop strings, no grammar, no confidence, end of text allowed, no cancel flag.with_*setters. - Generation
- The result of one
generatecall: text, generated token count, why it stopped, the mean token log-probability when asked for, and the call’s metrics. Built byLocalModelimplementations withnew. - Grammar
- A GBNF grammar that constrains generation, with the name of its start
rule. Built by
Grammar::new, which refuses empty text, text overMAX_BYTES, an interior NUL, and a root that is not a rule name. - Local
Catalog - A checked list of downloadable models: unique ids, at most one
recommended row per class, at most 256 rows.
getfinds a row andpickchooses one for a device class and RAM budget. - Local
Chat - A local model behind the
Modelinterface, so the same agent runs on device. Renders requests through the model’s chat template, generates on a blocking thread and cancels when the future is dropped. - Local
Chat Settings - Settings of a
LocalChat: tool mode, the temperature used when a request has none, the sampling seed, whether end of text is banned and an optional minimum confidence. Defaults are plan rows S18 to S22. - Local
Embedder - On-device embeddings from a
LocalModel, for knowledge search with no network. Mean-pooled, L2-normalized vectors (the mean of the token rows); batches of at most 32 texts run off the async runtime. - Local
Info - Fixed facts about a loaded model: context length in tokens, vocabulary size, embedding size (0 when the model cannot embed) and whether it carries a chat template. Read through the getters.
- Local
Metrics - Speed and memory of one call: prefill and decode tokens per second, time to first token, peak resident memory when the platform reports it, and prompt tokens reused from the cache. All zero by default.
- Local
Spec - One downloadable model: id, https URL, SHA-256, size in bytes,
resident RAM estimate, context, device class, licence, embedding
support and the recommended flag. Checked by
new;with_*setters. - Model
Cache - A folder of downloaded model files, one
<id>-<sha256>.ggufper row, so a row with another digest never reuses a file. A file is used only when its size and SHA-256 match, hashed once per process;fetchchecks the SHA-256 before a file gets its final name. No call downloads alone.
Enums§
- Chat
Role - The speaker of one chat message.
as_strgives the role name that chat templates expect:system,user,assistant,tool. DefaultUser. - Device
Class - The device a catalog row targets:
Phone,Pi(single-board computers) orDesktop.pickfilters by it. DefaultDesktop. - Flow
- What a token callback tells
generate: keep going or stop now.Stopends the run withGenStop::Stop. Used by streaming and by stop checks in the caller. - GenStop
- Why a generation ended: the model’s end of text, the token limit, a
stop string or
Flow::Stop, or the cancel flag. A full context is an error (LocalError::ContextFull), not a stop. - Local
Error - Every failure of an on-device model: load, tokenize, decode, context
size, grammar, unsupported operations, the model cache and catalog.
Messages name the cause and never hold prompt or reply text.
LocalChatmaps these toProviderErrorKind::Serverwith this text. - Tool
Mode - Whether a local model may answer in text when tools are offered.
Autoallows text or one tool call;Requiredallows only a call. DefaultAuto.
Constants§
- REPLY_
MAX_ BYTES - The largest local reply
parse_replyreads, in bytes (1 MiB). Longer replies are refused before any JSON parsing. Bounds memory on a runaway generation.
Traits§
- Local
Model - One in-process model: tokenizer, chat template, generation, scoring
and embeddings. Calls block;
LocalChatruns them off the async runtime. llama.cpp implements it behindllama; 0.4 adds the platform models. Object safe, soArc<dyn LocalModel>works.
Functions§
- output_
grammar - Builds the grammar for
output_schema: generation must produce one JSON value of the schema. Input: the schema. Output: the grammar, orLocalError::Grammarfor a schema the converter refuses. - parse_
reply - Turns a local model’s reply into message parts. One leading
<think>...</think>block is dropped first; with tools, a reply that is exactly{"tool":"<name>","arguments":{..}}becomes oneToolCallwith idlocal-1; anything else is oneTextpart. Errors: over 1 MiB, a reply that ends inside its<think>block, an unknown tool, or a malformed call object. - schema_
to_ gbnf - Converts a JSON Schema into a GBNF grammar whose start rule is
root. Input: the schema; the supported keywords are listed in the 0.3 plan (R2). Output: the grammar, orLocalError::Grammarnaming the first unsupported keyword, a broken bound, a cyclic$refor the size cap. - tool_
grammar - Builds the grammar for a turn with tools: one root branch per tool,
{"tool":"<name>","arguments":<schema>}, plus a plain-text branch inToolMode::Autothat cannot open with{or<. Each branch may follow one<think>...</think>block, as a thinking model writes. Errors: no tools, more than 64, a root schema nottype: "object". - tool_
prompt - Builds the system prompt block that lists the tools and the reply
format
parse_replyreads. Tool names and schemas are written as JSON, so a name cannot break out of its quotes. Empty for no tools.