use edgee::{Edgee, Message, InputObject, Tool, FunctionDefinition, JsonSchema};
use std::collections::HashMap;
use serde_json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Edgee::from_env()?;
// Define the weather function
let function = FunctionDefinition {
name: "get_weather".to_string(),
description: Some("Get the current weather for a location".to_string()),
parameters: JsonSchema {
schema_type: "object".to_string(),
properties: Some({
let mut props = HashMap::new();
props.insert("location".to_string(), serde_json::json!({
"type": "string",
"description": "The city name"
}));
props.insert("unit".to_string(), serde_json::json!({
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}));
props
}),
required: Some(vec!["location".to_string()]),
description: None,
},
};
// Step 1: Initial request with tools
let input = InputObject::new(vec![
Message::user("What is the weather in Paris and Tokyo?")
])
.with_tools(vec![Tool::function(function)]);
let response1 = client.send("gpt-4o", input).await?;
// Step 2: Execute all tool calls
let mut messages = vec![
Message::user("What is the weather in Paris and Tokyo?")
];
// Add assistant's message
if let Some(message) = response1.message() {
messages.push(message.clone());
}
if let Some(tool_calls) = response1.tool_calls() {
for tool_call in tool_calls {
let args: serde_json::Value = serde_json::from_str(&tool_call.function.arguments)?;
let result = get_weather(
args["location"].as_str().unwrap(),
args.get("unit").and_then(|v| v.as_str())
);
messages.push(Message::tool(
tool_call.id.clone(),
serde_json::to_string(&result)?
));
}
}
// Step 3: Send results back
let function2 = FunctionDefinition {
name: "get_weather".to_string(),
description: Some("Get the current weather for a location".to_string()),
parameters: JsonSchema {
schema_type: "object".to_string(),
properties: Some({
let mut props = HashMap::new();
props.insert("location".to_string(), serde_json::json!({
"type": "string",
"description": "The city name"
}));
props.insert("unit".to_string(), serde_json::json!({
"type": "string",
"enum": ["celsius", "fahrenheit"]
}));
props
}),
required: Some(vec!["location".to_string()]),
description: None,
},
};
let input2 = InputObject::new(messages)
.with_tools(vec![Tool::function(function2)]);
let response2 = client.send("gpt-4o", input2).await?;
println!("{}", response2.text().unwrap_or(""));
Ok(())
}
fn get_weather(location: &str, unit: Option<&str>) -> serde_json::Value {
serde_json::json!({
"location": location,
"temperature": 15,
"unit": unit.unwrap_or("celsius"),
"condition": "sunny"
})
}