api_gemini 0.5.0

Gemini's API for accessing large language models (LLMs).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Batch Mode API for async job-based processing with 50% cost discount.
//!
//! Provides methods for creating batch jobs, polling status, and retrieving results.
//! Batch Mode offers 50% cost discount with 24-hour SLO.
//!
//! **NOTE:** As of 2025-10-11, this implementation uses mock responses. The real
//! Batch API endpoints (e.g., `/v1/batches`) are not yet available in v1beta.
//! Replace mock implementations with real API calls when the endpoints become available.

use crate::
{
  client ::Client,
  error ::Error,
  models ::
  {
    GenerateContentRequest,
    GenerateContentResponse,
    ContentEmbedding,
    Content,
    Part,
    batch ::*,
  },
};
use std::time::{ Duration, SystemTime };

/// API for managing batch jobs with async processing.
#[ derive( Debug ) ]
pub struct BatchApi< 'a >
{
  client : &'a Client,
}

impl< 'a > BatchApi< 'a >
{
  /// Create a new BatchApi instance.
  #[ inline ]
  pub fn new( client : &'a Client ) -> Self
  {
    Self { client }
  }

  /// Create a batch job with inline content generation requests.
  ///
  /// # Arguments
  ///
  /// * `model` - Model name (e.g., "gemini-2.5-flash")
  /// * `requests` - Vec of GenerateContentRequest to process
  ///
  /// # Returns
  ///
  /// Returns a BatchJob with job_id for status polling.
  ///
  /// # Errors
  ///
  /// Returns error if job creation fails or request is invalid.
  pub async fn create_inline(
    &self,
    model : &str,
    requests : Vec< GenerateContentRequest >
  ) -> Result< BatchJob, Error >
  {
    // Use client field to access base_url even in mock implementation
    let _base_url = &self.client.base_url;

    let job_id = format!( "batch_job_{}", uuid::Uuid::new_v4() );
    let request_count = requests.len();

    // Create batch job (mock implementation - replace with real API call)
    let batch_job = BatchJob
    {
      job_id : job_id.clone(),
      state : BatchJobState::Pending,
      model : model.to_string(),
      request_count,
      create_time : Some( SystemTime::now() ),
      expiration_time : Some( SystemTime::now() + Duration::from_secs( 86400 ) ), // 24 hours
      error : None,
    };

    Ok( batch_job )
  }

  /// Get status of a batch job.
  ///
  /// # Arguments
  ///
  /// * `job_id` - The batch job identifier
  ///
  /// # Returns
  ///
  /// Returns BatchJobStatus with current state and progress.
  ///
  /// # Errors
  ///
  /// Returns error if job not found or API call fails.
  pub async fn get_status( &self, job_id : &str ) -> Result< BatchJobStatus, Error >
  {
    // Mock implementation - replace with real API call
    let status = BatchJobStatus
    {
      job_id : job_id.to_string(),
      state : BatchJobState::Running,
      completed_count : 0,
      failed_count : 0,
      update_time : Some( SystemTime::now() ),
      error : None,
    };

    Ok( status )
  }

  /// Wait for batch job completion and retrieve results.
  ///
  /// Polls job status until completion or timeout.
  ///
  /// # Arguments
  ///
  /// * `job_id` - The batch job identifier
  /// * `timeout` - Maximum time to wait
  ///
  /// # Returns
  ///
  /// Returns BatchJobResults with all responses.
  ///
  /// # Errors
  ///
  /// Returns error if timeout reached or job fails.
  pub async fn wait_and_retrieve(
    &self,
    job_id : &str,
    timeout : Duration
  ) -> Result< BatchJobResults, Error >
  {
    let start = SystemTime::now();
    let poll_interval = Duration::from_secs( 5 );

    loop
    {
      let status = self.get_status( job_id ).await?;

      match status.state
      {
        BatchJobState::Succeeded | BatchJobState::PartiallyCompleted =>
        {
          return self.retrieve_results( job_id ).await;
        }
        BatchJobState::Failed =>
        {
          return Err( Error::ApiError(
            status.error.unwrap_or_else( || "Batch job failed".to_string() )
          ) );
        }
        BatchJobState::Cancelled =>
        {
          return Err( Error::ApiError( "Batch job was cancelled".to_string() ) );
        }
        BatchJobState::Pending | BatchJobState::Running =>
        {
          // Check timeout
          if start.elapsed().unwrap_or( Duration::ZERO ) > timeout
          {
            return Err( Error::ApiError( "Batch job timeout".to_string() ) );
          }

          // Wait before next poll
          tokio ::time::sleep( poll_interval ).await;
        }
      }
    }
  }

  /// Retrieve results from a completed batch job.
  ///
  /// # Arguments
  ///
  /// * `job_id` - The batch job identifier
  ///
  /// # Returns
  ///
  /// Returns BatchJobResults with responses and billing info.
  ///
  /// # Errors
  ///
  /// Returns error if results not available or expired.
  async fn retrieve_results( &self, job_id : &str ) -> Result< BatchJobResults, Error >
  {
    // Mock implementation - replace with real API call
    let results = BatchJobResults
    {
      job_id : job_id.to_string(),
      state : BatchJobState::Succeeded,
      responses : vec!
      [
        GenerateContentResponse
        {
          candidates : vec!
          [
            crate ::models::Candidate
            {
              content : Content
              {
                parts : vec!
                [
                  Part
                  {
                    text : Some( "Mock response".to_string() ),
                    ..Default::default()
                  }
                ],
                role : "model".to_string(),
              },
              finish_reason : Some( "STOP".to_string() ),
              safety_ratings : None,
              citation_metadata : None,
              token_count : Some( 10 ),
              index : Some( 0 ),
            }
          ],
          prompt_feedback : None,
          usage_metadata : None,
          grounding_metadata : None,
        }
      ],
      billing_metadata : Some( BatchBillingMetadata
      {
        discount_percentage : 50,
        standard_cost : 0.02,
        discounted_cost : 0.01,
        total_tokens : 100,
      } ),
      retrieve_time : Some( SystemTime::now() ),
    };

    Ok( results )
  }

  /// Cancel a running batch job.
  ///
  /// # Arguments
  ///
  /// * `job_id` - The batch job identifier
  ///
  /// # Errors
  ///
  /// Returns error if job cannot be cancelled or not found.
  pub async fn cancel( &self, job_id : &str ) -> Result< (), Error >
  {
    // Mock implementation - replace with real API call
    let _ = job_id;
    Ok( () )
  }

  /// List all batch jobs.
  ///
  /// # Returns
  ///
  /// Returns BatchJobList with jobs and optional next page token.
  ///
  /// # Errors
  ///
  /// Returns error if list operation fails.
  pub async fn list( &self ) -> Result< BatchJobList, Error >
  {
    self.list_with_page_size( None, None ).await
  }

  /// List batch jobs with pagination token.
  ///
  /// # Arguments
  ///
  /// * `page_token` - Token from previous list response
  ///
  /// # Returns
  ///
  /// Returns next page of batch jobs.
  ///
  /// # Errors
  ///
  /// Returns error if token invalid or list fails.
  pub async fn list_with_token( &self, page_token : &str ) -> Result< BatchJobList, Error >
  {
    self.list_with_page_size( None, Some( page_token.to_string() ) ).await
  }

  /// Internal list implementation with page size and token.
  async fn list_with_page_size(
    &self,
    _page_size : Option< i32 >,
    _page_token : Option< String >
  ) -> Result< BatchJobList, Error >
  {
    // Mock implementation - replace with real API call
    let list = BatchJobList
    {
      jobs : vec![],
      next_page_token : None,
    };

    Ok( list )
  }

  /// Create a batch job for embedding generation.
  ///
  /// # Arguments
  ///
  /// * `model` - Embedding model name (e.g., "text-embedding-004")
  /// * `texts` - Vec of text strings to embed
  ///
  /// # Returns
  ///
  /// Returns BatchJob for polling and result retrieval.
  ///
  /// # Errors
  ///
  /// Returns error if job creation fails.
  pub async fn create_embedding_batch(
    &self,
    model : &str,
    texts : Vec< String >
  ) -> Result< BatchJob, Error >
  {
    let job_id = format!( "batch_embed_{}", uuid::Uuid::new_v4() );
    let request_count = texts.len();

    let batch_job = BatchJob
    {
      job_id : job_id.clone(),
      state : BatchJobState::Pending,
      model : model.to_string(),
      request_count,
      create_time : Some( SystemTime::now() ),
      expiration_time : Some( SystemTime::now() + Duration::from_secs( 86400 ) ),
      error : None,
    };

    Ok( batch_job )
  }

  /// Wait for embedding batch completion and retrieve results.
  ///
  /// # Arguments
  ///
  /// * `job_id` - The batch job identifier
  /// * `timeout` - Maximum time to wait
  ///
  /// # Returns
  ///
  /// Returns BatchEmbeddingResults with all embeddings.
  ///
  /// # Errors
  ///
  /// Returns error if timeout or job fails.
  pub async fn wait_and_retrieve_embeddings(
    &self,
    job_id : &str,
    timeout : Duration
  ) -> Result< BatchEmbeddingResults, Error >
  {
    let start = SystemTime::now();
    let poll_interval = Duration::from_secs( 5 );

    loop
    {
      let status = self.get_status( job_id ).await?;

      match status.state
      {
        BatchJobState::Succeeded | BatchJobState::PartiallyCompleted =>
        {
          return self.retrieve_embedding_results( job_id ).await;
        }
        BatchJobState::Failed =>
        {
          return Err( Error::ApiError(
            status.error.unwrap_or_else( || "Batch job failed".to_string() )
          ) );
        }
        BatchJobState::Cancelled =>
        {
          return Err( Error::ApiError( "Batch job was cancelled".to_string() ) );
        }
        BatchJobState::Pending | BatchJobState::Running =>
        {
          if start.elapsed().unwrap_or( Duration::ZERO ) > timeout
          {
            return Err( Error::ApiError( "Batch job timeout".to_string() ) );
          }

          tokio ::time::sleep( poll_interval ).await;
        }
      }
    }
  }

  /// Retrieve embedding results from completed job.
  async fn retrieve_embedding_results( &self, job_id : &str ) -> Result< BatchEmbeddingResults, Error >
  {
    // Mock implementation - replace with real API call
    let results = BatchEmbeddingResults
    {
      job_id : job_id.to_string(),
      state : BatchJobState::Succeeded,
      embeddings : vec!
      [
        ContentEmbedding
        {
          values : vec![ 0.1, 0.2, 0.3 ],
        }
      ],
      billing_metadata : Some( BatchBillingMetadata
      {
        discount_percentage : 50,
        standard_cost : 0.01,
        discounted_cost : 0.005,
        total_tokens : 50,
      } ),
    };

    Ok( results )
  }
}