Wilker commited on
Commit
43b01ba
·
1 Parent(s): 5d621c4

chore: script para listar modelos que respondem para chat

Browse files
Files changed (1) hide show
  1. scripts/list_chat_models.py +111 -0
scripts/list_chat_models.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Lista todos os modelos disponíveis que efetivamente respondem para chat."""
3
+
4
+ import time
5
+ import json
6
+ from pathlib import Path
7
+
8
+ import httpx
9
+
10
+ from thc_cli.core.config import load_config
11
+ from thc_cli.core.providers import get_provider
12
+
13
+ PROVIDERS_TO_TEST = ["mistral", "gemini", "openrouter", "huggingface", "github_models"]
14
+
15
+
16
+ def _get_hf_key():
17
+ painel_path = Path.home() / '.painel_keys'
18
+ painel_data = json.loads(painel_path.read_text(encoding='utf-8'))
19
+ return next((v.get('chave') if isinstance(v, dict) else v for k, v in painel_data.items() if 'hf' in k.lower() or 'hugging' in k.lower()), None)
20
+
21
+
22
+ def _format_error(e: Exception) -> str:
23
+ text = str(e)
24
+ if "Rate limited" in text or "429" in text:
25
+ return "rate_limited"
26
+ if "413" in text:
27
+ return "413_payload_too_large"
28
+ if "403" in text:
29
+ return "403_forbidden"
30
+ if "402" in text:
31
+ return "402_payment_required"
32
+ if "Name or service not known" in text or "ConnectError" in text:
33
+ return "network_error"
34
+ return text[:80]
35
+
36
+
37
+ def main():
38
+ cfg = load_config()
39
+ results = []
40
+
41
+ for provider_name in PROVIDERS_TO_TEST:
42
+ try:
43
+ provider = get_provider(provider_name, cfg)
44
+ except Exception as e:
45
+ print(f"[{provider_name}] erro ao obter provider: {e}")
46
+ continue
47
+
48
+ try:
49
+ models = provider.list_models()
50
+ except Exception as e:
51
+ print(f"[{provider_name}] erro ao listar modelos: {e}")
52
+ continue
53
+
54
+ print(f"\n[{provider_name}] testando {len(models)} modelos...")
55
+ for m in models:
56
+ model_key = m.get("key", "")
57
+ start = time.time()
58
+ try:
59
+ if provider_name == "huggingface":
60
+ url = "https://router.huggingface.co/v1/chat/completions"
61
+ headers = {
62
+ "Authorization": f"Bearer {provider.api_key}",
63
+ "Content-Type": "application/json",
64
+ }
65
+ payload = {
66
+ "model": model_key,
67
+ "messages": [{"role": "user", "content": "diga oi em uma palavra"}],
68
+ "max_tokens": 20,
69
+ }
70
+ with httpx.Client(timeout=30) as client:
71
+ r = client.post(url, headers=headers, json=payload)
72
+ data = r.json()
73
+ text = data.get("choices", [{}])[0].get("message", {}).get("content", "")
74
+ elapsed = time.time() - start
75
+ if r.status_code == 200 and text:
76
+ results.append((provider_name, model_key, "ok", elapsed, text[:60]))
77
+ print(f" ok {model_key} ({elapsed:.1f}s) -> {text[:40]}")
78
+ else:
79
+ results.append((provider_name, model_key, f"http_{r.status_code}", elapsed, ""))
80
+ print(f" http_{r.status_code} {model_key} ({elapsed:.1f}s)")
81
+ else:
82
+ reply = provider.chat_completion(
83
+ messages=[{"role": "user", "content": "diga oi em uma palavra"}],
84
+ model=model_key,
85
+ max_tokens=20,
86
+ )
87
+ text = reply.get("choices", [{}])[0].get("message", {}).get("content", "")
88
+ elapsed = time.time() - start
89
+ if text:
90
+ results.append((provider_name, model_key, "ok", elapsed, text[:60]))
91
+ print(f" ok {model_key} ({elapsed:.1f}s) -> {text[:40]}")
92
+ else:
93
+ results.append((provider_name, model_key, "sem_texto", elapsed, ""))
94
+ print(f" sem_texto {model_key} ({elapsed:.1f}s)")
95
+ except Exception as e:
96
+ elapsed = time.time() - start
97
+ err = _format_error(e)
98
+ results.append((provider_name, model_key, err, elapsed, ""))
99
+ print(f" {err} {model_key} ({elapsed:.1f}s)")
100
+
101
+ print("\n=== MODELOS QUE RESPONDEM PARA CHAT ===")
102
+ ok = [r for r in results if r[2] == "ok"]
103
+ for provider_name, model_key, status, elapsed, text in ok:
104
+ print(f"{provider_name} | {model_key} | {elapsed:.1f}s | {text}")
105
+
106
+ print(f"\nTotal respondendo: {len(ok)}/{len(results)}")
107
+ return ok
108
+
109
+
110
+ if __name__ == "__main__":
111
+ main()