Skip to content

Commit 251ba32

Browse files
committed
fix: update examples
1 parent d66d2bc commit 251ba32

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

examples/custom_tool.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
5656

5757
function_handlers.insert(
5858
"get_current_weather".to_string(),
59-
Box::new(|args: &mut serde_json::Value| {
60-
if let Some(_location) = args.get("location").and_then(|v| v.as_str()) {
61-
// This is a dummy implementation, would normally call an external API, etc.
62-
Ok(serde_json::json!({ "temperature": 15, "condition": "Cloudy" }))
63-
} else {
64-
Err("Missing 'location' argument".to_string())
65-
}
66-
}),
59+
FunctionHandler::Sync(Box::new(|args: &mut serde_json::Value| {
60+
let location = args
61+
.get("location")
62+
.and_then(|v| v.as_str())
63+
.unwrap_or("London, UK");
64+
let weather_info = format!(
65+
"The current weather in {} is sunny with a temperature of 20°C.",
66+
location
67+
);
68+
Ok(serde_json::json!({
69+
"weather": weather_info
70+
}))
71+
})),
6772
);
6873

6974
let response = client

examples/tool_param_types.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use gemini_client_rs::{
4646
ParameterPropertyArray, ParameterPropertyBoolean, ParameterPropertyInteger,
4747
ParameterPropertyString, Role, Tool, ToolConfig, ToolConfigFunctionDeclaration,
4848
},
49-
GeminiClient,
49+
FunctionHandler, GeminiClient,
5050
};
5151

5252
use dotenvy::dotenv;
@@ -333,14 +333,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
333333
std::env::var("GEMINI_MODEL_NAME").unwrap_or_else(|_| "gemini-2.5-flash".to_string());
334334

335335
// Set up function handler
336-
let mut function_handlers: HashMap<
337-
String,
338-
Box<dyn Fn(&mut serde_json::Value) -> Result<serde_json::Value, String> + Send + Sync>,
339-
> = HashMap::new();
336+
let mut function_handlers: HashMap<String, FunctionHandler> = HashMap::new();
340337

341338
function_handlers.insert(
342339
"schedule_meeting".to_string(),
343-
Box::new(|args: &mut serde_json::Value| {
340+
FunctionHandler::Sync(Box::new(|args: &mut serde_json::Value| {
344341
// Deserialize the arguments using the MeetingRequest struct
345342
let meeting_request: MeetingRequest = serde_json::from_value(args.clone())
346343
.map_err(|e| format!("Failed to deserialize meeting request: {}", e))?;
@@ -375,7 +372,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
375372
// Serialize the response back to JSON
376373
serde_json::to_value(response)
377374
.map_err(|e| format!("Failed to serialize meeting response: {}", e))
378-
}),
375+
})),
379376
);
380377

381378
// Make the request

0 commit comments

Comments
 (0)