proxy_example/
proxy_example.rs

1/// AI-lib 代理服务器支持示例 - AI-lib proxy server support 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!("🌐 AI-lib Proxy Server Support Example");
8    println!("=====================================");
9
10    // Check proxy configuration
11    match std::env::var("AI_PROXY_URL") {
12        Ok(proxy_url) => {
13            println!("✅ Proxy configuration detected: {}", proxy_url);
14            println!("   All HTTP requests will go through this proxy server");
15        }
16        Err(_) => {
17            println!("ℹ️  AI_PROXY_URL environment variable not set");
18            println!("   To use proxy, set: export AI_PROXY_URL=http://proxy.example.com:8080");
19        }
20    }
21
22    println!("\n🚀 Creating AI client...");
23    let client = AiClient::new(Provider::Groq)?;
24    println!(
25        "✅ Client created successfully, provider: {:?}",
26        client.current_provider()
27    );
28
29    // Create test request
30    let request = ChatCompletionRequest::new(
31        "llama3-8b-8192".to_string(),
32        vec![Message {
33            role: Role::User,
34            content: Content::Text("Hello! This request may go through a proxy.".to_string()),
35            function_call: None,
36        }],
37    );
38
39    println!("\n📤 Preparing to send request...");
40    println!("   Model: {}", request.model);
41    println!("   Message: {}", request.messages[0].content.as_text());
42
43    // Get model list (this request will also go through proxy)
44    match client.list_models().await {
45        Ok(models) => {
46            println!("\n📋 Model list obtained through proxy:");
47            for model in models {
48                println!("   • {}", model);
49            }
50        }
51        Err(e) => {
52            println!("\n⚠️  Failed to get model list: {}", e);
53            println!("   This may be due to:");
54            println!("   • GROQ_API_KEY environment variable not set");
55            println!("   • Proxy server configuration error");
56            println!("   • Network connection issue");
57        }
58    }
59
60    println!("\n💡 Proxy Configuration Instructions:");
61    println!("   • Set environment variable: AI_PROXY_URL=http://your-proxy:port");
62    println!("   • Supports HTTP and HTTPS proxies");
63    println!("   • Supports authenticated proxies: http://user:pass@proxy:port");
64    println!("   • All AI providers will automatically use this proxy configuration");
65
66    Ok(())
67}