File size: 1,527 Bytes
29f893d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Fetch an OpenRouter model metadata snapshot for the Space.

Run when you want to refresh model_info_snapshot.json:

    python fetch_openrouter_model_info.py

No API key is required for the public /models endpoint at the time of writing.
"""

import json
from pathlib import Path

import requests

MODEL_FAMILIES_PATH = Path("model_families.json")
OUTPUT = Path("model_info_snapshot.json")
OPENROUTER_MODELS_URL = "https://openrouter.ai/api/v1/models"


def load_model_families() -> dict:
    return json.loads(MODEL_FAMILIES_PATH.read_text(encoding="utf-8"))


def main() -> None:
    model_families = load_model_families()
    wanted = {model for models in model_families.values() for model in models}
    response = requests.get(OPENROUTER_MODELS_URL, timeout=30)
    response.raise_for_status()
    data = response.json().get("data", [])
    by_id = {item.get("id"): item for item in data}

    snapshot = {}
    for family, models in model_families.items():
        for model in models:
            item = by_id.get(model, {"id": model, "name": model})
            item = dict(item)
            item["model_family"] = family
            snapshot[model] = item

    missing = sorted(wanted - set(by_id))
    OUTPUT.write_text(json.dumps(snapshot, ensure_ascii=False, indent=2), encoding="utf-8")
    print(f"Wrote {len(snapshot)} models to {OUTPUT}")
    if missing:
        print("Missing from OpenRouter response:")
        for model in missing:
            print(f"- {model}")


if __name__ == "__main__":
    main()