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`], and text-to-speech models with the `speak`
29//! role, [`hw::Purpose::Speak`]), [`hw::plan`] to pick models that fit, [`hw::transcriber`]
30//! for the speech-to-text one and [`hw::speaker`] for a text-to-speech one.
31//! - [`speech`]: what a speaking model does ([`SpeechProfile`]), from the catalog, from a
32//! hand-placed GGUF's headers ([`speech::from_gguf`]) or kept with its limits.
33//! - `llama` (feature): the llama.cpp [`Engine`].
34//! - `agent` (feature): `weirdnet_agent::ModelBackend` over the worker.
35//!
36//! Nothing here knows an app: callers pass paths, consent and callbacks.
37#![forbid(unsafe_code)]
38
39pub mod artifact;
40pub mod catalog;
41pub mod cpu;
42pub mod download;
43pub mod fit;
44pub mod gguf;
45pub mod hf;
46pub mod hw;
47pub mod limits;
48pub mod prompt;
49pub mod speech;
50pub mod store;
51#[cfg(test)]
52mod testing;
53pub mod token;
54pub mod worker;
55
56#[cfg(feature = "agent")]
57pub mod agent;
58#[cfg(feature = "llama")]
59pub mod llama;
60
61pub use artifact::{Artifact, ArtifactFile, ArtifactFormat, ArtifactKind, Provenance, inspect};
62pub use catalog::{Device, ModelSpec, human};
63pub use download::{Consent, Progress, download, download_adapter, download_parts};
64pub use hf::{AdapterFormat, Hub, RepoAdapter};
65pub use limits::{LimitSource, LimitsRecord, ModelLimits};
66pub use prompt::{ChatMessage, ChatRole};
67pub use speech::SpeechProfile;
68pub use store::{DiskSpace, Imported, ModelLayout, ModelStore, StoredAdapter, StoredModel, StoredProjector};
69pub use token::HfToken;
70pub use worker::{Chunk, Engine, GenerateRequest, Generation, Stats, Worker};
71
72/// Everything that can go wrong here.
73#[derive(Debug, thiserror::Error)]
74pub enum Error {
75 #[error("{0}")]
76 Io(#[from] std::io::Error),
77 #[error("network: {0}")]
78 Http(String),
79 #[error("{name}: checksum mismatch (got {got}); the file was deleted")]
80 Checksum { name: String, got: String },
81 #[error("download stopped at {done} of {total} (run it again to resume)")]
82 Incomplete { done: String, total: String },
83 #[error("download cancelled at {done} of {total} (run it again to resume)")]
84 Cancelled { done: String, total: String },
85 #[error("consent was given for another model")]
86 Consent,
87 #[error("{0:?} is not a Hugging Face model id (owner/name)")]
88 BadRepo(String),
89 #[error("{0}")]
90 Model(String),
91 /// This CPU lacks instructions the compiled-in llama.cpp uses; see [`cpu`].
92 #[error("local models need CPU instructions this one lacks: {}", missing.join(", "))]
93 UnsupportedCpu { missing: Vec<String> },
94 /// `cpu-dispatch` builds: no llama.cpp CPU backend library could be loaded.
95 #[error("no llama.cpp CPU backend found in {} (ship the ggml-cpu libraries there)", dir.display())]
96 NoCpuBackend { dir: std::path::PathBuf },
97}
98
99pub type Result<T> = std::result::Result<T, Error>;