I’ve been going through Generative AI for Beginners course on GitHub over the last couple of days.

In general, I found it quite useful not only for beginners, but also for those who have some experience with AI and ML. It’s a great way to get started with the latest developments in the field. I also found it useful to get a better understanding of the Azure OpenAI platform and its capabilities.

However, I found that some of the Python modules were written with an older API schema in mind. More specifically, the openai library is now at v1.0.6, but the code in the course is written for v0.26. This means that some of the code will not work as expected.

In this post, I’ll show you how to generate images using DALL-E in Azure OpenAI with Python using the latest API schema.

Here’s the full code I wrote to generate an image using DALL-E in Azure OpenAI with Python using a custom prompt:

The code is pretty straightforward. I will explain it in more detail in the sections below. I used a combination of code from the MS Learn website and the Generative AI For Beginners course. I also modified it a little bit to include a user prompt instead of a hardcoded value. This Python script uses the openai library to generate an image using DALL-E model in Azure OpenAI.

Note

You would need an instance of Azure OpenAI Service with a DALL-E model deployed. At the time of writing, it was available in Sweden Central Azure region.

Python Packages

Let’s have a look at Python packages that are required to run this code:

  • openai - This is the main package that is used to generate an image using DALL-E in Azure OpenAI with Python.
  • os - This package is used to get the current working directory.
  • json - This package is used to parse the JSON response from the API.
  • requests - This package is used to make HTTP requests to the API.
  • dotenv - This package is used to load environment variables from an .env file in your working directory.
  • PIL - This package is used to save the image to a file.

Environment Variables

You will need to set the following environment variables in your .env file:

  • AZURE_OPENAI_ENDPOINT='[value]'
  • AZURE_OPENAI_KEY='[value]'
  • AZURE_OPENAI_DEPLOYMENT='[value]'
  • AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT='[value]'

Note

If you are using conda to manage your Python environments, some environment variables tend to be sticky. I had to add the following code to workaround it: load_dotenv(find_dotenv(), override=True), which seems to have fixed the issue.

Authenticate to Azure OpenAI Service

The following code is used to authenticate to Azure OpenAI Service:

1
2
3
4
5
client = AzureOpenAI(
  azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), 
  api_key=os.getenv("AZURE_OPENAI_KEY"),  
  api_version="2023-12-01-preview"
)

Make sure to use the current API version, which is 2023-12-01-preview at the time of writing.

Generate an Image

The following code is used to generate an image using DALL-E in Azure OpenAI with Python:

1
2
3
4
5
result = client.images.generate(
    model="dalle3", # the name of your DALL-E 3 deployment
    prompt=input("Enter a query: "),
    n=1
)

You can also hardcode the prompt value instead of using the input() function.

Parse the Response

The following code is used to parse the response from the Azure OpenaAI API:

1
json_response = json.loads(result.model_dump_json())

Get the Image

The following code is used to get the image from the response:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Set the directory for the stored image
image_dir = os.path.join(os.curdir, 'images')

# If the directory doesn't exist, create it
if not os.path.isdir(image_dir):
    os.mkdir(image_dir)

# Initialize the image path (note the filetype should be png)
image_path = os.path.join(image_dir, 'generated_image.png')

# Retrieve the generated image
image_url = json_response["data"][0]["url"]  # extract image URL from response

Save the Image

The following code is used to save the image to a file:

1
2
3
generated_image = requests.get(image_url).content  # download the image
with open(image_path, "wb") as image_file:
    image_file.write(generated_image)

Display the Image

The following code is used to display the image:

1
2
3
# Display the image in the default image viewer
image = Image.open(image_path)
image.show()

Conclusion

That’s it! You can now generate images using DALL-E in Azure OpenAI with Python using the current API schema.

Happy coding with Python and the Azure OpenAI Service!

🐳