Initial Commit
This commit is contained in:
113
examples/function-call.rs
Normal file
113
examples/function-call.rs
Normal file
@@ -0,0 +1,113 @@
|
||||
use std::{env, error::Error};
|
||||
|
||||
use google_genai::prelude::{
|
||||
Content, FunctionDeclaration, GeminiClient, GenerateContentRequest, Part, PartData, Role, Tools,
|
||||
};
|
||||
use serde_json::json;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
tracing_subscriber::fmt().init();
|
||||
let _ = dotenvy::dotenv();
|
||||
|
||||
let api_key = env::var("GEMINI_API_KEY")?;
|
||||
let gemini_client = GeminiClient::new(api_key);
|
||||
|
||||
let mut contents = vec![
|
||||
Content::builder()
|
||||
.role(Role::User)
|
||||
.add_text_part("What is the sbrubbles value of 213 and 231?")
|
||||
.build(),
|
||||
];
|
||||
|
||||
let sum_parameters = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"left": {
|
||||
"type": "integer",
|
||||
"description": "The first value for the sbrubbles calculation"
|
||||
},
|
||||
"right": {
|
||||
"type": "integer",
|
||||
"description": "The second value for the sbrubbles calculation"
|
||||
}
|
||||
},
|
||||
"required": ["left", "right"]
|
||||
});
|
||||
|
||||
let sum_result = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"result": {
|
||||
"type": "integer",
|
||||
"description": "The sbrubbles value calculation result",
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let sum_function = FunctionDeclaration {
|
||||
name: String::from("sbrubbles"),
|
||||
description: String::from("Calculates the sbrubbles value"),
|
||||
parameters: None,
|
||||
parameters_json_schema: Some(sum_parameters),
|
||||
response: None,
|
||||
response_json_schema: Some(sum_result),
|
||||
};
|
||||
|
||||
println!("{}", serde_json::to_string_pretty(&sum_function).unwrap());
|
||||
|
||||
let tools = Tools {
|
||||
function_declarations: Some(vec![sum_function]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let request = GenerateContentRequest::builder()
|
||||
.contents(contents.clone())
|
||||
.tools(vec![tools.clone()])
|
||||
.build();
|
||||
|
||||
let mut response = gemini_client
|
||||
.generate_content(&request, "gemini-3-pro-preview")
|
||||
.await?;
|
||||
|
||||
while let Some(candidate) = response.candidates.last()
|
||||
&& let Some(content) = &candidate.content
|
||||
&& let Some(parts) = &content.parts
|
||||
&& let Some(part) = parts.last()
|
||||
&& let PartData::FunctionCall { id, name, args, .. } = &part.data
|
||||
{
|
||||
contents.push(content.clone());
|
||||
match args {
|
||||
Some(args) => println!("Function call: {name}, {args}"),
|
||||
None => println!("Function call: {name}"),
|
||||
}
|
||||
|
||||
contents.push(Content {
|
||||
role: Some(Role::User),
|
||||
parts: Some(vec![Part {
|
||||
data: PartData::FunctionResponse {
|
||||
id: id.clone(),
|
||||
name: name.clone(),
|
||||
response: json!({"result": 1234}),
|
||||
will_continue: None,
|
||||
},
|
||||
media_resolution: None,
|
||||
part_metadata: None,
|
||||
thought: None,
|
||||
thought_signature: part.thought_signature.clone(),
|
||||
}]),
|
||||
});
|
||||
let request = GenerateContentRequest::builder()
|
||||
.contents(contents.clone())
|
||||
.tools(vec![tools.clone()])
|
||||
.build();
|
||||
|
||||
println!("{contents:?}");
|
||||
response = gemini_client
|
||||
.generate_content(&request, "gemini-3-pro-preview")
|
||||
.await?;
|
||||
}
|
||||
|
||||
println!("Response: {:#?}", response.candidates);
|
||||
Ok(())
|
||||
}
|
||||
26
examples/generate-content.rs
Normal file
26
examples/generate-content.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use std::{env, error::Error};
|
||||
|
||||
use google_genai::prelude::{Content, GeminiClient, GenerateContentRequest, Role};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
tracing_subscriber::fmt().init();
|
||||
let _ = dotenvy::dotenv();
|
||||
|
||||
let api_key = env::var("GEMINI_API_KEY")?;
|
||||
let gemini_client = GeminiClient::new(api_key);
|
||||
let prompt = vec![
|
||||
Content::builder()
|
||||
.role(Role::User)
|
||||
.add_text_part("What is the airspeed of an unladen swallow?")
|
||||
.build(),
|
||||
];
|
||||
|
||||
let request = GenerateContentRequest::builder().contents(prompt).build();
|
||||
let response = gemini_client
|
||||
.generate_content(&request, "gemini-3-pro-preview")
|
||||
.await?;
|
||||
|
||||
println!("Response: {:?}", response.candidates[0].get_text().unwrap());
|
||||
Ok(())
|
||||
}
|
||||
50
examples/generate-image.rs
Normal file
50
examples/generate-image.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use std::{env, error::Error, io::Cursor};
|
||||
|
||||
use google_genai::prelude::{
|
||||
GeminiClient, PersonGeneration, PredictImageRequest, PredictImageRequestParameters,
|
||||
PredictImageRequestParametersOutputOptions, PredictImageRequestPrompt,
|
||||
PredictImageSafetySetting,
|
||||
};
|
||||
use image::{ImageFormat, ImageReader};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
tracing_subscriber::fmt().init();
|
||||
let _ = dotenvy::dotenv();
|
||||
|
||||
let api_key = env::var("GEMINI_API_KEY")?;
|
||||
let gemini_client = GeminiClient::new(api_key);
|
||||
|
||||
let prompt = "
|
||||
Create an image of a tuxedo cat riding a rocket to the moon.";
|
||||
let request = PredictImageRequest {
|
||||
instances: vec![PredictImageRequestPrompt {
|
||||
prompt: prompt.to_string(),
|
||||
}],
|
||||
parameters: PredictImageRequestParameters {
|
||||
sample_count: 1,
|
||||
aspect_ratio: Some("1:1".to_string()),
|
||||
output_options: Some(PredictImageRequestParametersOutputOptions {
|
||||
mime_type: Some("image/jpeg".to_string()),
|
||||
compression_quality: Some(75),
|
||||
}),
|
||||
person_generation: Some(PersonGeneration::AllowAdult),
|
||||
safety_setting: Some(PredictImageSafetySetting::BlockLowAndAbove),
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
|
||||
println!("Request: {:#?}", serde_json::to_string(&request).unwrap());
|
||||
|
||||
let mut result = gemini_client
|
||||
.predict_image(&request, "imagen-4.0-generate-001")
|
||||
.await?;
|
||||
|
||||
let result = result.predictions.pop().unwrap();
|
||||
|
||||
let format = ImageFormat::from_mime_type(result.mime_type).unwrap();
|
||||
let img =
|
||||
ImageReader::with_format(Cursor::new(result.bytes_base64_encoded), format).decode()?;
|
||||
img.save("output.jpg")?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user