Tuchuanhuhuhu commited on
Commit
588dc20
1 Parent(s): e0bc027

如果JSON解析出错,显示内容

Browse files
Files changed (1) hide show
  1. utils.py +6 -1
utils.py CHANGED
@@ -160,7 +160,12 @@ def stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, prev
160
  if chunk:
161
  chunk = chunk.decode()
162
  chunklength = len(chunk)
163
- chunk = json.loads(chunk[6:])
 
 
 
 
 
164
  # decode each line as response data is in bytes
165
  if chunklength > 6 and "delta" in chunk['choices'][0]:
166
  finish_reason = chunk['choices'][0]['finish_reason']
 
160
  if chunk:
161
  chunk = chunk.decode()
162
  chunklength = len(chunk)
163
+ try:
164
+ chunk = json.loads(chunk[6:])
165
+ except json.JSONDecodeError:
166
+ status_text = f"JSON解析错误。请重置对话。收到的内容: {chunk}"
167
+ yield get_return_value()
168
+ break
169
  # decode each line as response data is in bytes
170
  if chunklength > 6 and "delta" in chunk['choices'][0]:
171
  finish_reason = chunk['choices'][0]['finish_reason']