rust_local_ai/lib.rs
1#![deny(unsafe_op_in_unsafe_fn)]
2#![doc = include_str!("../README.md")]
3
4use std::sync::Arc;
5
6mod backend;
7mod error;
8mod model;
9mod session;
10mod types;
11
12pub use backend::{Backend, BackendSession, ResponseStream};
13pub use error::{LocalAiError, Result};
14pub use model::LocalAiModel;
15pub use session::LocalAiSession;
16pub use types::*;
17
18/// Detects the best OS/distribution-managed backend available in this build:
19/// Apple Foundation Models on macOS, Windows AI (Phi Silica) on Windows, and
20/// Ubuntu inference snaps on Linux.
21///
22/// Detection never installs a provider, starts a service, or downloads a model.
23/// Check [`LocalAiModel::availability`] before use.
24pub async fn detect() -> Result<LocalAiModel> {
25 Ok(LocalAiModel::from_backend(backend::platform_backend()))
26}
27
28/// Uses a local service that speaks the OpenAI chat-completions API, such as
29/// llama.cpp's server, Ollama, LM Studio or Foundry Local.
30///
31/// `base_url` is the API root, e.g. `http://localhost:11434/v1`. Only plain
32/// `http://` URLs on this machine (`localhost`, `127.0.0.0/8`, `::1`) are
33/// accepted, so prompts never leave it. When `model` is `None`, the first model
34/// listed by the service's `/models` endpoint is used. Nothing is contacted
35/// until the model is used.
36pub fn openai_compatible(base_url: &str, model: Option<&str>) -> Result<LocalAiModel> {
37 let service = backend::openai::Service::new(base_url, model.map(str::to_owned))?;
38 Ok(LocalAiModel::from_backend(Arc::new(
39 backend::openai::OpenAiBackend { service },
40 )))
41}
42
43/// Uses a specific Ubuntu inference snap (for example `"qwen3"`) instead of the
44/// first suitable one [`detect`] would pick.
45///
46/// Setting the `RUST_LOCAL_AI_INFERENCE_SNAP` environment variable has the
47/// same effect on [`detect`].
48#[cfg(target_os = "linux")]
49pub fn inference_snap(name: &str) -> LocalAiModel {
50 LocalAiModel::from_backend(backend::linux::backend(Some(name.to_owned())))
51}
52
53#[cfg(feature = "genui")]
54pub mod genui;
55#[cfg(any(test, feature = "testing"))]
56pub mod testing;