Skip to content

Rust Quick Start

Examples below match the basic_usage example in the repository.

[dependencies]
ai-lib-rust = "1.2.0"
tokio = { version = "1", features = ["full"] }
futures = "0.3" # only needed for streaming

Optional capabilities:

ai-lib-rust = { version = "1.2.0", features = ["embeddings", "telemetry"] }
# or features = ["full"]
Terminal window
export DEEPSEEK_API_KEY="your-key-here"
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(())
}

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(())
}
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(())
}
use ai_lib_rust::AiClient;
use std::sync::Arc;
let client = Arc::new(AiClient::new("openai/gpt-4o").await?);

Set a local checkout of ai-protocol:

Terminal window
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?;
Terminal window
cd ai-lib-rust
DEEPSEEK_API_KEY=your_key cargo run --example basic_usage