Tuchuanhuhuhu commited on
Commit
fc2938f
·
1 Parent(s): 18cf6f9

bugfix: 某些第三方API服务提供商使用和OpenAI不同的返回格式 fix #912

Browse files
Files changed (1) hide show
  1. modules/models/OpenAI.py +16 -8
modules/models/OpenAI.py CHANGED
@@ -201,14 +201,22 @@ class OpenAIClient(BaseLLMModel):
201
  print(i18n("JSON解析错误,收到的内容: ") + f"{chunk}")
202
  error_msg += chunk
203
  continue
204
- if chunk_length > 6 and "delta" in chunk["choices"][0]:
205
- if chunk["choices"][0]["finish_reason"] == "stop":
206
- break
207
- try:
208
- yield chunk["choices"][0]["delta"]["content"]
209
- except Exception as e:
210
- # logging.error(f"Error: {e}")
211
- continue
 
 
 
 
 
 
 
 
212
  if error_msg:
213
  raise Exception(error_msg)
214
 
 
201
  print(i18n("JSON解析错误,收到的内容: ") + f"{chunk}")
202
  error_msg += chunk
203
  continue
204
+ try:
205
+ if chunk_length > 6 and "delta" in chunk["choices"][0]:
206
+ if "finish_reason" in chunk["choices"][0]:
207
+ finish_reason = chunk["choices"][0]["finish_reason"]
208
+ else:
209
+ finish_reason = chunk["finish_reason"]
210
+ if finish_reason == "stop":
211
+ break
212
+ try:
213
+ yield chunk["choices"][0]["delta"]["content"]
214
+ except Exception as e:
215
+ # logging.error(f"Error: {e}")
216
+ continue
217
+ except:
218
+ print(f"ERROR: {chunk}")
219
+ continue
220
  if error_msg:
221
  raise Exception(error_msg)
222