localbrain (Rust client)
Rust client for a localbrain endpoint — free, private, local AI. It makes async HTTP calls to a local, OpenAI-compatible model, so it's a tiny dependency (reqwest + serde). No API key.
# Cargo.toml
[]
= "0.1"
= { = "1", = ["macros", "rt-multi-thread"] }
use Client;
async
Getting an endpoint
This crate is only the client. To download a model and serve the endpoint it talks to, run the CLI in your project once (Node tool):
Run inside a Cargo project and npx localbrain now wires Rust for you: it adds this crate to Cargo.toml and generates a src/ai.rs helper. In a Leptos crate it detects the leptos dependency, adds localbrain as an optional dependency behind your ssr feature, and generates a #[server] function instead. Every edit is shown as a diff first and is reversible with localbrain undo. You can still wire it by hand using the examples below.
Confirm it's alive without writing any code:
Run the example
The crate ships a runnable example. With an endpoint up:
It chats, classifies, and extracts a typed struct — a copy-paste starting point for your own code. See examples/basic.rs.
API
new // from LOCALBRAIN_URL / default
builder.base_url.model.build
ai.chat.await? .chat_with.await? // ChatOptions { temperature, max_tokens, system, stop }
ai.classify.await? // exactly one label
ai..await? // T: Deserialize; schema is a serde_json JSON Schema
ai.summarize.await? .embed.await? .health.await?
Errors are a typed localbrain::Error (EndpointDown, ModelNotReady, Timeout, BadResponse, SchemaViolation).
Structured extraction:
use Deserialize;
use json;
let schema = json!;
let invoice: Invoice = ai.extract.await?;
Framework examples
Axum
[]
= "0.1"
= "0.7"
= { = "1", = ["macros", "rt-multi-thread"] }
= { = "1", = ["derive"] }
use ;
use Client;
use ;
use Arc;
async
async
Actix Web
[]
= "0.1"
= "4"
= { = "1", = ["derive"] }
use ;
use Client;
use ;
async
async
Leptos
Leptos components run as WASM in the browser, so call localbrain from a server function (#[server]) — its body only compiles/runs on the server, which is where the AI call belongs. Make the crate a server-only (optional) dependency enabled by your ssr feature:
[]
= "0.7"
= { = "1", = ["derive"] }
# server-only:
= { = "0.1", = true }
[]
= ["leptos/hydrate"]
= ["leptos/ssr", "dep:localbrain"] # localbrain only in the server build
use *;
// Runs on the server; the browser calls it over HTTP automatically.
pub async
Leptos 0.6: use
use leptos::*;andcreate_server_action::<Triage>()instead ofServerAction::<Triage>::new(). The#[server]function body is identical.
Because localbrain sits behind the ssr feature and is only used inside #[server] bodies, it never enters your WASM/client bundle.
Plain Tokio
See examples/basic.rs — run it with cargo run --example basic while an endpoint is up.
Notes
- Small local model: great at classify / extract / summarize / route / light chat — not GPT-class reasoning. For hard tasks, keep a cloud model and route only those calls out.
Clientis cheap toclone()(shares one connection pool) — store it in your app state.
License
MIT