Merge pull request #1142 from melambert/api-error

Simple retry on Open AI chat if a Rate Limit or 502 Bad Gateway…
pull/1179/head
Richard Beales 2023-04-13 20:39:56 +01:00 committed by GitHub
commit b8301118a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 15 deletions

View File

@ -1,13 +1,21 @@
import time
import openai import openai
from colorama import Fore
from config import Config from config import Config
cfg = Config() cfg = Config()
openai.api_key = cfg.openai_api_key openai.api_key = cfg.openai_api_key
# Overly simple abstraction until we create something better # Overly simple abstraction until we create something better
# simple retry mechanism when getting a rate error or a bad gateway
def create_chat_completion(messages, model=None, temperature=cfg.temperature, max_tokens=None)->str: def create_chat_completion(messages, model=None, temperature=cfg.temperature, max_tokens=None)->str:
"""Create a chat completion using the OpenAI API""" """Create a chat completion using the OpenAI API"""
response = None
num_retries = 5
for attempt in range(num_retries):
try:
if cfg.use_azure: if cfg.use_azure:
response = openai.ChatCompletion.create( response = openai.ChatCompletion.create(
deployment_id=cfg.get_azure_deployment_id_for_model(model), deployment_id=cfg.get_azure_deployment_id_for_model(model),
@ -23,5 +31,22 @@ def create_chat_completion(messages, model=None, temperature=cfg.temperature, ma
temperature=temperature, temperature=temperature,
max_tokens=max_tokens max_tokens=max_tokens
) )
break
except openai.error.RateLimitError:
if cfg.debug_mode:
print(Fore.RED + "Error: ", "API Rate Limit Reached. Waiting 20 seconds..." + Fore.RESET)
time.sleep(20)
except openai.error.APIError as e:
if e.http_status == 502:
if cfg.debug_mode:
print(Fore.RED + "Error: ", "API Bad gateway. Waiting 20 seconds..." + Fore.RESET)
time.sleep(20)
else:
raise
if attempt == num_retries - 1:
raise
if response is None:
raise RuntimeError("Failed to get response after 5 retries")
return response.choices[0].message["content"] return response.choices[0].message["content"]