Skip to content

Commit

Permalink
Catch all completion errors generically (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerpint authored Nov 8, 2023
1 parent 6388f0d commit 008e2ba
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions buster/completers/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from typing import Iterator

import openai
from openai import OpenAI

from buster.completers import Completer
Expand Down Expand Up @@ -42,18 +43,24 @@ def complete(self, prompt: str, user_input: str, completion_kwargs=None) -> (str
try:
error = False
response = client.chat.completions.create(messages=messages, **completion_kwargs)
except openai.InvalidRequestError:
except openai.BadRequestError:
error = True
logger.exception("Invalid request to OpenAI API. See traceback:")
error_message = "Something went wrong with connecting with OpenAI, try again soon!"
error_message = "Something went wrong while connecting with OpenAI, try again soon!"
return error_message, error

except openai.error.RateLimitError:
except openai.RateLimitError:
error = True
logger.exception("RateLimit error from OpenAI. See traceback:")
error_message = "OpenAI servers seem to be overloaded, try again later!"
return error_message, error

except Exception as e:
error = True
logger.exception("Some kind of error happened trying to generate the response. See traceback:")
error_message = "Something went wrong with connecting with OpenAI, try again soon!"
return error_message, error

if completion_kwargs.get("stream") is True:
# We are entering streaming mode, so here were just wrapping the streamed
# openai response to be easier to handle later
Expand Down

0 comments on commit 008e2ba

Please sign in to comment.