| Crates.io | gmini |
| lib.rs | gmini |
| version | 0.1.1 |
| created_at | 2025-02-22 23:31:58.736773+00 |
| updated_at | 2025-02-22 23:36:55.239305+00 |
| description | A modular and asynchronous Rust SDK for interacting with the Google Gemini API. |
| homepage | |
| repository | https://github.com/RustSandbox/gemini |
| max_upload_size | |
| id | 1565920 |
| size | 82,391 |
A modular and asynchronous Rust SDK for interacting with the Google Gemini API. This project demonstrates best practices in Rust including modular code organization, async programming using Tokio, proper error handling, and JSON serialization/deserialization with Serde.
The Gemini SDK is designed to enable developers to easily integrate the Google Gemini API into their Rust applications. It provides functionality for creating content generation requests, handling streaming responses, and delivering end-to-end error handling. The SDK is built using a modular architecture to ensure each part of the code base is focused on a single responsibility.
models - Definitions for all data structures and enums.utils - Helper functions for building requests.config - Environment configuration, access token retrieval, and API URL construction.api - Functions for sending API requests and processing streaming responses.gemini - Contains the Gemini struct and an async trait (Think) for performing API calls.reqwest to interact with the Gemini API.serde to convert between Rust data structures and JSON..env file using the dotenv crate.gmini/
├── Cargo.toml
└── src
├── main.rs // Application entry point: creates a Gemini instance and calls the `Think` trait.
├── models.rs // Contains all data models (e.g., GenerateContentRequest, Content, GenerationConfig).
├── utils.rs // Contains helper functions (e.g., create_request, create_safety_settings).
├── config.rs // Handles environment variables, access token retrieval, and API URL construction.
├── api.rs // Contains API functions: making HTTP requests and processing streaming responses.
└── gemini.rs // Defines the Gemini struct and the async trait `Think` which implements the core functionality.
gcloud CLI tool (required to retrieve an access token).env file in the project root with the following variables:PROJECT_ID="your-project-id"
LOCATION_ID="your-location-id"
API_ENDPOINT="your-api-endpoint"
MODEL_ID="your-model-id"
GENERATE_CONTENT_API="your-generate-content-api"
Run the following command to build the project:
cargo build
To run the project and see the Gemini SDK in action, execute:
cargo run
This will:
gcloud CLI.Below is an example snippet illustrating how to use the Gemini SDK in your code:
use gemini::Think;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new Gemini instance using the async trait implementation
let gemini = gemini::Gemini::new().await?;
// Get a generated response from the API using the `think` method
let response = gemini.think("Write a hello world program in Python").await?;
// Print the response to the console
println!("{}", response);
Ok(())
}
models.rsContains the data structure definitions (e.g., GenerateContentRequest, Content, GenerationConfig, StreamResponse, etc.) that represent the request and response formats expected by the Gemini API.
utils.rsIncludes helper functions such as:
create_request(): Constructs a sample GenerateContentRequest with default values.create_safety_settings(): Creates a list of safety settings to include in the request.config.rsResponsible for:
dotenv::dotenv() and std::env::var.gcloud auth print-access-token command.api.rsImplements functions to:
reqwest to send a POST request with proper headers.gemini.rsDefines the Gemini struct and provides an async trait Think with two methods:
new(): Initializes a new Gemini instance by creating the HTTP client, loading configuration, and retrieving an access token.think(): Accepts a prompt, constructs the request, sends it to the API, processes the response, and returns the generated content.Key dependencies used in this project include:
Specify the project license here (e.g., MIT License).
This project demonstrates a clean and modular approach to building an asynchronous REST API client in Rust. Contributions and improvements are welcomed!