builder_pattern/
builder_pattern.rs1use ai_lib::types::common::Content;
3use ai_lib::{AiClient, AiClientBuilder, ChatCompletionRequest, Message, Provider, Role};
4use std::time::Duration;
5
6#[tokio::main]
7async fn main() -> Result<(), Box<dyn std::error::Error>> {
8 println!("🚀 AI Client Builder Pattern Example");
9 println!("===================================");
10
11 println!("\n📋 Example 1: Simplest usage");
13 println!(" Automatically detect GROQ_BASE_URL and AI_PROXY_URL from environment variables");
14
15 let client = AiClientBuilder::new(Provider::Groq).build()?;
16 println!(
17 "✅ Client created successfully, provider: {:?}",
18 client.current_provider()
19 );
20
21 println!("\n📋 Example 2: Custom base_url");
23 println!(" Use custom Groq server address");
24
25 let client = AiClientBuilder::new(Provider::Groq)
26 .with_base_url("https://custom.groq.com")
27 .build()?;
28 println!("✅ Client created successfully with custom base_url");
29
30 println!("\n📋 Example 3: Custom base_url and proxy");
32 println!(" Use custom server and proxy");
33
34 let client = AiClientBuilder::new(Provider::Groq)
35 .with_base_url("https://custom.groq.com")
36 .with_proxy(Some("http://proxy.example.com:8080"))
37 .build()?;
38 println!("✅ Client created successfully with custom base_url and proxy");
39
40 println!("\n📋 Example 4: Full custom configuration");
42 println!(" Custom timeout, connection pool and other advanced configurations");
43
44 let client = AiClientBuilder::new(Provider::Groq)
45 .with_base_url("https://custom.groq.com")
46 .with_proxy(Some("http://proxy.example.com:8080"))
47 .with_timeout(Duration::from_secs(60))
48 .with_pool_config(32, Duration::from_secs(90))
49 .build()?;
50 println!("✅ Client created successfully with full custom configuration");
51
52 println!("\n📋 Example 5: Use convenient builder method");
54 println!(" Create builder through AiClient::builder()");
55
56 let client = AiClient::builder(Provider::Groq)
57 .with_base_url("https://custom.groq.com")
58 .with_proxy(Some("http://proxy.example.com:8080"))
59 .build()?;
60 println!("✅ Client created successfully using convenient builder method");
61
62 println!("\n📋 Example 6: Environment variable priority demonstration");
64 println!(" Set environment variables, then use builder");
65
66 std::env::set_var("GROQ_BASE_URL", "https://env.groq.com");
68 std::env::set_var("AI_PROXY_URL", "http://env.proxy.com:8080");
69
70 let client = AiClientBuilder::new(Provider::Groq).build()?;
72 println!("✅ Client created successfully using environment variable configuration");
73
74 let client = AiClientBuilder::new(Provider::Groq)
76 .with_base_url("https://explicit.groq.com")
77 .with_proxy(Some("http://explicit.proxy.com:8080"))
78 .build()?;
79 println!(
80 "✅ Client created successfully, explicit configuration overrides environment variables"
81 );
82
83 println!("\n📋 Example 7: Different provider configurations");
85
86 let groq_client = AiClientBuilder::new(Provider::Groq)
88 .with_base_url("https://custom.groq.com")
89 .build()?;
90 println!("✅ Groq client created successfully");
91
92 let deepseek_client = AiClientBuilder::new(Provider::DeepSeek)
94 .with_base_url("https://custom.deepseek.com")
95 .with_proxy(Some("http://proxy.example.com:8080"))
96 .build()?;
97 println!("✅ DeepSeek client created successfully");
98
99 let ollama_client = AiClientBuilder::new(Provider::Ollama)
101 .with_base_url("http://localhost:11434")
102 .build()?;
103 println!("✅ Ollama client created successfully");
104
105 println!("\n📋 Example 8: Error handling");
107 println!(" Try to set custom configuration for unsupported provider");
108
109 match AiClientBuilder::new(Provider::OpenAI)
110 .with_base_url("https://custom.openai.com")
111 .build()
112 {
113 Ok(_) => println!("❌ This should not succeed"),
114 Err(e) => println!("✅ Correctly caught error: {}", e),
115 }
116
117 println!("\n🎉 All examples completed!");
118 println!("\n💡 Advantages of builder pattern:");
119 println!(" 1. Automatic environment variable detection, reducing configuration code");
120 println!(" 2. Support for progressive custom configuration");
121 println!(" 3. Method chaining for cleaner code");
122 println!(" 4. Backward compatible, existing code requires no changes");
123 println!(" 5. Support for advanced configuration (timeout, connection pool, etc.)");
124
125 Ok(())
126}