Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enchancement/informative openai messages #79

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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