| 19 | #include <ai/ai.h> |
| 20 | |
| 21 | int main() { |
| 22 | std::cout << "AI SDK C++ - Streaming Chat Example\n"; |
| 23 | std::cout << "====================================\n\n"; |
| 24 | |
| 25 | // Example 1: Basic streaming with iterator |
| 26 | std::cout << "1. Basic streaming (iterator approach):\n"; |
| 27 | std::cout |
| 28 | << "Prompt: Write a short story about a robot learning to paint.\n\n"; |
| 29 | std::cout << "Response: "; |
| 30 | |
| 31 | auto client = ai::openai::create_client(); |
| 32 | ai::GenerateOptions gen_options1; |
| 33 | gen_options1.model = ai::openai::models::kGpt54; |
| 34 | gen_options1.prompt = |
| 35 | "Write a short story about a robot learning " |
| 36 | "to paint. Keep it under 200 words."; |
| 37 | ai::StreamOptions options1(std::move(gen_options1)); |
| 38 | auto stream1 = client.stream_text(options1); |
| 39 | |
| 40 | for (const auto& event : stream1) { |
| 41 | if (event.is_text_delta()) { |
| 42 | std::cout << event.text_delta << std::flush; |
| 43 | } else if (event.is_error()) { |
| 44 | std::cout << "\nError: " << event.error.value_or("Unknown error") << "\n"; |
| 45 | break; |
| 46 | } else if (event.is_finish()) { |
| 47 | std::cout << "\n\n[Stream finished]\n\n"; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Example 2: Streaming with options and callbacks |
| 53 | std::cout << "2. Streaming with callbacks:\n"; |
| 54 | std::cout << "Prompt: Explain quantum computing in simple terms.\n\n"; |
| 55 | |
| 56 | // Set up callbacks |
| 57 | std::string accumulated_text; |
| 58 | int chunk_count = 0; |
| 59 | |
| 60 | auto text_callback = [&](const std::string& chunk) { |
| 61 | std::cout << chunk << std::flush; |
| 62 | accumulated_text += chunk; |
| 63 | chunk_count++; |
| 64 | }; |
| 65 | |
| 66 | auto complete_callback = [&](const ai::GenerateResult& result) { |
| 67 | std::cout << "\n\n[Stream completed]\n"; |
| 68 | std::cout << "Total chunks received: " << chunk_count << "\n"; |
| 69 | std::cout << "Final text length: " << accumulated_text.length() |
| 70 | << " characters\n"; |
| 71 | std::cout << "Token usage: " << result.usage.total_tokens << " tokens\n"; |
| 72 | std::cout << "Finish reason: " << result.finishReasonToString() << "\n\n"; |
| 73 | }; |
| 74 | |
| 75 | auto error_callback = [](const std::string& error) { |
| 76 | std::cout << "\nError in stream: " << error << "\n\n"; |
| 77 | }; |
| 78 |
nothing calls this directly
no test coverage detected