Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Developing AI Systems with the OpenAI API

Cursus bekijken

Oefeninstructies

  • Import the tenacity library with required functions: retry, wait_random_exponential, and stop_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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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."}))
Code bewerken en uitvoeren