AI-102 Study Series Exercise 10: Image Generation with DALL-E in Azure AI Foundry
Overview This exercise demonstrates how to generate images using AI with Azure AI Foundry and the OpenAI DALL-E model. The goal is to develop an application that interacts with the model to create AI-generated images. Steps & Configuration Details 1. Create an Azure AI Foundry Project Open Azure AI Foundry portal (https://ai.azure.com) and sign in. Search for dall-e-3 and select Use this model. Configuration Items: Azure AI Foundry Resource: A valid name. Subscription: Your Azure subscription. Resource Group: Select or create a resource group. Region: Choose any AI Services-supported location. Deployment Name: dall-e-3 (default). 2. Test the Model in the Playground Navigate to Playgrounds → Images Playground. Ensure DALL-E model deployment is selected. Submit prompts: Create an image of a robot eating spaghetti. Review the generated image and refine prompts. 3. Prepare the Application Configuration Open Azure AI Foundry portal → Models + Endpoints. Click Get Endpoint to retrieve the connection string. Open Azure Portal (https://portal.azure.com). Launch Azure Cloud Shell (PowerShell environment). Clone the repository: rm -r mslearn-ai-vision -f git clone https://github.com/MicrosoftLearning/mslearn-ai-vision Navigate to the correct folder: Python: cd mslearn-ai-vision/Labfiles/dalle-client/python C#: cd mslearn-ai-vision/Labfiles/dalle-client/c-sharp Install dependencies: Python: python -m venv labenv ./labenv/bin/Activate.ps1 pip install -r requirements.txt azure-identity azure-ai-projects openai requests C#: dotnet add package Azure.Identity dotnet add package Azure.AI.Projects --version 1.0.0-beta.9 dotnet add package Azure.AI.OpenAI Open the configuration file: Python: .env C#: appsettings.json Update Configuration Values: Project Endpoint Model Deployment Name (dall-e-3) Save the configuration file. 4. Add Code to Use Azure OpenAI Open the code file: Python: dalle-client.py C#: Program.cs Add references: Python: from dotenv import load_dotenv from azure.identity import DefaultAzureCredential from azure.ai.projects import AIProjectClient import requests C#: using Azure.Identity; using Azure.AI.Projects; using Azure.AI.OpenAI; using OpenAI.Images; Initialize the OpenAI client: Python: project_client = AIProjectClient( endpoint=project_connection, credential=DefaultAzureCredential( exclude_environment_credential=True, exclude_managed_identity_credential=True ) ) openai_client = project_client.inference.get_azure_openai_client(api_version="2024-06-01") C#: DefaultAzureCredentialOptions options = new() { ExcludeEnvironmentCredential = true, ExcludeManagedIdentityCredential = true }; ImageClient openAIimageClient = new AzureOpenAIClient( new Uri(project_connection), new DefaultAzureCredential(options) ).GetImageClient(model_deployment); Generate an image: Python: result = openai_client.images.generate( model=model_deployment, prompt=input_text, n=1 ) json_response = json.loads(result.model_dump_json()) image_url = json_response["data"][0]["url"] C#: GeneratedImage imageGeneration = await openAIimageClient.GenerateImageAsync( input_text, new ImageGenerationOptions() { Size = GeneratedImageSize.W1024xH1024 } ); imageUrl = imageGeneration.ImageUri; 5. Run Your Application Open Azure Cloud Shell. Run the application: Python: python dalle-client.py C#: dotnet run Example prompt: Create an image of a robot eating pizza. The response should include a generated image. 6. Clean Up Delete Azure resources to avoid unnecessary costs: Open Azure Portal (https://portal.azure.com). Navigate to Resource Groups. Select the resource group and click Delete.