bobocup commited on
Commit
9129169
·
verified ·
1 Parent(s): b860111

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -62
app.py CHANGED
@@ -132,35 +132,18 @@ async def access_control(request: Request, call_next):
132
 
133
  return await call_next(request)
134
 
135
- # 修改 stream_generator 函数
136
  async def stream_generator(response):
137
- buffer = ""
138
- first_chunk = True
139
  try:
140
  async for chunk in response.aiter_bytes():
141
- if first_chunk:
142
- # 第一个响应来得快一些
143
- await asyncio.sleep(0.1)
144
- first_chunk = False
145
- else:
146
- # 后续响应平滑输出
147
- await asyncio.sleep(0.05)
148
 
149
- chunk_str = chunk.decode('utf-8')
150
- buffer += chunk_str
151
-
152
- while '\n' in buffer:
153
- line, buffer = buffer.split('\n', 1)
154
- line = line.strip()
155
- if line:
156
- if line.startswith('data: '):
157
- yield f"{line}\n\n"
158
- else:
159
- yield f"data: {line}\n\n"
160
-
161
  except Exception as e:
162
  print(f"Stream error: {str(e)}")
163
- yield f"data: [ERROR] {str(e)}\n\n"
 
164
 
165
  # 修改 handle_api_request 函数中的超时设置
166
  async def handle_api_request(url: str, headers: dict, method: str = "GET", body: dict = None, for_chat: bool = False):
@@ -324,60 +307,50 @@ async def add_key(request: Request):
324
  @app.post("/api/v1/chat/completions")
325
  async def chat_completions(request: Request):
326
  try:
327
- # 获取请求体
328
  body = await request.body()
329
  body_json = json.loads(body)
330
 
 
 
 
331
  headers = {
332
  "Content-Type": "application/json",
333
- "Accept": "text/event-stream" if body_json.get("stream") else "application/json"
 
334
  }
335
 
336
  print("Starting chat completion request...")
337
- response = await handle_api_request(
338
- url=f"{Config.OPENAI_API_BASE}/chat/completions",
339
- headers=headers,
340
- method="POST",
341
- body=body_json,
342
- for_chat=True
343
- )
344
 
345
- if not response:
346
- raise HTTPException(status_code=500, detail="Failed to get response from API")
 
347
 
348
- print(f"Chat completion response status: {response.status_code}")
349
-
350
- # 处理流式响应
351
- if body_json.get("stream"):
352
- return StreamingResponse(
353
- stream_generator(response),
354
- media_type="text/event-stream",
355
- headers={
356
- "Cache-Control": "no-cache",
357
- "Connection": "keep-alive",
358
- "Content-Type": "text/event-stream",
359
- "X-Accel-Buffering": "no" # 禁用 Nginx 缓冲
360
- }
361
- )
362
-
363
- # 处理普通响应
364
- return Response(
365
- content=response.text,
366
- media_type="application/json",
367
- status_code=response.status_code
368
- )
369
 
370
  except Exception as e:
371
  print(f"Chat completion error: {str(e)}")
372
- error_detail = str(e)
373
- if len(error_detail) > 200: # 如果错误信息太长,只保留前200个字符
374
- error_detail = error_detail[:200] + "..."
375
- raise HTTPException(status_code=500, detail=error_detail)
376
 
377
  # 代理其他请求
378
- @app.api_route("/api/v1/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
379
- async def proxy(path: str, request: Request):
380
- if path == "chat/completions":
381
  return await chat_completions(request)
382
 
383
  try:
 
132
 
133
  return await call_next(request)
134
 
135
+ # 流式响应生成器
136
  async def stream_generator(response):
 
 
137
  try:
138
  async for chunk in response.aiter_bytes():
139
+ if chunk:
140
+ yield chunk
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)})
146
+ yield f"data: {error_msg}\n\n"
147
 
148
  # 修改 handle_api_request 函数中的超时设置
149
  async def handle_api_request(url: str, headers: dict, method: str = "GET", body: dict = None, for_chat: bool = False):
 
307
  @app.post("/api/v1/chat/completions")
308
  async def chat_completions(request: Request):
309
  try:
 
310
  body = await request.body()
311
  body_json = json.loads(body)
312
 
313
+ # 强制使用流式响应
314
+ body_json["stream"] = True
315
+
316
  headers = {
317
  "Content-Type": "application/json",
318
+ "Accept": "text/event-stream",
319
+ "Connection": "keep-alive"
320
  }
321
 
322
  print("Starting chat completion request...")
 
 
 
 
 
 
 
323
 
324
+ async with httpx.AsyncClient(timeout=None) as client:
325
+ key = get_chat_key()
326
+ headers["Authorization"] = f"Bearer {key}"
327
 
328
+ async with client.stream(
329
+ method="POST",
330
+ url=f"{Config.OPENAI_API_BASE}/chat/completions",
331
+ headers=headers,
332
+ json=body_json
333
+ ) as response:
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)}")
350
+ raise HTTPException(status_code=500, detail=str(e))
 
 
 
351
 
352
  # 代理其他请求
353
+
 
 
354
  return await chat_completions(request)
355
 
356
  try: