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.
Latihan ini adalah bagian dari kursus
Developing AI Systems with the OpenAI API
Petunjuk latihan
- Import the
tenacitylibrary 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.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# 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."}))