Avoiding rate limits with retry
You've created a function to run Chat Completions with a custom message but have noticed it sometimes fails due to rate limits. You decide to use the @retry
decorator from the tenacity
library to avoid errors when possible.
This exercise is part of the course
Developing AI Systems with the OpenAI API
Exercise instructions
- Import the
tenacity
library with required functions:retry
,wait_random_exponential
, andstop_after_attempt
. - Create an OpenAI API client.
- Complete the retry decorators with the parameters required to start retrying at an interval of 5 seconds, up to 40 seconds, and to stop after 4 attempts.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the tenacity library
from ____ import ____
client = OpenAI(api_key="")
# Add the appropriate parameters to the decorator
@retry(____, ____)
def get_response(model, message):
response = client.chat.completions.create(
model=model,
messages=[message]
)
return response.choices[0].message.content
print(get_response("gpt-4o-mini", {"role": "user", "content": "List ten holiday destinations."}))