gemini-chat-api 0.1.5

Async Rust client for Google's internal Gemini Chat API
Documentation

Features

  • Asynchronous: Built on tokio and reqwest for non-blocking I/O.
  • Conversation Management: Maintains chat history and context.
  • File & Image Uploads: Support for sending images and files in prompts.
  • Multiple Models: Support for Gemini 2.0 Flash, 2.5 Pro, and others.
  • Auto-Rotation: Automatically rotates cookies to keep the session alive.
  • Browser Impersonation: Mimics Chrome headers to ensure successful authentication.

Note: Image generation and downloading features from the Python library are not supported in this Rust port. This client focuses on chat and text interaction.

Installation

Add the following to your Cargo.toml:

[dependencies]

gemini-chat-api = "0.1.4"

(Or path dependency if working locally)

Usage

Prerequisites

You need to obtain your __Secure-1PSID and __Secure-1PSIDTS cookies from Google Gemini.

  1. Go to https://gemini.google.com/app
  2. Open your browser's developer tools (F12).
  3. Go to the "Application" (or "Storage") tab.
  4. Under "Cookies" -> "https://gemini.google.com", find the __Secure-1PSID and __Secure-1PSIDTS cookies.
  5. Create a JSON file (e.g., cookies.json) with the following format:
[
    {
        "name": "__Secure-1PSID",
        "value": "YOUR_VALUE_HERE"
    },
    {
        "name": "__Secure-1PSIDTS",
        "value": "YOUR_VALUE_HERE"
    }
]

Quick Start

use gemini_chat_api::{utils::load_cookies, AsyncChatbot, Model};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Load cookies from file
    let (secure_1psid, secure_1psidts) = load_cookies("cookies.json")?;

    println!("Cookies loaded successfully.");

    // Initialize chatbot with 30s timeout
    let mut chatbot = AsyncChatbot::new(
        &secure_1psid,
        &secure_1psidts,
        Model::G2_5Pro,
        None, // No proxy
        30,   // Timeout in seconds
    )
    .await?;

    println!("Chatbot initialized.");

    // Ask a question
    println!("Sending message: 'Hello from Rust example!'");
    let response = chatbot.ask("Hello from Rust example!", None).await?;

    println!("--------------------------------------------------");
    println!("Gemini Response:");
    println!("{}", response.content);
    println!("--------------------------------------------------");

    Ok(())
}

Documentation

For detailed API documentation, visit docs.rs/gemini-chat-api.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/cool-feature)
  3. Commit your changes (git commit -m 'Add some cool feature')
  4. Push to the branch (git push origin feature/cool-feature)
  5. Open a Pull Request

Acknowledgements

This project is a Rust port of the Python Gemini-Chat-API by OEvortex. Special thanks to the original author for their work on reverse-engineering the Gemini API.

Support

If this crate saves you time or helps your work, support is appreciated:

Ko-fi

License

This project is licensed under the MIT License; see the license for details.