AI-102 Study Series Exercise 8: AI-Powered Application with Azure OpenAI

Overview This exercise demonstrates how to develop an AI-powered application using Azure OpenAI Service. The goal is to integrate generative AI into a chatbot or other applications using REST APIs or SDKs. Steps & Configuration Details 1. Clone the Repository Open Visual Studio Code. Run the following command to clone the repository: git clone https://github.com/MicrosoftLearning/mslearn-openai Open the cloned folder in Visual Studio Code. 2. Provision an Azure OpenAI Resource Sign into Azure Portal (https://portal.azure.com). Create an Azure OpenAI resource with the following settings: Subscription: Your Azure subscription. Resource Group: Select or create a resource group. Region: Choose from: East US East US 2 North Central US South Central US Sweden Central West US West US 3 Name: A unique name. Pricing Tier: Standard S0. 3. Deploy a Model Open Azure Cloud Shell (Bash environment). Run the following command, replacing placeholders with actual values: az cognitiveservices account deployment create \ -g <your_resource_group> \ -n <your_OpenAI_service> \ --deployment-name gpt-4o \ --model-name gpt-4o \ --model-version 2024-05-13 \ --model-format OpenAI \ --sku-name "Standard" \ --sku-capacity 5 Configuration Items: Deployment Name: gpt-4o Model Name: gpt-4o Model Version: 2024-05-13 SKU Capacity: 5 (measured in thousands of tokens per minute). 4. Configure Your Application Open Visual Studio Code. Navigate to: C#: Labfiles/01-app-develop/CSharp Python: Labfiles/01-app-develop/Python Open an integrated terminal and install the Azure OpenAI SDK: C#: dotnet add package Azure.AI.OpenAI --version 2.1.0 Python: pip install openai==1.65.2 Open the configuration file: C#: appsettings.json Python: .env Update Configuration Values: Azure OpenAI Endpoint API Key Deployment Name Save the configuration file. 5. Add Code to Use Azure OpenAI Open the code file: C#: Program.cs Python: application.py Add the Azure OpenAI package: C#: using Azure.AI.OpenAI; using OpenAI.Chat; Python: from openai import AsyncAzureOpenAI Configure the Azure OpenAI client: C#: AzureOpenAIClient azureClient = new ( new Uri(oaiEndpoint), new ApiKeyCredential(oaiKey) ); ChatClient chatClient = azureClient.GetChatClient(oaiDeploymentName); Python: client = AsyncAzureOpenAI( azure_endpoint=azure_oai_endpoint, api_key=azure_oai_key, api_version="2024-02-15-preview" ) Format and send the request: C#: ChatCompletionOptions chatCompletionOptions = new ChatCompletionOptions() { Temperature = 0.7f, MaxOutputTokenCount = 800 }; ChatCompletion completion = chatClient.CompleteChat( [new SystemChatMessage(systemMessage), new UserChatMessage(userMessage)], chatCompletionOptions ); Console.WriteLine($"{completion.Role}: {completion.Content[0].Text}"); Python: messages = [ {"role": "system", "content": system_message}, {"role": "user", "content": user_message}, ] response = await client.chat.completions.create( model=model, messages=messages, temperature=0.7, max_tokens=800 ) print(response.choices[0].message.content) 6. Run Your Application Open Visual Studio Code. Run the application: C#: dotnet run Python: python application.py Test different prompts to observe AI responses. 7. 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.

June 8, 2025 · 2 min · Taner

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.

June 7, 2025 · 2 min · Taner

AI-102 Study Series Exercise 11: AI Agent for Expense Claims with GPT-4o

