Skip to content

Commit

Permalink
Rationalised OpenAI error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
machinewrapped committed Mar 8, 2024
1 parent 2d31306 commit f78ea9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
31 changes: 14 additions & 17 deletions PySubtitle/Providers/OpenAI/ChatGPTClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ def _send_messages(self, messages : list[str], temperature):
Make a request to the OpenAI API to provide a translation
"""
content = {}
retries = 0

while retries <= self.max_retries and not self.aborted:
for retry in range(self.max_retries):
if self.aborted:
raise TranslationAbortedError()

try:
response = self.client.chat.completions.create(
model=self.model,
Expand Down Expand Up @@ -61,24 +63,19 @@ def _send_messages(self, messages : list[str], temperature):
time.sleep(retry_seconds)
continue
else:
logging.warning("Rate limit hit, quota exceeded. Please wait until the quota resets.")
raise
raise TranslationImpossibleError("OpenAI account quota reached, please upgrade your plan", content)

except (openai.APIConnectionError, openai.APITimeoutError) as e:
except openai.APITimeoutError as e:
if self.aborted:
raise TranslationAbortedError()

if isinstance(e, openai.APIConnectionError):
raise TranslationImpossibleError(str(e), content)
elif retries == self.max_retries:
logging.warning(f"OpenAI failure {str(e)}, aborting after {retries} retries...")
raise
else:
retries += 1
sleep_time = self.backoff_time * 2.0**retries
logging.warning(f"OpenAI error {str(e)}, retrying in {sleep_time}...")
time.sleep(sleep_time)
continue

sleep_time = self.backoff_time * 2.0**retry
logging.warning(f"OpenAI error {str(e)}, retrying in {sleep_time}...")
time.sleep(sleep_time)
continue

except openai.APIConnectionError as e:
raise TranslationAbortedError() if self.aborted else TranslationImpossibleError(str(e), content)

except Exception as e:
raise TranslationImpossibleError(f"Unexpected error communicating with OpenAI", content, error=e)
Expand Down
28 changes: 13 additions & 15 deletions PySubtitle/Providers/OpenAI/InstructGPTClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ def _send_messages(self, messages : list, temperature):
Make a request to the OpenAI API to provide a translation
"""
content = {}
retries = 0

prompt = self._build_prompt(messages)

while retries <= self.max_retries and not self.aborted:
for retry in range(self.max_retries):
if self.aborted:
raise TranslationAbortedError()

try:
response = self.client.completions.create(
model=self.model,
Expand Down Expand Up @@ -72,21 +74,17 @@ def _send_messages(self, messages : list, temperature):
logging.warning("Rate limit hit, quota exceeded. Please wait until the quota resets.")
raise

except (openai.APIConnectionError, openai.APITimeoutError) as e:
except openai.APITimeoutError as e:
if self.aborted:
raise TranslationAbortedError()

if isinstance(e, openai.APIConnectionError):
raise TranslationImpossibleError(str(e), content)
elif retries == self.max_retries:
logging.warning(f"OpenAI failure {str(e)}, aborting after {retries} retries...")
raise
else:
retries += 1
sleep_time = self.backoff_time * 2.0**retries
logging.warning(f"OpenAI error {str(e)}, retrying in {sleep_time}...")
time.sleep(sleep_time)
continue

sleep_time = self.backoff_time * 2.0**retry
logging.warning(f"OpenAI error {str(e)}, retrying in {sleep_time}...")
time.sleep(sleep_time)
continue

except openai.APIConnectionError as e:
raise TranslationAbortedError() if self.aborted else TranslationImpossibleError(str(e), content)

except Exception as e:
raise TranslationImpossibleError(f"Unexpected error communicating with OpenAI", content, error=e)
Expand Down

0 comments on commit f78ea9c

Please sign in to comment.