batch_generate/
batch_generate.rs1use gemini_rust::{BatchResultItem, BatchStatus, Gemini, Message};
15use std::time::Duration;
16
17#[tokio::main]
18async fn main() -> Result<(), Box<dyn std::error::Error>> {
19 let api_key = std::env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY not set");
21
22 let gemini = Gemini::new(api_key);
24
25 let request1 = gemini
27 .generate_content()
28 .with_message(Message::user("What is the meaning of life?"))
29 .build();
30
31 let request2 = gemini
33 .generate_content()
34 .with_message(Message::user("What is the best programming language?"))
35 .build();
36
37 let batch = gemini
39 .batch_generate_content_sync()
40 .with_request(request1)
41 .with_request(request2)
42 .execute()
43 .await?;
44
45 println!("Batch created successfully!");
47 println!("Batch Name: {}", batch.name());
48
49 println!("Waiting for batch to complete...");
51 match batch.wait_for_completion(Duration::from_secs(5)).await {
52 Ok(final_status) => {
53 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 }
94 }
95
96 Ok(())
97}