BatchBuilder

Struct BatchBuilder 

Source
pub struct BatchBuilder { /* private fields */ }
Expand description

A builder for creating and executing synchronous batch content generation requests.

This builder simplifies the process of constructing a batch request, allowing you to add multiple GenerateContentRequest items and then execute them as a single long-running operation.

Implementations§

Source§

impl BatchBuilder

Source

pub fn with_name(self, name: String) -> Self

Sets the user-friendly display name for the batch request.

Examples found in repository?
examples/batch_cancel.rs (line 25)
15async fn main() -> Result<()> {
16    // Get the API key from the environment
17    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
18
19    // Create the Gemini client
20    let gemini = Gemini::new(api_key);
21
22    // Create a batch with multiple requests
23    let mut batch_generate_content = gemini
24        .batch_generate_content_sync()
25        .with_name("batch_cancel_example".to_string());
26
27    // Add several requests to make the batch take some time to process
28    for i in 1..=10 {
29        let request = gemini
30            .generate_content()
31            .with_message(Message::user(format!(
32                "Write a creative story about a robot learning to paint, part {}. Make it at least 100 words long.",
33                i
34            )))
35            .build();
36
37        batch_generate_content = batch_generate_content.with_request(request);
38    }
39
40    // Build and start the batch
41    let batch = batch_generate_content.execute().await?;
42    println!("Batch created successfully!");
43    println!("Batch Name: {}", batch.name());
44    println!("Press CTRL-C to cancel the batch operation...");
45
46    // Wrap the batch in an Arc<Mutex<Option<Batch>>> to allow safe sharing
47    let batch = Arc::new(Mutex::new(Some(batch)));
48    let batch_clone = Arc::clone(&batch);
49
50    // Spawn a task to handle CTRL-C
51    let cancel_task = tokio::spawn(async move {
52        // Wait for CTRL-C signal
53        signal::ctrl_c().await.expect("Failed to listen for CTRL-C");
54        println!("Received CTRL-C, canceling batch operation...");
55
56        // Take the batch from the Option, leaving None.
57        // The lock is released immediately after this block.
58        let mut batch_to_cancel = batch_clone.lock().await;
59
60        if let Some(batch) = batch_to_cancel.take() {
61            // Cancel the batch operation
62            match batch.cancel().await {
63                Ok(()) => {
64                    println!("Batch canceled successfully!");
65                }
66                Err((batch, e)) => {
67                    println!("Failed to cancel batch: {}. Retrying...", e);
68                    // Retry once
69                    match batch.cancel().await {
70                        Ok(()) => {
71                            println!("Batch canceled successfully on retry!");
72                        }
73                        Err((_, retry_error)) => {
74                            eprintln!("Failed to cancel batch even on retry: {}", retry_error);
75                        }
76                    }
77                }
78            }
79        } else {
80            println!("Batch was already processed.");
81        }
82    });
83
84    // Wait for a short moment to ensure the cancel task is ready
85    tokio::time::sleep(Duration::from_millis(100)).await;
86
87    // Wait for the batch to complete or be canceled
88    if let Some(batch) = batch.lock().await.take() {
89        println!("Waiting for batch to complete or be canceled...");
90        match batch.wait_for_completion(Duration::from_secs(5)).await {
91            Ok(final_status) => {
92                // Cancel task is no longer needed since batch completed
93                cancel_task.abort();
94
95                println!("Batch completed with status: {:?}", final_status);
96
97                // Print some details about the results
98                match final_status {
99                    gemini_rust::BatchStatus::Succeeded { .. } => {
100                        println!("Batch succeeded!");
101                    }
102                    gemini_rust::BatchStatus::Cancelled => {
103                        println!("Batch was cancelled as requested.");
104                    }
105                    gemini_rust::BatchStatus::Expired => {
106                        println!("Batch expired.");
107                    }
108                    _ => {
109                        println!("Batch finished with an unexpected status.");
110                    }
111                }
112            }
113            Err((batch, e)) => {
114                // This could happen if there was a network error while polling
115                println!("Error while waiting for batch completion: {}", e);
116
117                // Try one more time to get the status
118                match batch.status().await {
119                    Ok(status) => println!("Current batch status: {:?}", status),
120                    Err(status_error) => println!("Error getting final status: {}", status_error),
121                }
122            }
123        }
124    }
125
126    Ok(())
127}
Source

