2 releases
| 0.2.1 | Apr 27, 2026 |
|---|---|
| 0.2.0 | Apr 27, 2026 |
#1833 in Web programming
245KB
5K
SLoC
⚠️ UNSTABLE — This crate is in active development. APIs may change without notice.
Platform-specific local AI runtimes for the Rusty AI SDK.
Each runtime is gated behind a Cargo feature flag:
browser— WASM browser AI (Chrome/Edge built-in AI)gemini-nano— Android Gemini Nano (Prompt API via JNI)foundationmodels— Apple Foundation Models (uniffi Swift bindings)phi-silica— Windows Phi Silica (C# bridge via build.rs)
rs_ai — Rust AI SDK
A comprehensive Rust SDK for building AI applications with cloud and local providers, streaming, and a clean async-first API.
Quick Start
[dependencies]
rs_ai = "0.2"
use rs_ai::rs_ai_claude;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let answer = rs_ai_claude()
.model("claude-sonnet-4-6")
.generate("What is 2+2?")
.await?;
println!("{}", answer);
Ok(())
}
Providers
| Function | Provider | Env Var |
|---|---|---|
rs_ai_claude() |
Anthropic Claude | ANTHROPIC_API_KEY |
rs_ai_chatgpt() |
OpenAI ChatGPT | OPENAI_API_KEY |
rs_ai_gemini() |
Google Gemini | GOOGLE_API_KEY |
rs_ai_xai() |
xAI Grok | XAI_API_KEY |
rs_ai_cloudflare(account_id) |
Cloudflare Workers AI | CLOUDFLARE_API_TOKEN |
rs_ai_compatible(base_url) |
Any OpenAI-compatible | OPENAI_API_KEY |
Local Runtimes
Add rs_ai_local to use on-device AI:
[dependencies]
rs_ai_local = { version = "0.2", features = ["gemini-nano"] }
Browser (Chrome Prompt API)
Enable the browser feature and compile for wasm32 to use the browser's built-in AI APIs:
rs_ai_local = { version = "0.2", features = ["browser"] }
Use from Rust WASM:
use rs_ai_local::browser::{wasm_bridge::WasmBrowserBridge, BrowserAiOptions};
let bridge = WasmBrowserBridge;
let answer = bridge
.generate("Explain Rust ownership in one paragraph", &BrowserAiOptions::default())
.await?;
Build for the web:
wasm-pack build --target web --features browser
Android (Gemini Nano)
Enable the feature and add initialization to your Activity. They call our init function — just one line of Kotlin:
// MainActivity.kt
class MainActivity : Activity() {
companion object {
init { System.loadLibrary("rs_ai_local") }
@JvmStatic external fun init(context: Context)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
init(this)
}
}
Then use from Rust — your code:
use rs_ai_local::gemini_nano::init_with_context;
use rs_ai_local::gemini_nano::GeminiNanoProvider;
// In your Rust code
let provider = GeminiNanoProvider::new(my_bridge);
let response = provider.model().generate("Hello!").await?;
macOS (Foundation Models)
Enable the feature. Build on macOS with Xcode — build.rs auto-compiles the Swift bridge:
rs_ai_local = { version = "0.2", features = ["foundationmodels"] }
Use from Rust:
use rs_ai_local::foundationmodels::{is_available, respond};
if is_available() {
let answer = respond("What is Rust?").await?;
}
Windows (Phi Silica)
Enable the feature. Build with .NET SDK — build.rs auto-compiles the C# bridge:
rs_ai_local = { version = "0.2", features = ["phi-silica"] }
Use from Rust:
use rs_ai_local::phi_silica::respond;
let answer = respond("Hello!").await?;
Examples
Streaming
use rs_ai::rs_ai_claude;
use futures::StreamExt;
let mut stream = rs_ai_claude().stream("Write a poem").await?;
while let Some(chunk) = stream.next().await {
print!("{}", chunk?);
}
Testing
cargo test
License
MPL-2.0
Dependencies
~11–33MB
~387K SLoC