1use gemini_rust::{
2 FunctionDeclaration, Gemini, PropertyDetails, FunctionParameters,
3 FunctionCallingMode, GenerationConfig
4};
5use std::env;
6
7#[tokio::main]
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9 let api_key = env::var("GEMINI_API_KEY")
11 .expect("GEMINI_API_KEY environment variable not set");
12
13 let client = Gemini::new(api_key);
15
16 println!("--- Simple generation ---");
18 let response = client
19 .generate_content()
20 .with_system_prompt("You are a helpful assistant.")
21 .with_user_message("Hello, can you tell me a joke about programming?")
22 .with_generation_config(
23 GenerationConfig {
24 temperature: Some(0.7),
25 max_output_tokens: Some(100),
26 ..Default::default()
27 }
28 )
29 .execute()
30 .await?;
31
32 println!("Response: {}", response.text());
33
34 println!("\n--- Function calling example ---");
36
37 let get_weather = FunctionDeclaration::new(
39 "get_weather",
40 "Get the current weather for a location",
41 FunctionParameters::object()
42 .with_property(
43 "location",
44 PropertyDetails::string("The city and state, e.g., San Francisco, CA"),
45 true,
46 )
47 .with_property(
48 "unit",
49 PropertyDetails::enum_type(
50 "The unit of temperature",
51 ["celsius", "fahrenheit"]
52 ),
53 false,
54 ),
55 );
56
57 let response = client
59 .generate_content()
60 .with_system_prompt("You are a helpful weather assistant.")
61 .with_user_message("What's the weather like in San Francisco right now?")
62 .with_function(get_weather)
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,
72 function_call.args
73 );
74
75 let location: String = function_call.get("location")?;
77 let unit = function_call.get::<String>("unit").unwrap_or_else(|_| String::from("celsius"));
78
79 println!("Location: {}, Unit: {}", location, unit);
80
81 let weather_response = format!(
83 "{{\"temperature\": 22, \"unit\": \"{}\", \"condition\": \"sunny\"}}",
84 unit
85 );
86
87 let final_response = client
89 .generate_content()
90 .with_system_prompt("You are a helpful weather assistant.")
91 .with_user_message("What's the weather like in San Francisco right now?")
92 .with_function_response_str("get_weather", weather_response)?
93 .with_generation_config(
94 GenerationConfig {
95 temperature: Some(0.7),
96 max_output_tokens: Some(100),
97 ..Default::default()
98 }
99 )
100 .execute()
101 .await?;
102
103 println!("Final response: {}", final_response.text());
104 } else {
105 println!("No function calls in the response.");
106 }
107
108 Ok(())
109}