stability: shim fallback map + health_loop (base<->fallback), 429 retry, watcher probes the shim; guide 7.5
This commit is contained in:
+59
-21
@@ -3,7 +3,7 @@
|
||||
|
||||
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 the gateway host (cron every 10 min).
|
||||
Runs on 2.27.98.196 (cron every 10 min).
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
@@ -13,9 +13,10 @@ import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
KEYS = "/opt/numes-shim/keys.json"
|
||||
BASE = "https://api.numes.ai/api/v1"
|
||||
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",
|
||||
@@ -33,33 +34,52 @@ def log(msg):
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
def first_key():
|
||||
"""Rotate across the key bank so one account never eats the whole probe load."""
|
||||
def all_keys():
|
||||
try:
|
||||
d = json.load(open(KEYS))
|
||||
if isinstance(d, dict):
|
||||
d = d.get("keys", [])
|
||||
if not d:
|
||||
return None
|
||||
return d[int(time.time() // 600) % len(d)]
|
||||
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):
|
||||
alias, slug = item
|
||||
k = first_key()
|
||||
if not k:
|
||||
return alias, 0, 0.0
|
||||
body = json.dumps({"model": slug,
|
||||
"""Один алиас через шим: 200 = жив (шим сам держит fallback), 503 = реально мёртв."""
|
||||
alias, _slug = item
|
||||
body = json.dumps({"model": alias,
|
||||
"messages": [{"role": "user", "content": "ping"}],
|
||||
"max_tokens": 16})
|
||||
"max_tokens": 32})
|
||||
t0 = time.time()
|
||||
r = subprocess.run(
|
||||
["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", "-m", "90",
|
||||
BASE + "/chat/completions", "-H", "Authorization: Bearer " + k,
|
||||
["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=120)
|
||||
capture_output=True, text=True, timeout=240)
|
||||
try:
|
||||
code = int((r.stdout or "0").strip() or 0)
|
||||
except Exception:
|
||||
@@ -90,14 +110,31 @@ def main():
|
||||
log("probe: " + " ".join("%s=%s/%.0fs" % (a, c, s) for a, c, s in res))
|
||||
|
||||
try:
|
||||
prev = json.load(open(STATE)).get("live", [])
|
||||
st = json.load(open(STATE))
|
||||
prev = st.get("live", [])
|
||||
fails = st.get("fails", {})
|
||||
except Exception:
|
||||
prev = []
|
||||
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)
|
||||
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))))
|
||||
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);"
|
||||
@@ -106,7 +143,8 @@ def main():
|
||||
psql("update channels set status=2 where name='%s';" % CHANNEL)
|
||||
|
||||
changed = sorted(live) != sorted(prev)
|
||||
json.dump({"live": sorted(live), "ts": int(time.time())}, open(STATE, "w"))
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user