| 65 | } |
| 66 | |
| 67 | int main() { |
| 68 | std::cout << "AI SDK C++ - Basic Tool Calling Example\n"; |
| 69 | std::cout << "=========================================\n\n"; |
| 70 | |
| 71 | // Create OpenAI client |
| 72 | auto client = ai::openai::create_client(); |
| 73 | |
| 74 | // Define tools using the helper function |
| 75 | auto weather_tool = ai::create_simple_tool( |
| 76 | "weather", "Get current weather information for a location", |
| 77 | {{"location", "string"}}, get_weather); |
| 78 | |
| 79 | auto attractions_tool = ai::create_simple_tool( |
| 80 | "cityAttractions", "Get popular tourist attractions for a city", |
| 81 | {{"city", "string"}}, get_city_attractions); |
| 82 | |
| 83 | // Create a tool set |
| 84 | ai::ToolSet tools = {{"weather", weather_tool}, |
| 85 | {"cityAttractions", attractions_tool}}; |
| 86 | |
| 87 | // Example 1: Single tool call |
| 88 | std::cout << "1. Single Tool Call Example:\n"; |
| 89 | std::cout << "Question: What's the weather like in San Francisco?\n\n"; |
| 90 | |
| 91 | ai::GenerateOptions options1; |
| 92 | options1.model = ai::openai::models::kGpt54; |
| 93 | options1.prompt = "What's the weather like in San Francisco?"; |
| 94 | options1.tools = tools; |
| 95 | options1.max_tokens = 200; |
| 96 | |
| 97 | auto result1 = client.generate_text(options1); |
| 98 | |
| 99 | if (result1) { |
| 100 | std::cout << "Assistant: " << result1.text << "\n"; |
| 101 | |
| 102 | if (result1.has_tool_calls()) { |
| 103 | std::cout << "\nTool calls made:\n"; |
| 104 | for (const auto& call : result1.tool_calls) { |
| 105 | std::cout << " - " << call.tool_name << ": " << call.arguments.dump() |
| 106 | << "\n"; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (result1.has_tool_results()) { |
| 111 | std::cout << "\nTool results:\n"; |
| 112 | for (const auto& result : result1.tool_results) { |
| 113 | std::cout << " - " << result.tool_name << ": " << result.result.dump() |
| 114 | << "\n"; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | std::cout << "Usage: " << result1.usage.total_tokens << " tokens\n\n"; |
| 119 | } else { |
| 120 | std::cout << "Error: " << result1.error_message() << "\n\n"; |
| 121 | } |
| 122 | |
| 123 | // Example 2: Multiple tools in one request |
| 124 | std::cout << "2. Multiple Tools Example:\n"; |
nothing calls this directly
no test coverage detected