2 releases
new 0.1.1 | Feb 22, 2025 |
---|---|
0.1.0 | Feb 22, 2025 |
#223 in Web programming
23KB
299 lines
Gemini SDK for Google Gemini API
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.
Overview
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.
Features
- Asynchronous API Calls: Uses the Tokio runtime for asynchronous HTTP communication.
- Modular Architecture: Separated into logical modules:
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.
- REST API Integration: Uses
reqwest
to interact with the Gemini API. - JSON Serialization/Deserialization: Uses
serde
to convert between Rust data structures and JSON. - Environment-based Configuration: Loads configuration from a
.env
file using thedotenv
crate.
Project Structure
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.
Setup
Prerequisites
- Rust 1.43+ (with async/await support)
- Cargo (Rust's package manager)
gcloud
CLI tool (required to retrieve an access token)- A
.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"
Building the Project
Run the following command to build the project:
cargo build
Running the Application
To run the project and see the Gemini SDK in action, execute:
cargo run
This will:
- Load the required environment variables.
- Retrieve an access token using the
gcloud
CLI. - Construct a request payload (with a prompt such as "Write a hello world program in Python").
- Send the request to the Gemini API.
- Process the streaming response and print the generated content.
Usage
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(())
}
Module Details
models.rs
Contains the data structure definitions (e.g., GenerateContentRequest
, Content
, GenerationConfig
, StreamResponse
, etc.) that represent the request and response formats expected by the Gemini API.
utils.rs
Includes helper functions such as:
create_request()
: Constructs a sampleGenerateContentRequest
with default values.create_safety_settings()
: Creates a list of safety settings to include in the request.
config.rs
Responsible for:
- Loading environment variables: Uses
dotenv::dotenv()
andstd::env::var
. - Access token retrieval: Executes the
gcloud auth print-access-token
command. - URL construction: Builds the full API URL based on the loaded configuration.
api.rs
Implements functions to:
- Make API requests: Uses
reqwest
to send a POST request with proper headers. - Process responses: Combines streaming response chunks by parsing JSON and extracting the final output.
gemini.rs
Defines 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.
Dependencies
Key dependencies used in this project include:
- dotenv: For loading environment variables.
- reqwest: For HTTP client functionality with JSON and streaming features.
- serde and serde_json: For JSON serialization and deserialization.
- tokio: For the asynchronous runtime.
- async-trait: To define asynchronous traits.
Future Enhancements
- Additional API Endpoints: Extend functionality as the Gemini API evolves.
- Enhanced Error Handling: Integrate robust error management and logging mechanisms.
- Testing: Implement unit and integration tests to ensure reliability.
- Improved Documentation: Expand examples and documentation for more comprehensive usage scenarios.
License
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!
Dependencies
~7–18MB
~240K SLoC