batch_generate/
batch_generate.rs

1//! Batch generate content example
2//!
3//! This example demonstrates how to use the synchronous batch generate content API to generate content for multiple requests at once.
4//!
5//! To run this example, you need to have a Gemini API key. You can get one from the Google AI Studio.
6//!
7//! Once you have the API key, you can run this example by setting the `GEMINI_API_KEY` environment variable:
8//!
9//! ```sh
10//! export GEMINI_API_KEY=your_api_key
11//! cargo run --package gemini-rust --example batch_generate
12//! ```
13
14use gemini_rust::{BatchResultItem, BatchStatus, Gemini, Message};
15use std::time::Duration;
16
17#[tokio::main]
18async fn main() -> Result<(), Box<dyn std::error::Error>> {
19    // Get the API key from the environment
20    let api_key = std::env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY not set");
21
22    // Create a new Gemini client
23    let gemini = Gemini::new(api_key);
24
25    // Create the first request
26    let request1 = gemini
27        .generate_content()
28        .with_message(Message::user("What is the meaning of life?"))
29        .build();
30
31    // Create the second request
32    let request2 = gemini
33        .generate_content()
34        .with_message(Message::user("What is the best programming language?"))
35        .build();
36
37    // Create the batch request
38    let batch = gemini
39        .batch_generate_content_sync()
40        .with_request(request1)
41        .with_request(request2)
42        .execute()
43        .await?;
44
45    // Print the batch information
46    println!("Batch created successfully!");
47    println!("Batch Name: {}", batch.name());
48
49    // Wait for the batch to complete
50    println!("Waiting for batch to complete...");
51    match batch.wait_for_completion(Duration::from_secs(5)).await {
52        Ok(final_status) => {
53            // Print the final status
54            match final_status {
55                BatchStatus::Succeeded { results } => {
56                    println!("Batch succeeded!");
57                    for item in results {
58                        match item {
59                            BatchResultItem::Success { key, response } => {
60                                println!("--- Response for Key {} ---", key);
61                                println!("{}", response.text());
62                            }
63                            BatchResultItem::Error { key, error } => {
64                                println!("--- Error for Key {} ---", key);
65                                println!("Code: {}, Message: {}", error.code, error.message);
66                                if let Some(details) = &error.details {
67                                    println!("Details: {}", details);
68                                }
69                            }
70                        }
71                    }
72                }
73                BatchStatus::Cancelled => {
74                    println!("Batch was cancelled.");
75                }
76                BatchStatus::Expired => {
77                    println!("Batch expired.");
78                }
79                _ => {
80                    println!(
81                        "Batch finished with an unexpected status: {:?}",
82                        final_status
83                    );
84                }
85            }
86        }
87        Err((_batch, e)) => {
88            println!(
89                "Batch failed: {}. You can retry with the returned batch.",
90                e
91            );
92            // Here you could retry: batch.wait_for_completion(Duration::from_secs(5)).await, etc.
93        }
94    }
95
96    Ok(())
97}