This workspace contains a Python script (retry.py
) that demonstrates how to implement a retry mechanism when interacting with the Azure OpenAI API. The script uses the AzureOpenAI client to make requests to the API and handles rate limit exceeded errors (HTTP status code 429) by either switching to a different model or retrying after a specified wait time.
- Python 3.6 or later
- An Azure account with an active OpenAI subscription
- The AzureOpenAI Python SDK
- Clone this repository to your local machine.
- Install the required Python packages using pip:
pip install openai
You need to set the following environment variables:
AZURE_OPENAI_ENDPOINT
: The endpoint for your Azure OpenAI service.AZURE_OPENAI_API_KEY
: The API key for your Azure OpenAI service. You can set these environment variables in your shell, or you can add them to a.env
file in the root of the project.
Run the retry.py script:
python retry.py
The script will make a request to the Azure OpenAI API using the gpt-4-ptu
model. If the rate limit is exceeded, it will either switch to the gpt-4-paygo
model or wait for a specified duration before retrying the request.