Demonstrates client-side tool/function calling where the AI model suggests tools to execute, and your ESP32 executes them locally.
- Define custom tools/functions
- AI decides when to call tools
- Execute tools on ESP32
- Return results to AI
- Multi-turn tool orchestration
Update WiFi and API key in tools_example.c.
cd examples/tools
idf.py build flash monitor- Defining tools with JSON schemas
- Setting up tool options
- Detecting tool calls in responses
- Executing tools locally
- Sending tool results back to model
// Define tools
xai_tool_t tools[] = {
{
.type = "function",
.function = {
.name = "get_temperature",
.description = "Get ESP32 temperature",
.parameters = "{\"type\":\"object\",\"properties\":{}}"
}
}
};
// Enable tools
xai_options_t options = xai_options_default();
options.tools = tools;
options.tool_count = 1;
options.tool_choice = "auto";
// Check for tool calls
if (response.tool_call_count > 0) {
// Execute each tool
char *result = execute_tool(response.tool_calls[0].function.name);
// Send result back
xai_message_t tool_msg = {
.role = XAI_ROLE_TOOL,
.content = result,
.tool_call_id = response.tool_calls[0].id
};
}Enable tools in menuconfig:
Component config → xAI Configuration → Feature Toggles → Enable tool calling support
- User asks question
- AI determines tools needed
- ESP32 executes tools locally
- Results sent back to AI
- AI synthesizes final answer
web_search- Server-side search toolsconversation- Multi-turn with tools