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

Function main

examples/tool_calling_async.cpp:166–430  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

164}
165
166int main() {
167 std::cout << "AI SDK C++ - Async Tool Calling Example\n";
168 std::cout << "========================================\n\n";
169
170 // Create OpenAI client
171 auto client = ai::openai::create_client();
172
173 // Define async tools
174 ai::ToolSet tools = {
175 {"fetch_news",
176 ai::create_simple_async_tool(
177 "fetch_news", "Fetch latest news articles for a given category",
178 {{"category", "string"}}, fetch_news_async)},
179 {"query_database",
180 ai::create_simple_async_tool(
181 "query_database", "Execute a database query and return results",
182 {{"query", "string"}}, query_database_async)},
183 {"calculate_stats",
184 ai::create_tool(
185 "Calculate statistics (sum, count, average) for an array of numbers",
186 ai::JsonValue{
187 {"type", "object"},
188 {"properties",
189 {{"data",
190 {{"type", "array"},
191 {"items", {{"type", "number"}}},
192 {"description",
193 "Array of numbers to calculate statistics for"}}}}},
194 {"required", {"data"}}},
195 calculate_stats)},
196 {"process_file", ai::create_simple_async_tool(
197 "process_file",
198 "Process a file with the specified operation "
199 "(analyze, compress, backup)",
200 {{"filename", "string"}, {"operation", "string"}},
201 process_file_async)}};
202
203 // Example 1: Single async tool call
204 std::cout << "1. Single Async Tool Example:\n";
205 std::cout << "Question: Get me the latest tech news\n\n";
206
207 auto start_time = std::chrono::high_resolution_clock::now();
208
209 ai::GenerateOptions options1;
210 options1.model = ai::openai::models::kGpt54;
211 options1.prompt = "Get me the latest tech news articles";
212 options1.tools = tools;
213 options1.max_tokens = 200;
214
215 auto result1 = client.generate_text(options1);
216
217 auto end_time = std::chrono::high_resolution_clock::now();
218 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
219 end_time - start_time);
220
221 if (result1) {
222 std::cout << "Assistant: " << result1.text << "\n";
223 std::cout << "Total execution time: " << duration.count() << "ms\n";

Callers

nothing calls this directly

Calls 10

create_simple_async_toolFunction · 0.85
create_toolFunction · 0.85
get_all_tool_callsMethod · 0.80
get_all_tool_resultsMethod · 0.80
create_clientFunction · 0.50
generate_textMethod · 0.45
error_messageMethod · 0.45
has_tool_resultsMethod · 0.45
is_successMethod · 0.45
is_multi_stepMethod · 0.45

Tested by

no test coverage detected