Rust クイックスタート
Rust クイックスタート
Section titled “Rust クイックスタート”インストール
Section titled “インストール”Cargo.toml に追加します:
[dependencies]ai-lib = { version = "0.7", features = ["full"] }tokio = { version = "1", features = ["full"] }futures = "0.3"すべての機能(embeddings、batch、guardrails、tokens、telemetry、routing_mvp、interceptors)を有効にするには full フィーチャーを使用します。または必要な機能のみ指定します(例:features = ["embeddings", "batch"])。
API キーを設定する
Section titled “API キーを設定する”export DEEPSEEK_API_KEY="your-key-here"基本チャット
Section titled “基本チャット”use ai_lib::{AiClient, Message};
#[tokio::main]async fn main() -> ai_lib::Result<()> { let client = AiClient::new("deepseek/deepseek-chat").await?;
let response = client.chat() .user("Explain quantum computing in simple terms") .temperature(0.7) .max_tokens(500) .execute() .await?;
println!("{}", response.content); Ok(())}ストリーミング
Section titled “ストリーミング”use ai_lib::{AiClient, StreamingEvent};use futures::StreamExt;
#[tokio::main]async fn main() -> ai_lib::Result<()> { let client = AiClient::new("deepseek/deepseek-chat").await?;
let mut stream = client.chat() .user("Write a haiku about Rust") .stream() .execute_stream() .await?;
while let Some(event) = stream.next().await { match event? { StreamingEvent::ContentDelta { text, .. } => print!("{text}"), StreamingEvent::StreamEnd { .. } => println!(), _ => {} } } Ok(())}ツール呼び出し
Section titled “ツール呼び出し”use ai_lib::{AiClient, ToolDefinition, StreamingEvent};use serde_json::json;use futures::StreamExt;
#[tokio::main]async fn main() -> ai_lib::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() .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 { name, .. } => println!("Calling: {name}"), StreamingEvent::PartialToolCall { arguments, .. } => print!("{arguments}"), StreamingEvent::ContentDelta { text, .. } => print!("{text}"), _ => {} } } Ok(())}マルチターン会話
Section titled “マルチターン会話”use ai_lib::{AiClient, Message, MessageRole};
#[tokio::main]async fn main() -> ai_lib::Result<()> { let client = AiClient::new("anthropic/claude-3-5-sonnet").await?;
let messages = vec![ Message::system("You are a helpful coding assistant."), Message::user("What is a closure in Rust?"), ];
let response = client.chat() .messages(messages) .execute() .await?;
println!("{}", response.content); Ok(())}let (response, stats) = client.chat() .user("Hello!") .execute_with_stats() .await?;
println!("Content: {}", response.content);println!("Total tokens: {}", stats.total_tokens);println!("Latency: {}ms", stats.latency_ms);次のステップ
Section titled “次のステップ”- AiClient API — 詳細な API リファレンス
- ストリーミングパイプライン — ストリーミングの仕組み
- 耐障害性 — サーキットブレーカー、レート制限
- 高度な機能 — 埋め込み、キャッシュ、プラグイン