Update app.py
Browse files
app.py
CHANGED
|
@@ -135,11 +135,10 @@ async def access_control(request: Request, call_next):
|
|
| 135 |
# 流式响应生成器
|
| 136 |
async def stream_generator(response):
|
| 137 |
try:
|
| 138 |
-
async for
|
| 139 |
-
if
|
| 140 |
-
yield
|
| 141 |
-
await asyncio.sleep(0.01)
|
| 142 |
-
|
| 143 |
except Exception as e:
|
| 144 |
print(f"Stream error: {str(e)}")
|
| 145 |
error_msg = json.dumps({"error": str(e)})
|
|
@@ -321,29 +320,30 @@ async def chat_completions(request: Request):
|
|
| 321 |
|
| 322 |
print("Starting chat completion request...")
|
| 323 |
|
| 324 |
-
async with httpx.AsyncClient(timeout=
|
| 325 |
key = get_chat_key()
|
| 326 |
headers["Authorization"] = f"Bearer {key}"
|
| 327 |
|
| 328 |
-
|
| 329 |
-
method="POST",
|
| 330 |
url=f"{Config.OPENAI_API_BASE}/chat/completions",
|
| 331 |
headers=headers,
|
| 332 |
-
json=body_json
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
|
|
|
|
|
|
| 347 |
|
| 348 |
except Exception as e:
|
| 349 |
print(f"Chat completion error: {str(e)}")
|
|
|
|
| 135 |
# 流式响应生成器
|
| 136 |
async def stream_generator(response):
|
| 137 |
try:
|
| 138 |
+
async for line in response.aiter_lines():
|
| 139 |
+
if line:
|
| 140 |
+
yield f"data: {line}\n\n"
|
| 141 |
+
await asyncio.sleep(0.01)
|
|
|
|
| 142 |
except Exception as e:
|
| 143 |
print(f"Stream error: {str(e)}")
|
| 144 |
error_msg = json.dumps({"error": str(e)})
|
|
|
|
| 320 |
|
| 321 |
print("Starting chat completion request...")
|
| 322 |
|
| 323 |
+
async with httpx.AsyncClient(timeout=httpx.Timeout(60.0)) as client:
|
| 324 |
key = get_chat_key()
|
| 325 |
headers["Authorization"] = f"Bearer {key}"
|
| 326 |
|
| 327 |
+
response = await client.post(
|
|
|
|
| 328 |
url=f"{Config.OPENAI_API_BASE}/chat/completions",
|
| 329 |
headers=headers,
|
| 330 |
+
json=body_json,
|
| 331 |
+
timeout=60.0
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
if response.status_code != 200:
|
| 335 |
+
raise HTTPException(status_code=response.status_code, detail="API request failed")
|
| 336 |
+
|
| 337 |
+
return StreamingResponse(
|
| 338 |
+
stream_generator(response),
|
| 339 |
+
media_type="text/event-stream",
|
| 340 |
+
headers={
|
| 341 |
+
"Cache-Control": "no-cache",
|
| 342 |
+
"Connection": "keep-alive",
|
| 343 |
+
"Content-Type": "text/event-stream",
|
| 344 |
+
"X-Accel-Buffering": "no"
|
| 345 |
+
}
|
| 346 |
+
)
|
| 347 |
|
| 348 |
except Exception as e:
|
| 349 |
print(f"Chat completion error: {str(e)}")
|