Spaces:
Sleeping
Sleeping
Revert - use best keys by success count
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import httpx
|
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
from fastapi.responses import JSONResponse, Response
|
| 5 |
import time
|
| 6 |
-
import random
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
@@ -29,8 +28,10 @@ def root():
|
|
| 29 |
return {"status": "ok", "keys_loaded": len(OLLAMA_KEYS), "healthy": len(healthy),
|
| 30 |
"keys_status": {v["prefix"]: {"s": v["success"], "h": v["healthy"]} for v in key_status.values()}}
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
@app.post("/v1/chat/completions")
|
| 36 |
async def chat(req: Request):
|
|
@@ -41,13 +42,11 @@ async def chat(req: Request):
|
|
| 41 |
body = await req.json()
|
| 42 |
model = body.get("model", "?")
|
| 43 |
|
| 44 |
-
|
| 45 |
-
if not
|
| 46 |
for v in key_status.values(): v["healthy"] = True
|
| 47 |
-
|
| 48 |
|
| 49 |
-
# RANDOMLY pick 2 keys
|
| 50 |
-
selected = random.sample(healthy, min(2, len(healthy)))
|
| 51 |
log(f"REQ: {model} -> {[key_status[k]['prefix'] for k in selected]}")
|
| 52 |
|
| 53 |
for key in selected:
|
|
@@ -75,7 +74,7 @@ def models(req: Request):
|
|
| 75 |
auth_key = req.headers.get("Authorization", "").replace("Bearer ", "")
|
| 76 |
if auth_key != MASTER_API_KEY:
|
| 77 |
return JSONResponse({"error": "Unauthorized"}, status_code=401)
|
| 78 |
-
for key in
|
| 79 |
try:
|
| 80 |
resp = httpx.get(f"{BASE_URL}/v1/models", headers={"Authorization": f"Bearer {key}"}, timeout=10)
|
| 81 |
if resp.status_code == 200:
|
|
|
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
from fastapi.responses import JSONResponse, Response
|
| 5 |
import time
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 28 |
return {"status": "ok", "keys_loaded": len(OLLAMA_KEYS), "healthy": len(healthy),
|
| 29 |
"keys_status": {v["prefix"]: {"s": v["success"], "h": v["healthy"]} for v in key_status.values()}}
|
| 30 |
|
| 31 |
+
def get_best_keys(max_failures=2, limit=2):
|
| 32 |
+
"""Get best healthy keys sorted by fewest failures"""
|
| 33 |
+
healthy = sorted([(k, v) for k, v in key_status.items() if v["healthy"]], key=lambda x: x[1]["success"])
|
| 34 |
+
return [k for k, v in healthy[:limit]]
|
| 35 |
|
| 36 |
@app.post("/v1/chat/completions")
|
| 37 |
async def chat(req: Request):
|
|
|
|
| 42 |
body = await req.json()
|
| 43 |
model = body.get("model", "?")
|
| 44 |
|
| 45 |
+
selected = get_best_keys(max_failures=2, limit=2)
|
| 46 |
+
if not selected:
|
| 47 |
for v in key_status.values(): v["healthy"] = True
|
| 48 |
+
selected = OLLAMA_KEYS[:2]
|
| 49 |
|
|
|
|
|
|
|
| 50 |
log(f"REQ: {model} -> {[key_status[k]['prefix'] for k in selected]}")
|
| 51 |
|
| 52 |
for key in selected:
|
|
|
|
| 74 |
auth_key = req.headers.get("Authorization", "").replace("Bearer ", "")
|
| 75 |
if auth_key != MASTER_API_KEY:
|
| 76 |
return JSONResponse({"error": "Unauthorized"}, status_code=401)
|
| 77 |
+
for key in get_best_keys(limit=2):
|
| 78 |
try:
|
| 79 |
resp = httpx.get(f"{BASE_URL}/v1/models", headers={"Authorization": f"Bearer {key}"}, timeout=10)
|
| 80 |
if resp.status_code == 200:
|