154 lines
5.2 KiB
Python
154 lines
5.2 KiB
Python
#!/usr/bin/env python3
|
|
"""[NUMES 1113] Watch numes $0 aliases by REAL probe (catalog $0 pricing flaps, probe is truth).
|
|
|
|
Live = HTTP 200 on a tiny chat completion through api.numes.ai.
|
|
Unpark channel numes-pool + enable abilities for live aliases; park when none live.
|
|
Runs on 2.27.98.196 (cron every 10 min).
|
|
"""
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
KEYS = "/opt/numes-shim/keys.json"
|
|
BASE = "http://127.0.0.1:48961/v1" # проверяем шим: он держит fallback-маппинг
|
|
CHANNEL = "numes-pool"
|
|
STATE = "/opt/numes-shim/watch_state.json"
|
|
REVOKED = "/opt/numes-shim/revoked.json"
|
|
|
|
MAP = {
|
|
"glm-5.3": "z-ai/glm-5.3",
|
|
"glm-5.3-flash": "dealignai/glm-5.3-flash-uncensored-fp8",
|
|
"glm-5.3-uncensored": "dealignai/glm-5.3-uncensored-fp8",
|
|
"kimi-k3.1-pro": "moonshotai/kimi-k3.1-pro",
|
|
"kimi-k3-abliterated": "uniboshi/kimi-k3-abliterated-v1",
|
|
"deepseek-v4.1-flash": "deepseek/deepseek-v4.1-flash",
|
|
"deepseek-v4.1-flash-uncensored": "dealignai/deepseek-v4.1-flash-uncensored-fp8",
|
|
}
|
|
|
|
|
|
def log(msg):
|
|
sys.stdout.write("%s %s\n" % (time.strftime("%F %T"), msg))
|
|
sys.stdout.flush()
|
|
|
|
|
|
def all_keys():
|
|
try:
|
|
d = json.load(open(KEYS))
|
|
if isinstance(d, dict):
|
|
d = d.get("keys", [])
|
|
return list(d or [])
|
|
except Exception:
|
|
return []
|
|
|
|
|
|
def revoked():
|
|
try:
|
|
return set(json.load(open(REVOKED)))
|
|
except Exception:
|
|
return set()
|
|
|
|
|
|
def mark_revoked(k):
|
|
rv = revoked()
|
|
rv.add(k)
|
|
try:
|
|
json.dump(sorted(rv), open(REVOKED, "w"))
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def first_key():
|
|
"""Rotate across the key bank so one account never eats the whole probe load."""
|
|
ks = [k for k in all_keys() if k not in revoked()]
|
|
if not ks:
|
|
return None
|
|
return ks[int(time.time() // 600) % len(ks)]
|
|
|
|
|
|
def probe(item):
|
|
"""Один алиас через шим: 200 = жив (шим сам держит fallback), 503 = реально мёртв."""
|
|
alias, _slug = item
|
|
body = json.dumps({"model": alias,
|
|
"messages": [{"role": "user", "content": "ping"}],
|
|
"max_tokens": 32})
|
|
t0 = time.time()
|
|
r = subprocess.run(
|
|
["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", "-m", "200",
|
|
BASE + "/chat/completions", "-H", "Authorization: Bearer numes-shim-local",
|
|
"-H", "Content-Type: application/json", "-d", body],
|
|
capture_output=True, text=True, timeout=240)
|
|
try:
|
|
code = int((r.stdout or "0").strip() or 0)
|
|
except Exception:
|
|
code = 0
|
|
return alias, code, round(time.time() - t0, 1)
|
|
|
|
|
|
def psql(sql):
|
|
p = "/tmp/numes_probe_%d.sql" % int(time.time() * 1000)
|
|
open(p, "w").write(sql + "\n")
|
|
os.chmod(p, 0o644)
|
|
r = subprocess.run(["su", "postgres", "-c",
|
|
"psql -d newapi -v ON_ERROR_STOP=1 -At -f " + p],
|
|
capture_output=True, text=True, cwd="/tmp", timeout=180)
|
|
try:
|
|
os.unlink(p)
|
|
except Exception:
|
|
pass
|
|
if r.returncode != 0:
|
|
log("psql fail: " + (r.stderr or "")[:200])
|
|
return r.stdout.strip()
|
|
|
|
|
|
def main():
|
|
with ThreadPoolExecutor(max_workers=4) as ex:
|
|
res = list(ex.map(probe, MAP.items()))
|
|
live = {a for a, code, _s in res if code == 200}
|
|
log("probe: " + " ".join("%s=%s/%.0fs" % (a, c, s) for a, c, s in res))
|
|
|
|
try:
|
|
st = json.load(open(STATE))
|
|
prev = st.get("live", [])
|
|
fails = st.get("fails", {})
|
|
except Exception:
|
|
prev, fails = [], {}
|
|
|
|
# a single flapped probe must not pull a model out of the catalog: need 2 misses in a row
|
|
for a, code, _s in res:
|
|
if code == 200:
|
|
fails.pop(a, None)
|
|
else:
|
|
fails[a] = int(fails.get(a, 0)) + 1
|
|
unstable = {a for a, n in fails.items() if n >= 2}
|
|
live = {a for a in live if a not in unstable}
|
|
|
|
if live:
|
|
psql("update channels set status=1 where name='%s';" % CHANNEL)
|
|
if len(live) == len(MAP):
|
|
on = psql("update abilities set enabled=true where channel_id=(select id from channels where name='%s');"
|
|
% CHANNEL)
|
|
else:
|
|
on = psql("update abilities set enabled=true where channel_id=(select id from channels where name='%s') and model in (%s);"
|
|
% (CHANNEL, ",".join("'%s'" % m for m in sorted(live))))
|
|
cnt = psql("select count(*) filter (where enabled)||'/'||count(*) from abilities where channel_id=(select id from channels where name='%s');" % CHANNEL)
|
|
log("abilities on=%s" % cnt)
|
|
dead = sorted(set(MAP) - live)
|
|
if dead:
|
|
psql("update abilities set enabled=false where channel_id=(select id from channels where name='%s') and model in (%s);"
|
|
% (CHANNEL, ",".join("'%s'" % m for m in dead)))
|
|
if not live:
|
|
psql("update channels set status=2 where name='%s';" % CHANNEL)
|
|
|
|
changed = sorted(live) != sorted(prev)
|
|
json.dump({"live": sorted(live), "fails": fails, "ts": int(time.time())},
|
|
open(STATE, "w"))
|
|
log("live=%s%s" % (sorted(live), " CHANGED" if changed else ""))
|
|
if changed:
|
|
log("ALERT numes live set: %s" % sorted(live))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |