use gpt5::{Gpt5Client, Gpt5Model, Gpt5RequestBuilder, ReasoningEffort, Tool, VerbosityLevel};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key =
std::env::var("OPENAI_API_KEY").expect("Please set OPENAI_API_KEY environment variable");
let client = Gpt5Client::new(api_key);
let calculator_tool = Tool {
tool_type: "function".to_string(),
name: Some("calculate".to_string()),
description: Some("Perform basic arithmetic calculations".to_string()),
parameters: Some(json!({
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "Mathematical expression to evaluate (e.g., '2 + 2', '10 * 5')"
}
},
"required": ["expression"]
})),
};
let weather_tool = Tool {
tool_type: "function".to_string(),
name: Some("get_weather".to_string()),
description: Some("Get current weather information for a city".to_string()),
parameters: Some(json!({
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city name to get weather for"
}
},
"required": ["city"]
})),
};
println!("🧮 Testing calculator function...");
let request = Gpt5RequestBuilder::new(Gpt5Model::Gpt5Nano)
.input("What is 15 * 8 + 42?")
.instructions("Use the calculator tool to solve this math problem")
.tools(vec![calculator_tool])
.tool_choice("auto")
.verbosity(VerbosityLevel::Medium)
.reasoning_effort(ReasoningEffort::Low)
.max_output_tokens(200)
.build();
let response = client.request(request).await?;
let function_calls = response.function_calls();
if !function_calls.is_empty() {
println!("🔧 Function calls made: {}", function_calls.len());
for call in function_calls {
println!(" Function: {}", call.name.as_deref().unwrap_or("unknown"));
println!(" Arguments: {}", call.arguments.as_deref().unwrap_or("{}"));
}
}
if let Some(text) = response.text() {
println!("🤖 Response: {}", text);
}
println!("\n🌤️ Testing weather function...");
let weather_request = Gpt5RequestBuilder::new(Gpt5Model::Gpt5Nano)
.input("What's the weather like in Tokyo?")
.instructions("Use the weather tool to get current conditions")
.tools(vec![weather_tool])
.tool_choice("auto")
.verbosity(VerbosityLevel::Low)
.reasoning_effort(ReasoningEffort::Low)
.max_output_tokens(150)
.build();
let weather_response = client.request(weather_request).await?;
let weather_calls = weather_response.function_calls();
if !weather_calls.is_empty() {
println!("🔧 Weather function calls made: {}", weather_calls.len());
for call in weather_calls {
println!(" Function: {}", call.name.as_deref().unwrap_or("unknown"));
println!(" Arguments: {}", call.arguments.as_deref().unwrap_or("{}"));
}
}
if let Some(text) = weather_response.text() {
println!("🤖 Response: {}", text);
}
Ok(())
}