api_gemini 0.5.0

Gemini's API for accessing large language models (LLMs).
Documentation
# Implementation Patterns

This document provides reusable code patterns for common use cases with the Gemini API client.

## Quick Response Pattern

Use this pattern for simple, fire-and-forget text generation:

```rust
use api_gemini::{ client::Client, models::*, error::Error };

// Helper function for quick text generation
async fn quick_generate( prompt: &str ) -> Result< String, Error >
{
  let client = Client::new().map_err( |_| Error::ConfigurationError( "Failed to create client".to_string() ) )?;
  let request = GenerateContentRequest
  {
    contents: vec!
    [
      Content
      {
        parts: vec!
        [
          Part
          {
            text: Some( prompt.to_string() ),
            ..Default::default()
          }
        ],
        role: "user".to_string(),
      }
    ],
    ..Default::default()
  };

  let response = client
    .models()
    .by_name( "gemini-1.5-pro-latest" )
    .generate_content( &request )
    .await?;

  Ok
  (
    response.candidates
      .first()
      .and_then( |c| c.content.parts.first() )
      .and_then( |p| p.text.as_ref() )
      .map( |s| s.to_string() )
      .unwrap_or_else( || "No response".to_string() )
  )
}
```

## Error-Resilient Pattern

Use this pattern when you need graceful degradation:

```rust
use api_gemini::{ client::Client, error::Error, models::* };

async fn quick_generate( prompt: &str ) -> Result< String, Error >
{
  let client = Client::new().map_err( |_| Error::ConfigurationError( "Failed to create client".to_string() ) )?;
  let request = GenerateContentRequest
  {
    contents: vec!
    [
      Content
      {
        parts: vec!
        [
          Part
          {
            text: Some( prompt.to_string() ),
            ..Default::default()
          }
        ],
        role: "user".to_string(),
      }
    ],
    ..Default::default()
  };

  let response = client.models()
    .by_name( "gemini-1.5-pro-latest" )
    .generate_content( &request )
    .await?;

  Ok
  (
    response.candidates
      .first()
      .and_then( |c| c.content.parts.first() )
      .and_then( |p| p.text.as_ref() )
      .map( |s| s.to_string() )
      .unwrap_or_else( || "No response".to_string() )
  )
}

// Robust generation with fallback
async fn generate_with_fallback( prompt: &str ) -> String
{
  let client = match Client::new()
  {
    Ok( c ) => c,
    Err( _ ) => return "API client unavailable".to_string(),
  };

  match quick_generate( prompt ).await
  {
    Ok( response ) => response,
    Err( Error::RateLimitError( _ ) ) => "Rate limited - try again later".to_string(),
    Err( Error::TimeoutError( _ ) ) => "Request timed out".to_string(),
    Err( _ ) => "Generation failed".to_string(),
  }
}
```

## Batch Processing Pattern

Use this pattern for processing multiple prompts with rate limiting:

```rust
use api_gemini::{ client::Client, models::*, error::Error };

async fn quick_generate( prompt: &str ) -> Result< String, Error >
{
  let client = Client::new().map_err( |_| Error::ConfigurationError( "Failed to create client".to_string() ) )?;
  let request = GenerateContentRequest
  {
    contents: vec!
    [
      Content
      {
        parts: vec!
        [
          Part
          {
            text: Some( prompt.to_string() ),
            ..Default::default()
          }
        ],
        role: "user".to_string(),
      }
    ],
    ..Default::default()
  };

  let response = client.models()
    .by_name( "gemini-1.5-pro-latest" )
    .generate_content( &request )
    .await?;

  Ok
  (
    response.candidates
      .first()
      .and_then( |c| c.content.parts.first() )
      .and_then( |p| p.text.as_ref() )
      .map( |s| s.to_string() )
      .unwrap_or_else( || "No response".to_string() )
  )
}

// Process multiple prompts efficiently
async fn batch_generate( prompts: Vec< &str > ) -> Vec< String >
{
  let client = Client::new().expect( "API client" );
  let mut results = Vec::new();

  for prompt in prompts
  {
    match quick_generate( prompt ).await
    {
      Ok( response ) => results.push( response ),
      Err( _ ) => results.push( "Failed".to_string() ),
    }

    // Rate limiting protection
    tokio::time::sleep( std::time::Duration::from_millis( 100 ) ).await;
  }

  results
}
```

## Usage Examples

See the `examples/` directory for complete, runnable examples demonstrating these patterns in context.