Struct FunctionParameters

Source
pub struct FunctionParameters {
    pub param_type: String,
    pub properties: Option<HashMap<String, PropertyDetails>>,
    pub required: Option<Vec<String>>,
}
Expand description

Parameters for a function

Fields§

§param_type: String

The type of the parameters

§properties: Option<HashMap<String, PropertyDetails>>

The properties of the parameters

§required: Option<Vec<String>>

The required properties

Implementations§

Source§

impl FunctionParameters

Source

pub fn object() -> Self

Create a new object parameter set

Examples found in repository?
examples/advanced.rs (line 19)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let api_key = env::var("GEMINI_API_KEY")?;
11
12    // Create client
13    let client = Gemini::new(api_key);
14
15    // Define a weather function
16    let get_weather = FunctionDeclaration::new(
17        "get_weather",
18        "Get the current weather for a location",
19        FunctionParameters::object()
20            .with_property(
21                "location",
22                PropertyDetails::string("The city and state, e.g., San Francisco, CA"),
23                true,
24            )
25            .with_property(
26                "unit",
27                PropertyDetails::enum_type("The unit of temperature", ["celsius", "fahrenheit"]),
28                false,
29            ),
30    );
31
32    // Create a request with function calling
33    println!("Sending function call request...");
34    let response = client
35        .generate_content()
36        .with_user_message("What's the weather like in Tokyo right now?")
37        .with_function(get_weather)
38        .with_function_calling_mode(FunctionCallingMode::Any)
39        .execute()
40        .await?;
41
42    // Check if there are function calls
43    if let Some(function_call) = response.function_calls().first() {
44        println!(
45            "Function call received: {} with args: {}",
46            function_call.name, function_call.args
47        );
48
49        // Get parameters from the function call
50        let location: String = function_call.get("location")?;
51        let unit = function_call
52            .get::<String>("unit")
53            .unwrap_or_else(|_| String::from("celsius"));
54
55        println!("Location: {}, Unit: {}", location, unit);
56
57        // Simulate function execution (in a real app, this would call a weather API)
58        // Create a JSON response object
59        let weather_response = serde_json::json!({
60            "temperature": 22,
61            "unit": unit,
62            "condition": "sunny",
63            "location": location
64        });
65
66        // Continue the conversation with the function result
67        // We need to replay the entire conversation with the function response
68        println!("Sending function response...");
69
70        // First, need to recreate the original prompt and the model's response
71        let mut final_request = client
72            .generate_content()
73            .with_user_message("What's the weather like in Tokyo right now?");
74
75        // Add the function call from the model's response
76        let mut call_content = Content::default();
77        call_content.parts.push(Part::FunctionCall {
78            function_call: (*function_call).clone(),
79        });
80        final_request.contents.push(call_content);
81
82        // Now add the function response using the JSON value
83        final_request = final_request.with_function_response("get_weather", weather_response);
84
85        // Execute the request
86        let final_response = final_request.execute().await?;
87
88        println!("Final response: {}", final_response.text());
89    } else {
90        println!("No function calls in the response.");
91        println!("Response text: {}", response.text());
92    }
93
94    Ok(())
95}
More examples
Hide additional examples
examples/simple.rs (line 38)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    // Get API key from environment variable
10    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
11
12    // Create client
13    let client = Gemini::new(api_key);
14
15    // Simple generation
16    println!("--- Simple generation ---");
17    let response = client
18        .generate_content()
19        .with_system_prompt("You are a helpful assistant.")
20        .with_user_message("Hello, can you tell me a joke about programming?")
21        .with_generation_config(GenerationConfig {
22            temperature: Some(0.7),
23            max_output_tokens: Some(100),
24            ..Default::default()
25        })
26        .execute()
27        .await?;
28
29    println!("Response: {}", response.text());
30
31    // Function calling example
32    println!("\n--- Function calling example ---");
33
34    // Define a weather function
35    let get_weather = FunctionDeclaration::new(
36        "get_weather",
37        "Get the current weather for a location",
38        FunctionParameters::object()
39            .with_property(
40                "location",
41                PropertyDetails::string("The city and state, e.g., San Francisco, CA"),
42                true,
43            )
44            .with_property(
45                "unit",
46                PropertyDetails::enum_type("The unit of temperature", ["celsius", "fahrenheit"]),
47                false,
48            ),
49    );
50
51    // Create a request with function calling
52    let response = client
53        .generate_content()
54        .with_system_prompt("You are a helpful weather assistant.")
55        .with_user_message("What's the weather like in San Francisco right now?")
56        .with_function(get_weather)
57        .with_function_calling_mode(FunctionCallingMode::Any)
58        .execute()
59        .await?;
60
61    // Check if there are function calls
62    if let Some(function_call) = response.function_calls().first() {
63        println!(
64            "Function call: {} with args: {}",
65            function_call.name, function_call.args
66        );
67
68        // Get parameters from the function call
69        let location: String = function_call.get("location")?;
70        let unit = function_call
71            .get::<String>("unit")
72            .unwrap_or_else(|_| String::from("celsius"));
73
74        println!("Location: {}, Unit: {}", location, unit);
75
76        // Create model content with function call
77        let model_content = Content::function_call((*function_call).clone());
78
79        // Add as model message
80        let model_message = Message {
81            content: model_content,
82            role: Role::Model,
83        };
84
85        // Simulate function execution
86        let weather_response = format!(
87            "{{\"temperature\": 22, \"unit\": \"{}\", \"condition\": \"sunny\"}}",
88            unit
89        );
90
91        // Continue the conversation with the function result
92        let final_response = client
93            .generate_content()
94            .with_system_prompt("You are a helpful weather assistant.")
95            .with_user_message("What's the weather like in San Francisco right now?")
96            .with_message(model_message)
97            .with_function_response_str("get_weather", weather_response)?
98            .with_generation_config(GenerationConfig {
99                temperature: Some(0.7),
100                max_output_tokens: Some(100),
101                ..Default::default()
102            })
103            .execute()
104            .await?;
105
106        println!("Final response: {}", final_response.text());
107    } else {
108        println!("No function calls in the response.");
109    }
110
111    Ok(())
112}
examples/google_search_with_functions.rs (line 22)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    // Get API key from environment variable
11    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
12
13    // Create client
14    let client = Gemini::new(api_key);
15
16    println!("--- Google Search with Function Calling example ---");
17
18    // Define a calculator function
19    let calculate = FunctionDeclaration::new(
20        "calculate",
21        "Perform a calculation",
22        FunctionParameters::object()
23            .with_property(
24                "operation",
25                PropertyDetails::enum_type(
26                    "The mathematical operation to perform",
27                    ["add", "subtract", "multiply", "divide"],
28                ),
29                true,
30            )
31            .with_property("a", PropertyDetails::number("The first number"), true)
32            .with_property("b", PropertyDetails::number("The second number"), true),
33    );
34
35    // Create function tool
36    let function_tool = Tool::new(calculate);
37
38    // Create a request with both tools
39    let response = client
40        .generate_content()
41        .with_user_message("What is the current Google stock price multiplied by 2?")
42        .with_tool(function_tool.clone())
43        .with_function_calling_mode(FunctionCallingMode::Any)
44        .execute()
45        .await?;
46
47    // Check if there are function calls
48    if let Some(function_call) = response.function_calls().first() {
49        println!(
50            "Function call: {} with args: {}",
51            function_call.name, function_call.args
52        );
53
54        // Handle the calculate function
55        if function_call.name == "calculate" {
56            let operation: String = function_call.get("operation")?;
57            let a: f64 = function_call.get("a")?;
58            let b: f64 = function_call.get("b")?;
59
60            println!("Calculation: {} {} {}", a, operation, b);
61
62            let result = match operation.as_str() {
63                "add" => a + b,
64                "subtract" => a - b,
65                "multiply" => a * b,
66                "divide" => a / b,
67                _ => panic!("Unknown operation"),
68            };
69
70            let function_response = json!({
71                "result": result,
72            })
73            .to_string();
74
75            // Based on the curl example, we need to structure the conversation properly:
76            // 1. A user message with the original query
77            // 2. A model message containing the function call
78            // 3. A user message containing the function response
79
80            // Construct conversation following the exact curl pattern
81            let mut conversation = client.generate_content();
82
83            // 1. Add user message with original query
84            conversation = conversation
85                .with_user_message("What is the current Google stock price multiplied by 2?");
86
87            // 2. Create model message with function call
88            let model_function_call = FunctionCall::new(
89                "calculate",
90                json!({
91                    "operation": operation,
92                    "a": a,
93                    "b": b
94                }),
95            );
96
97            // Create model content with function call
98            let model_content = Content::function_call(model_function_call).with_role(Role::Model);
99
100            // Add as model message
101            let model_message = Message {
102                content: model_content,
103                role: Role::Model,
104            };
105            conversation = conversation.with_message(model_message);
106
107            // 3. Add user message with function response
108            conversation =
109                conversation.with_function_response_str("calculate", function_response)?;
110
111            // Execute the request
112            let final_response = conversation.execute().await?;
113
114            println!("Final response: {}", final_response.text());
115        } else {
116            println!("Unknown function call: {}", function_call.name);
117        }
118    } else {
119        println!("No function calls in the response.");
120        println!("Direct response: {}", response.text());
121    }
122
123    Ok(())
124}
examples/thinking_advanced.rs (line 60)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    // Get API key from environment variable
10    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
11
12    // Create client
13    let client = Gemini::with_model(api_key, "models/gemini-2.5-pro".to_string());
14
15    println!("=== Gemini 2.5 Thinking Advanced Example ===\n");
16
17    // Example 1: Streaming with thinking
18    println!("--- Example 1: Streaming with thinking ---");
19    let mut stream = client
20        .generate_content()
21        .with_system_prompt("You are a mathematics expert skilled at solving complex mathematical problems.")
22        .with_user_message("Solve this math problem: Find the sum of the first 50 prime numbers. Please explain your solution process in detail.")
23        .with_thinking_budget(2048)
24        .with_thoughts_included(true)
25        .execute_stream()
26        .await?;
27
28    println!("Streaming response:");
29    let mut thoughts_shown = false;
30    while let Some(chunk_result) = stream.next().await {
31        match chunk_result {
32            Ok(chunk) => {
33                // Check if there's thinking content
34                let thoughts = chunk.thoughts();
35                if !thoughts.is_empty() && !thoughts_shown {
36                    println!("\nThinking process:");
37                    for (i, thought) in thoughts.iter().enumerate() {
38                        println!("Thought {}: {}", i + 1, thought);
39                    }
40                    println!("\nAnswer:");
41                    thoughts_shown = true;
42                }
43
44                // Display general text content
45                print!("{}", chunk.text());
46                std::io::Write::flush(&mut std::io::stdout())?;
47            }
48            Err(e) => eprintln!("Streaming error: {}", e),
49        }
50    }
51    println!("\n");
52
53    // Example 2: Thinking combined with function calls
54    println!("--- Example 2: Thinking combined with function calls ---");
55
56    // Define a calculator function
57    let calculator = FunctionDeclaration::new(
58        "calculate",
59        "Perform basic mathematical calculations",
60        FunctionParameters::object()
61            .with_property(
62                "expression",
63                PropertyDetails::string(
64                    "The mathematical expression to calculate, e.g., '2 + 3 * 4'",
65                ),
66                true,
67            )
68            .with_property(
69                "operation_type",
70                PropertyDetails::enum_type("Type of calculation", ["arithmetic", "advanced"]),
71                false,
72            ),
73    );
74
75    let response = client
76        .generate_content()
77        .with_system_prompt("You are a mathematics assistant. When calculations are needed, use the provided calculator function.")
78        .with_user_message("Calculate the result of (15 + 25) * 3 - 8 and explain the calculation steps.")
79        .with_function(calculator)
80        .with_thinking_budget(1024)
81        .with_thoughts_included(true)
82        .execute()
83        .await?;
84
85    // Display thinking process
86    let thoughts = response.thoughts();
87    if !thoughts.is_empty() {
88        println!("Thinking process:");
89        for (i, thought) in thoughts.iter().enumerate() {
90            println!("Thought {}: {}\n", i + 1, thought);
91        }
92    }
93
94    // Check for function calls
95    let function_calls = response.function_calls();
96    if !function_calls.is_empty() {
97        println!("Function calls:");
98        for (i, call) in function_calls.iter().enumerate() {
99            println!("Call {}: {} Args: {}", i + 1, call.name, call.args);
100        }
101        println!();
102    }
103
104    println!("Answer: {}\n", response.text());
105
106    // Example 3: Complex reasoning task
107    println!("--- Example 3: Complex reasoning task ---");
108    let complex_response = client
109        .generate_content()
110        .with_system_prompt("You are a logical reasoning expert.")
111        .with_user_message(
112            "There are three people: Alice, Bob, and Carol, who live in red, green, and blue houses respectively.\
113            Given:\
114            1. The person in the red house owns a cat\
115            2. Bob does not live in the green house\
116            3. Carol owns a dog\
117            4. The green house is to the left of the red house\
118            5. Alice does not own a cat\
119            Please reason out which color house each person lives in and what pets they own.",
120        )
121        .with_thinking_config(
122            ThinkingConfig::new()
123                .with_thinking_budget(3072)
124                .with_thoughts_included(true),
125        )
126        .execute()
127        .await?;
128
129    // Display thinking process
130    let complex_thoughts = complex_response.thoughts();
131    if !complex_thoughts.is_empty() {
132        println!("Reasoning process:");
133        for (i, thought) in complex_thoughts.iter().enumerate() {
134            println!("Reasoning step {}: {}\n", i + 1, thought);
135        }
136    }
137
138    println!("Conclusion: {}\n", complex_response.text());
139
140    // Display token usage statistics
141    if let Some(usage) = &complex_response.usage_metadata {
142        println!("Token usage statistics:");
143        println!("  Prompt tokens: {}", usage.prompt_token_count);
144        println!(
145            "  Response tokens: {}",
146            usage.candidates_token_count.unwrap_or(0)
147        );
148        if let Some(thinking_tokens) = usage.thoughts_token_count {
149            println!("  Thinking tokens: {}", thinking_tokens);
150        }
151        println!("  Total tokens: {}", usage.total_token_count);
152    }
153
154    Ok(())
155}
examples/tools.rs (line 22)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    // Get API key from environment variable
11    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
12
13    // Create client
14    let client = Gemini::new(api_key);
15
16    println!("--- Tools example with multiple functions ---");
17
18    // Define a weather function
19    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    // Define a calculator function
36    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    // Create a tool with multiple functions
53    let tool = Tool::with_functions(vec![get_weather, calculate]);
54
55    // Create a request with tool functions
56    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    // Process function calls
68    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        // Handle different function calls
75        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                // Based on the curl example, we need to structure the conversation properly:
97                // 1. A user message with the original query
98                // 2. A model message containing the function call
99                // 3. A user message containing the function response
100
101                // Construct conversation following the exact curl pattern
102                let mut conversation = client.generate_content();
103
104                // 1. Add user message with original query and system prompt
105                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                // 2. Create model content with function call
110                let model_content = Content::function_call((*function_call).clone());
111
112                // Add as model message
113                let model_message = Message {
114                    content: model_content,
115                    role: Role::Model,
116                };
117                conversation = conversation.with_message(model_message);
118
119                // 3. Add user message with function response
120                conversation =
121                    conversation.with_function_response_str("calculate", function_response)?;
122
123                // Execute the request
124                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                // Based on the curl example, we need to structure the conversation properly:
144                // 1. A user message with the original query
145                // 2. A model message containing the function call
146                // 3. A user message containing the function response
147
148                // Construct conversation following the exact curl pattern
149                let mut conversation = client.generate_content();
150
151                // 1. Add user message with original query and system prompt
152                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                // 2. Create model content with function call
157                let model_content = Content::function_call((*function_call).clone());
158
159                // Add as model message
160                let model_message = Message {
161                    content: model_content,
162                    role: Role::Model,
163                };
164                conversation = conversation.with_message(model_message);
165
166                // 3. Add user message with function response
167                conversation =
168                    conversation.with_function_response_str("get_weather", weather_response)?;
169
170                // Execute the request
171                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}
Source

