use api_xai::{ Client, XaiEnvironmentImpl, Secret, ChatCompletionRequest, Message, Tool, ClientApiAccessors };
use serde_json::json;
#[ tokio::main ]
async fn main() -> Result< (), Box< dyn core::error::Error > >
{
let secret = Secret::load_with_fallbacks( "XAI_API_KEY" )?;
let env = XaiEnvironmentImpl::new( secret )?;
let client = Client::build( env )?;
println!( "đ XAI Grok API - Function Calling Example\n" );
let get_weather = Tool::function(
"get_current_weather",
"Get the current weather in a given location",
json!({
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["location"]
})
);
let request = ChatCompletionRequest::former()
.model( "grok-2-1212".to_string() )
.messages( vec![
Message::user( "What's the weather like in Tokyo?" )
] )
.tools( vec![ get_weather.clone() ] )
.form();
println!( "đ¤ Sending request with weather tool definition...\n" );
let response = client.chat().create( request ).await?;
let choice = &response.choices[ 0 ];
if let Some( tool_calls ) = &choice.message.tool_calls
{
println!( "đ§ Model requested {} function call(s):\n", tool_calls.len() );
for tool_call in tool_calls
{
println!( " Function : {}", tool_call.function.name );
println!( " Arguments : {}", tool_call.function.arguments );
let args : serde_json::Value = serde_json::from_str( &tool_call.function.arguments )?;
let location = args[ "location" ].as_str().unwrap_or( "unknown" );
println!( " â Simulating weather lookup for : {location}\n" );
let weather_result = json!({
"temperature": 22,
"unit": "celsius",
"condition": "sunny",
"humidity": 65
});
let followup = ChatCompletionRequest::former()
.model( "grok-2-1212".to_string() )
.messages( vec![
Message::user( "What's the weather like in Tokyo?" ),
choice.message.clone(),
Message::tool( &tool_call.id, weather_result.to_string() ),
] )
.tools( vec![ get_weather.clone() ] )
.form();
println!( "đ¤ Sending function result back to model...\n" );
let final_response = client.chat().create( followup ).await?;
if let Some( content ) = &final_response.choices[ 0 ].message.content
{
println!( "đ¤ Final Answer:\n{content}\n" );
}
}
}
else
{
println!( "âšī¸ Model responded directly without calling function:\n" );
if let Some( content ) = &choice.message.content
{
println!( "{content}\n" );
}
}
Ok( () )
}