Update app.py
Browse files
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 |
-
|
| 377 |
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|