config_driven_example/
config_driven_example.rs

1/// 配置驱动的AI-lib示例 - Config-driven AI-lib example
2use ai_lib::types::common::Content;
3use ai_lib::{AiClient, ChatCompletionRequest, Message, Provider, Role};
4
5#[tokio::main]
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("🚀 Config-driven AI-lib Example");
8    println!("================================");
9
10    // Demonstrate the advantages of config-driven approach: easy provider switching
11    let providers = vec![
12        (Provider::Groq, "Groq"),
13        (Provider::OpenAI, "OpenAI"),
14        (Provider::DeepSeek, "DeepSeek"),
15    ];
16
17    for (provider, name) in providers {
18        println!("\n📡 Testing Provider: {}", name);
19
20        // Create client - just change the enum value
21        let client = AiClient::new(provider)?;
22        println!(
23            "✅ Client created successfully: {:?}",
24            client.current_provider()
25        );
26
27        // Get model list
28        match client.list_models().await {
29            Ok(models) => println!("📋 Available models: {:?}", models),
30            Err(e) => println!("⚠️  Failed to get model list: {}", e),
31        }
32
33        // Create test request
34        let request = ChatCompletionRequest::new(
35            "test-model".to_string(),
36            vec![Message {
37                role: Role::User,
38                content: Content::Text("Hello from ai-lib!".to_string()),
39                function_call: None,
40            }],
41        );
42
43        println!("📤 Request prepared, model: {}", request.model);
44        println!("   (Need to set corresponding API_KEY environment variable for actual calls)");
45    }
46
47    println!("\n🎯 Core advantages of config-driven approach:");
48    println!("   • Zero-code switching: just change Provider enum value");
49    println!("   • Unified interface: all providers use the same API");
50    println!("   • Rapid expansion: add new compatible providers with just configuration");
51
52    Ok(())
53}