Spaces:
Runtime error
Runtime error
tianlong12
commited on
Commit
•
b3ba3af
1
Parent(s):
dd193a1
Update app.py
Browse files
app.py
CHANGED
@@ -96,19 +96,24 @@ def chat_completions():
|
|
96 |
|
97 |
elif '"final":true' in event.data:
|
98 |
final_data = json.loads(event.data)
|
99 |
-
|
|
|
|
|
100 |
if finish_reason == 'length':
|
101 |
messages.append({"role": "assistant", "content": full_response})
|
102 |
messages.append({"role": "user", "content": "请继续你的输出,不要重复之前的内容"})
|
103 |
break # 跳出当前循环,继续下一次请求
|
104 |
else:
|
|
|
|
|
|
|
|
|
|
|
105 |
yield f"data: {json.dumps(format_openai_response('', finish_reason))}\n\n"
|
106 |
yield "data: [DONE]\n\n"
|
107 |
return # 完全结束生成
|
108 |
-
|
109 |
-
# 如果因为 length 而跳出内部循环,则继续外部循环
|
110 |
|
111 |
-
#
|
112 |
yield f"data: {json.dumps(format_openai_response('', 'stop'))}\n\n"
|
113 |
yield "data: [DONE]\n\n"
|
114 |
|
|
|
96 |
|
97 |
elif '"final":true' in event.data:
|
98 |
final_data = json.loads(event.data)
|
99 |
+
response_message = final_data.get('responseMessage', {})
|
100 |
+
finish_reason = response_message.get('finish_reason', 'stop')
|
101 |
+
|
102 |
if finish_reason == 'length':
|
103 |
messages.append({"role": "assistant", "content": full_response})
|
104 |
messages.append({"role": "user", "content": "请继续你的输出,不要重复之前的内容"})
|
105 |
break # 跳出当前循环,继续下一次请求
|
106 |
else:
|
107 |
+
# 正常结束,发送最后的内容(如果有的话)
|
108 |
+
last_content = response_message.get('text', '')
|
109 |
+
if last_content and last_content != full_response:
|
110 |
+
yield f"data: {json.dumps(format_openai_response(last_content[len(full_response):]))}\n\n"
|
111 |
+
|
112 |
yield f"data: {json.dumps(format_openai_response('', finish_reason))}\n\n"
|
113 |
yield "data: [DONE]\n\n"
|
114 |
return # 完全结束生成
|
|
|
|
|
115 |
|
116 |
+
# 如果因为多次长度限制而最终结束,发送一个停止信号
|
117 |
yield f"data: {json.dumps(format_openai_response('', 'stop'))}\n\n"
|
118 |
yield "data: [DONE]\n\n"
|
119 |
|