| 6 | #include <ai/logger.h> |
| 7 | |
| 8 | int main() { |
| 9 | try { |
| 10 | // Enable info logging |
| 11 | ai::logger::install_logger(std::make_shared<ai::logger::ConsoleLogger>( |
| 12 | ai::logger::LogLevel::kLogLevelInfo)); |
| 13 | |
| 14 | // Get OpenRouter API key from environment variable |
| 15 | const char* api_key_env = std::getenv("OPENROUTER_API_KEY"); |
| 16 | if (!api_key_env) { |
| 17 | std::cerr << "Error: OPENROUTER_API_KEY environment variable not set\n"; |
| 18 | std::cerr << "Please set your OpenRouter API key:\n"; |
| 19 | std::cerr << " export OPENROUTER_API_KEY=your-api-key-here\n"; |
| 20 | return 1; |
| 21 | } |
| 22 | std::string api_key(api_key_env); |
| 23 | |
| 24 | // Create OpenAI client with OpenRouter's base URL |
| 25 | // OpenRouter provides an OpenAI-compatible API endpoint |
| 26 | auto client = |
| 27 | ai::openai::create_client(api_key, "https://openrouter.ai/api"); |
| 28 | |
| 29 | std::cout << "=== OpenRouter Example (OpenAI-compatible) ===\n\n"; |
| 30 | |
| 31 | // Test simple generation with a model available on OpenRouter |
| 32 | std::cout << "Testing text generation with OpenRouter...\n\n"; |
| 33 | |
| 34 | // Using a model that's available on OpenRouter |
| 35 | // Common models: "openai/gpt-5.6-sol", "anthropic/claude-sonnet-5", |
| 36 | // "meta-llama/llama-3.1-8b-instruct" See https://openrouter.ai/models for |
| 37 | // available models |
| 38 | ai::GenerateOptions options( |
| 39 | "anthropic/claude-sonnet-5", "You are a helpful assistant.", |
| 40 | "What are the benefits of using OpenRouter for AI applications? Give a " |
| 41 | "brief answer."); |
| 42 | |
| 43 | auto result = client.generate_text(options); |
| 44 | |
| 45 | if (result) { |
| 46 | std::cout << "Response: " << result.text << "\n"; |
| 47 | std::cout << "Model: " << result.model.value_or("unknown") << "\n"; |
| 48 | std::cout << "Tokens used: " << result.usage.total_tokens << "\n"; |
| 49 | std::cout << "Finish reason: " << result.finishReasonToString() << "\n"; |
| 50 | } else { |
| 51 | std::cout << "Error: " << result.error_message() << "\n"; |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | // Test streaming with OpenRouter |
| 56 | std::cout << "\n\nTesting streaming with OpenRouter...\n"; |
| 57 | |
| 58 | ai::GenerateOptions stream_opts("anthropic/claude-sonnet-5", |
| 59 | "You are a creative writer.", |
| 60 | "Write a haiku about API compatibility."); |
| 61 | ai::StreamOptions stream_options(stream_opts); |
| 62 | |
| 63 | auto stream = client.stream_text(stream_options); |
| 64 | |
| 65 | std::cout << "Haiku:\n"; |
nothing calls this directly
no test coverage detected