#gemini #google #api-key #response #model #async-api #api-calls

ask_gemini

A library for interacting with the Google Gemini API

5 releases

0.1.4 Jun 3, 2024
0.1.3 May 30, 2024
0.1.2 May 30, 2024
0.1.1 May 30, 2024
0.1.0 May 30, 2024

#960 in Web programming

Download history 21/week @ 2024-08-26 4/week @ 2024-09-02 2/week @ 2024-09-09 9/week @ 2024-09-16 61/week @ 2024-09-23 25/week @ 2024-09-30 27/week @ 2024-10-14 4/week @ 2024-10-21 13/week @ 2024-10-28 23/week @ 2024-11-04 13/week @ 2024-11-18 21/week @ 2024-11-25 17/week @ 2024-12-02

52 downloads per month
Used in commiter

MIT license

9KB
101 lines

Google Gemini API for Rust: ask_gemini

Google Gemini API for Rust

The ask_gemini crate is a powerful and easy-to-use Rust crate designed for making asynchronous API calls to the Google Gemini API. This library simplifies the process of sending prompts and fetching responses, handling all network and serialization tasks internally. It is ideal for developers looking to integrate Google Gemini API capabilities into their Rust applications efficiently.

Donate

Please consider supporting this project by donating:

  • USDT / ETH / ... : 0xE4C3595D8a1F73F3b3EC0e7c75065C33B6C81f6D
  • DASH: Xu6NwsnSyybjQ52vZKaU8txNurzc6FudFz
  • Bitcoin (TRX): TE6Y5CNyLDCeZjChTf7wFAuRfFznPdREoR
  • Bitcoin (BTC): 1PH8EnCMxWNwvFKsk8PxSx66f8CF9kiyw1

Features

  • Asynchronous API Calls: Built on top of tokio and reqwest, ask_gemini offers non-blocking API calls to Gemini.
  • Error Handling: Comprehensive error handling that provides clear error messages related to network issues or data serialization.
  • Customizable: Supports custom API keys and model specifications, with defaults provided for immediate setup and use.
  • Secure: Implements best practices for safe API requests and ensures data integrity.

Google Gemini API key

Get your Google Gemini API key from the Google AI Studio.

Installation

Add ask_gemini to your Cargo.toml using cargo:

cargo add ask_gemini tokio

Or directly edit your Cargo.toml file:

[dependencies]
ask_gemini = "0.1.2"
tokio = "1.38.0"

Usage

Below is a simple example on how to use ask_gemini to send a prompt to the Gemini API and receive a response:

use ask_gemini::Gemini;

#[tokio::main]
async fn main() {
    let gemini = Gemini::new(Some("your_api_key_here"), None);
    let prompt = "Hello, world!";

    match gemini.ask(prompt).await {
        Ok(response) => println!("Response: {:?}", response),
        Err(e) => eprintln!("Error: {}", e),
    }
}

Using Environment Variables

You can also set the API key as an environment variable:

export GEMINI_API_KEY="your_api_key_here"

Then create a new instance of Gemini without specifying the API key:

use ask_gemini::Gemini;

#[tokio::main]
async fn main() {
    let gemini = Gemini::new(None, None);
    let prompt = "Hello, world!";

    match gemini.ask(prompt).await {
        Ok(response) => println!("Response: {:?}", response),
        Err(e) => eprintln!("Error: {}", e),
    }
}

Custom Model

You can specify a custom model when creating a new instance of Gemini:

use ask_gemini::Gemini;

#[tokio::main]
async fn main() {
    let gemini = Gemini::new(None, Some("model_name_here"));
    let prompt = "Hello, world!";

    match gemini.ask(prompt).await {
        Ok(response) => println!("Response: {:?}", response),
        Err(e) => eprintln!("Error: {}", e),
    }
}

Configuration

  • API Key: Ensure you have a valid API key from the Gemini API. This can be specified when creating an instance of Gemini or set as an environment variable GEMINI_API_KEY.
  • Model: Optionally specify the model when creating an instance of Gemini. The default model is used if none is specified.

Contributions

Contributions are welcome! Please feel free to submit pull requests or create issues for bugs, questions, or new features.

https://github.com/vvmspace/ask_gemini

Dependencies

~7–18MB
~233K SLoC