Skip to main content

weirdnet_model/
lib.rs

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