Rust Quick Start
Rust Quick Start
Section titled “Rust Quick Start”Examples below match the basic_usage example in the repository.
Installation
Section titled “Installation”[dependencies]ai-lib-rust = "1.2.0"tokio = { version = "1", features = ["full"] }futures = "0.3" # only needed for streamingOptional capabilities:
ai-lib-rust = { version = "1.2.0", features = ["embeddings", "telemetry"] }# or features = ["full"]API key
Section titled “API key”export DEEPSEEK_API_KEY="your-key-here"Basic chat
Section titled “Basic chat”use ai_lib_rust::{AiClient, Message};
#[tokio::main]async fn main() -> ai_lib_rust::Result<()> { let client = AiClient::new("deepseek/deepseek-chat").await?;
let response = client .chat() .messages(vec![ Message::system("You are a helpful assistant."), Message::user("Explain quantum computing in simple terms."), ]) .temperature(0.7) .max_tokens(500) .execute() .await?;
println!("{}", response.content); Ok(())}Streaming
Section titled “Streaming”Event variant is PartialContentDelta (not ContentDelta):
use ai_lib_rust::{AiClient, Message, StreamingEvent};use futures::StreamExt;
#[tokio::main]async fn main() -> ai_lib_rust::Result<()> { let client = AiClient::new("deepseek/deepseek-chat").await?;
let mut stream = client .chat() .messages(vec![Message::user("Write a haiku about Rust.")]) .stream() .execute_stream() .await?;
while let Some(event) = stream.next().await { match event? { StreamingEvent::PartialContentDelta { content, .. } => print!("{content}"), StreamingEvent::StreamEnd { .. } => break, _ => {} } } Ok(())}Tool calling (streaming)
Section titled “Tool calling (streaming)”use ai_lib_rust::{AiClient, Message, StreamingEvent, ToolDefinition};use futures::StreamExt;use serde_json::json;
#[tokio::main]async fn main() -> ai_lib_rust::Result<()> { let client = AiClient::new("openai/gpt-4o").await?;
let weather_tool = ToolDefinition { name: "get_weather".into(), description: Some("Get current weather".into()), parameters: json!({ "type": "object", "properties": { "city": { "type": "string" } }, "required": ["city"] }), };
let mut stream = client .chat() .messages(vec![Message::user("What's the weather in Tokyo?")]) .tools(vec![weather_tool]) .stream() .execute_stream() .await?;
while let Some(event) = stream.next().await { match event? { StreamingEvent::ToolCallStarted { tool_name, .. } => { println!("Calling: {tool_name}"); } StreamingEvent::PartialToolCall { arguments, .. } => print!("{arguments}"), StreamingEvent::PartialContentDelta { content, .. } => print!("{content}"), _ => {} } } Ok(())}Share AiClient across tasks
Section titled “Share AiClient across tasks”use ai_lib_rust::AiClient;use std::sync::Arc;
let client = Arc::new(AiClient::new("openai/gpt-4o").await?);Protocol manifests
Section titled “Protocol manifests”Set a local checkout of ai-protocol:
export AI_PROTOCOL_DIR="/path/to/ai-protocol"Or pass a base path in code:
use ai_lib_rust::protocol::ProtocolLoader;
let loader = ProtocolLoader::new().with_base_path("./ai-protocol");let manifest = loader.load_provider("openai").await?;Run the shipped example
Section titled “Run the shipped example”cd ai-lib-rustDEEPSEEK_API_KEY=your_key cargo run --example basic_usageNext steps
Section titled “Next steps”- Overview — architecture & feature boundaries
- Client API — builder reference
- Streaming — pipeline operators
- Resilience — opt-in policy layer