pub fn with_property( self, name: impl Into<String>, details: PropertyDetails, required: bool, ) -> Self

Add a property to the parameters

Examples found in repository?
examples/advanced.rs (lines 20-24)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let api_key = env::var("GEMINI_API_KEY")?;
11
12    // Create client
13    let client = Gemini::new(api_key);
14
15    // Define a weather function
16    let get_weather = FunctionDeclaration::new(
17        "get_weather",
18        "Get the current weather for a location",
19        FunctionParameters::object()
20            .with_property(
21                "location",
22                PropertyDetails::string("The city and state, e.g., San Francisco, CA"),
23                true,
24            )
25            .with_property(
26                "unit",
27                PropertyDetails::enum_type("The unit of temperature", ["celsius", "fahrenheit"]),
28                false,
29            ),
30    );
31
32    // Create a request with function calling
33    println!("Sending function call request...");
34    let response = client
35        .generate_content()
36        .with_user_message("What's the weather like in Tokyo right now?")
37        .with_function(get_weather)
38        .with_function_calling_mode(FunctionCallingMode::Any)
39        .execute()
40        .await?;
41
42    // Check if there are function calls
43    if let Some(function_call) = response.function_calls().first() {
44        println!(
45            "Function call received: {} with args: {}",
46            function_call.name, function_call.args
47        );
48
49        // Get parameters from the function call
50        let location: String = function_call.get("location")?;
51        let unit = function_call
52            .get::<String>("unit")
53            .unwrap_or_else(|_| String::from("celsius"));
54
55        println!("Location: {}, Unit: {}", location, unit);
56
57        // Simulate function execution (in a real app, this would call a weather API)
58        // Create a JSON response object
59        let weather_response = serde_json::json!({
60            "temperature": 22,
61            "unit": unit,
62            "condition": "sunny",
63            "location": location
64        });
65
66        // Continue the conversation with the function result
67        // We need to replay the entire conversation with the function response
68        println!("Sending function response...");
69
70        // First, need to recreate the original prompt and the model's response
71        let mut final_request = client
72            .generate_content()
73            .with_user_message("What's the weather like in Tokyo right now?");
74
75        // Add the function call from the model's response
76        let mut call_content = Content::default();
77        call_content.parts.push(Part::FunctionCall {
78            function_call: (*function_call).clone(),
79        });
80        final_request.contents.push(call_content);
81
82        // Now add the function response using the JSON value
83        final_request = final_request.with_function_response("get_weather", weather_response);
84
85        // Execute the request
86        let final_response = final_request.execute().await?;
87
88        println!("Final response: {}", final_response.text());
89    } else {
90        println!("No function calls in the response.");
91        println!("Response text: {}", response.text());
92    }
93
94    Ok(())
95}
More examples
Hide additional examples
examples/simple.rs (lines 39-43)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    // Get API key from environment variable
10    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
11
12    // Create client
13    let client = Gemini::new(api_key);
14
15    // Simple generation
16    println!("--- Simple generation ---");
17    let response = client
18        .generate_content()
19        .with_system_prompt("You are a helpful assistant.")
20        .with_user_message("Hello, can you tell me a joke about programming?")
21        .with_generation_config(GenerationConfig {
22            temperature: Some(0.7),
23            max_output_tokens: Some(100),
24            ..Default::default()
25        })
26        .execute()
27        .await?;
28
29    println!("Response: {}", response.text());
30
31    // Function calling example
32    println!("\n--- Function calling example ---");
33
34    // Define a weather function
35    let get_weather = FunctionDeclaration::new(
36        "get_weather",
37        "Get the current weather for a location",
38        FunctionParameters::object()
39            .with_property(
40                "location",
41                PropertyDetails::string("The city and state, e.g., San Francisco, CA"),
42                true,
43            )
44            .with_property(
45                "unit",
46                PropertyDetails::enum_type("The unit of temperature", ["celsius", "fahrenheit"]),
47                false,
48            ),
49    );
50
51    // Create a request with function calling
52    let response = client
53        .generate_content()
54        .with_system_prompt("You are a helpful weather assistant.")
55        .with_user_message("What's the weather like in San Francisco right now?")
56        .with_function(get_weather)
57        .with_function_calling_mode(FunctionCallingMode::Any)
58        .execute()
59        .await?;
60
61    // Check if there are function calls
62    if let Some(function_call) = response.function_calls().first() {
63        println!(
64            "Function call: {} with args: {}",
65            function_call.name, function_call.args
66        );
67
68        // Get parameters from the function call
69        let location: String = function_call.get("location")?;
70        let unit = function_call
71            .get::<String>("unit")
72            .unwrap_or_else(|_| String::from("celsius"));
73
74        println!("Location: {}, Unit: {}", location, unit);
75
76        // Create model content with function call
77        let model_content = Content::function_call((*function_call).clone());
78
79        // Add as model message
80        let model_message = Message {
81            content: model_content,
82            role: Role::Model,
83        };
84
85        // Simulate function execution
86        let weather_response = format!(
87            "{{\"temperature\": 22, \"unit\": \"{}\", \"condition\": \"sunny\"}}",
88            unit
89        );
90
91        // Continue the conversation with the function result
92        let final_response = client
93            .generate_content()
94            .with_system_prompt("You are a helpful weather assistant.")
95            .with_user_message("What's the weather like in San Francisco right now?")
96            .with_message(model_message)
97            .with_function_response_str("get_weather", weather_response)?
98            .with_generation_config(GenerationConfig {
99                temperature: Some(0.7),
100                max_output_tokens: Some(100),
101                ..Default::default()
102            })
103            .execute()
104            .await?;
105
106        println!("Final response: {}", final_response.text());
107    } else {
108        println!("No function calls in the response.");
109    }
110
111    Ok(())
112}
examples/google_search_with_functions.rs (lines 23-30)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    // Get API key from environment variable
11    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
12
13    // Create client
14    let client = Gemini::new(api_key);
15
16    println!("--- Google Search with Function Calling example ---");
17
18    // Define a calculator function
19    let calculate = FunctionDeclaration::new(
20        "calculate",
21        "Perform a calculation",
22        FunctionParameters::object()
23            .with_property(
24                "operation",
25                PropertyDetails::enum_type(
26                    "The mathematical operation to perform",
27                    ["add", "subtract", "multiply", "divide"],
28                ),
29                true,
30            )
31            .with_property("a", PropertyDetails::number("The first number"), true)
32            .with_property("b", PropertyDetails::number("The second number"), true),
33    );
34
35    // Create function tool
36    let function_tool = Tool::new(calculate);
37
38    // Create a request with both tools
39    let response = client
40        .generate_content()
41        .with_user_message("What is the current Google stock price multiplied by 2?")
42        .with_tool(function_tool.clone())
43        .with_function_calling_mode(FunctionCallingMode::Any)
44        .execute()
45        .await?;
46
47    // Check if there are function calls
48    if let Some(function_call) = response.function_calls().first() {
49        println!(
50            "Function call: {} with args: {}",
51            function_call.name, function_call.args
52        );
53
54        // Handle the calculate function
55        if function_call.name == "calculate" {
56            let operation: String = function_call.get("operation")?;
57            let a: f64 = function_call.get("a")?;
58            let b: f64 = function_call.get("b")?;
59
60            println!("Calculation: {} {} {}", a, operation, b);
61
62            let result = match operation.as_str() {
63                "add" => a + b,
64                "subtract" => a - b,
65                "multiply" => a * b,
66                "divide" => a / b,
67                _ => panic!("Unknown operation"),
68            };
69
70            let function_response = json!({
71                "result": result,
72            })
73            .to_string();
74
75            // Based on the curl example, we need to structure the conversation properly:
76            // 1. A user message with the original query
77            // 2. A model message containing the function call
78            // 3. A user message containing the function response
79
80            // Construct conversation following the exact curl pattern
81            let mut conversation = client.generate_content();
82
83            // 1. Add user message with original query
84            conversation = conversation
85                .with_user_message("What is the current Google stock price multiplied by 2?");
86
87            // 2. Create model message with function call
88            let model_function_call = FunctionCall::new(
89                "calculate",
90                json!({
91                    "operation": operation,
92                    "a": a,
93                    "b": b
94                }),
95            );
96
97            // Create model content with function call
98            let model_content = Content::function_call(model_function_call).with_role(Role::Model);
99
100            // Add as model message
101            let model_message = Message {
102                content: model_content,
103                role: Role::Model,
104            };
105            conversation = conversation.with_message(model_message);
106
107            // 3. Add user message with function response
108            conversation =
109                conversation.with_function_response_str("calculate", function_response)?;
110
111            // Execute the request
112            let final_response = conversation.execute().await?;
113
114            println!("Final response: {}", final_response.text());
115        } else {
116            println!("Unknown function call: {}", function_call.name);
117        }
118    } else {
119        println!("No function calls in the response.");
120        println!("Direct response: {}", response.text());
121    }
122
123    Ok(())
124}
examples/thinking_advanced.rs (lines 61-67)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    // Get API key from environment variable
10    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
11
12    // Create client
13    let client = Gemini::with_model(api_key, "models/gemini-2.5-pro".to_string());
14
15    println!("=== Gemini 2.5 Thinking Advanced Example ===\n");
16
17    // Example 1: Streaming with thinking
18    println!("--- Example 1: Streaming with thinking ---");
19    let mut stream = client
20        .generate_content()
21        .with_system_prompt("You are a mathematics expert skilled at solving complex mathematical problems.")
22        .with_user_message("Solve this math problem: Find the sum of the first 50 prime numbers. Please explain your solution process in detail.")
23        .with_thinking_budget(2048)
24        .with_thoughts_included(true)
25        .execute_stream()
26        .await?;
27
28    println!("Streaming response:");
29    let mut thoughts_shown = false;
30    while let Some(chunk_result) = stream.next().await {
31        match chunk_result {
32            Ok(chunk) => {
33                // Check if there's thinking content
34                let thoughts = chunk.thoughts();
35                if !thoughts.is_empty() && !thoughts_shown {
36                    println!("\nThinking process:");
37                    for (i, thought) in thoughts.iter().enumerate() {
38                        println!("Thought {}: {}", i + 1, thought);
39                    }
40                    println!("\nAnswer:");
41                    thoughts_shown = true;
42                }
43
44                // Display general text content
45                print!("{}", chunk.text());
46                std::io::Write::flush(&mut std::io::stdout())?;
47            }
48            Err(e) => eprintln!("Streaming error: {}", e),
49        }
50    }
51    println!("\n");
52
53    // Example 2: Thinking combined with function calls
54    println!("--- Example 2: Thinking combined with function calls ---");
55
56    // Define a calculator function
57    let calculator = FunctionDeclaration::new(
58        "calculate",
59        "Perform basic mathematical calculations",
60        FunctionParameters::object()
61            .with_property(
62                "expression",
63                PropertyDetails::string(
64                    "The mathematical expression to calculate, e.g., '2 + 3 * 4'",
65                ),
66                true,
67            )
68            .with_property(
69                "operation_type",
70                PropertyDetails::enum_type("Type of calculation", ["arithmetic", "advanced"]),
71                false,
72            ),
73    );
74
75    let response = client
76        .generate_content()
77        .with_system_prompt("You are a mathematics assistant. When calculations are needed, use the provided calculator function.")
78        .with_user_message("Calculate the result of (15 + 25) * 3 - 8 and explain the calculation steps.")
79        .with_function(calculator)
80        .with_thinking_budget(1024)
81        .with_thoughts_included(true)
82        .execute()
83        .await?;
84
85    // Display thinking process
86    let thoughts = response.thoughts();
87    if !thoughts.is_empty() {
88        println!("Thinking process:");
89        for (i, thought) in thoughts.iter().enumerate() {
90            println!("Thought {}: {}\n", i + 1, thought);
91        }
92    }
93
94    // Check for function calls
95    let function_calls = response.function_calls();
96    if !function_calls.is_empty() {
97        println!("Function calls:");
98        for (i, call) in function_calls.iter().enumerate() {
99            println!("Call {}: {} Args: {}", i + 1, call.name, call.args);
100        }
101        println!();
102    }
103
104    println!("Answer: {}\n", response.text());
105
106    // Example 3: Complex reasoning task
107    println!("--- Example 3: Complex reasoning task ---");
108    let complex_response = client
109        .generate_content()
110        .with_system_prompt("You are a logical reasoning expert.")
111        .with_user_message(
112            "There are three people: Alice, Bob, and Carol, who live in red, green, and blue houses respectively.\
113            Given:\
114            1. The person in the red house owns a cat\
115            2. Bob does not live in the green house\
116            3. Carol owns a dog\
117            4. The green house is to the left of the red house\
118            5. Alice does not own a cat\
119            Please reason out which color house each person lives in and what pets they own.",
120        )
121        .with_thinking_config(
122            ThinkingConfig::new()
123                .with_thinking_budget(3072)
124                .with_thoughts_included(true),
125        )
126        .execute()
127        .await?;
128
129    // Display thinking process
130    let complex_thoughts = complex_response.thoughts();
131    if !complex_thoughts.is_empty() {
132        println!("Reasoning process:");
133        for (i, thought) in complex_thoughts.iter().enumerate() {
134            println!("Reasoning step {}: {}\n", i + 1, thought);
135        }
136    }
137
138    println!("Conclusion: {}\n", complex_response.text());
139
140    // Display token usage statistics
141    if let Some(usage) = &complex_response.usage_metadata {
142        println!("Token usage statistics:");
143        println!("  Prompt tokens: {}", usage.prompt_token_count);
144        println!(
145            "  Response tokens: {}",
146            usage.candidates_token_count.unwrap_or(0)
147        );
148        if let Some(thinking_tokens) = usage.thoughts_token_count {
149            println!("  Thinking tokens: {}", thinking_tokens);
150        }
151        println!("  Total tokens: {}", usage.total_token_count);
152    }
153
154    Ok(())
155}
examples/tools.rs (lines 23-27)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    // Get API key from environment variable
11    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
12
13    // Create client
14    let client = Gemini::new(api_key);
15
16    println!("--- Tools example with multiple functions ---");
17
18    // Define a weather function
19    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    // Define a calculator function
36    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    // Create a tool with multiple functions
53    let tool = Tool::with_functions(vec![get_weather, calculate]);
54
55    // Create a request with tool functions
56    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    // Process function calls
68    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        // Handle different function calls
75        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                // Based on the curl example, we need to structure the conversation properly:
97                // 1. A user message with the original query
98                // 2. A model message containing the function call
99                // 3. A user message containing the function response
100
101                // Construct conversation following the exact curl pattern
102                let mut conversation = client.generate_content();
103
104                // 1. Add user message with original query and system prompt
105                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                // 2. Create model content with function call
110                let model_content = Content::function_call((*function_call).clone());
111
112                // Add as model message
113                let model_message = Message {
114                    content: model_content,
115                    role: Role::Model,
116                };
117                conversation = conversation.with_message(model_message);
118
119                // 3. Add user message with function response
120                conversation =
121                    conversation.with_function_response_str("calculate", function_response)?;
122
123                // Execute the request
124                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                // Based on the curl example, we need to structure the conversation properly:
144                // 1. A user message with the original query
145                // 2. A model message containing the function call
146                // 3. A user message containing the function response
147
148                // Construct conversation following the exact curl pattern
149                let mut conversation = client.generate_content();
150
151                // 1. Add user message with original query and system prompt
152                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                // 2. Create model content with function call
157                let model_content = Content::function_call((*function_call).clone());
158
159                // Add as model message
160                let model_message = Message {
161                    content: model_content,
162                    role: Role::Model,
163                };
164                conversation = conversation.with_message(model_message);
165
166                // 3. Add user message with function response
167                conversation =
168                    conversation.with_function_response_str("get_weather", weather_response)?;
169
170                // Execute the request
171                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}

Trait Implementations§

Source§

impl Clone for FunctionParameters

Source§

fn clone(&self) -> FunctionParameters

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FunctionParameters

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for FunctionParameters

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for FunctionParameters

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,