Query a single model via OpenRouter API. Args: model: OpenRouter model identifier (e.g., "openai/gpt-4o") messages: List of message dicts with 'role' and 'content' timeout: Request timeout in seconds Returns: Response dict with 'content' and optional 'r
(
model: str,
messages: List[Dict[str, str]],
timeout: float = 120.0
)
| 6 | |
| 7 | |
| 8 | async def query_model( |
| 9 | model: str, |
| 10 | messages: List[Dict[str, str]], |
| 11 | timeout: float = 120.0 |
| 12 | ) -> Optional[Dict[str, Any]]: |
| 13 | """ |
| 14 | Query a single model via OpenRouter API. |
| 15 | |
| 16 | Args: |
| 17 | model: OpenRouter model identifier (e.g., "openai/gpt-4o") |
| 18 | messages: List of message dicts with 'role' and 'content' |
| 19 | timeout: Request timeout in seconds |
| 20 | |
| 21 | Returns: |
| 22 | Response dict with 'content' and optional 'reasoning_details', or None if failed |
| 23 | """ |
| 24 | headers = { |
| 25 | "Authorization": f"Bearer {OPENROUTER_API_KEY}", |
| 26 | "Content-Type": "application/json", |
| 27 | } |
| 28 | |
| 29 | payload = { |
| 30 | "model": model, |
| 31 | "messages": messages, |
| 32 | } |
| 33 | |
| 34 | try: |
| 35 | async with httpx.AsyncClient(timeout=timeout) as client: |
| 36 | response = await client.post( |
| 37 | OPENROUTER_API_URL, |
| 38 | headers=headers, |
| 39 | json=payload |
| 40 | ) |
| 41 | response.raise_for_status() |
| 42 | |
| 43 | data = response.json() |
| 44 | message = data['choices'][0]['message'] |
| 45 | |
| 46 | return { |
| 47 | 'content': message.get('content'), |
| 48 | 'reasoning_details': message.get('reasoning_details') |
| 49 | } |
| 50 | |
| 51 | except Exception as e: |
| 52 | print(f"Error querying model {model}: {e}") |
| 53 | return None |
| 54 | |
| 55 | |
| 56 | async def query_models_parallel( |
no outgoing calls
no test coverage detected