Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ Cargo.lock
# Only here because this is a library, and I use main.rs for testing purposes.
src/main.rs
Testing/

examples/images/response.png
examples/images/response.json
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "2.0"
tokio = { version = "1.43", features = ["full"] }
base64 = { version = "0.21", optional = true }

[dev-dependencies]
tokio = { version = "1.43", default-features = false, features = [
"macros",
"rt-multi-thread",
] }

[features]
default = []
use-base64 = ["base64"]
75 changes: 75 additions & 0 deletions examples/generate_image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#[cfg(feature = "use-base64")]
use base64::Engine;
#[cfg(feature = "use-base64")]
use base64::engine::general_purpose;
use std::fs;
// function calling example
use gemini_rs::types::{InlineData, Part};
use serde::{Deserialize, Serialize};
/// Proposal : Image enum for processing in-line image data
/// [API Reference](https://ai.google.dev/gemini-api/docs/image-understanding#supported-formats)
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum InlineDataMimeTypeEnum {
#[serde(rename = "image/png")]
ImagePng,
#[serde(rename = "image/jpeg")]
ImageJpeg,
#[serde(rename = "image/webp")]
ImageWebp,
#[serde(rename = "image/heic")]
ImageHeic,
#[serde(rename = "image/heif")]
ImageHeif,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = gemini_rs::Client::instance();

let mut chat_list = vec![];
chat_list.push(Part::text("Remake Tree well"));
#[cfg(feature = "use-base64")]
{
chat_list.push(Part::inline_data(
"image/jpeg",
&general_purpose::STANDARD.encode(&fs::read("examples/images/tree.jpg")?),
));
}

let response = client
.chat("gemini-2.5-flash-image-preview")
.send_parted_messages(chat_list)
.await?;
let image = response.candidates[0]
.content
.parts
.iter()
.filter(|a| {
if let Part {
inline_data:
Some(InlineData {
mime_type: _,
data: _,
}),
..
} = a
{
true
} else {
false
}
})
.map(|img| img.inline_data.as_ref().unwrap().data.clone());

#[cfg(feature = "use-base64")]
{
let image_data = general_purpose::STANDARD.decode(image.collect::<Vec<_>>()[0].as_str())?;
let _ = fs::write("examples/images/response.png", &image_data)?;
}
#[cfg(not(feature = "use-base64"))]
{
let collected = image.collect::<Vec<_>>();
let image_data = collected[0].as_str();
let _ = fs::write("examples/images/response.json", image_data)?;
}
Ok(())
}
Binary file added examples/images/tree.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ impl<T> Chat<T> {
parts: vec![types::Part::text(message)],
});

self.generate_content().await
}
pub async fn send_parted_messages(&mut self, parts: Vec<types::Part>) -> Result<Response> {
self.history.push(types::Content {
role: types::Role::User,
parts,
});

self.generate_content().await
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ impl Part {
..Default::default()
}
}
pub fn inline_data(mime_type: &str, data: &str) -> Self {
Self {
inline_data: Some(InlineData {
mime_type: mime_type.into(),
data: data.into(),
}),
..Default::default()
}
}
}

/// Metadata for a video File
Expand Down Expand Up @@ -254,6 +263,7 @@ pub struct FileData {
///
/// [API Reference](https://ai.google.dev/api/caching#Blob)
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct InlineData {
pub mime_type: String,
pub data: String,
Expand Down
Loading