1use gemini_rust::{
2 Content, FunctionCallingMode, FunctionDeclaration, FunctionParameters, Gemini, Message,
3 PropertyDetails, Role, Tool,
4};
5use serde_json::json;
6use std::env;
7
8#[tokio::main]
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10 let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
12
13 let client = Gemini::new(api_key);
15
16 println!("--- Tools example with multiple functions ---");
17
18 let get_weather = FunctionDeclaration::new(
20 "get_weather",
21 "Get the current weather for a location",
22 FunctionParameters::object()
23 .with_property(
24 "location",
25 PropertyDetails::string("The city and state, e.g., San Francisco, CA"),
26 true,
27 )
28 .with_property(
29 "unit",
30 PropertyDetails::enum_type("The unit of temperature", ["celsius", "fahrenheit"]),
31 false,
32 ),
33 );
34
35 let calculate = FunctionDeclaration::new(
37 "calculate",
38 "Perform a calculation",
39 FunctionParameters::object()
40 .with_property(
41 "operation",
42 PropertyDetails::enum_type(
43 "The mathematical operation to perform",
44 ["add", "subtract", "multiply", "divide"],
45 ),
46 true,
47 )
48 .with_property("a", PropertyDetails::number("The first number"), true)
49 .with_property("b", PropertyDetails::number("The second number"), true),
50 );
51
52 let tool = Tool::with_functions(vec![get_weather, calculate]);
54
55 let response = client
57 .generate_content()
58 .with_system_prompt(
59 "You are a helpful assistant that can check weather and perform calculations.",
60 )
61 .with_user_message("What's 42 times 12?")
62 .with_tool(tool)
63 .with_function_calling_mode(FunctionCallingMode::Any)
64 .execute()
65 .await?;
66
67 if let Some(function_call) = response.function_calls().first() {
69 println!(
70 "Function call: {} with args: {}",
71 function_call.name, function_call.args
72 );
73
74 match function_call.name.as_str() {
76 "calculate" => {
77 let operation: String = function_call.get("operation")?;
78 let a: f64 = function_call.get("a")?;
79 let b: f64 = function_call.get("b")?;
80
81 println!("Calculation: {} {} {}", a, operation, b);
82
83 let result = match operation.as_str() {
84 "add" => a + b,
85 "subtract" => a - b,
86 "multiply" => a * b,
87 "divide" => a / b,
88 _ => panic!("Unknown operation"),
89 };
90
91 let function_response = json!({
92 "result": result,
93 })
94 .to_string();
95
96 let mut conversation = client.generate_content();
103
104 conversation = conversation
106 .with_system_prompt("You are a helpful assistant that can check weather and perform calculations.")
107 .with_user_message("What's 42 times 12?");
108
109 let model_content = Content::function_call((*function_call).clone());
111
112 let model_message = Message {
114 content: model_content,
115 role: Role::Model,
116 };
117 conversation = conversation.with_message(model_message);
118
119 conversation =
121 conversation.with_function_response_str("calculate", function_response)?;
122
123 let final_response = conversation.execute().await?;
125
126 println!("Final response: {}", final_response.text());
127 }
128 "get_weather" => {
129 let location: String = function_call.get("location")?;
130 let unit = function_call
131 .get::<String>("unit")
132 .unwrap_or_else(|_| String::from("celsius"));
133
134 println!("Weather request for: {}, Unit: {}", location, unit);
135
136 let weather_response = json!({
137 "temperature": 22,
138 "unit": unit,
139 "condition": "sunny"
140 })
141 .to_string();
142
143 let mut conversation = client.generate_content();
150
151 conversation = conversation
153 .with_system_prompt("You are a helpful assistant that can check weather and perform calculations.")
154 .with_user_message("What's 42 times 12?");
155
156 let model_content = Content::function_call((*function_call).clone());
158
159 let model_message = Message {
161 content: model_content,
162 role: Role::Model,
163 };
164 conversation = conversation.with_message(model_message);
165
166 conversation =
168 conversation.with_function_response_str("get_weather", weather_response)?;
169
170 let final_response = conversation.execute().await?;
172
173 println!("Final response: {}", final_response.text());
174 }
175 _ => println!("Unknown function"),
176 }
177 } else {
178 println!("No function calls in the response.");
179 println!("Response: {}", response.text());
180 }
181
182 Ok(())
183}