Spaces:
Sleeping
Sleeping
OraCatQAQ
commited on
Commit
·
289605c
1
Parent(s):
7184fb2
update
Browse files
app.py
CHANGED
@@ -232,60 +232,71 @@ async def stream_openai_response(response, request_id: str, model: str, api_key,
|
|
232 |
full_response = ""
|
233 |
|
234 |
try:
|
235 |
-
#
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
if line.startswith(b'data: '):
|
241 |
try:
|
242 |
-
|
|
|
243 |
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
"
|
261 |
-
|
262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
}
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
"object": "chat.completion.chunk",
|
273 |
-
"created": timestamp,
|
274 |
-
"model": model,
|
275 |
-
"choices": [
|
276 |
-
{
|
277 |
-
"index": 0,
|
278 |
-
"delta": {},
|
279 |
-
"finish_reason": "stop"
|
280 |
-
}
|
281 |
-
]
|
282 |
-
}
|
283 |
-
yield f"data: {json.dumps(chunk)}\n\n"
|
284 |
-
yield "data: [DONE]\n\n"
|
285 |
-
|
286 |
-
except json.JSONDecodeError:
|
287 |
-
logger.warning(f"无法解析响应: {line}")
|
288 |
|
|
|
|
|
|
|
289 |
except Exception as e:
|
290 |
logger.error(f"流式响应处理出错: {str(e)}")
|
291 |
|
@@ -370,16 +381,24 @@ async def create_chat_completion(
|
|
370 |
current_token_index = (TOKEN_INDEX - 1) % len(tokens) if len(tokens) > 0 else 0
|
371 |
|
372 |
try:
|
373 |
-
# 发送请求到DeepSider API
|
374 |
response = requests.post(
|
375 |
f"{DEEPSIDER_API_BASE}/chat/conversation",
|
376 |
headers=headers,
|
377 |
json=payload,
|
378 |
-
stream=True
|
|
|
379 |
)
|
380 |
|
381 |
-
#
|
|
|
|
|
|
|
|
|
382 |
if response.status_code != 200:
|
|
|
|
|
|
|
|
|
383 |
error_msg = f"DeepSider API请求失败: {response.status_code}"
|
384 |
try:
|
385 |
error_data = response.json()
|
@@ -419,11 +438,13 @@ async def create_chat_completion(
|
|
419 |
# 返回OpenAI格式的完整响应
|
420 |
return await generate_openai_response(full_response, request_id, chat_request.model)
|
421 |
|
422 |
-
except
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
|
|
|
|
427 |
|
428 |
@app.get("/admin/balance")
|
429 |
async def get_account_balance():
|
|
|
232 |
full_response = ""
|
233 |
|
234 |
try:
|
235 |
+
# 修改1:使用iter_content替代iter_lines
|
236 |
+
buffer = bytearray()
|
237 |
+
for chunk in response.iter_content(chunk_size=None):
|
238 |
+
if chunk:
|
239 |
+
buffer.extend(chunk)
|
|
|
240 |
try:
|
241 |
+
text = buffer.decode('utf-8')
|
242 |
+
lines = text.split('\n')
|
243 |
|
244 |
+
for line in lines[:-1]:
|
245 |
+
if line.startswith('data: '):
|
246 |
+
# 修改2:增加异常捕获和日志
|
247 |
+
try:
|
248 |
+
data = json.loads(line[6:])
|
249 |
+
# 修改3:增加调试日志
|
250 |
+
logger.debug(f"Received data: {data}")
|
251 |
+
|
252 |
+
if data.get('code') == 202 and data.get('data', {}).get('type') == "chat":
|
253 |
+
content = data.get('data', {}).get('content', '')
|
254 |
+
if content:
|
255 |
+
full_response += content
|
256 |
+
chunk = {
|
257 |
+
"id": f"chatcmpl-{request_id}",
|
258 |
+
"object": "chat.completion.chunk",
|
259 |
+
"created": timestamp,
|
260 |
+
"model": model,
|
261 |
+
"choices": [
|
262 |
+
{
|
263 |
+
"index": 0,
|
264 |
+
"delta": {
|
265 |
+
"content": content
|
266 |
+
},
|
267 |
+
"finish_reason": None
|
268 |
+
}
|
269 |
+
]
|
270 |
+
}
|
271 |
+
yield f"data: {json.dumps(chunk)}\n\n"
|
272 |
+
|
273 |
+
elif data.get('code') == 203:
|
274 |
+
# 生成完成信号
|
275 |
+
chunk = {
|
276 |
+
"id": f"chatcmpl-{request_id}",
|
277 |
+
"object": "chat.completion.chunk",
|
278 |
+
"created": timestamp,
|
279 |
+
"model": model,
|
280 |
+
"choices": [
|
281 |
+
{
|
282 |
+
"index": 0,
|
283 |
+
"delta": {},
|
284 |
+
"finish_reason": "stop"
|
285 |
+
}
|
286 |
+
]
|
287 |
}
|
288 |
+
yield f"data: {json.dumps(chunk)}\n\n"
|
289 |
+
yield "data: [DONE]\n\n"
|
290 |
+
|
291 |
+
except json.JSONDecodeError as e:
|
292 |
+
logger.warning(f"JSON解析失败: {line}, 错误: {str(e)}")
|
293 |
+
continue
|
294 |
+
|
295 |
+
buffer = bytearray(lines[-1].encode('utf-8'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
|
297 |
+
except UnicodeDecodeError:
|
298 |
+
continue
|
299 |
+
|
300 |
except Exception as e:
|
301 |
logger.error(f"流式响应处理出错: {str(e)}")
|
302 |
|
|
|
381 |
current_token_index = (TOKEN_INDEX - 1) % len(tokens) if len(tokens) > 0 else 0
|
382 |
|
383 |
try:
|
|
|
384 |
response = requests.post(
|
385 |
f"{DEEPSIDER_API_BASE}/chat/conversation",
|
386 |
headers=headers,
|
387 |
json=payload,
|
388 |
+
stream=True,
|
389 |
+
timeout=30 # 新增超时设置
|
390 |
)
|
391 |
|
392 |
+
# 新增调试日志
|
393 |
+
logger.info(f"请求头: {headers}")
|
394 |
+
logger.info(f"请求体: {payload}")
|
395 |
+
logger.info(f"响应状态码: {response.status_code}")
|
396 |
+
|
397 |
if response.status_code != 200:
|
398 |
+
# 新增详细错误日志
|
399 |
+
logger.error(f"DeepSider API错误响应头: {response.headers}")
|
400 |
+
logger.error(f"错误响应体: {response.text}")
|
401 |
+
|
402 |
error_msg = f"DeepSider API请求失败: {response.status_code}"
|
403 |
try:
|
404 |
error_data = response.json()
|
|
|
438 |
# 返回OpenAI格式的完整响应
|
439 |
return await generate_openai_response(full_response, request_id, chat_request.model)
|
440 |
|
441 |
+
except requests.exTimeout as e:
|
442 |
+
logger.error(f"请求超时: {str(e)}")
|
443 |
+
raise HTTPException(status_code=504, detail="上游服务响应超时")
|
444 |
+
|
445 |
+
except requests.RequestException as e:
|
446 |
+
logger.error(f"网络请求异常: {str(e)}")
|
447 |
+
raise HTTPException(status_code=502, detail="网关错误")
|
448 |
|
449 |
@app.get("/admin/balance")
|
450 |
async def get_account_balance():
|