smgc commited on
Commit
d1b5549
·
verified ·
1 Parent(s): 034428f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -369,17 +369,25 @@ def stream_notdiamond_response(response, model):
369
  """流式处理 notdiamond API 响应。"""
370
  buffer = ""
371
  previous_content = ""
 
372
 
373
  for chunk in response.iter_content(1024):
374
  if chunk:
375
  buffer += chunk.decode('utf-8')
376
- current_content = buffer.split('\n\n')[-1] # 获取最新的内容块
377
 
378
- if current_content:
379
- new_content = current_content[len(previous_content):] # 提取新内容
380
- previous_content = current_content # 更新已处理的内容
381
-
382
- yield create_openai_chunk(new_content, model)
 
 
 
 
 
 
 
383
 
384
  yield create_openai_chunk('', model, 'stop')
385
 
 
369
  """流式处理 notdiamond API 响应。"""
370
  buffer = ""
371
  previous_content = ""
372
+ content_pattern = re.compile(r'data: (.*?)\n\n', re.DOTALL)
373
 
374
  for chunk in response.iter_content(1024):
375
  if chunk:
376
  buffer += chunk.decode('utf-8')
377
+ matches = content_pattern.findall(buffer)
378
 
379
+ for match in matches:
380
+ try:
381
+ data = json.loads(match)
382
+ current_content = data['choices'][0]['delta'].get('content', '')
383
+ new_content = current_content[len(previous_content):] # 提取新内容
384
+ previous_content = current_content # 更新已处理的内容
385
+
386
+ if new_content:
387
+ yield create_openai_chunk(new_content, model)
388
+ except json.JSONDecodeError:
389
+ logger.error(f"Failed to decode JSON: {match}")
390
+ continue
391
 
392
  yield create_openai_chunk('', model, 'stop')
393