Crate google_ai_rs

Crate google_ai_rs 

Source
Expand description

Rust client for Google’s Generative AI APIs

Provides a highly ergonomic, type-safe, and performant interface for interacting with Google’s Generative AI services, including Gemini.

§💡 Highlights

  • Minimal Overhead: The core Client is tiny and allocation-light.
  • Configurable: TLS, JWT auth, and dependency minimization via features.
  • Fluent API: Builder-style configuration for temperature, safety settings, tools, etc.
  • Type-Safe Schemas: Use AsSchema to validate responses at compile-time.
  • Stateful Chat: The Session struct handles conversation history for you.
  • Multi-Modal Input: Mix text and images with Part or your own TryIntoContents impl.

§🚀 Quickstart (Chat Session)

A simple example of starting a chat session and streaming a response.

use google_ai_rs::{Client, GenerativeModel};
use std::io::{stdout, Write};
use tokio::io::AsyncWriteExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new("YOUR_API_KEY").await?;
    let model = client.generative_model("gemini-1.5-pro");

    let mut chat = model.start_chat();
    println!("🤖 Initializing chat session...");

    let prompt = "Explain 'Zero-shot learning' with a simple analogy.";

    let mut stream = chat.stream_send_message(prompt).await?;

    print!("🤖 ");
    let _ = stdout().flush();
    stream.write_to_sync(&mut tokio::io::stdout()).await?;

    println!();
    Ok(())
}

§📎 Multi-modal Input with TryIntoContents

Build your own structs that can be turned into model input. Great for combining text + images.

use google_ai_rs::{Client, Part, Error, content::TryIntoContents, Content};

struct UserQuery {
    text: String,
    attachments: Vec<Part>,
}

impl TryIntoContents for UserQuery {
    fn try_into_contents(self) -> Result<Vec<Content>, Error> {
        let mut parts = vec![Part::from(self.text)];
        parts.extend(self.attachments);
        Ok(vec![Content { role: "user".into(), parts }])
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new("YOUR_API_KEY").await?;
    let model = client.generative_model("gemini-1.5-flash");

    let product_image = std::fs::read("path/to/product.jpg")?;

    let user_query = UserQuery {
        text: "Analyze this product shot for defects".into(),
        attachments: vec![Part::blob("image/jpeg", product_image)],
    };

    let response = model.generate_content(user_query).await?;
    println!("{}", response.text());
    Ok(())
}

§🧾 Type-Safe Response Parsing with AsSchema

Strongly typed schemas ensure you get the structure you expect.

To enable type-safe response parsing, turn on the serde feature:

use google_ai_rs::{AsSchema, AsSchemaWithSerde, Client, Map};
use serde::Deserialize;
use std::collections::HashMap;

#[derive(AsSchemaWithSerde)]
struct PriceInfo(f32, bool); // (price, in stock)

#[derive(AsSchema, Deserialize, PartialEq, Eq, Hash)]
struct FashionBag {
    brand: String,
    style: String,
    material: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new("YOUR_API_KEY").await?;

    let model = client.typed_model::<Map<HashMap<FashionBag, PriceInfo>>>("gemini-1.5-flash");

    let inventory = model
        .generate_content("List 3 luxury bags with prices and stock status.")
        .await?;

    for (bag, price) in &inventory {
        println!("{} {}: ${} (in stock: {})", bag.brand, bag.style, price.0, price.1);
    }
    Ok(())
}

Re-exports§

pub use auth::Auth;
pub use client::Client;
pub use client::SharedClient;
pub use error::Error;
pub use genai::GenerativeModel;
pub use genai::TypedModel;
pub use genai::TypedResponse;
pub use crate::schema::AsSchema;
pub use crate::schema::Map;
pub use crate::schema::MapTrait;
pub use crate::schema::SchemaType;
pub use crate::schema::Tuple;
pub use content::IntoContent;
pub use content::IntoContents;
pub use content::IntoParts;
pub use content::TryFromCandidates;
pub use content::TryFromContents;
pub use content::TryIntoContent;
pub use content::TryIntoContents;

Modules§

auth
chat
client
content
embedding
error
genai
schema

Structs§

CachedContent
Content that has been preprocessed and can be used in subsequent request to GenerativeService.
Candidate
A response candidate generated from the model.
Content
The base structured datatype containing multi-part content of a message.
FunctionCall
A predicted FunctionCall returned from the model that contains a string representing the FunctionDeclaration.name with the arguments and their values.
GenerationConfig
Configuration options for model generation and outputs. Not all parameters are configurable for every model.
Part
A datatype containing media that is part of a multi-part Content message.
Schema
The Schema object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. Represents a select subset of an OpenAPI 3.0 schema object.
Tool
Tool details that the model may use to generate response.

Enums§

Data
TaskType
Type of task for which the embedding will be used.

Derive Macros§

AsSchema
Derive macro for AsSchema trait.