Update app.py
Browse files
app.py
CHANGED
|
@@ -134,15 +134,21 @@ async def access_control(request: Request, call_next):
|
|
| 134 |
|
| 135 |
# 流式响应生成器
|
| 136 |
async def stream_generator(response):
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
# 修改 handle_api_request 函数中的超时设置
|
| 148 |
async def handle_api_request(url: str, headers: dict, method: str = "GET", body: dict = None, for_chat: bool = False):
|
|
@@ -309,41 +315,31 @@ async def chat_completions(request: Request):
|
|
| 309 |
body = await request.body()
|
| 310 |
body_json = json.loads(body)
|
| 311 |
|
| 312 |
-
# 强制使用流式响应
|
| 313 |
-
body_json["stream"] = True
|
| 314 |
-
|
| 315 |
headers = {
|
| 316 |
"Content-Type": "application/json",
|
| 317 |
"Accept": "text/event-stream",
|
| 318 |
-
"Connection": "keep-alive"
|
| 319 |
}
|
| 320 |
|
| 321 |
print("Starting chat completion request...")
|
| 322 |
|
| 323 |
-
async with httpx.AsyncClient(
|
| 324 |
key = get_chat_key()
|
| 325 |
headers["Authorization"] = f"Bearer {key}"
|
| 326 |
|
| 327 |
-
|
| 328 |
-
|
|
|
|
| 329 |
headers=headers,
|
| 330 |
json=body_json,
|
| 331 |
-
timeout=
|
| 332 |
-
)
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 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)}")
|
|
|
|
| 134 |
|
| 135 |
# 流式响应生成器
|
| 136 |
async def stream_generator(response):
|
| 137 |
+
async for line in response.aiter_lines():
|
| 138 |
+
if line.strip(): # 确保行不是空的
|
| 139 |
+
try:
|
| 140 |
+
# 如果是JSON格式的数据,直接转发
|
| 141 |
+
if line.startswith('{'):
|
| 142 |
+
yield f"data: {line}\n\n"
|
| 143 |
+
# 如果已经是SSE格式,直接发送
|
| 144 |
+
elif line.startswith('data: '):
|
| 145 |
+
yield f"{line}\n\n"
|
| 146 |
+
# 其他情况,添加data前缀
|
| 147 |
+
else:
|
| 148 |
+
yield f"data: {line}\n\n"
|
| 149 |
+
except Exception as e:
|
| 150 |
+
print(f"Error processing line: {str(e)}")
|
| 151 |
+
continue
|
| 152 |
|
| 153 |
# 修改 handle_api_request 函数中的超时设置
|
| 154 |
async def handle_api_request(url: str, headers: dict, method: str = "GET", body: dict = None, for_chat: bool = False):
|
|
|
|
| 315 |
body = await request.body()
|
| 316 |
body_json = json.loads(body)
|
| 317 |
|
|
|
|
|
|
|
|
|
|
| 318 |
headers = {
|
| 319 |
"Content-Type": "application/json",
|
| 320 |
"Accept": "text/event-stream",
|
|
|
|
| 321 |
}
|
| 322 |
|
| 323 |
print("Starting chat completion request...")
|
| 324 |
|
| 325 |
+
async with httpx.AsyncClient() as client:
|
| 326 |
key = get_chat_key()
|
| 327 |
headers["Authorization"] = f"Bearer {key}"
|
| 328 |
|
| 329 |
+
async with client.stream(
|
| 330 |
+
"POST",
|
| 331 |
+
f"{Config.OPENAI_API_BASE}/chat/completions",
|
| 332 |
headers=headers,
|
| 333 |
json=body_json,
|
| 334 |
+
timeout=None
|
| 335 |
+
) as response:
|
| 336 |
+
if response.status_code != 200:
|
| 337 |
+
raise HTTPException(status_code=response.status_code, detail="API request failed")
|
| 338 |
+
|
| 339 |
+
return StreamingResponse(
|
| 340 |
+
stream_generator(response),
|
| 341 |
+
media_type="text/event-stream"
|
| 342 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
except Exception as e:
|
| 345 |
print(f"Chat completion error: {str(e)}")
|