function_calling/
function_calling.rs1use gemini_client_api::gemini::ask::Gemini;
2use gemini_client_api::gemini::types::request::Tool;
3use gemini_client_api::gemini::types::sessions::Session;
4use gemini_client_api::gemini::utils::{GeminiSchema, execute_function_calls, gemini_function};
5use std::env;
6use std::error::Error;
7
8#[gemini_function]
11fn add_numbers(
13 a: f64,
15 b: f64,
17) -> f64 {
18 println!("[Executing Tool] adding {} + {}", a, b);
19 a + b
20}
21
22#[gemini_function]
23fn get_temperature(location: String) -> Result<String, &'static str> {
25 println!("[Executing Tool] getting temperature for {}", location);
26 Err("API is out of service")
27}
28
29#[tokio::main]
30async fn main() -> Result<(), Box<dyn Error>> {
31 let mut session = Session::new(10);
32 let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
33
34 let ai =
36 Gemini::new(api_key, "gemini-2.5-flash", None).set_tools(vec![Tool::FunctionDeclarations(
37 vec![
38 add_numbers::gemini_schema(),
39 get_temperature::gemini_schema(),
40 ],
41 )]);
42
43 let prompt = "What is 123.45 plus 678.9, and what's the weather like in London?";
44 println!("User: {}\n", prompt);
45
46 let mut response = ai.ask(session.ask(prompt)).await?;
48
49 loop {
51 if response.get_chat().has_function_call() {
52 println!("Gemini requested function calls...");
53
54 let results = execute_function_calls!(session, add_numbers, get_temperature);
56
57 for (idx, res) in results.iter().enumerate() {
58 if let Some(r) = res {
59 println!(" Call #{} result: {:?}", idx, r);
60 }
61 }
62
63 response = ai.ask(&mut session).await?;
65 } else {
66 println!("\nGemini: {}", response.get_chat().get_text_no_think(""));
68 break;
69 }
70 }
71
72 Ok(())
73}
74
75#[tokio::test]
76async fn handle_manually() {
77 let mut session = Session::new(10);
78 let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
79
80 let ai =
82 Gemini::new(api_key, "gemini-2.5-flash", None).set_tools(vec![Tool::FunctionDeclarations(
83 vec![
84 add_numbers::gemini_schema(),
85 get_temperature::gemini_schema(),
86 ],
87 )]);
88
89 let prompt = "What is 123.45 plus 678.9, and what's the weather like in London?";
90 println!("User: {}\n", prompt);
91
92 let mut response = ai.ask(session.ask(prompt)).await?;
94
95 loop {
97 if response.get_chat().has_function_call() {
98 println!("Gemini requested function calls...");
99
100 let _ = execute_function_calls!(session, add_numbers);
102
103 for call in response.get_chat().get_function_calls() {
104 if call.name() == "get_temperature" {
105 let (location,) =
106 get_temperature::parse_arguments(call.args().as_ref().unwrap())
107 .expect("Gemini responded with wrong argument format");
108
109 println!("[Executing call] getting temperature for {}", location);
110 session .add_function_response(
112 call.name(),
113 format!("temperature of {location} is 38 degree Celsius"),
114 )
115 .unwrap();
116 }
117 }
118
119 response = ai.ask(&mut session).await?;
121 } else {
122 println!("\nGemini: {}", response.get_chat().get_text_no_think(""));
124 break;
125 }
126 }
127}