laya/lib.rs
1//! Laya typed decisions in Rust.
2//!
3//! An independent Rust port of the Laya decision models: a bidirectional
4//! encoder answers constrained questions — a choice, a rubric score, or the
5//! probability of a proposition — in one forward pass, with no token-by-token
6//! decoding and no generated JSON.
7//!
8//! ```no_run
9//! use laya::Agent;
10//! use serde_json::json;
11//!
12//! let agent = Agent::from_pretrained("aac6fef/laya-mlx")?;
13//! let result = agent.predict(
14//! &json!("I was billed twice. Please refund the duplicate."),
15//! &json!({
16//! "department": {
17//! "type": "choice",
18//! "instructions": "Who should handle this?",
19//! "criteria": ["billing", "technical", "sales"]
20//! }
21//! }),
22//! )?;
23//! println!("{}", result.answers["department"]);
24//! # Ok::<(), laya::Error>(())
25//! ```
26//!
27//! Unlike the MLX package this is ported from, inference runs on CPU, CUDA and
28//! Metal. Build with `--features metal` or `--features cuda` for GPU support.
29
30pub mod agent;
31pub mod config;
32pub mod convert;
33pub mod model;
34pub mod router;
35pub mod weights;
36
37#[cfg(feature = "hub")]
38pub mod hub;
39
40pub use agent::{Agent, AgentBuilder, Answer, Prediction};
41pub use config::{AgentConfig, EncoderConfig};
42pub use model::{DecisionModel, ModernBert};
43pub use router::{ModelName, RouteDecision, Router};
44
45pub use laya_core::{calibrate, email, lang, presets, prompt, question};
46pub use laya_core::{Criteria, Question, QuestionKind, Tokenizer};
47
48/// Errors raised by the runtime.
49#[derive(Debug, thiserror::Error)]
50pub enum Error {
51 #[error(transparent)]
52 Core(#[from] laya_core::Error),
53
54 #[error(transparent)]
55 Candle(#[from] candle_core::Error),
56
57 #[error(transparent)]
58 Io(#[from] std::io::Error),
59
60 #[error(transparent)]
61 Json(#[from] serde_json::Error),
62
63 #[error("Not a complete Laya checkpoint: {0} is missing")]
64 IncompleteCheckpoint(std::path::PathBuf),
65
66 #[error("{0}")]
67 Config(String),
68
69 #[error("Non-finite model outputs; retry with dtype=\"float32\"")]
70 NonFinite,
71
72 #[error("{0}")]
73 Hub(String),
74}
75
76pub type Result<T> = std::result::Result<T, Error>;