Skip to main content

Crate troupekit_local

Crate troupekit_local 

Source
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 llama feature, through the tk_llama C shim. Every unsafe block 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 update and finish, plus one-shot digest and lowercase to_hex.

Structs§

ChatMessage
One message given to render_chat: a role and its text. Built with new; read through role and content. The text is passed to the template unchanged.
GenParams
Settings for one generate call. Defaults: 512 tokens, temperature 0.7, top-p 0.95, seed 0x5452_4B54, no stop strings, no grammar, no confidence, end of text allowed, no cancel flag. with_* setters.
Generation
The result of one generate call: text, generated token count, why it stopped, the mean token log-probability when asked for, and the call’s metrics. Built by LocalModel implementations with new.
Grammar
A GBNF grammar that constrains generation, with the name of its start rule. Built by Grammar::new, which refuses empty text, text over MAX_BYTES, an interior NUL, and a root that is not a rule name.
LocalCatalog
A checked list of downloadable models: unique ids, at most one recommended row per class, at most 256 rows. get finds a row and pick chooses one for a device class and RAM budget.
LocalChat
A local model behind the Model interface, 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.
LocalChatSettings
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.
LocalEmbedder
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.
LocalInfo
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.
LocalMetrics
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.
LocalSpec
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.
ModelCache
A folder of downloaded model files, one <id>-<sha256>.gguf per 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; fetch checks the SHA-256 before a file gets its final name. No call downloads alone.

Enums§

ChatRole
The speaker of one chat message. as_str gives the role name that chat templates expect: system, user, assistant, tool. Default User.
DeviceClass
The device a catalog row targets: Phone, Pi (single-board computers) or Desktop. pick filters by it. Default Desktop.
Flow
What a token callback tells generate: keep going or stop now. Stop ends the run with GenStop::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.
LocalError
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. LocalChat maps these to ProviderErrorKind::Server with this text.
ToolMode
Whether a local model may answer in text when tools are offered. Auto allows text or one tool call; Required allows only a call. Default Auto.

Constants§

REPLY_MAX_BYTES
The largest local reply parse_reply reads, in bytes (1 MiB). Longer replies are refused before any JSON parsing. Bounds memory on a runaway generation.

Traits§

LocalModel
One in-process model: tokenizer, chat template, generation, scoring and embeddings. Calls block; LocalChat runs them off the async runtime. llama.cpp implements it behind llama; 0.4 adds the platform models. Object safe, so Arc<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, or LocalError::Grammar for 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 one ToolCall with id local-1; anything else is one Text part. 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, or LocalError::Grammar naming the first unsupported keyword, a broken bound, a cyclic $ref or 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 in ToolMode::Auto that 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 not type: "object".
tool_prompt
Builds the system prompt block that lists the tools and the reply format parse_reply reads. Tool names and schemas are written as JSON, so a name cannot break out of its quotes. Empty for no tools.