You are just blocked with an error until you have rate freed for the request.
You could also follow the advice of the message and wait the given amount of time before proceeding…that way OpenAI doesn’t have to take further action.
from openai import OpenAI
import re
def get_context_error(err_msg):
"""Search for the time value (assumes it is a single number: seconds)"""
match = re.search(r'Please try again in ([\d.]+)', err_msg)
if match:
return match.group(1)
else:
raise ValueError("No try again time found in error message")
def chat_call(modelparam):
"""talk to AI"""
cl = OpenAI()
try:
response = cl.chat.completions.create(
model=modelparam, max_tokens=25,
messages=[{"role": "system", "content": "hello"}]
)
return response.choices[0].message
except Exception as e:
err = e
# print(f"Error: {err}")
if err.code == 'rate_limit_exceeded':
time.sleep(get_context_error(err.body['message']))
else:
raise ValueError(err)
if __name__ == "__main__":
model = "gpt-4-1106-preview" # just chat completion models
message = chat_call(model) # use model
print(message.model_dump())