pub fn with_requests(self, requests: Vec<GenerateContentRequest>) -> Self

Sets all requests for the batch operation, replacing any existing requests.

Source

pub fn with_request(self, request: GenerateContentRequest) -> Self

Adds a single GenerateContentRequest to the batch.

Examples found in repository?
examples/batch_generate.rs (line 40)
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}
More examples
Hide additional examples
examples/batch_cancel.rs (line 37)
15async fn main() -> Result<()> {
16    // Get the API key from the environment
17    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
18
19    // Create the Gemini client
20    let gemini = Gemini::new(api_key);
21
22    // Create a batch with multiple requests
23    let mut batch_generate_content = gemini
24        .batch_generate_content_sync()
25        .with_name("batch_cancel_example".to_string());
26
27    // Add several requests to make the batch take some time to process
28    for i in 1..=10 {
29        let request = gemini
30            .generate_content()
31            .with_message(Message::user(format!(
32                "Write a creative story about a robot learning to paint, part {}. Make it at least 100 words long.",
33                i
34            )))
35            .build();
36
37        batch_generate_content = batch_generate_content.with_request(request);
38    }
39
40    // Build and start the batch
41    let batch = batch_generate_content.execute().await?;
42    println!("Batch created successfully!");
43    println!("Batch Name: {}", batch.name());
44    println!("Press CTRL-C to cancel the batch operation...");
45
46    // Wrap the batch in an Arc<Mutex<Option<Batch>>> to allow safe sharing
47    let batch = Arc::new(Mutex::new(Some(batch)));
48    let batch_clone = Arc::clone(&batch);
49
50    // Spawn a task to handle CTRL-C
51    let cancel_task = tokio::spawn(async move {
52        // Wait for CTRL-C signal
53        signal::ctrl_c().await.expect("Failed to listen for CTRL-C");
54        println!("Received CTRL-C, canceling batch operation...");
55
56        // Take the batch from the Option, leaving None.
57        // The lock is released immediately after this block.
58        let mut batch_to_cancel = batch_clone.lock().await;
59
60        if let Some(batch) = batch_to_cancel.take() {
61            // Cancel the batch operation
62            match batch.cancel().await {
63                Ok(()) => {
64                    println!("Batch canceled successfully!");
65                }
66                Err((batch, e)) => {
67                    println!("Failed to cancel batch: {}. Retrying...", e);
68                    // Retry once
69                    match batch.cancel().await {
70                        Ok(()) => {
71                            println!("Batch canceled successfully on retry!");
72                        }
73                        Err((_, retry_error)) => {
74                            eprintln!("Failed to cancel batch even on retry: {}", retry_error);
75                        }
76                    }
77                }
78            }
79        } else {
80            println!("Batch was already processed.");
81        }
82    });
83
84    // Wait for a short moment to ensure the cancel task is ready
85    tokio::time::sleep(Duration::from_millis(100)).await;
86
87    // Wait for the batch to complete or be canceled
88    if let Some(batch) = batch.lock().await.take() {
89        println!("Waiting for batch to complete or be canceled...");
90        match batch.wait_for_completion(Duration::from_secs(5)).await {
91            Ok(final_status) => {
92                // Cancel task is no longer needed since batch completed
93                cancel_task.abort();
94
95                println!("Batch completed with status: {:?}", final_status);
96
97                // Print some details about the results
98                match final_status {
99                    gemini_rust::BatchStatus::Succeeded { .. } => {
100                        println!("Batch succeeded!");
101                    }
102                    gemini_rust::BatchStatus::Cancelled => {
103                        println!("Batch was cancelled as requested.");
104                    }
105                    gemini_rust::BatchStatus::Expired => {
106                        println!("Batch expired.");
107                    }
108                    _ => {
109                        println!("Batch finished with an unexpected status.");
110                    }
111                }
112            }
113            Err((batch, e)) => {
114                // This could happen if there was a network error while polling
115                println!("Error while waiting for batch completion: {}", e);
116
117                // Try one more time to get the status
118                match batch.status().await {
119                    Ok(status) => println!("Current batch status: {:?}", status),
120                    Err(status_error) => println!("Error getting final status: {}", status_error),
121                }
122            }
123        }
124    }
125
126    Ok(())
127}
Source

