Skip to content

Commit

Permalink
Enchancement/informative openai messages (#79)
Browse files Browse the repository at this point in the history
1 - If the openai api has retried 5 times, the final openai error message gets returned
2 - Updated linear backoff to exponential backoff so per-minute rate-limits are also included in the backoff (total amount of backoff is greater than 60sec across 5 retries)
  • Loading branch information
MartBakler authored Nov 21, 2023
1 parent 120ca0c commit c572467
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/monkey_patch/language_models/openai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def generate(self, model, system_message, prompt, **kwargs):
choice = None
# initiate response so exception logic doesnt error out when checking for error in response
response = {}
while counter < 5:
while counter <= 5:
try:
openai_headers = {
"Authorization": f"Bearer {self.api_key}",
Expand All @@ -66,14 +66,15 @@ def generate(self, model, system_message, prompt, **kwargs):
response = response.json()
choice = response["choices"][0]["message"]["content"].strip("'")
break
except Exception:
except Exception as e:
if ("error" in response and
"code" in response["error"] and
response["error"]["code"] == 'invalid_api_key'):
raise Exception(f"The supplied OpenAI API key {self.api_key} is invalid")

time.sleep(1 + 3 * counter)
if counter == 5:
raise Exception(f"OpenAI API failed to generate a response: {e}")
counter += 1
time.sleep(2 ** counter)
continue

if not choice:
Expand Down

0 comments on commit c572467

Please sign in to comment.