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