| 112 | } |
| 113 | |
| 114 | void demonstrate_streaming_errors() { |
| 115 | std::cout << "4. Streaming Error Handling\n"; |
| 116 | std::cout << "===========================\n\n"; |
| 117 | |
| 118 | std::cout << "Testing streaming with invalid model:\n"; |
| 119 | |
| 120 | auto client = ai::openai::create_client(); |
| 121 | ai::GenerateOptions gen_options; |
| 122 | gen_options.model = "invalid-streaming-model"; |
| 123 | gen_options.prompt = "Tell me a story"; |
| 124 | ai::StreamOptions stream_options(std::move(gen_options)); |
| 125 | auto stream = client.stream_text(stream_options); |
| 126 | |
| 127 | // Check for immediate errors |
| 128 | if (stream.has_error()) { |
| 129 | std::cout << "Stream error detected: " << stream.error_message() << "\n\n"; |
| 130 | } else { |
| 131 | // Process stream and handle errors during iteration |
| 132 | for (const auto& event : stream) { |
| 133 | if (event.is_error()) { |
| 134 | std::cout << "Stream event error: " << event.error.value_or("Unknown") |
| 135 | << "\n"; |
| 136 | break; |
| 137 | } else if (event.is_text_delta()) { |
| 138 | std::cout << event.text_delta; |
| 139 | } else if (event.is_finish()) { |
| 140 | std::cout << "\nStream completed successfully\n"; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | std::cout << "\n"; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void demonstrate_exception_handling() { |
| 149 | std::cout << "5. Exception Handling\n"; |
no test coverage detected