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