weirdnet_model/lib.rs
1//! Weirdnet local models.
2//!
3//! - [`artifact`]: inspect and hash model files a person brings (GGUF models, split models,
4//! LoRA adapters and projectors; safetensors and MLX model directories; PEFT adapters),
5//! refusing pickles, code and symlinks, and the [`Provenance`] record kept next to them;
6//! [`ModelStore::import`] places them in the store.
7//! - [`catalog`]: pinned Gemma 4 E2B and E4B GGUF files (Hugging Face repo, file, size, SHA-256).
8//! - [`limits`]: per-model media and request limits ([`ModelLimits`]: context in use, images per
9//! request and their token cost, audio length and cost, video, request bytes), read from a
10//! model and its projector when it loads ([`limits::measure`]) and kept next to its files
11//! ([`ModelStore::record_limits`]). Unknown figures stay absent, never defaulted.
12//! - [`store`] and [`mod@download`]: where model files and LoRA adapters live (list and remove
13//! them too), and a consent-gated download that resumes a `.part` file and checks size and
14//! SHA-256 before the file appears, for single and split models and for adapters.
15//! - [`hf`]: search the Hugging Face Hub, read a repo's GGUF files (split files grouped) and a
16//! file's header, so any repo can become a [`ModelSpec`]; find a repo's LoRA adapters (GGUF
17//! and PEFT), which are never listed as models; [`HfToken`] for gated repos, read on the
18//! downloading node only.
19//! - [`gguf`]: the GGUF header fields that size the KV cache, and whether a file is a model, a
20//! LoRA adapter or a projector (with what a projector takes, [`gguf::ProjectorInfo`]).
21//! - [`fit`]: pick the largest file whose weights and KV cache fit the memory budget.
22//! - [`prompt`]: render chat turns with the model's own Jinja template, and the `<tool_call>`
23//! tag format for tools (prompt text and a streaming parser).
24//! - [`worker`]: an [`Engine`] on its own thread, streaming text with cancel.
25//! - [`cpu`]: whether this CPU can run the compiled-in llama.cpp (checked before it loads).
26//! - [`hw`]: what accelerators and memory this machine has, the memory budget for models, a
27//! tiered catalog with footprints (chat, embedding and a speech-to-text model with the
28//! `transcribe` role, [`hw::Purpose::Transcribe`]), [`hw::plan`] to pick models that fit and
29//! [`hw::transcriber`] for the speech-to-text one.
30//! - `llama` (feature): the llama.cpp [`Engine`].
31//! - `agent` (feature): `weirdnet_agent::ModelBackend` over the worker.
32//!
33//! Nothing here knows an app: callers pass paths, consent and callbacks.
34#![forbid(unsafe_code)]
35
36pub mod artifact;
37pub mod catalog;
38pub mod cpu;
39pub mod download;
40pub mod fit;
41pub mod gguf;
42pub mod hf;
43pub mod hw;
44pub mod limits;
45pub mod prompt;
46pub mod store;
47#[cfg(test)]
48mod testing;
49pub mod token;
50pub mod worker;
51
52#[cfg(feature = "agent")]
53pub mod agent;
54#[cfg(feature = "llama")]
55pub mod llama;
56
57pub use artifact::{Artifact, ArtifactFile, ArtifactFormat, ArtifactKind, Provenance, inspect};
58pub use catalog::{Device, ModelSpec, human};
59pub use download::{Consent, Progress, download, download_adapter, download_parts};
60pub use hf::{AdapterFormat, Hub, RepoAdapter};
61pub use limits::{LimitSource, LimitsRecord, ModelLimits};
62pub use prompt::{ChatMessage, ChatRole};
63pub use store::{DiskSpace, Imported, ModelLayout, ModelStore, StoredAdapter, StoredModel, StoredProjector};
64pub use token::HfToken;
65pub use worker::{Chunk, Engine, GenerateRequest, Generation, Stats, Worker};
66
67/// Everything that can go wrong here.
68#[derive(Debug, thiserror::Error)]
69pub enum Error {
70 #[error("{0}")]
71 Io(#[from] std::io::Error),
72 #[error("network: {0}")]
73 Http(String),
74 #[error("{name}: checksum mismatch (got {got}); the file was deleted")]
75 Checksum { name: String, got: String },
76 #[error("download stopped at {done} of {total} (run it again to resume)")]
77 Incomplete { done: String, total: String },
78 #[error("download cancelled at {done} of {total} (run it again to resume)")]
79 Cancelled { done: String, total: String },
80 #[error("consent was given for another model")]
81 Consent,
82 #[error("{0:?} is not a Hugging Face model id (owner/name)")]
83 BadRepo(String),
84 #[error("{0}")]
85 Model(String),
86 /// This CPU lacks instructions the compiled-in llama.cpp uses; see [`cpu`].
87 #[error("local models need CPU instructions this one lacks: {}", missing.join(", "))]
88 UnsupportedCpu { missing: Vec<String> },
89 /// `cpu-dispatch` builds: no llama.cpp CPU backend library could be loaded.
90 #[error("no llama.cpp CPU backend found in {} (ship the ggml-cpu libraries there)", dir.display())]
91 NoCpuBackend { dir: std::path::PathBuf },
92}
93
94pub type Result<T> = std::result::Result<T, Error>;