pub async fn execute_tool_calls_parallel<F, Exec>(
tool_calls: Vec<ToolCall>,
executor: Exec,
) -> Vec<Result<ToolResult, Box<dyn Error + Send + Sync>>>Expand description
Executes multiple tool calls in parallel.
Takes a list of tool calls and an executor function, runs them
concurrently using tokio::spawn, and returns all results.
§Arguments
tool_calls- List of tool calls to executeexecutor- Async function that executes a single tool call
§Type Parameters
F- Future returned by the executorExec- Executor function type
§Returns
Vector of tool results in the same order as input tool calls.
§Examples
use api_xai::{ ToolCall, ToolResult, execute_tool_calls_parallel };
use serde_json::json;
// Execute all tool calls in parallel
let results = execute_tool_calls_parallel( tool_calls, | call | async move {
// Your tool execution logic here
match call.function.name.as_str() {
"get_weather" => {
let result = json!({ "temperature": 72, "conditions": "sunny" });
Ok( ToolResult::new( call.id, &result ) )
}
_ => {
Err( format!( "Unknown function : {name}", name = call.function.name ).into() )
}
}
} ).await;