File size: 2,110 Bytes
99e41a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
#!/usr/bin/env python3
"""Check which API keys are configured and working. Never prints key values.

  python scripts/check_keys.py               # ping each configured LLM provider + HF
  python scripts/check_keys.py --list-models # also list live model ids per provider
"""
import argparse
import json
import os
import sys
import urllib.request
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from robotics.llm.providers import PROVIDERS, LLMError, chat, list_models  # noqa: E402


def check_hf() -> str:
    tok = os.environ.get("HF_TOKEN")
    if not tok:
        return "not set"
    req = urllib.request.Request("https://huggingface.co/api/whoami-v2",
                                 headers={"Authorization": f"Bearer {tok}"})
    try:
        with urllib.request.urlopen(req, timeout=20) as r:
            return f"ok (user: {json.loads(r.read()).get('name')})"
    except Exception as e:  # noqa: BLE001
        return f"FAILED ({type(e).__name__})"


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--list-models", action="store_true")
    a = ap.parse_args()
    print(f"{'HF_TOKEN':20} {check_hf()}")
    for name in ("KAGGLE_API_TOKEN", "TINKER_API_KEY", "REACTOR_API_KEY", "WANDB_API_KEY"):
        print(f"{name:20} {'set' if os.environ.get(name) else 'not set'}")
    for p in PROVIDERS.values():
        if not p.api_key:
            print(f"{p.key_env:20} not set")
            continue
        try:
            text, _ = chat([{"role": "user", "content": "Reply with the word OK."}],
                           providers=[p.name], max_tokens=256, retries_per_provider=1)
            status = f"ok ({p.model}: {text.strip()[:20]!r})"
        except LLMError as e:
            status = f"FAILED {e}"
        print(f"{p.key_env:20} {status}")
        if a.list_models:
            try:
                print("    models:", ", ".join(list_models(p)[:40]))
            except Exception as e:  # noqa: BLE001
                print(f"    models: unavailable ({type(e).__name__})")


if __name__ == "__main__":
    main()