DALL E
Azure DALL·E image generation example
Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742
This notebook shows how to generate images with the Azure OpenAI service.
Setup
First, we install the necessary dependencies.
Additionally, to properly access the Azure OpenAI Service, we need to create the proper resources at the Azure Portal (you can check a detailed guide on how to do this in the Microsoft Docs)
Once the resource is created, the first thing we need to use is its endpoint. You can get the endpoint by looking at the "Keys and Endpoints" section under the "Resource Management" section. Having this, we will set up the SDK using this information:
Authentication
The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure credentials.
Authentication using API key
To set up the OpenAI SDK to use an Azure API Key, we need to set up the api_type to azure and set api_key to a key associated with your endpoint (you can find this key in "Keys and Endpoints" under "Resource Management" in the Azure Portal)
Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
Authentication using Microsoft Active Directory
Let's now see how we can get a key via Microsoft Active Directory Authentication.
A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:
Generations
With setup and authentication complete, you can now generate images on the Azure OpenAI service and retrieve them from the returned URLs.
1. Generate the images
The first step in this process is to actually generate the images:
Having the response from the Image.create call, we download from the URL using requests.
With the image downloaded, we use the Pillow library to open and display it: