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
//! Synchronous client wrappers for blocking API operations.
//!
//! This module provides synchronous versions of the async Gemini API client,
//! using a managed Tokio runtime to block on async operations.

use core::time::Duration;
use crate::error::Error;
use crate::models::{
  GenerateContentRequest, GenerateContentResponse,
  EmbedContentRequest, EmbedContentResponse,
  ListModelsResponse, CreateCachedContentRequest, CachedContentResponse,
  ListCachedContentsResponse, UpdateCachedContentRequest,
};
use super::Client;

/// Synchronous client builder for blocking API operations
#[ derive( Debug, Clone ) ]
pub struct SyncClientBuilder
{
  api_key : Option< String >,
  timeout : Option< Duration >,
}

impl SyncClientBuilder
{
  /// Create a new sync client builder
  #[ must_use ]
  #[ inline ]
  pub fn new() -> Self
  {
    Self {
      api_key : None,
      timeout : None,
    }
  }

  /// Set the API key for authentication
  #[ must_use ]
  #[ inline ]
  pub fn api_key< S : Into< String > >( mut self, api_key : S ) -> Self
  {
    self.api_key = Some( api_key.into() );
    self
  }

  /// Set the request timeout
  #[ must_use ]
  #[ inline ]
  pub fn timeout( mut self, timeout : Duration ) -> Self
  {
    self.timeout = Some( timeout );
    self
  }


  /// Build the synchronous client
  ///
  /// # Errors
  ///
  /// Returns an error if the client cannot be built or if required parameters are missing
  #[ inline ]
  pub fn build( self ) -> Result< SyncClient, Error >
  {
    let api_key = self.api_key.ok_or_else( || Error::AuthenticationError(
      "API key is required for sync client".to_string()
    ) )?;

    let rt = tokio::runtime::Runtime::new()
      .map_err( |e| Error::NetworkError( format!( "Failed to create tokio runtime : {e}" ) ) )?;

    let client = rt.block_on( async {
      let mut builder = Client::builder().api_key( api_key );

      if let Some( timeout ) = self.timeout
      {
        builder = builder.timeout( timeout );
      }

      // Note : max_retries might not be available on ClientBuilder API
      // This would be a future enhancement when the builder API supports it

      builder.build()
    } )?;

    Ok( SyncClient {
      client,
      runtime : std::sync::Arc::new( rt ),
    } )
  }
}

impl Default for SyncClientBuilder
{
  #[ inline ]
  fn default() -> Self
  {
    Self::new()
  }
}

/// Synchronous client providing blocking API operations
#[ derive( Debug ) ]
pub struct SyncClient
{
  client : Client,
  runtime : std::sync::Arc< tokio::runtime::Runtime >,
}

impl SyncClient
{
  /// Get access to the models API
  #[ must_use ]
  #[ inline ]
  pub fn models( &self ) -> SyncModelsApi< '_ >
  {
    SyncModelsApi {
      client : &self.client,
      runtime : &self.runtime,
    }
  }

  /// Get access to the cached content API
  #[ must_use ]
  #[ inline ]
  pub fn cached_content( &self ) -> SyncCachedContentApi< '_ >
  {
    SyncCachedContentApi {
      client : &self.client,
      runtime : &self.runtime,
    }
  }
}

/// Synchronous models API providing blocking operations
#[ derive( Debug ) ]
pub struct SyncModelsApi< 'a >
{
  client : &'a Client,
  runtime : &'a tokio::runtime::Runtime,
}

impl SyncModelsApi< '_ >
{
  /// List all available models synchronously
  ///
  /// # Errors
  ///
  /// Returns an error if the API request fails
  #[ inline ]
  pub fn list( &self ) -> Result< ListModelsResponse, Error >
  {
    self.runtime.block_on( async {
      self.client.models().list().await
    } )
  }

  /// Get a specific model by name synchronously
  ///
  /// # Errors
  ///
  /// Returns an error if the model is not found or the API request fails
  #[ inline ]
  pub fn by_name< S : AsRef< str > >( &self, name : S ) -> Result< SyncModelApi< '_ >, Error >
  {
    let model_name = name.as_ref().to_string();

    Ok( SyncModelApi {
      client : self.client,
      runtime : self.runtime,
      model_name,
    } )
  }
}

/// Synchronous model API for specific model operations
#[ derive( Debug ) ]
pub struct SyncModelApi< 'a >
{
  client : &'a Client,
  runtime : &'a tokio::runtime::Runtime,
  model_name : String,
}

impl SyncModelApi< '_ >
{
  /// Generate content synchronously
  ///
  /// # Errors
  ///
  /// Returns an error if the content generation fails
  #[ inline ]
  pub fn generate_content( &self, request : &GenerateContentRequest ) -> Result< GenerateContentResponse, Error >
  {
    self.runtime.block_on( async {
      self.client.models().by_name( &self.model_name )
        .generate_content( request ).await
    } )
  }

