quickstart/
quickstart.rs

1/// AI-lib 快速开始示例 - AI-lib quickstart example
2use ai_lib::types::common::Content;
3use ai_lib::{AiClient, AiClientBuilder, ChatCompletionRequest, Message, Provider, Role};
4
5#[tokio::main]
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("🚀 AI-lib Quickstart Example");
8    println!("============================");
9
10    // 🎯 Simplest usage - create client with one line of code
11    println!("\n📋 Simplest usage:");
12    let client = AiClient::new(Provider::Groq)?;
13    println!("✅ Client created successfully!");
14
15    // 🔧 If you need custom configuration, use builder pattern
16    println!("\n📋 Custom configuration:");
17    let client = AiClientBuilder::new(Provider::Groq)
18        .with_base_url("https://custom.groq.com") // Optional: custom server
19        .with_proxy(Some("http://proxy.example.com:8080")) // Optional: custom proxy
20        .build()?;
21    println!("✅ Custom client created successfully!");
22
23    // 📝 Create chat request
24    println!("\n📋 Create chat request:");
25    let request = ChatCompletionRequest::new(
26        "llama3-8b-8192".to_string(), // Model name
27        vec![Message {
28            role: Role::User,
29            content: Content::Text("Hello! How are you?".to_string()),
30            function_call: None,
31        }],
32    );
33    println!("✅ Request created successfully!");
34
35    // 🌐 Send request (requires GROQ_API_KEY environment variable)
36    println!("\n📋 Send request:");
37    println!("   Note: Set GROQ_API_KEY environment variable for actual API calls");
38    println!("   Usage: export GROQ_API_KEY=your_api_key_here");
39
40    // Check if API key is available
41    match std::env::var("GROQ_API_KEY") {
42        Ok(_) => {
43            println!("✅ GROQ_API_KEY detected, ready to send actual requests");
44            // Uncomment the following code to send actual request
45            // let response = client.chat_completion(request).await?;
46            // println!("🤖 AI response: {}", response.choices[0].message.content.as_text());
47        }
48        Err(_) => {
49            println!("ℹ️  GROQ_API_KEY not set, skipping actual request");
50            println!("   This is a demo showing how to build requests");
51        }
52    }
53
54    // 🎨 More customization options
55    println!("\n📋 More customization options:");
56    let advanced_client = AiClientBuilder::new(Provider::Groq)
57        .with_timeout(std::time::Duration::from_secs(60)) // 60 second timeout
58        .with_pool_config(16, std::time::Duration::from_secs(60)) // Connection pool config
59        .build()?;
60    println!("✅ Advanced configuration client created successfully!");
61
62    // 🔄 Switch to other providers
63    println!("\n📋 Switch to other providers:");
64    let deepseek_client = AiClient::new(Provider::DeepSeek)?;
65    println!("✅ DeepSeek client created successfully!");
66
67    let ollama_client = AiClient::new(Provider::Ollama)?;
68    println!("✅ Ollama client created successfully!");
69
70    println!("\n🎉 Quickstart completed!");
71    println!("\n💡 Key points:");
72    println!("   1. AiClient::new() - Simplest usage with automatic environment detection");
73    println!("   2. AiClientBuilder - Builder pattern with custom configuration support");
74    println!(
75        "   3. Environment variable priority: Explicit setting > Environment variable > Default"
76    );
77    println!("   4. Support for all mainstream AI providers");
78    println!("   5. Backward compatible, existing code requires no changes");
79
80    Ok(())
81}