javaeeduke commited on
Commit
1da5973
·
verified ·
1 Parent(s): 88d8700

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +13 -14
main.py CHANGED
@@ -276,20 +276,19 @@ async def chat_completions(
276
 
277
  logger.info(f"转发请求 → {provider_name} | 模型: {model} | 流式: {stream}")
278
 
279
- # ── 流式响应 ──
280
- if stream:
281
- async def event_stream():
282
- async with httpx.AsyncClient(timeout=120) as client:
283
- async with client.stream(
284
- "POST", url, headers=headers, json=body
285
- ) as resp:
286
- async for chunk in resp.aiter_text():
287
- yield chunk
288
-
289
- return StreamingResponse(
290
- event_stream(),
291
- media_type="text/event-stream",
292
- )
293
 
294
  # ── 普通响应 ──
295
  async with httpx.AsyncClient(timeout=120) as client:
 
276
 
277
  logger.info(f"转发请求 → {provider_name} | 模型: {model} | 流式: {stream}")
278
 
279
+ # 流式响应中增加状态码检查
280
+ if stream:
281
+ async def event_stream():
282
+ async with httpx.AsyncClient(timeout=120) as client:
283
+ async with client.stream("POST", url, headers=headers, json=body) as resp:
284
+ if resp.status_code != 200:
285
+ err = await resp.aread()
286
+ logger.error(f"上游流式错误 {resp.status_code}: {err[:300]}")
287
+ yield json.dumps({"error": err.decode(errors='ignore')})
288
+ return
289
+ async for chunk in resp.aiter_text():
290
+ yield chunk
291
+ return StreamingResponse(event_stream(), media_type="text/event-stream")
 
292
 
293
  # ── 普通响应 ──
294
  async with httpx.AsyncClient(timeout=120) as client: