|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | + |
| 4 | +import httpx |
| 5 | + |
| 6 | + |
| 7 | +async def fetch_models(base_url: str, api_key: str | None = None) -> dict: |
| 8 | + url = f"{base_url.rstrip('/')}/models" |
| 9 | + headers = {"Authorization": f"Bearer {api_key}"} if api_key else None |
| 10 | + async with httpx.AsyncClient(timeout=30.0) as client: |
| 11 | + response = await client.get(url, headers=headers) |
| 12 | + response.raise_for_status() |
| 13 | + return response.json() |
| 14 | + |
| 15 | + |
| 16 | +def parse_model_ids(response: dict) -> list[str]: |
| 17 | + return [model.get("id") for model in response.get("data", []) if "id" in model] |
| 18 | + |
| 19 | + |
| 20 | +if __name__ == "__main__": |
| 21 | + api_key = os.environ["API_KEY"] |
| 22 | + base_url = os.environ.get("API_BASE_URL", "https://api.openai.com/v1") |
| 23 | + |
| 24 | + async def main() -> None: |
| 25 | + or_models_response, provider_models_response = await asyncio.gather( |
| 26 | + fetch_models("https://openrouter.ai/api/v1"), |
| 27 | + fetch_models(base_url, api_key), |
| 28 | + ) |
| 29 | + provider_model_ids = parse_model_ids(provider_models_response) |
| 30 | + or_models = or_models_response.get("data", []) |
| 31 | + |
| 32 | + found_models = [] |
| 33 | + not_found_models = [] |
| 34 | + |
| 35 | + for model_id in provider_model_ids: |
| 36 | + model = next( |
| 37 | + ( |
| 38 | + model |
| 39 | + for model in or_models |
| 40 | + if (model.get("id") == model_id) |
| 41 | + or (model.get("id").split("/")[-1] == model_id) |
| 42 | + or (model.get("canonical_slug") == model_id) |
| 43 | + or (model.get("canonical_slug").split("/")[-1] == model_id) |
| 44 | + ), |
| 45 | + None, |
| 46 | + ) |
| 47 | + if model: |
| 48 | + found_models.append(model) |
| 49 | + else: |
| 50 | + not_found_models.append(model_id) |
| 51 | + print("\nFound models:") |
| 52 | + for model in found_models: |
| 53 | + print(model.get("id").split("/")[-1], model.get("pricing").get("prompt")) |
| 54 | + print("\nNot found models:") |
| 55 | + for model in not_found_models: |
| 56 | + print(model) |
| 57 | + |
| 58 | + asyncio.run(main()) |
0 commit comments