Overview This exercise demonstrates how to create an AI agent using Azure AI Foundry. The agent assists employees with expense claims by leveraging a GPT-4o model and grounding responses in a corporate expenses policy document. Steps & Configuration Details 1. Create an Azure AI Foundry Project Open Azure AI Foundry portal (https://ai.azure.com) and sign in. Select Create an agent. 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. Project Name: A unique name. After creation, the Agents playground opens automatically, with a GPT-4o base model deployed. ...

June 7, 2025 · 2 min · Taner

AI-102 Study Series Exercise 12: AI Agent with Code Interpreter in Azure AI Foundry

Overview This exercise demonstrates how to develop an AI agent using Azure AI Foundry. The agent analyzes data and generates charts dynamically using the Code Interpreter tool. Steps & Configuration Details 1. Create an Azure AI Foundry Project Open Azure AI Foundry portal (https://ai.azure.com) and sign in. Select Create an agent. 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. Project Name: A unique name. After creation, the Agents playground opens automatically, with a GPT-4o base model deployed. ...

June 7, 2025 · 3 min · Taner

AI-102 Study Series Exercise 13: AI Agent with Custom Functions in Azure AI Foundry

Overview This exercise demonstrates how to create an AI agent using Azure AI Foundry with custom function tools. The agent collects details of a technical issue and generates a support ticket. Steps & Configuration Details 1. Create an Azure AI Foundry Project Open Azure AI Foundry portal (https://ai.azure.com) and sign in. Select Create an agent. 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. Project Name: A unique name. After creation, the Agents playground opens automatically, with a GPT-4o base model deployed. ...

June 7, 2025 · 3 min · Taner

AI-102 Study Series Exercise 14: AI Agent for Expense Claims with Semantic Kernel

Overview This exercise demonstrates how to develop an AI agent using Azure AI Foundry and the Semantic Kernel SDK. The agent processes expense claims by analyzing data and generating structured responses. Steps & Configuration Details 1. Deploy a Model in Azure AI Foundry Open Azure AI Foundry portal (https://ai.azure.com) and sign in. Search for gpt-4o 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: gpt-4o (default). 2. Clone the Repository Open Azure Portal (https://portal.azure.com). Launch Azure Cloud Shell (PowerShell environment). Clone the repository: rm -r ai-agents -f git clone https://github.com/MicrosoftLearning/mslearn-ai-agents ai-agents Navigate to the correct folder: cd ai-agents/Labfiles/04-semantic-kernel/python Install dependencies: python -m venv labenv ./labenv/bin/Activate.ps1 pip install python-dotenv azure-identity semantic-kernel[azure] Open the configuration file: code .env Update Configuration Values: Project Endpoint (copied from Azure AI Foundry portal). Model Deployment Name (gpt-4o) Save the configuration file. 3. Implement the AI Agent Open the agent code file: code semantic-kernel.py Add references: from dotenv import load_dotenv from azure.identity.aio import DefaultAzureCredential from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread from semantic_kernel.functions import kernel_function from typing import Annotated Define an Email Plugin for expense claim submission: class EmailPlugin: """A Plugin to simulate email functionality.""" @kernel_function(description="Sends an email.") def send_email(self, to: Annotated[str, "Recipient"], subject: Annotated[str, "Email Subject"], body: Annotated[str, "Email Body"]): print(f"\nTo: {to}") print(f"Subject: {subject}") print(f"{body}\n") Load configuration settings: load_dotenv() ai_agent_settings = AzureAIAgentSettings() Connect to Azure AI Foundry: async with ( DefaultAzureCredential(exclude_environment_credential=True, exclude_managed_identity_credential=True) as creds, AzureAIAgent.create_client(credential=creds) as project_client, ): Define the AI agent: expenses_agent_def = await project_client.agents.create_agent( model=ai_agent_settings.model_deployment_name, name="expenses_agent", instructions="""You are an AI assistant for expense claim submission. When a user submits expenses data and requests an expense claim, use the plug-in function to send an email to [email protected] with the subject 'Expense Claim' and a body that contains itemized expenses with a total. Then confirm to the user that you've done so.""" ) Create a Semantic Kernel Agent: expenses_agent = AzureAIAgent( client=project_client, definition=expenses_agent_def, plugins=[EmailPlugin()] ) Process expenses data: thread: AzureAIAgentThread = AzureAIAgentThread(client=project_client) try: prompt_messages = [f"{prompt}: {expenses_data}"] response = await expenses_agent.get_response(thread_id=thread.id, messages=prompt_messages) print(f"\n# {response.name} :\n {response}") except Exception as e: print(e) finally: await thread.delete() if thread else None await project_client.agents.delete_agent(expenses_agent.id) 4. Run the AI Agent Sign into Azure: az login Run the application: python semantic-kernel.py Example prompt: Submit an expense claim. The agent should generate an email for an expense claim. 5. 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.

June 7, 2025 · 2 min · Taner

AI-102 Study Series Exercise 15: Multi-Agent Orchestration with Semantic Kernel

Overview This exercise demonstrates how to orchestrate multiple AI agents using Azure AI Foundry and the Semantic Kernel SDK. The solution involves two agents: Incident Manager Agent – Analyzes service logs and recommends resolution actions. DevOps Assistant Agent – Executes corrective actions and updates logs. Steps & Configuration Details 1. Deploy a Model in Azure AI Foundry Open Azure AI Foundry portal (https://ai.azure.com) and sign in. Search for gpt-4o 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: gpt-4o (default). Tokens per Minute Rate Limit: 40,000 TPM (adjusted in Models and Endpoints). 2. Clone the Repository Open Azure Portal (https://portal.azure.com). Launch Azure Cloud Shell (PowerShell environment). Clone the repository: rm -r ai-agents -f git clone https://github.com/MicrosoftLearning/mslearn-ai-agents ai-agents Navigate to the correct folder: cd ai-agents/Labfiles/05-agent-orchestration/Python Install dependencies: python -m venv labenv ./labenv/bin/Activate.ps1 pip install python-dotenv azure-identity semantic-kernel[azure] Open the configuration file: code .env Update Configuration Values: Project Endpoint (copied from Azure AI Foundry portal). Model Deployment Name (gpt-4o) Save the configuration file. 3. Implement AI Agents Open the agent code file: code agent_chat.py Add references: from azure.identity import DefaultAzureCredential from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AgentGroupChat from semantic_kernel.functions import kernel_function from typing import Annotated Define the Incident Manager Agent: incident_agent_definition = await client.agents.create_agent( model=ai_agent_settings.model_deployment_name, name="Incident_Manager", instructions="Analyze service logs, identify issues, and recommend resolution actions." ) agent_incident = AzureAIAgent( client=client, definition=incident_agent_definition, plugins=[LogFilePlugin()] ) Define the DevOps Assistant Agent: devops_agent_definition = await client.agents.create_agent( model=ai_agent_settings.model_deployment_name, name="DevOps_Assistant", instructions="Execute corrective actions based on recommendations from the Incident Manager." ) agent_devops = AzureAIAgent( client=client, definition=devops_agent_definition, plugins=[DevopsPlugin()] ) 4. Implement Multi-Agent Strategies Define Selection Strategy (determines which agent responds next): class SelectionStrategy: async def select_agent(self, agents, history): if history[-1].name == "DevOps_Assistant" or history[-1].role == "User": return next(agent for agent in agents if agent.name == "Incident_Manager") return next(agent for agent in agents if agent.name == "DevOps_Assistant") Define Termination Strategy (ends conversation when resolution is complete): class ApprovalTerminationStrategy: async def should_agent_terminate(self, agent, history): return "no action needed" in history[-1].content.lower() 5. Implement Multi-Agent Chat Create a Group Chat: chat = AgentGroupChat( agents=[agent_incident, agent_devops], termination_strategy=ApprovalTerminationStrategy(agents=[agent_incident], maximum_iterations=10, automatic_reset=True), selection_strategy=SelectionStrategy(agents=[agent_incident, agent_devops]) ) Append log file data: await chat.add_chat_message(logfile_msg) Invoke the chat: async for response in chat.invoke(): if response is None or not response.name: continue print(f"{response.content}") 6. Run the AI Agent Sign into Azure: az login Run the application: python agent_chat.py Example output: INCIDENT_MANAGER > /home/.../logs/log1.log | Restart service ServiceX DEVOPS_ASSISTANT > Service ServiceX restarted successfully. INCIDENT_MANAGER > No action needed. 7. 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. This summary captures the essential steps while highlighting all configuration items and code references required for orchestrating multiple AI agents in Azure AI Foundry. ...

June 7, 2025 · 3 min · Taner

AI-102 Study Series Exercise 16: Text Analysis with Azure AI Language

Overview This exercise demonstrates how to analyze text using Azure AI Language, including language detection, sentiment analysis, key phrase extraction, and entity recognition. Steps & Configuration Details 1. Provision an Azure AI Language Resource Open Azure Portal (https://portal.azure.com) and sign in. Select Create a resource → Search for Language Service → Click Create. Configuration Items: Subscription: Your Azure subscription. Resource Group: Select or create a resource group. Region: Choose any available region. Name: Enter a unique name. Pricing Tier: F0 (Free) or S (Standard). Responsible AI Notice: Agree. After provisioning, navigate to Keys and Endpoint in the Resource Management section. ...

June 7, 2025 · 3 min · Taner

AI-102 Study Series Exercise 17: Question Answering with Azure AI Language

Overview This exercise demonstrates how to create a Question Answering solution using Azure AI Language. The solution enables users to query a knowledge base of FAQs using natural language. Steps & Configuration Details 1. Provision an Azure AI Language Resource Open Azure Portal (https://portal.azure.com) and sign in. Select Create a resource → Search for Language Service → Click Create. Configuration Items: Subscription: Your Azure subscription. Resource Group: Select or create a resource group. Region: Choose any available location. Name: Enter a unique name. Pricing Tier: F0 (Free) or S (Standard). Azure Search Region: Same global region as Language resource. Azure Search Pricing Tier: Free (F) or Basic (B). Responsible AI Notice: Agree. After provisioning, navigate to Keys and Endpoint in the Resource Management section. ...

June 7, 2025 · 3 min · Taner

AI-102 Study Series Exercise 18: Conversational Language Understanding

Overview This exercise demonstrates how to create a conversational language understanding model using Azure AI Language Service. The model interprets user input, predicts intent, and identifies relevant entities. Steps & Configuration Details 1. Provision an Azure AI Language Resource Open Azure Portal (https://portal.azure.com) and sign in. Select Create a resource → Search for Language Service → Click Create. Configuration Items: Subscription: Your Azure subscription. Resource Group: Select or create a resource group. Region: Choose from: East US West US 3 North Europe South Central US Name: Enter a unique name. Pricing Tier: F0 (Free) or S (Standard). Responsible AI Notice: Agree. After provisioning, navigate to Keys and Endpoint in the Resource Management section. ...

June 7, 2025 · 3 min · Taner