function_calling/
function_calling.rs1use gpt5::{Gpt5Client, Gpt5Model, Gpt5RequestBuilder, ReasoningEffort, Tool, VerbosityLevel};
7use serde_json::json;
8
9#[tokio::main]
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11 let api_key =
12 std::env::var("OPENAI_API_KEY").expect("Please set OPENAI_API_KEY environment variable");
13
14 let client = Gpt5Client::new(api_key);
15
16 let calculator_tool = Tool {
18 tool_type: "function".to_string(),
19 name: "calculate".to_string(),
20 description: "Perform basic arithmetic calculations".to_string(),
21 parameters: json!({
22 "type": "object",
23 "properties": {
24 "expression": {
25 "type": "string",
26 "description": "Mathematical expression to evaluate (e.g., '2 + 2', '10 * 5')"
27 }
28 },
29 "required": ["expression"]
30 }),
31 };
32
33 let weather_tool = Tool {
35 tool_type: "function".to_string(),
36 name: "get_weather".to_string(),
37 description: "Get current weather information for a city".to_string(),
38 parameters: json!({
39 "type": "object",
40 "properties": {
41 "city": {
42 "type": "string",
43 "description": "The city name to get weather for"
44 }
45 },
46 "required": ["city"]
47 }),
48 };
49
50 println!("š§® Testing calculator function...");
51
52 let request = Gpt5RequestBuilder::new(Gpt5Model::Gpt5Nano)
54 .input("What is 15 * 8 + 42?")
55 .instructions("Use the calculator tool to solve this math problem")
56 .tools(vec![calculator_tool])
57 .tool_choice("auto")
58 .verbosity(VerbosityLevel::Medium)
59 .reasoning_effort(ReasoningEffort::Low)
60 .max_output_tokens(200)
61 .build();
62
63 let response = client.request(request).await?;
65
66 let function_calls = response.function_calls();
68 if !function_calls.is_empty() {
69 println!("š§ Function calls made: {}", function_calls.len());
70 for call in function_calls {
71 println!(" Function: {}", call.name.as_deref().unwrap_or("unknown"));
72 println!(" Arguments: {}", call.arguments.as_deref().unwrap_or("{}"));
73 }
74 }
75
76 if let Some(text) = response.text() {
78 println!("š¤ Response: {}", text);
79 }
80
81 println!("\nš¤ļø Testing weather function...");
82
83 let weather_request = Gpt5RequestBuilder::new(Gpt5Model::Gpt5Nano)
85 .input("What's the weather like in Tokyo?")
86 .instructions("Use the weather tool to get current conditions")
87 .tools(vec![weather_tool])
88 .tool_choice("auto")
89 .verbosity(VerbosityLevel::Low)
90 .reasoning_effort(ReasoningEffort::Low)
91 .max_output_tokens(150)
92 .build();
93
94 let weather_response = client.request(weather_request).await?;
95
96 let weather_calls = weather_response.function_calls();
97 if !weather_calls.is_empty() {
98 println!("š§ Weather function calls made: {}", weather_calls.len());
99 for call in weather_calls {
100 println!(" Function: {}", call.name.as_deref().unwrap_or("unknown"));
101 println!(" Arguments: {}", call.arguments.as_deref().unwrap_or("{}"));
102 }
103 }
104
105 if let Some(text) = weather_response.text() {
106 println!("š¤ Response: {}", text);
107 }
108
109 Ok(())
110}