MCPcopy Create free account
hub / github.com/ClickHouse/ai-sdk-cpp / main

Function main

examples/tool_calling_basic.cpp:67–208  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

65}
66
67int 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";

Callers

nothing calls this directly

Calls 6

create_simple_toolFunction · 0.85
create_clientFunction · 0.50
generate_textMethod · 0.45
has_tool_callsMethod · 0.45
has_tool_resultsMethod · 0.45
error_messageMethod · 0.45

Tested by

no test coverage detected