OraCatQAQ commited on
Commit
289605c
·
1 Parent(s): 7184fb2
Files changed (1) hide show
  1. app.py +79 -58
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
- # 将DeepSider响应流转换为OpenAI流格式
236
- for line in response.iter_lines():
237
- if not line:
238
- continue
239
-
240
- if line.startswith(b'data: '):
241
  try:
242
- data = json.loads(line[6:].decode('utf-8'))
 
243
 
244
- if data.get('code') == 202 and data.get('data', {}).get('type') == "chat":
245
- # 获取正文内容
246
- content = data.get('data', {}).get('content', '')
247
- if content:
248
- full_response += content
249
-
250
- # 生成OpenAI格式的流式响应
251
- chunk = {
252
- "id": f"chatcmpl-{request_id}",
253
- "object": "chat.completion.chunk",
254
- "created": timestamp,
255
- "model": model,
256
- "choices": [
257
- {
258
- "index": 0,
259
- "delta": {
260
- "content": content
261
- },
262
- "finish_reason": None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  }
264
- ]
265
- }
266
- yield f"data: {json.dumps(chunk)}\n\n"
267
-
268
- elif data.get('code') == 203:
269
- # 生成完成信号
270
- chunk = {
271
- "id": f"chatcmpl-{request_id}",
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 HTTPException:
423
- raise
424
- except Exception as e:
425
- logger.exception("处理请求时出错")
426
- raise HTTPException(status_code=500, detail=f"内部服务器错误: {str(e)}")
 
 
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():