bobocup commited on
Commit
0828051
·
verified ·
1 Parent(s): 4266554

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -32
app.py CHANGED
@@ -134,15 +134,21 @@ async def access_control(request: Request, call_next):
134
 
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)})
145
- yield f"data: {error_msg}\n\n"
 
 
 
 
 
 
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(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)}")
 
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)}")