smgc commited on
Commit
69075e3
1 Parent(s): 45eb20f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -158,11 +158,16 @@ def messages():
158
 
159
  def on_query_progress(data):
160
  nonlocal response_text
161
- if 'text' in data:
162
- text = json.loads(data['text'])
163
- chunk = text['chunks'][-1] if text['chunks'] else None
164
- if chunk:
165
- response_text.append(chunk)
 
 
 
 
 
166
 
167
  # 检查是否是最终响应
168
  if data.get('final', False):
@@ -253,11 +258,16 @@ def handle_non_stream(previous_messages, msg_id, model):
253
 
254
  def on_query_progress(data):
255
  nonlocal response_text
256
- if 'text' in data:
257
- text = json.loads(data['text'])
258
- chunk = text['chunks'][-1] if text['chunks'] else None
259
- if chunk:
260
- response_text.append(chunk)
 
 
 
 
 
261
 
262
  # 检查是否是最终响应
263
  if data.get('final', False):
 
158
 
159
  def on_query_progress(data):
160
  nonlocal response_text
161
+ try:
162
+ if 'text' in data:
163
+ text = json.loads(data['text']) # 尝试解析为JSON
164
+ chunk = text['chunks'][-1] if text['chunks'] else None
165
+ if chunk:
166
+ response_text.append(chunk)
167
+ except json.JSONDecodeError:
168
+ # 如果解析失败,说明不是有效的JSON,忽略或记录错误
169
+ logging.error("Failed to parse response as JSON")
170
+ response_text.append("Invalid response format from API.")
171
 
172
  # 检查是否是最终响应
173
  if data.get('final', False):
 
258
 
259
  def on_query_progress(data):
260
  nonlocal response_text
261
+ try:
262
+ if 'text' in data:
263
+ text = json.loads(data['text']) # 尝试解析为JSON
264
+ chunk = text['chunks'][-1] if text['chunks'] else None
265
+ if chunk:
266
+ response_text.append(chunk)
267
+ except json.JSONDecodeError:
268
+ # 如果解析失败,说明不是有效的JSON,忽略或记录错误
269
+ logging.error("Failed to parse response as JSON")
270
+ response_text.append("Invalid response format from API.")
271
 
272
  # 检查是否是最终响应
273
  if data.get('final', False):