pub fn build(self) -> BatchGenerateContentRequest

Constructs the final BatchGenerateContentRequest from the builder’s configuration.

This method consumes the builder.

Source

pub async fn execute(self) -> Result<Batch>

Submits the batch request to the Gemini API and returns a Batch handle.

This method consumes the builder and initiates the long-running batch operation.

Examples found in repository?
examples/batch_generate.rs (line 42)
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}
More examples
Hide additional examples
examples/batch_cancel.rs (line 41)
15async fn main() -> Result<()> {
16    // Get the API key from the environment
17    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
18
19    // Create the Gemini client
20    let gemini = Gemini::new(api_key);
21
22    // Create a batch with multiple requests
23    let mut batch_generate_content = gemini
24        .batch_generate_content_sync()
25        .with_name("batch_cancel_example".to_string());
26
27    // Add several requests to make the batch take some time to process
28    for i in 1..=10 {
29        let request = gemini
30            .generate_content()
31            .with_message(Message::user(format!(
32                "Write a creative story about a robot learning to paint, part {}. Make it at least 100 words long.",
33                i
34            )))
35            .build();
36
37        batch_generate_content = batch_generate_content.with_request(request);
38    }
39
40    // Build and start the batch
41    let batch = batch_generate_content.execute().await?;
42    println!("Batch created successfully!");
43    println!("Batch Name: {}", batch.name());
44    println!("Press CTRL-C to cancel the batch operation...");
45
46    // Wrap the batch in an Arc<Mutex<Option<Batch>>> to allow safe sharing
47    let batch = Arc::new(Mutex::new(Some(batch)));
48    let batch_clone = Arc::clone(&batch);
49
50    // Spawn a task to handle CTRL-C
51    let cancel_task = tokio::spawn(async move {
52        // Wait for CTRL-C signal
53        signal::ctrl_c().await.expect("Failed to listen for CTRL-C");
54        println!("Received CTRL-C, canceling batch operation...");
55
56        // Take the batch from the Option, leaving None.
57        // The lock is released immediately after this block.
58        let mut batch_to_cancel = batch_clone.lock().await;
59
60        if let Some(batch) = batch_to_cancel.take() {
61            // Cancel the batch operation
62            match batch.cancel().await {
63                Ok(()) => {
64                    println!("Batch canceled successfully!");
65                }
66                Err((batch, e)) => {
67                    println!("Failed to cancel batch: {}. Retrying...", e);
68                    // Retry once
69                    match batch.cancel().await {
70                        Ok(()) => {
71                            println!("Batch canceled successfully on retry!");
72                        }
73                        Err((_, retry_error)) => {
74                            eprintln!("Failed to cancel batch even on retry: {}", retry_error);
75                        }
76                    }
77                }
78            }
79        } else {
80            println!("Batch was already processed.");
81        }
82    });
83
84    // Wait for a short moment to ensure the cancel task is ready
85    tokio::time::sleep(Duration::from_millis(100)).await;
86
87    // Wait for the batch to complete or be canceled
88    if let Some(batch) = batch.lock().await.take() {
89        println!("Waiting for batch to complete or be canceled...");
90        match batch.wait_for_completion(Duration::from_secs(5)).await {
91            Ok(final_status) => {
92                // Cancel task is no longer needed since batch completed
93                cancel_task.abort();
94
95                println!("Batch completed with status: {:?}", final_status);
96
97                // Print some details about the results
98                match final_status {
99                    gemini_rust::BatchStatus::Succeeded { .. } => {
100                        println!("Batch succeeded!");
101                    }
102                    gemini_rust::BatchStatus::Cancelled => {
103                        println!("Batch was cancelled as requested.");
104                    }
105                    gemini_rust::BatchStatus::Expired => {
106                        println!("Batch expired.");
107                    }
108                    _ => {
109                        println!("Batch finished with an unexpected status.");
110                    }
111                }
112            }
113            Err((batch, e)) => {
114                // This could happen if there was a network error while polling
115                println!("Error while waiting for batch completion: {}", e);
116
117                // Try one more time to get the status
118                match batch.status().await {
119                    Ok(status) => println!("Current batch status: {:?}", status),
120                    Err(status_error) => println!("Error getting final status: {}", status_error),
121                }
122            }
123        }
124    }
125
126    Ok(())
127}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,