Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -276,20 +276,19 @@ async def chat_completions(
|
|
| 276 |
|
| 277 |
logger.info(f"转发请求 → {provider_name} | 模型: {model} | 流式: {stream}")
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 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:
|