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

Function demonstrate_recovery_patterns

examples/error_handling.cpp:175–269  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

173}
174
175void demonstrate_recovery_patterns() {
176 std::cout << "6. Error Recovery Patterns\n";
177 std::cout << "==========================\n\n";
178
179 // Pattern 1: Fallback models
180 std::cout << "Pattern 1 - Fallback models:\n";
181
182 std::vector<std::string> fallback_models = {
183 "primary-model-v3", // This will fail
184 ai::openai::models::kGpt54, // This should work (if API key is available)
185 ai::openai::models::kGpt54Mini // Faster fallback
186 };
187
188 std::string prompt = "What is machine learning?";
189 ai::GenerateResult final_result;
190
191 for (const auto& model : fallback_models) {
192 std::cout << "Trying model: " << model << "... ";
193 auto client = ai::openai::create_client();
194 ai::GenerateOptions options;
195 options.model = model;
196 options.prompt = prompt;
197 auto result = client.generate_text(options);
198
199 if (result) {
200 std::cout << "Success!\n";
201 final_result = std::move(result);
202 break;
203 } else {
204 std::cout << "Failed (" << result.error_message() << ")\n";
205 }
206 }
207
208 if (final_result) {
209 std::cout << "Final result: " << final_result.text.substr(0, 100)
210 << "...\n\n";
211 } else {
212 std::cout << "All fallback models failed\n\n";
213 }
214
215 // Pattern 2: Retry with exponential backoff
216 std::cout << "Pattern 2 - Retry with backoff:\n";
217
218 int max_retries = 3;
219 int retry_count = 0;
220 int base_delay_ms = 1000;
221
222 while (retry_count < max_retries) {
223 std::cout << "Attempt " << (retry_count + 1) << "/" << max_retries
224 << "... ";
225
226 auto client = ai::openai::create_client();
227 ai::GenerateOptions options;
228 options.model = "unstable-model";
229 options.prompt = prompt;
230 auto result = client.generate_text(options);
231
232 if (result) {

Callers 1

mainFunction · 0.85

Calls 3

create_clientFunction · 0.50
generate_textMethod · 0.45
error_messageMethod · 0.45

Tested by

no test coverage detected