use adk_gemini::{Content, FunctionCallingMode, FunctionDeclaration, Gemini, Message, Role, Tool};
use display_error_chain::DisplayErrorChain;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::env;
use std::process::ExitCode;
use tracing::info;
#[derive(Default, Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[schemars(description = "The unit of temperature")]
#[serde(rename_all = "lowercase")]
enum Unit {
#[default]
Celsius,
Fahrenheit,
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(default)]
struct Weather {
location: String,
unit: Option<Unit>,
}
impl Default for Weather {
fn default() -> Self {
Weather { location: "".to_string(), unit: Some(Unit::Celsius) }
}
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
struct WeatherResponse {
temperature: i32,
unit: String,
condition: String,
}
#[derive(Default, Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[schemars(description = "The mathematical operation to perform")]
#[serde(rename_all = "lowercase")]
enum Operation {
#[default]
Add,
Subtract,
Multiply,
Divide,
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(default)]
struct Calculation {
operation: Operation,
a: f64,
b: f64,
}
impl Default for Calculation {
fn default() -> Self {
Calculation { operation: Operation::Add, a: 0.0, b: 0.0 }
}
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
struct CalculationResponse {
result: f64,
}
#[tokio::main]
async fn main() -> ExitCode {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing::level_filters::LevelFilter::INFO.into())
.from_env_lossy(),
)
.init();
match do_main().await {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
let error_chain = DisplayErrorChain::new(e.as_ref());
tracing::error!(error.debug = ?e, error.chained = %error_chain, "execution failed");
ExitCode::FAILURE
}
}
}
async fn do_main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
let client = Gemini::new(api_key).expect("unable to create Gemini API client");
info!("starting tools example with multiple functions");
let get_weather =
FunctionDeclaration::new("get_weather", "Get the current weather for a location", None)
.with_parameters::<Weather>()
.with_response::<WeatherResponse>();
let calculate = FunctionDeclaration::new("calculate", "Perform a calculation", None)
.with_parameters::<Calculation>()
.with_response::<CalculationResponse>();
let tool = Tool::with_functions(vec![get_weather, calculate]);
let response = client
.generate_content()
.with_system_prompt(
"You are a helpful assistant that can check weather and perform calculations.",
)
.with_user_message("What's 42 times 12?")
.with_tool(tool)
.with_function_calling_mode(FunctionCallingMode::Any)
.execute()
.await?;
if let Some(function_call) = response.function_calls().first() {
info!(
function_name = function_call.name,
args = ?function_call.args,
"function call received"
);
match function_call.name.as_str() {
"calculate" => {
let calculation: Calculation = serde_json::from_value(function_call.args.clone())?;
info!(
operation = ?calculation.operation,
a = calculation.a,
b = calculation.b,
"performing calculation"
);
let result = match calculation.operation {
Operation::Add => calculation.a + calculation.b,
Operation::Subtract => calculation.a - calculation.b,
Operation::Multiply => calculation.a * calculation.b,
Operation::Divide => calculation.a / calculation.b,
};
let function_response = CalculationResponse { result };
let mut conversation = client.generate_content();
conversation = conversation
.with_system_prompt("You are a helpful assistant that can check weather and perform calculations.")
.with_user_message("What's 42 times 12?");
let model_content = Content::function_call((*function_call).clone());
let model_message = Message { content: model_content, role: Role::Model };
conversation = conversation.with_message(model_message);
conversation =
conversation.with_function_response("calculate", function_response)?;
let final_response = conversation.execute().await?;
info!(response = final_response.text(), "final response received");
}
"get_weather" => {
let weather: Weather = serde_json::from_value(function_call.args.clone())?;
info!(
location = weather.location,
unit = ?weather.unit,
"weather request received"
);
let unit_str = match weather.unit.unwrap_or_default() {
Unit::Celsius => "celsius",
Unit::Fahrenheit => "fahrenheit",
};
let weather_response = WeatherResponse {
temperature: 22,
unit: unit_str.to_string(),
condition: "sunny".to_string(),
};
let mut conversation = client.generate_content();
conversation = conversation
.with_system_prompt("You are a helpful assistant that can check weather and perform calculations.")
.with_user_message("What's 42 times 12?");
let model_content = Content::function_call((*function_call).clone());
let model_message = Message { content: model_content, role: Role::Model };
conversation = conversation.with_message(model_message);
conversation =
conversation.with_function_response("get_weather", weather_response)?;
let final_response = conversation.execute().await?;
info!(response = final_response.text(), "final response received");
}
_ => info!(function_name = function_call.name, "unknown function call"),
}
} else {
info!("no function calls in response");
info!(response = response.text(), "direct response received");
}
Ok(())
}