  /// Generate content with streaming synchronously
  ///
  /// Collects all streaming responses into a vector of content strings.
  /// This method blocks until all streaming data is received.
  ///
  /// # Arguments
  ///
  /// * `request` - The content generation request
  ///
  /// # Returns
  ///
  /// Returns a vector of content strings from the streaming response
  ///
  /// # Errors
  ///
  /// Returns an error if the streaming request fails or if content parsing fails
  #[ cfg( feature = "streaming" ) ]
  #[ inline ]
  pub fn generate_content_stream( &self, request : &GenerateContentRequest ) -> Result< Vec< String >, Error >
  {
    use futures::StreamExt;

    self.runtime.block_on( async {
      let stream = self.client.models().by_name( &self.model_name )
        .generate_content_stream( request ).await?;

      let mut results = Vec::new();

      // Pin the stream to make it Unpin
      tokio ::pin!( stream );

      while let Some( response_result ) = stream.next().await
      {
        match response_result
        {
          Ok( response ) => {
            // Extract text content from streaming response
            if let Some( candidates ) = response.candidates
            {
              for candidate in candidates
              {
                for part in candidate.content.parts
                {
                  if let Some( text ) = part.text
                  {
                    results.push( text );
                  }
                }
              }
            }
          },
          Err( e ) => return Err( e ),
        }
      }

      Ok( results )
    } )
  }

  /// Generate embeddings synchronously
  ///
  /// # Errors
  ///
  /// Returns an error if the embedding generation fails
  #[ inline ]
  pub fn embed_content( &self, request : &EmbedContentRequest ) -> Result< EmbedContentResponse, Error >
  {
    self.runtime.block_on( async {
      self.client.models().by_name( &self.model_name )
        .embed_content( request ).await
    } )
  }

  /// Count tokens in the provided content synchronously
  ///
  /// # Arguments
  ///
  /// * `request` - The count tokens request containing content to analyze
  ///
  /// # Returns
  ///
  /// Returns the count tokens response with total token count
  ///
  /// # Errors
  ///
  /// Returns an error if the token counting fails or if the request is invalid
  #[ inline ]
  pub fn count_tokens( &self, request : &crate::models::CountTokensRequest ) -> Result< crate::models::CountTokensResponse, Error >
  {
    self.runtime.block_on( async {
      self.client.models().by_name( &self.model_name )
        .count_tokens( request ).await
    } )
  }
}

/// Synchronous cached content API providing blocking operations
#[ derive( Debug ) ]
pub struct SyncCachedContentApi< 'a >
{
  client : &'a Client,
  runtime : &'a tokio::runtime::Runtime,
}

impl SyncCachedContentApi< '_ >
{
  /// Create new cached content synchronously
  ///
  /// # Arguments
  ///
  /// * `request` - The create cached content request
  ///
  /// # Returns
  ///
  /// Returns the cached content response with details of the created cache
  ///
  /// # Errors
  ///
  /// Returns an error if the cached content creation fails
  #[ inline ]
  pub fn create( &self, request : &CreateCachedContentRequest ) -> Result< CachedContentResponse, Error >
  {
    self.runtime.block_on( async {
      self.client.cached_content().create( request ).await
    } )
  }

  /// List all cached contents synchronously
  ///
  /// # Arguments
  ///
  /// * `page_size` - Optional maximum number of cached contents to return per page
  /// * `page_token` - Optional token for retrieving subsequent pages
  ///
  /// # Returns
  ///
  /// Returns the list cached contents response with the available cache entries
  ///
  /// # Errors
  ///
  /// Returns an error if the listing operation fails
  #[ inline ]
  pub fn list( &self, page_size : Option< i32 >, page_token : Option< &str > ) -> Result< ListCachedContentsResponse, Error >
  {
    self.runtime.block_on( async {
      self.client.cached_content().list( page_size, page_token ).await
    } )
  }

  /// Get a specific cached content by ID synchronously
  ///
  /// # Arguments
  ///
  /// * `cache_id` - The unique identifier of the cached content to retrieve
  ///
  /// # Returns
  ///
  /// Returns the cached content response with the requested cache details
  ///
  /// # Errors
  ///
  /// Returns an error if the cached content is not found or the request fails
  #[ inline ]
  pub fn get( &self, cache_id : &str ) -> Result< CachedContentResponse, Error >
  {
    self.runtime.block_on( async {
      self.client.cached_content().get( cache_id ).await
    } )
  }

  /// Update cached content properties synchronously
  ///
  /// # Arguments
  ///
  /// * `cache_id` - The unique identifier of the cached content to update
  /// * `request` - The update cached content request with the changes
  ///
  /// # Returns
  ///
  /// Returns the updated cached content response
  ///
  /// # Errors
  ///
  /// Returns an error if the update operation fails or the cache is not found
  #[ inline ]
  pub fn update( &self, cache_id : &str, request : &UpdateCachedContentRequest ) -> Result< CachedContentResponse, Error >
  {
    self.runtime.block_on( async {
      self.client.cached_content().update( cache_id, request ).await
    } )
  }

  /// Delete cached content synchronously
  ///
  /// # Arguments
  ///
  /// * `cache_id` - The unique identifier of the cached content to delete
  ///
  /// # Returns
  ///
  /// Returns `Ok(())` if the cached content was successfully deleted
  ///
  /// # Errors
  ///
  /// Returns an error if the deletion fails or the cache is not found
  #[ inline ]
  pub fn delete( &self, cache_id : &str ) -> Result< (), Error >
  {
    self.runtime.block_on( async {
      self.client.cached_content().delete( cache_id ).await
    } )
  }
}