batch_processing/
batch_processing.rs1use 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 Batch Processing Example");
8 println!("==================================");
9
10 let client = AiClient::new(Provider::Groq)?;
12 println!(
13 "✅ Created client with provider: {:?}",
14 client.current_provider()
15 );
16
17 let requests = vec![
19 ChatCompletionRequest::new(
20 "llama3-8b-8192".to_string(),
21 vec![Message {
22 role: Role::User,
23 content: Content::Text("What is the capital of France?".to_string()),
24 function_call: None,
25 }],
26 )
27 .with_temperature(0.7)
28 .with_max_tokens(50),
29 ChatCompletionRequest::new(
30 "llama3-8b-8192".to_string(),
31 vec![Message {
32 role: Role::User,
33 content: Content::Text("What is 2 + 2?".to_string()),
34 function_call: None,
35 }],
36 )
37 .with_temperature(0.1)
38 .with_max_tokens(20),
39 ChatCompletionRequest::new(
40 "llama3-8b-8192".to_string(),
41 vec![Message {
42 role: Role::User,
43 content: Content::Text("Tell me a short joke.".to_string()),
44 function_call: None,
45 }],
46 )
47 .with_temperature(0.9)
48 .with_max_tokens(100),
49 ChatCompletionRequest::new(
50 "llama3-8b-8192".to_string(),
51 vec![Message {
52 role: Role::User,
53 content: Content::Text(
54 "What is the largest planet in our solar system?".to_string(),
55 ),
56 function_call: None,
57 }],
58 )
59 .with_temperature(0.5)
60 .with_max_tokens(60),
61 ];
62
63 println!(
64 "📤 Prepared {} requests for batch processing",
65 requests.len()
66 );
67
68 println!("\n🔄 Method 1: Batch processing with concurrency limit (2)");
70 let start_time = std::time::Instant::now();
71
72 let responses = client
73 .chat_completion_batch(requests.clone(), Some(2))
74 .await?;
75
76 let duration = start_time.elapsed();
77 println!("⏱️ Batch processing completed in {:?}", duration);
78
79 for (i, response) in responses.iter().enumerate() {
81 match response {
82 Ok(resp) => {
83 println!(
84 "✅ Request {}: {}",
85 i + 1,
86 resp.choices[0].message.content.as_text()
87 );
88 }
89 Err(e) => {
90 println!("❌ Request {} failed: {}", i + 1, e);
91 }
92 }
93 }
94
95 println!("\n🧠 Method 2: Smart batch processing");
97 let start_time = std::time::Instant::now();
98
99 let responses = client.chat_completion_batch_smart(requests.clone()).await?;
100
101 let duration = start_time.elapsed();
102 println!("⏱️ Smart batch processing completed in {:?}", duration);
103
104 let successful: Vec<_> = responses.iter().filter_map(|r| r.as_ref().ok()).collect();
106 let failed: Vec<_> = responses
107 .iter()
108 .enumerate()
109 .filter_map(|(i, r)| r.as_ref().err().map(|e| (i, e)))
110 .collect();
111
112 println!("📊 Results:");
113 println!(" ✅ Successful: {}/{}", successful.len(), responses.len());
114 println!(" ❌ Failed: {}/{}", failed.len(), responses.len());
115 println!(
116 " 📈 Success rate: {:.1}%",
117 (successful.len() as f64 / responses.len() as f64) * 100.0
118 );
119
120 println!("\n🚀 Method 3: Unlimited concurrent batch processing");
122 let start_time = std::time::Instant::now();
123
124 let responses = client.chat_completion_batch(requests, None).await?;
125
126 let duration = start_time.elapsed();
127 println!(
128 "⏱️ Unlimited concurrent processing completed in {:?}",
129 duration
130 );
131
132 for (i, response) in responses.iter().enumerate() {
134 match response {
135 Ok(resp) => {
136 println!(
137 "✅ Request {}: {}",
138 i + 1,
139 resp.choices[0].message.content.as_text()
140 );
141 }
142 Err(e) => {
143 println!("❌ Request {} failed: {}", i + 1, e);
144 }
145 }
146 }
147
148 println!("\n🎉 Batch processing example completed successfully!");
149 Ok(())
150}