Skip to content

Commit

Permalink
feat: catch potential errors when unpacking response
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiechen committed Feb 9, 2024
1 parent 87cb840 commit b26dec8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/handyllm/openai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ def wrapper(response):
logger.info('\n'.join(log_strs))
response = wrapper(response)
else:
log_strs.append(cls.converter.chat2raw([response['choices'][0]['message']]))
try:
log_strs.append(cls.converter.chat2raw([response['choices'][0]['message']]))
except (KeyError, IndexError):
log_strs.append("Wrong response format, no message found")
log_strs.append(" OUTPUT END ".center(50, '-')+"\n")
logger.info('\n'.join(log_strs))
except Exception as e:
Expand Down Expand Up @@ -301,7 +304,10 @@ def wrapper(response):
logger.info('\n'.join(log_strs))
response = wrapper(response)
else:
log_strs.append(response['choices'][0]['text'])
try:
log_strs.append(response['choices'][0]['text'])
except (KeyError, IndexError):
log_strs.append("Wrong response format, no text found")
log_strs.append(" OUTPUT END ".center(50, '-')+"\n")
logger.info('\n'.join(log_strs))
except Exception as e:
Expand Down

0 comments on commit b26dec8

Please sign in to comment.