use rai_sdk::{ClientBuilder, Model, Result, Tool, ToolContext, schemars::JsonSchema};
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
struct WeatherArgs {
city: String,
#[serde(default = "default_unit")]
unit: String,
}
fn default_unit() -> String {
"celsius".to_string()
}
async fn get_weather(args: WeatherArgs, _ctx: ToolContext) -> Result<serde_json::Value> {
println!(
" [Tool Execution] Fetching weather for {} in {}...",
args.city, args.unit
);
let temp = if args.city.to_lowercase() == "paris" {
22
} else {
18
};
Ok(json!({
"city": args.city,
"temperature": temp,
"unit": args.unit,
"condition": "Sunny",
}))
}
#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let weather_tool = Tool::new("get_current_weather")
.description("Get the current weather in a given city.")
.handler(get_weather)?;
let client = ClientBuilder::new()
.model(Model::gpt4o_mini())
.tools(vec![weather_tool])
.build()?;
let request = client
.request()
.prompt("What is the weather like in Paris right now?");
println!("Sending request to OpenAI (expecting tool use)...");
let response = request.generate().await?;
println!("\nFinal Response:\n{}", response.text());
Ok(())
}