litert_lm_rust/lib.rs
1//! Safe, idiomatic Rust bindings for [LiteRT-LM](https://github.com/google-ai-edge/LiteRT-LM).
2//!
3//! This crate wraps the stable LiteRT-LM **C API** (`c/engine.h`), which is the supported
4//! FFI surface for the C++ Conversation / Engine runtime documented in `litert.md`.
5//!
6//! # Features
7//! - **Conversation API** — chat-style `send_message` / streaming
8//! - **Multimodal** — vision & audio backends, image/audio content parts (path or blob)
9//! - **MTP** — Multi-Token Prediction via speculative decoding
10//! - **Session API** — lower-level prefill / decode / generate_content
11//! - Tools, thinking/reasoning, constrained decoding, LoRA, benchmarks
12//!
13//! # Quick start (text)
14//! ```no_run
15//! use litert_lm_rust::{Backend, Engine, Message};
16//!
17//! let engine = Engine::builder("model.litertlm")
18//! .backend(Backend::Cpu)
19//! .build()?;
20//! let mut conversation = engine.create_conversation(Default::default())?;
21//! let reply = conversation.send_message(Message::user("Hello!"))?;
22//! println!("{}", reply);
23//! # Ok::<(), litert_lm_rust::Error>(())
24//! ```
25//!
26//! # Multimodal
27//! ```no_run
28//! use litert_lm_rust::{Backend, ContentPart, Engine, Message};
29//!
30//! let engine = Engine::builder("gemma-3n.litertlm")
31//! .backend(Backend::Cpu)
32//! .vision_backend(Backend::Gpu)
33//! .audio_backend(Backend::Cpu)
34//! .build()?;
35//! let mut conversation = engine.create_conversation(Default::default())?;
36//! let reply = conversation.send_message(Message::user_parts([
37//! ContentPart::text("Describe this image:"),
38//! ContentPart::image_path("photo.jpg"),
39//! ])?)?;
40//! # Ok::<(), litert_lm_rust::Error>(())
41//! ```
42//!
43//! # MTP (Multi-Token Prediction)
44//! ```no_run
45//! use litert_lm_rust::{Backend, Engine};
46//!
47//! let engine = Engine::builder("model.litertlm")
48//! .backend(Backend::Gpu)
49//! .multi_token_prediction(true) // enable_speculative_decoding
50//! .build()?;
51//! # Ok::<(), litert_lm_rust::Error>(())
52//! ```
53//!
54//! # Native library
55//! Link against a built LiteRT-LM C shared library (`bazel build //c:engine` or equivalent).
56//! Set `LITERT_LM_LIB_DIR` to the directory containing the library.
57
58#![cfg_attr(docsrs, feature(doc_cfg))]
59#![allow(non_upper_case_globals)]
60#![allow(non_camel_case_types)]
61#![allow(non_snake_case)]
62#![allow(dead_code)]
63#![allow(clippy::missing_safety_doc)]
64
65mod benchmark;
66mod conversation;
67mod engine;
68mod error;
69mod ffi;
70mod input;
71mod message;
72mod sampler;
73mod session;
74mod stream;
75
76pub use benchmark::BenchmarkInfo;
77pub use conversation::{
78 ConstraintType, Conversation, ConversationConfig, ConversationOptionalArgs,
79 NoRepeatNgramConfig, RepetitionPenaltyConfig, SendOptions, SuppressTokensConfig,
80 ThinkingConfig,
81};
82pub use engine::{ActivationDataType, Backend, Engine, EngineBuilder, LogSeverity};
83pub use error::{Error, Result};
84pub use input::InputData;
85pub use message::{ContentPart, Message, Role};
86pub use sampler::{SamplerParams, SamplerType};
87pub use session::{Responses, Session, SessionConfig};
88pub use stream::{StreamChunk, StreamEvent, StreamEventReceiver};
89
90/// Crate version string.
91pub const VERSION: &str = env!("CARGO_PKG_VERSION");
92
93/// Set the minimum log severity for the native LiteRT-LM library.
94pub fn set_min_log_level(level: LogSeverity) {
95 unsafe { ffi::litert_lm_set_min_log_level(level.into_ffi()) }
96}