gemini_crate 0.1.0

A robust Rust client library for Google's Gemini AI API with built-in error handling, retry logic, and comprehensive model support
Documentation
//! Model listing example using the Gemini crate
//!
//! This example demonstrates how to list all available Gemini models
//! and their capabilities using the gemini_crate library.
//!
//! Usage:
//! 1. Set your GEMINI_API_KEY in a .env file
//! 2. Run: cargo run --example list_models
//! 3. View all available models with their details

use gemini_crate::{client::GeminiClient, errors::Error};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load environment variables from .env file
    dotenvy::dotenv().ok();

    // Create the Gemini client
    let client = match GeminiClient::new() {
        Ok(c) => c,
        Err(Error::Config(msg)) => {
            eprintln!("❌ Configuration error: {}", msg);
            eprintln!("💡 Make sure to set GEMINI_API_KEY in your .env file");
            eprintln!("   Example: GEMINI_API_KEY=your_api_key_here");
            return Ok(());
        }
        Err(e) => {
            eprintln!("❌ Failed to create Gemini client: {}", e);
            return Ok(());
        }
    };

    println!("🤖 Gemini API - Available Models");
    println!("=================================\n");

    // Fetch the list of available models
    print!("📡 Fetching available models... ");
    std::io::Write::flush(&mut std::io::stdout())?;

    match client.list_models().await {
        Ok(models_response) => {
            println!("✅ Done!\n");

            if models_response.models.is_empty() {
                println!("⚠️  No models found.");
                return Ok(());
            }

            println!("Found {} models:\n", models_response.models.len());

            // Group models by type for better organization
            let mut text_generation_models = Vec::new();
            let mut embedding_models = Vec::new();
            let mut image_models = Vec::new();
            let mut other_models = Vec::new();

            for model in &models_response.models {
                if model
                    .supported_generation_methods
                    .contains(&"generateContent".to_string())
                {
                    if model.name.contains("embedding") {
                        embedding_models.push(model);
                    } else if model.name.contains("imagen") || model.name.contains("veo") {
                        image_models.push(model);
                    } else {
                        text_generation_models.push(model);
                    }
                } else if model
                    .supported_generation_methods
                    .contains(&"embedContent".to_string())
                    || model
                        .supported_generation_methods
                        .contains(&"embedText".to_string())
                {
                    embedding_models.push(model);
                } else if model
                    .supported_generation_methods
                    .contains(&"predict".to_string())
                    || model
                        .supported_generation_methods
                        .contains(&"predictLongRunning".to_string())
                {
                    image_models.push(model);
                } else {
                    other_models.push(model);
                }
            }

            // Display text generation models
            if !text_generation_models.is_empty() {
                println!("📝 TEXT GENERATION MODELS");
                println!("-------------------------");
                for model in text_generation_models {
                    print_model_details(model);
                }
                println!();
            }

            // Display embedding models
            if !embedding_models.is_empty() {
                println!("🔢 EMBEDDING MODELS");
                println!("-------------------");
                for model in embedding_models {
                    print_model_details(model);
                }
                println!();
            }

            // Display image/video models
            if !image_models.is_empty() {
                println!("🎨 IMAGE/VIDEO MODELS");
                println!("---------------------");
                for model in image_models {
                    print_model_details(model);
                }
                println!();
            }

            // Display other models
            if !other_models.is_empty() {
                println!("🔧 OTHER MODELS");
                println!("---------------");
                for model in other_models {
                    print_model_details(model);
                }
                println!();
            }

            // Show recommended models for common use cases
            println!("💡 RECOMMENDED MODELS FOR COMMON USE CASES");
            println!("------------------------------------------");
            println!("🚀 Fast text generation:     gemini-2.5-flash");
            println!("🧠 Complex reasoning:        gemini-2.5-pro");
            println!("🔄 Always latest version:    gemini-flash-latest");
            println!("📊 Text embeddings:          text-embedding-004");
            println!("🎯 Stable production:        gemini-2.0-flash-001");
        }
        Err(Error::Network(e)) => {
            println!("");
            eprintln!("🌐 Network error: {}", e);
            eprintln!("💡 Check your internet connection and try again.");
        }
        Err(Error::Api(msg)) => {
            println!("");
            eprintln!("🚫 API error: {}", msg);
            eprintln!("💡 Check your API key and try again.");
        }
        Err(e) => {
            println!("");
            eprintln!("❌ Unexpected error: {}", e);
        }
    }

    Ok(())
}

/// Helper function to print model details in a nice format
fn print_model_details(model: &gemini_crate::client::Model) {
    println!("  🔹 {}", model.name);
    if !model.display_name.is_empty() {
        println!("     Display Name: {}", model.display_name);
    }
    if !model.description.is_empty() {
        println!("     Description:  {}", model.description);
    }
    if !model.version.is_empty() {
        println!("     Version:      {}", model.version);
    }
    if model.input_token_limit > 0 {
        println!("     Input Limit:  {} tokens", model.input_token_limit);
    }
    if model.output_token_limit > 0 {
        println!("     Output Limit: {} tokens", model.output_token_limit);
    }
    if !model.supported_generation_methods.is_empty() {
        println!(
            "     Methods:      {:?}",
            model.supported_generation_methods
        );
    }
    println!();
}