Asynchronous handler for OpenRouter API requests using httpx, with mapping for user-friendly model names.
| 12 | logger = logging.getLogger("openrouter_handler") |
| 13 | |
| 14 | class OpenRouterAPIHandler(BaseAPIHandler): |
| 15 | """ |
| 16 | Asynchronous handler for OpenRouter API requests using httpx, |
| 17 | with mapping for user-friendly model names. |
| 18 | """ |
| 19 | OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions" |
| 20 | |
| 21 | def __init__(self): |
| 22 | super().__init__("openrouter") |
| 23 | |
| 24 | # {"name": "google/gemini-2.0-flash-exp:free", "parameters": "unknown"}, |
| 25 | # {"name": "google/gemma-3-27b-it:free", "parameters": "27B"}, |
| 26 | # {"name": "deepseek/deepseek-r1-zero:free", "parameters": "671B"}, |
| 27 | # {"name": "deepseek/deepseek-v3-base:free", "parameters": "671B"}, # Example model ID |
| 28 | |
| 29 | # --- Model Mapping --- |
| 30 | # Dictionary mapping display names to actual model IDs and parameter sizes |
| 31 | self.model_map = { |
| 32 | # --- Add your desired mappings here --- |
| 33 | # "gemma-3-27b-or": { |
| 34 | # "model_id": "google/gemma-3-27b-it:free", |
| 35 | # "parameters": "27B", |
| 36 | # "multimodal": True |
| 37 | # }, |
| 38 | # Default model for everyone (free + pro). Paid OpenRouter route (no |
| 39 | # ":free" suffix), so it is not rate-limited like the free variant. |
| 40 | # Prefer cheap+fast providers in order, but DON'T hard-pin: OpenRouter's |
| 41 | # DeepInfra pool is shared across all their customers and gets rate-limited |
| 42 | # on its own under load (confirmed live: 429 "engine_overloaded" 3/3 tries |
| 43 | # while Cloudflare/Novita served fine). allow_fallbacks left True so a bad |
| 44 | # provider degrades to the next one instead of hard-failing the request. |
| 45 | "gemma-4-26b-a4b-it": { |
| 46 | "model_id": "google/gemma-4-26b-a4b-it", |
| 47 | "parameters": "26BA4", |
| 48 | "multimodal": True, |
| 49 | "pro": False, |
| 50 | "provider_routing": {"order": ["deepinfra", "cloudflare", "novita"], "allow_fallbacks": True}, |
| 51 | }, |
| 52 | "gemma-3-4b-it": { |
| 53 | "model_id": "google/gemma-3-4b-it", |
| 54 | "parameters": "4B", |
| 55 | "multimodal": True, |
| 56 | "pro": False, |
| 57 | }, |
| 58 | "gemma-3-12b-it": { |
| 59 | "model_id": "google/gemma-3-12b-it", |
| 60 | "parameters": "12B", |
| 61 | "multimodal": True, |
| 62 | "pro": False, |
| 63 | }, |
| 64 | "qwen3.7-flash": { |
| 65 | "model_id": "qwen/qwen3.7-flash", |
| 66 | "parameters": "N/A", |
| 67 | "multimodal": True, |
| 68 | "pro": True, |
| 69 | }, |
| 70 | "qwen3-vl-8b": { |
| 71 | "model_id": "qwen/qwen3-vl-8b-instruct", |