LifeVta commited on
Commit
9931d55
·
verified ·
1 Parent(s): 0f3cf4e

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +57 -0
app/main.py CHANGED
@@ -337,6 +337,63 @@ def create_app() -> FastAPI:
337
  reconcile_refresh_runtime()
338
  return await call_next(request)
339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  @app.exception_handler(AppError)
341
  async def _app_error_handler(request: Request, exc: AppError):
342
  return JSONResponse(exc.to_dict(), status_code=exc.status)
 
337
  reconcile_refresh_runtime()
338
  return await call_next(request)
339
 
340
+ # ===========================================================================
341
+ # 🔥 新增:GLM 5.2 全局路由拦截中间件 (完美兼容流式传输与原版 Body 转发)
342
+ # ===========================================================================
343
+ @app.middleware("http")
344
+ async def _glm_intercept_middleware(request: Request, call_next):
345
+ # 仅拦截发往聊天对齐接口的 POST 请求
346
+ if request.method == "POST" and "chat/completions" in request.url.path:
347
+ body_bytes = await request.body()
348
+ try:
349
+ import json
350
+ body_json = json.loads(body_bytes)
351
+
352
+ # 命中目标模型标记
353
+ if body_json.get("model") in ["glm-5.2", "@cf/zai-org/glm-5.2"]:
354
+ import httpx
355
+ from fastapi.responses import StreamingResponse
356
+
357
+ # 强行注入你的 Cloudflare 凭证
358
+ CF_ACCOUNT_ID = "1b18653b5658eab10682cb5396b9d446"
359
+ CF_API_TOKEN = "cfut_5xFTKKEOnH25XHPKpq9y2l7VtTgqibSGBFF93PxR1663887a"
360
+
361
+ cf_url = f"https://api.cloudflare.com/client/v4/accounts/{CF_ACCOUNT_ID}/ai/v1/chat/completions"
362
+ cf_headers = {
363
+ "Authorization": f"Bearer {CF_API_TOKEN}",
364
+ "Content-Type": "application/json"
365
+ }
366
+
367
+ # 转换模型名称为 Cloudflare 官方认识的 ID
368
+ body_json["model"] = "@cf/zai-org/glm-5.2"
369
+ is_stream = body_json.get("stream", False)
370
+
371
+ # 1. 流式返回处理 (Stream)
372
+ if is_stream:
373
+ async def cloudflare_stream_generator():
374
+ async with httpx.AsyncClient() as client:
375
+ async with client.stream("POST", cf_url, headers=cf_headers, json=body_json, timeout=60.0) as resp:
376
+ async for chunk in resp.aiter_bytes():
377
+ yield chunk
378
+ return StreamingResponse(cloudflare_stream_generator(), media_type="text/event-stream")
379
+
380
+ # 2. 非流式返回处理
381
+ else:
382
+ async with httpx.AsyncClient() as client:
383
+ resp = await client.post(cf_url, headers=cf_headers, json=body_json, timeout=60.0)
384
+ return JSONResponse(resp.json(), status_code=resp.status_code)
385
+
386
+ except Exception:
387
+ pass # 发生非预期解析错误时,滑轨进原系统兜底处理
388
+
389
+ # 恢复被 await request.body() 消费掉的内存流,防止原版路由器(Grok逻辑)在二次读取时死锁卡住
390
+ async def re_receive():
391
+ return {"type": "http.request", "body": body_bytes, "more_body": False}
392
+ request._receive = re_receive
393
+
394
+ return await call_next(request)
395
+ # ===========================================================================
396
+
397
  @app.exception_handler(AppError)
398
  async def _app_error_handler(request: Request, exc: AppError):
399
  return JSONResponse(exc.to_dict(), status_code=exc.status)