bobocup commited on
Commit
61ea9f0
·
verified ·
1 Parent(s): 78862b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -10
app.py CHANGED
@@ -134,20 +134,21 @@ async def access_control(request: Request, call_next):
134
 
135
  # 流式响应生成器
136
  async def stream_generator(response):
137
- async for chunk in response.aiter_bytes():
138
- try:
139
- # 解码字节为文本
140
  chunk_str = chunk.decode('utf-8')
141
- # 处理多行数据
142
  for line in chunk_str.split('\n'):
143
- if line.strip(): # 忽略空行
 
144
  if line.startswith('data: '):
145
  yield f"{line}\n\n"
146
  else:
147
  yield f"data: {line}\n\n"
148
- except Exception as e:
149
- print(f"Stream error: {str(e)}")
150
- continue
 
 
151
 
152
  # 处理API请求
153
  async def handle_api_request(url: str, headers: dict, method: str = "GET", body: dict = None, for_chat: bool = False):
@@ -167,7 +168,8 @@ async def handle_api_request(url: str, headers: dict, method: str = "GET", body:
167
 
168
  headers["Authorization"] = f"Bearer {key}"
169
 
170
- async with httpx.AsyncClient(timeout=60.0) as client:
 
171
  response = await client.request(
172
  method=method,
173
  url=url,
@@ -308,7 +310,6 @@ async def chat_completions(request: Request):
308
  body = await request.body()
309
  body_json = json.loads(body)
310
 
311
- # 获取headers
312
  headers = {
313
  "Content-Type": "application/json",
314
  "Accept": "text/event-stream" if body_json.get("stream") else "application/json"
 
134
 
135
  # 流式响应生成器
136
  async def stream_generator(response):
137
+ try:
138
+ async for chunk in response.aiter_bytes():
 
139
  chunk_str = chunk.decode('utf-8')
 
140
  for line in chunk_str.split('\n'):
141
+ line = line.strip()
142
+ if line:
143
  if line.startswith('data: '):
144
  yield f"{line}\n\n"
145
  else:
146
  yield f"data: {line}\n\n"
147
+ # 添加小延迟以保持连接
148
+ await asyncio.sleep(0.01)
149
+ except Exception as e:
150
+ print(f"Stream error: {str(e)}")
151
+ yield f"data: [ERROR] {str(e)}\n\n"
152
 
153
  # 处理API请求
154
  async def handle_api_request(url: str, headers: dict, method: str = "GET", body: dict = None, for_chat: bool = False):
 
168
 
169
  headers["Authorization"] = f"Bearer {key}"
170
 
171
+ # 修改超时设置
172
+ async with httpx.AsyncClient(timeout=httpx.Timeout(30.0, read=None)) as client:
173
  response = await client.request(
174
  method=method,
175
  url=url,
 
310
  body = await request.body()
311
  body_json = json.loads(body)
312
 
 
313
  headers = {
314
  "Content-Type": "application/json",
315
  "Accept": "text/event-stream" if body_json.get("stream") else "application/json"