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 println!("--- Function Calling Example ---");
44 let prompt = "What is 123.45 plus 678.9, and what's the weather like in London?";
45 println!("User: {}\n", prompt);
46
47 let mut response = ai.ask(session.ask(prompt)).await?;
49
50 loop {
52 if response.get_chat().has_function_call() {
53 println!("Gemini requested function calls...");
54
55 let results = execute_function_calls!(session, add_numbers, get_temperature);
57
58 for (idx, res) in results.iter().enumerate() {
59 if let Some(r) = res {
60 println!(" Call #{} result: {:?}", idx, r);
61 }
62 }
63
64 response = ai.ask(&mut session).await?;
66 } else {
67 println!("\nGemini: {}", response.get_chat().get_text_no_think(""));
69 break;
70 }
71 }
72
73 Ok(())
74}
75
76#[tokio::test]
77async fn handle_manually() {
78 use gemini_client_api::gemini::types::request::PartType;
79 let mut session = Session::new(10);
80 let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
81
82 let ai =
84 Gemini::new(api_key, "gemini-2.5-flash", None).set_tools(vec![Tool::FunctionDeclarations(
85 vec![
86 add_numbers::gemini_schema(),
87 get_temperature::gemini_schema(),
88 ],
89 )]);
90
91 println!("--- Function Calling Example ---");
92 let prompt = "What is 123.45 plus 678.9, and what's the weather like in London?";
93 println!("User: {}\n", prompt);
94
95 let mut response = ai.ask(session.ask(prompt)).await?;
97
98 loop {
100 if response.get_chat().has_function_call() {
101 println!("Gemini requested function calls...");
102
103 let _ = execute_function_calls!(session, add_numbers);
105
106 for call in response.get_chat().get_function_calls() {
107 if call.name() == "get_temperature" {
108 get_temperature::execute_with_closure(
109 call.args().as_ref().unwrap(),
110 |location| {
111 println!("[Executing Closure] getting temperature for {}", location);
112 session .add_function_response(
114 "get_temperature",
115 format!("temperature of {location} is 38 degree Celsius"),
116 )
117 .unwrap();
118 },
119 )
120 .expect("Gemini responded with wrong argument format")
121 }
122 }
123
124 response = ai.ask(&mut session).await?;
126 } else {
127 println!("\nGemini: {}", response.get_chat().get_text_no_think(""));
129 break;
130 }
131 }
132}