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

Function main

examples/retry_config_example.cpp:23–163  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21#include <ai/retry/retry_policy.h>
22
23int main() {
24 std::cout << "AI SDK C++ - Retry Configuration Example\n";
25 std::cout << "========================================\n\n";
26
27 // Example 1: Default retry configuration
28 std::cout << "1. Client with default retry configuration:\n";
29 std::cout << " - Max retries: 2\n";
30 std::cout << " - Initial delay: 2000ms\n";
31 std::cout << " - Backoff factor: 2.0\n\n";
32
33 auto default_client = ai::openai::create_client();
34
35 ai::GenerateOptions options;
36 options.model = ai::openai::models::kGpt54Mini;
37 options.prompt = "Say 'Hello with default retry config!'";
38
39 auto result1 = default_client.generate_text(options);
40 if (result1) {
41 std::cout << "Response: " << result1.text << "\n\n";
42 } else {
43 std::cout << "Error: " << result1.error_message() << "\n\n";
44 }
45
46 // Example 2: Aggressive retry configuration for unreliable networks
47 std::cout << "2. Client with aggressive retry configuration:\n";
48 std::cout << " - Max retries: 5 (for unreliable networks)\n";
49 std::cout << " - Initial delay: 1000ms (faster initial retry)\n";
50 std::cout << " - Backoff factor: 1.5 (gentler backoff)\n\n";
51
52 ai::retry::RetryConfig aggressive_config;
53 aggressive_config.max_retries = 5;
54 aggressive_config.initial_delay = std::chrono::milliseconds(1000);
55 aggressive_config.backoff_factor = 1.5;
56
57 // Get API key from environment
58 const char* api_key = std::getenv("OPENAI_API_KEY");
59 if (!api_key) {
60 std::cerr << "Error: OPENAI_API_KEY environment variable not set\n";
61 return 1;
62 }
63
64 auto aggressive_client = ai::openai::create_client(
65 api_key, "https://api.openai.com", aggressive_config);
66
67 options.prompt = "Say 'Hello with aggressive retry config!'";
68
69 auto result2 = aggressive_client.generate_text(options);
70 if (result2) {
71 std::cout << "Response: " << result2.text << "\n\n";
72 } else {
73 std::cout << "Error: " << result2.error_message() << "\n\n";
74 }
75
76 // Example 3: Conservative retry configuration for rate-limited APIs
77 std::cout << "3. Client with conservative retry configuration:\n";
78 std::cout << " - Max retries: 3\n";
79 std::cout
80 << " - Initial delay: 5000ms (longer wait to avoid rate limits)\n";

Callers

nothing calls this directly

Calls 3

create_clientFunction · 0.50
generate_textMethod · 0.45
error_messageMethod · 0.45

Tested by

no test coverage detected