---
lab:
title: TechLab: Case Management Agent
description:
level: 400
duration: 180 minutes
islab: true
primarytopics:
---
@lab.Title
Please enter your own Microsoft alias (without the @microsoft.com) in the field below before starting the lab. This information is required to correctly associate your activity with your learner record and ensure your completion is accurately captured and reported. For data integrity and compliance reasons, learners may only enter their own alias. After completing the lab, please allow up to 5 business days for all reporting systems to fully reflect your completion.
@lab.ActivityGroup(aliascapture)
===
[!note] This lab is a technical deep dive on implementing well architected AI solutions that use AKS and APIM. Before you begin, please take a moment to consider your experience and comfort level with this topic and answer these questions.
@lab.ActivityGroup(initialsurvey)
===
In this lab, you will deploy the AKS Store demo app to an AKS cluster. The app uses Azure OpenAI service to generate product descriptions when you add new products. You will configure API Management service to implement smart load balancing for multiple Azure OpenAI instances.
[!NOTE] Visit [https://learn.microsoft.com/en-us/samples/azure-samples/aks-store-demo/aks-store-demo/]( "Azure Store demo app") to learn more about the demo app.
Estimated time to complete: 180 minutes.
To complete the exercises in this lab, you'll be provided an Azure subscription and credentials.
The type text icon !IMAGEtypetext.png feature used in this lab will send the specified text string to the active window in the virtual machine. Always compare the text in the lab document with the typed text in the virtual machine and verify that the expected text displays.
This lab uses text boxes in the instructions area. You will paste information into the text boxes at various points during the lab. This allows you to easily reuse the information later in the lab. After you enter information into a text box, click outside of the text box to ensure that the system stores the data for you.
After completing the exercises, you will be able to:
- Provision Azure Services
- Deploy an app to an Azure Kubernetes Service (AKS) cluster
- Deploy a microservice that connects a single Azure OpenAI instance to an AKS cluster
- Test the app you deployed
- Configure APIM to connect to and manage two Azure OpenAI instances in different regions
- Reconfigure the app to use APIM
- Redeploy and test the app
You are developing an app that will manage products and sales for a pet store. You plan to host the app on an AKS cluster to ensure that the solution is scalable. Currently, you have a single Azure OpenAI instance.
You want to host a service on the AKS cluster that uses Azure OpenAI to generate descriptions for new products when users add products to the product catalog.
In the future you plan to add additional Azure OpenAI instances, perhaps in different regions from the existing Azure OpenAI instance. You want to aggregate multiple Azure OpenAI instances behind a unified API endpoint using Azure API Management.
Usernames and passwords will be provided in the instructions on the following pages.
In the lower-right of the lab interface, select Next to continue to the exercise.
===
This lab requires an AKS cluster, two Azure OpenAI instances, and Azure API Management (APIM) resources. The process to deploy these resources automatically began when you launched the lab.
We include the steps for provisioning services here in Exercise 0 in case you want to review the process or manually deploy the resources.
[!NOTE] After Azure OpenAI services are provisioned there is an initialization and startup process that occurs. We pre-provision these resources to give time for the initialization and startup processes to complete.
Estimated time to manually provision the resources: 75 minutes
After completing the tasks in this exercise, you'll be able to:
- Provision a resource group
- Provision Application Insights and API Management
- Provision two Azure OpenAI instances and deploy a model to each instance
- Provision an AKS cluster
===
In this task you'll select Azure regions for deploying resources. You'll also verify that the resource group exists in the region.
-
Sign in to the virtual machine as LabAdmin using +++@lab.VirtualMachine(W-11CL-1).Password+++ as the password.
-
Open Microsoft Edge and go to the Azure Portal at +++portal.azure.com+++.
-
Sign in to the Azure portal using the following credentials:
- Username: +++@lab.CloudPortalCredential(User1).Username+++
- Password: +++@lab.CloudPortalCredential(User1).AccessToken+++
-
On the Stay signed dialog, selec= Yes.
-
Dismiss the various pop-up messages that display. On the Welcome to Microsoft Azure page, select Cancel.
-
On the Home page of the Azure portal, search for and select +++Resource groups+++.
-
Select the @lab.CloudResourceGroup(rg-CSU).Name resource group.
-
The ChatGPT models you will deploy are only available in specific Azure regions. From the list below, select the region where @lab.CloudResourceGroup(rg-CSU).Name is deployed.
@lab.DropDownList(PrimaryResourceRegion)[East US,France Central, North Central US, UK South, West Europe]
[!ALERT] The OpenAI models that you are using in this lab are only available in specific regions. It is important that you deploy all resources except for the second Azure OpenAI instance to the same region where the resource group is deployed.
-
You must deploy the second Azure OpenAI instance in a region that is different from @lab.Variable(PrimaryResourceRegion). From the list below, select a region where you will deploy the instance.
@lab.DropDownList(SecondaryResourceRegion)[East US,France Central, North Central US, UK South, West Europe]
===
In this task you will create an Application Insights app. API Management will collect data about requests, responses, errors, and other metrics and forward the data to Application Insights. You can use Application Insights to understand how the solution is operating.
[!KNOWLEDGE] If you want to learn more about integrating Application Insights with Azure API Management, go to Integrate Azure API Management with Azure Application Insights
-
On the Home page of the Azure portal, search for and select +++Log Analytics workspaces+++.
-
Select + Create.
-
Configure the workspace by using the values in the following table. Leave all other settings at their default values.
Tab Setting Value Basics Resource group @lab.CloudResourceGroup(rg-CSU).Name Basics Name +++DefaultWorkspace-@lab.LabInstance.Id-EUS+++ Basics Region @lab.Variable(PrimaryResourceRegion) -
Select Review + Create, and then select Create.
-
On the Home page of the Azure portal, search for and select +++Application Insights+++.
-
Select + Create
-
Configure Application Insights by using the values in the following table. Leave all other settings at their default values.
Tab Setting Value Basics Resource group @lab.CloudResourceGroup(rg-CSU).Name Basics Name +++app-insights-@lab.LabInstance.Id+++ Basics Region @lab.Variable(PrimaryResourceRegion) Basics Workspace DefaultWorkspace-@lab.LabInstance.Id-EUS [!ALERT] On the Basics tab the region that displays may be something other than the region where the resource group is deployed. You must deploy this resource to the @lab.Variable(PrimaryResourceRegion) region.
-
Select Review & Create. After the validation step completes select Create.
[!NOTE] It may take 2-3 minutes for the provisioning process to complete.
===
Azure API Management (APIM) abstracts the architecture and complexity of backend services from consumers. APIM Securely exposes services hosted in Azure as APIs. In this task, you will deploy APIM.
[!KNOWLEDGE] If you want to learn more about APIM, go to What is Azure API Management?
-
On the Home page of the Azure portal, search for and select +++API Management services+++.
-
Select + Create.
-
Configure the Basics tab by using the values in the following table. Leave all other settings at their default values.
Tab Setting Value Basics Resource group @lab.CloudResourceGroup(rg-CSU).Name Basics Region @lab.Variable(PrimaryResourceRegion) Basics Resource name +++apim-@lab.LabInstance.Id+++ Basics Organization name +++Contoso+++ Basics Administrator email +++@lab.CloudPortalCredential(User1).Username+++ Basics Pricing tier Standard v2 (99.95% SLA) -
Configure the Monitor + secure tab by using the values in the following table. Leave all other settings at their default values.
Tab Setting Value Monitor + secure Log Analytics checkbox Selected Monitor + secure Log Analytic workspace DefaultWorkspace-@lab.LabInstance.Id-EUS Monitor + secure Application Insights checkbox Selected Monitor + secure Choose an instance app-insights-@lab.LabInstance.Id -
On the Managed Identity tab, verify that the Status checkbox is selected.
[!ALERT] You must select the Status checkbox to configure a managed identity for this resource. Failure to do so will result in errors later in the lab.
-
Select Review & Create. After the validation step completes, select Create.
===
Azure OpenAI Service provides REST API access to OpenAI language models. In this task, you will deploy an Azure OpenAI instance.
[!KNOWLEDGE] If you want to learn more about Azure OpenAI Service, go to What is Azure OpenAI Service?
-
On the Home page of the Azure portal, search for and select +++Azure OpenAI+++.
-
Select + Create Azure OpenAI.
-
Configure the Azure OpenAI instance using the values in the following table. Leave all other settings at their default values.
Tab Setting Value Basics Resource group @lab.CloudResourceGroup(rg-CSU).Name Basics Region @lab.Variable(PrimaryResourceRegion) Basics Name +++OpenAI-Instance1-@lab.LabInstance.Id+++ Basics Pricing tier Standard S0 [!ALERT] On the Basics tab the region that displays may be something other than the region where the resource group is deployed. You must deploy this resource to the @lab.Variable(PrimaryResourceRegion) region.
-
Select Next three times. After the validation step completes, select Create.
[!NOTE] It may take 2-5 minutes for the deployment process to complete.
-
When Azure notifies you that the Azure OpenAI instance is provisioned, click on Deployment details and the OpenAI-Instance1-@lab.LabInstance.Id resource link.
-
On the upper-right of the Overview page, select Click here to view endpoints.
-
To the right of KEY 1, select Copy to copy the key value.
-
Paste the key value in the following textbox:
@lab.TextBox(OpenAIInstance1Key)
[!NOTE] For this textbox and all other textboxes, be sure to click outside the textbox after pasting data. This ensures that the data will be available for your use later in the lab.
-
To the right of Endpoint, select Copy to copy the Endpoint value.
-
Paste the Endpoint string in the following textbox:
@lab.TextBox(OpenAIInstance1Endpoint)
[!NOTE] You will use the endpoint and key in a later exercise to deploy an AI service to the AKS cluster.
===
Task 5 - Deploy a model to OpenAI-Instance1-@lab.LabInstance.Id
You can deploy a variety of models to Azure OpenAI. The models that you can deploy vary by region, deployment types, and capacity limits. In this task you will deploy the GPT 4o model to the Azure OpenAI instance that you created.
[!KNOWLEDGE] If you want to learn more about Azure OpenAI Deployment Types, go to Deployment Types
[!KNOWLEDGE] If you want to learn more about Azure OpenAI Models, go to Models
-
Go to the Overview page for OpenAI-Instance1-@lab.LabInstance.Id.
-
Select Explore Azure AI Foundry portal.
[!NOTE] Azure AI Foundry portal is in preview and is subject to change. The tool will open in a new browser window. Azure AI Foundry portal may take a couple of minutes to initialize.
-
In Chat playground, select + Create new deployment and then select From base models.
-
In the Select a model window, select Search and enter +++gpt-4o+++.
-
Select gpt-4o and then select Confirm.
[!NOTE] You may see a message alerting you about a failure to retrieve fine-tuned models. You can dismiss and ignore this message. This lab does not make use of fine-tuned models.
-
In the Deploy model gpt-4o window, confirm Deployment name is set to +++gpt-4o+++, then select Deploy.
===
In this task you will deploy a second Azure OpenAI instance to the resource group that you created.
-
Go to the Microsoft Azure portal at +++portal.azure.com+++ and then search for and select +++Azure OpenAI+++.
-
Select + Create.
-
Configure the Azure OpenAI instance using the values in the following table. Leave all other settings at their default values.
Tab Setting Value Basics Resource group @lab.CloudResourceGroup(rg-CSU).Name Basics Region @lab.Variable(SecondaryResourceRegion) Basics Name +++OpenAI-Instance2-@lab.LabInstance.Id+++ Basics Pricing tier Standard S0 [!ALERT] For the purposes of this lab, we want to host the Azure OpenAI instances in different regions. You deployed Azure OpenAI-Instance2-@lab.LabInstance.Id to the @lab.Variable(PrimaryResourceRegion) region. You must deploy this resource to the @lab.Variable(SecondaryResourceRegion) region.
-
Select Next three times and then select Create.
-
When Azure notifies you that the Azure OpenAI instance is provisioned, click on Deployment details and the OpenAI-Instance2-@lab.LabInstance.Id resource link.
-
On the upper-right of the Overview page, select Click here to view endpoints.
-
To the right of KEY 1, select Copy to copy the key value.
-
Paste the key value in the following textbox:
@lab.TextBox(OpenAIInstance2Key)
[!NOTE] For this textbox and all other textboxes, be sure to click outside the textbox after pasting data. This ensures that the data will be available for your use later in the lab.
-
To the right of Endpoint, select Copy to copy the Endpoint value.
-
Paste the Endpoint string in the following textbox:
@lab.TextBox(OpenAIInstance2Endpoint)
===
Task 7 - Deploy a model to OpenAI-Instance 2-@lab.LabInstance.Id
In this task you will deploy the GPT 4o model to the second Azure OpenAI instance that you created.
-
Go to the Overview page for OpenAI-Instance2-@lab.LabInstance.Id.
-
Select Explore Azure AI Foundry portal.
[!NOTE] Azure AI Foundry portal is in preview and is subject to change. The tool will open in a new browser window. Azure AI Foundry portal may take a couple of minutes to initialize.
-
In Chat playground, select + Create new deployment and then select From base models.
-
In the Select a model window, select Search and enter +++gpt-4o+++.
-
Select gpt-4o and then select Confirm.
[!NOTE] You may see a message alerting you about a failure to retrieve fine-tuned models. You can dismiss and ignore this message. This lab does not make use of fine-tuned models.
-
In the Deploy model gpt-4o window, confirm Deployment name is set to +++gpt-4o+++, then select Deploy.
===
In this task you will provision the AKS cluster that will host the AKS Store Demo app and the Python-based service.
[!KNOWLEDGE] If you want to learn more about Azure Kubernetes Service, go to What is Azure Kubernetes Service?
-
Go to the Microsoft Azure portal at +++portal.azure.com+++.
-
On the Home page of the Azure portal, search for and select +++Kubernetes services+++.
-
Select Create and then select Kubernetes cluster.
[!NOTE] The options that you see when you select Create are changing. If you do not see Create a Kubernetes cluster in the list of available options, select the option to manually create a Kubernetes cluster without Azure Arc.
-
Configure the cluster using the values in the following table.
Tab Setting Value Basics Resource group @lab.CloudResourceGroup(rg-CSU).Name Basics Cluster preset configuration Dev/Test Basics Kubernetes cluster name +++aks-cluster-@lab.LabInstance.Id+++ Basics Region @lab.Variable(PrimaryResourceRegion) Networking Network configuration Azure CNI Overlay Monitoring Enable Container Logs Clear the checkbox, if selected. Monitoring Enable Prometheus metrics Clear the checkbox, if selected. [!ALERT] On the Basics tab the region that displays may be something other than the region where the resource group is deployed. Deploy this resource to the @lab.Variable(PrimaryResourceRegion) region.
Ensure that you are deploying a Dev/Test configuration and not the default Production configuration. Also, ensure that the Enable Container logs & Enable Prometheus metrics checkboxs are cleared. Failure to take these actions will result in a policy violation and failed deployment.
This can occur if Azure has limited resources in the region.
-
Select Review & Create. After the validation step completes select Create.
[!NOTE] It may take 10-15 minutes for the provisioning process to complete.
===
#Knowledge Check
@lab.Activity(Question11)
===
In this exercise, you will verify that all required resources are deployed in Azure. You will test the Azure OpenAI Service deployments to ensure that they are operational. You will then test the AKS cluster to ensure that the nodes are running. Finally, you will verify that the APIM service is online.
Estimated time to review and verify resources: 10 minutes
After completing the tasks in this exercise, you will be able to:
- Verify that all required resources are deployed
- Test the GPT 4o deployments using the Azure AI Foundry portal Playground chat tool
- Verify that the AKS cluster nodes are running
===
In this task you will review the list of resources that are deployed to ensure that you have all of the resources needed to support this lab.
-
On the Home page of the Azure portal, search for and select +++Resource groups+++.
-
Select the @lab.CloudResourceGroup(rg-CSU).Name resource group from the list.
-
Review the list of resources that are deployed to the resource group. You should see the following required resources:
Resource Type Name AKS cluster aks-cluster-@lab.LabInstance.Id API Management apim-@lab.LabInstance.Id Application Insights app-insights-@lab.LabInstance.Id Azure OpenAI Instance OpenAI-Instance1-@lab.LabInstance.Id Azure OpenAI Instance OpenAI-Instance2-@lab.LabInstance.Id [!NOTE] Azure may deploy additional supporting resources to the resource group.
===
Task 2 - Verify operation of the OpenAI-Instance1-@lab.LabInstance.Id GPT 4o deployment
In this task you submit a chat prompt to ensure that the deployed model returns a response.
[!NOTE] After you deploy a model to an Azure OpenAI instance it takes some time before the model is ready to respond to requests. For recently deployed models, you may see an error message instead of a response. If you see an error message, wait 5 minutes and submit a request again.
-
Select the OpenAI-Instance1-@lab.LabInstance.Id resource.
-
Review the configuration for the service.
-
On the Overview tab for the Azure OpenAI instance, select Go to Foundry portal.
-
In Azure AI Foundry portal, select Deployments and then select gpt4o.
-
On the deployment page for the deployment, select Open in Playground.
-
In the chat window, enter the text +++What is the population for the most populous country/region in the world?+++ and press Enter.
-
Verify that either China or India are returned as part of the response.
===
Task 3 - Verify operation of the OpenAI-Instance2-@lab.LabInstance.Id GPT 4o deployment
In this task you submit a chat prompt to ensure that the deployed model returns a response.
[!NOTE] After you deploy a model to an Azure OpenAI instance it takes some time before the model is ready to respond to requests. For recently deployed models, you may see an error message instead of a response. If you see an error message, wait 5 minutes and submit a request again.
-
Go to the Microsoft Azure portal at +++portal.azure.com+++ and select the @lab.CloudResourceGroup(rg-CSU).Name resource group.
-
Review the configuration for the service.
-
On the Overview tab for the Azure OpenAI instance, select Go to Foundry portal.
-
In Azure AI Foundry portal, select Deployments and then select gpt4o.
-
On the deployment page for the deployment, select Open in Playground.
-
In the chat window, enter the text +++What is the highest mountain in the world and how tall is it?+++ and press Enter.
-
Verify that Mount Everest is returned as part of the response.
[!NOTE] You may see an error instead of a response: If you see an error, wait 5 minutes and try asking the same question again.
===
The AKS cluster must be fully provisioned and in a Ready state before you can deploy apps and services to the cluster. In this task, you will verify that the cluster nodes are running.
-
Go to the Microsoft Azure portal at +++portal.azure.com+++ and select the @lab.CloudResourceGroup(rg-CSU).Name resource group.
-
Select aks-cluster-@lab.LabInstance.Id.
-
Review the configuration for the service.
-
On the Properties tab of the Overview page, select Node pools.
-
Verify that the Power state for the pool is Running.
===
After deployment, APIM takes some time for configuration steps before the service becomes available for use. In this task, you will verify that APIM is online and ready for use.
-
On the Home page of the Azure portal, search for and select +++Resource groups+++.
-
Select the @lab.CloudResourceGroup(rg-CSU).Name resource group from the list and then select apim-@lab.LabInstance.Id.
-
Review the configuration for the service.
-
In the Essentials section at the top of the Overview page, verify that the status for the service is Online.
[!NOTE] Azure must activate APIM once the service is deployed. You may see that the status for the service is Activating. Do not proceed to the next task until the status for the service is Online.
===
#Knowledge Check
@lab.Activity(Question9)
===
In this exercise, you will deploy the AKS Store Demo app to the AKS cluster. You will then deploy a Python-based microservice that consumes Azure OpenAI services to the cluster. Finally, you will test the operation of the app and microservice.
Estimated time to deploy and test the app and microservice: 20 minutes
After completing the tasks in this exercise, you will be able to:
- Deploy the AKS Store Demo app to the AKS cluster
- Deploy a microservice that consumes OpenAI services to the AKS cluster
- Test the AKS Store Demo app
===
In this task, you will use Kubectl commands in an Azure Cloud Shell window to deploy the AKS Store demo app from a GitHub repository to the AKS cluster.
-
In the Azure portal, select the Cloud Shell icon on the top menu bar to open the Cloud Shell interface.
-
Select Bash.
-
If prompted, select No storage account required, select the @lab.CloudSubscription.Name subscription, and then select Apply. Wait for Cloud Shell to initialize.
-
Enter the following command at the command prompt and press Enter. This command associates the cluster subscription with the Cloud Shell environment.
+++az account set --subscription @lab.CloudSubscription.Id+++
-
Enter the following command at the command prompt and press Enter. This command downloads cluster credentials.
+++az aks get-credentials --resource-group @lab.CloudResourceGroup(rg-CSU).Name --name aks-cluster-@lab.LabInstance.Id --overwrite-existing+++
[!NOTE] You should see a message reporting that the cluster is merged as the current context.
-
Enter the following command at the command prompt and press Enter. This command verifies your connection to the cluster.
+++kubectl get nodes+++
[!NOTE] The Status value for each node should show as Ready.
-
Enter the following command at the command prompt and press Enter. This command installs the AKS Store application.
+++kubectl apply -f https://raw.githubusercontent.com/Azure-Samples/aks-store-demo/main/aks-store-all-in-one.yaml+++
[!NOTE] The Cloud Shell output will list the various services that are deployed to the cluster. Leave Cloud Shell open.
===
The AKS Store demo app uses Azure OpenAI to suggest product descriptions when users add new products to the store's catalog.
After a user enters some product keywords and selects a button in the store user interface, a Python-based microservice sends the keywords to Azure OpenAI. The Azure OpenAI service generates a product description and populates the Description field in the store app.
In this task, you will create a configuration file and deploy the microservice to the AKS cluster by using Kubectl commands.
-
Open a new browser tab and go to +++github.com/Azure-Samples/aks-store-demo+++.
-
Locate and select the ai-service.yaml file. You may need to scroll down through the list to see the file.
-
On the menu bar for the code window, select Download.
-
In the virtual machine (VM), open File Explorer and go to the Downloads folder.
-
Double-click ai-service.yaml to open the file in Visual Studio Code.
[!NOTE] Visual Studio Code may open several tabs in addition to the tab for the Azure OpenAI-service.yaml file. Close other tabs or select the ai-service.yaml tab. Visual Studio Code may open using a dark background. To change this, select File > Preferences > Theme > Color Theme and then select your desired theme.
-
Locate the USE_AZURE_OPENAI environment variable and set the value to +++True+++.
[!NOTE] This environment variable was not included in some recent versions of the ai-service.yaml file. Disregard this step if the environment variable is not present.
-
Locate the USE_AZURE_AD environment variable and set the value to +++False+++.
-
Locate the AZURE_OPENAI_DEPLOYMENT_NAME environment variable and set the value to +++gpt-4o+++.
-
In the ai-service.yaml file, locate the AZURE_OPENAI_ENDPOINT environment variable and set the value to +++@lab.Variable(OpenAIInstance1Endpoint)+++.
-
In the ai-service.yaml file, locate the AZURE_OPENAI_KEY environment variable and set the value to +++@lab.Variable(OpenAIInstance1Key)+++.
[!NOTE] Directly adding sensitive information such as API keys to your Kubernetes manifest files is not secure and the information may accidentally get committed to code repositories. We added it here for simplicity. For production workloads, use Managed Identity to authenticate to Azure OpenAI service instead or store your secrets in Azure Key Vault.
[!NOTE] The section of the ai-service.yaml file that you modified should resemble the following image: 
-
Save and close the ai-service.yaml file.
-
Return to the Azure page where Cloud Shell is display open.
-
On the menu bar for Cloud Shell, select Manage Files and then select Upload.
-
In the Open dialog, go to the Downloads folder.
-
Select ai-service.yaml and then select Open.
-
After you see the confirmation message that the file is uploaded successfully, enter the following command at the Cloud Shell command prompt and press Enter. This command deploys the microservice to the cluster.
+++kubectl apply -f ai-service.yaml+++
-
Enter the following command at the Cloud Shell command prompt and press Enter. This command verifies that the microservice is running.
+++kubectl get services+++
-
Leave the Cloud Shell window open.
===
In this task, you will open the AKS Store Demo app and enter information for a new product. You will ensure that a product description is suggested after you select the Ask AI assistant button in the user interface.
-
Enter the following command at the Cloud Shell command prompt and press Enter. This command displays the status for each pod in the cluster.
+++kubectl get pods+++
[!NOTE] It may take 1-2 minutes for the ai-service pod to initialize. If the service is not initialized, you will see 0/1 in the Ready column. After the service is initialized, you should see 1/1 in the Ready column and Running in the Status column. Rerun the kubectl get pods command to see updated status messages.
-
Enter the following command at the Cloud Shell command prompt and press Enter. This command displays information about the app including the app's public IP address. Leave Cloud Shell open.
+++kubectl get service store-admin+++
[!NOTE] The app exposes the Store Admin site to the internet via a public load balancer provisioned by the Kubernetes service. This process can take a few minutes to complete. EXTERNAL IP initially shows pending until the service comes up and shows the IP address.
-
Copy the value from the EXTERNAL-IP column and paste the value into the following text field:
@lab.TextBox(StoreAdminExternalIP)
-
Close Cloud Shell.
-
Open a web browser and go to +++http://@lab.Variable(StoreAdminExternalIP)+++. The admin portal for the app will be displayed.
[!Alert] You may see a status message indicating that the system is fetching product records. You can disregard the message. You will see the list of products when you go to the Products page in the next step.
-
Select Products from the app menu. A list of products will be displayed.
-
Select Add Product. The new product page will be displayed.
[!NOTE] If the AI service is running successfully, you should see a button with the text Ask AI Assistant to the right of the description field.
-
Enter the following values on the new product page:
Field Value Name +++Dog Smart Collar+++ Price +++39.99+++ Keywords +++bluetooth, safety, dogs, technology, smartphone, app, collar+++ -
Select Ask AI Assistant.
[!NOTE] The button will not display on the page if the microsoervice is not deployed or if there was a deployment error.
After selecting the Ask AI Assistant, wait for a response. The Description text field may display the text Thinking while the app contacts the service to retrieve a product description. -
Select Save Product to save the new product record.
-
Close the browser tab.
===
#Knowledge Check
@lab.Activity(Question12)
===
In exercise two, you deployed a solution that connects to a single Azure OpenAI instance to handle requests for product descriptioin suggestions. If the Azure OpenAI service becomes unavailable due to network, capacity, or other issues, the solution will not suggest product descriptions.
To reduce the likelihood of failure, you can deploy multiple Azure OpenAI instances (to the same region or different regions). Then, you can use APIM to receive requests and distribute the requests to the Azure OpenAI instances. In this exercise, you will configure APIM to load balance the two Azure OpenAI instances.
To implement this capability, you will create a backend for each of the two Azure OpenAI instances. The backend establishes parameters that APIM uses to decide whether to distribute requests to an individual Azure OpenAI instance.
You will also create a pool backend. The pool backend includes a list of all of the Azure OpenAI instances that APIM can communicate with. When APIM receives a request, it uses the pool backend to identify services that may be available and then uses health information from the individual instances to decide how to forward the request.
Later, in exercise 4 you will reconfigure the microservice so that it uses the APIM endpoint and key instead of the enpoint and key for a single Azure OpenAI service.
Please review the descriptions and linked articles for each task to learn more.
Estimated time to complete: 45 minutes
After completing the tasks in this exercise, you will be able to:
- Verify that the APIM service is online
- Add the managed identity for APIM to each Azure OpenAI instance
- Add an APIM backend for each Azure OpenAI instance
- Create a pool backend for APIM
- Create an inbound policy for APIM
===
Task 1 - Add the managed identity of APIM to OpenAI-Instance1-@lab.LabInstance.Id.
Managed identities provide a way for services like APIM to authenticate themselves with other services without the need to store and manage credentials.
[!KNOWLEDGE] If you want to learn more using managed identities with Azure OpenAI Service, go to Securely use Azure OpenAI On Your Data
In this task you will configure a managed identity to allow APIM access to one of the Azure OpenAI instances.
-
On the Home page of the Azure portal, search for and select +++Resource groups+++.
-
Select the @lab.CloudResourceGroup(rg-CSU).Name resource group from the list and then select OpenAI-Instance1-@lab.LabInstance.Id.
-
On the left navigation pane, select Access control (IAM).
-
Select +Add and then select Add role assignment.
-
Search for and select the +++Cognitive Services OpenAI User+++ role and then select Next.
-
Select Managed identity and then select +Select members.
-
On the Select managed identities pane, select API Management service (1) from the Managed identity drop-down list.
[!alert] If you do not see your APIM service in the list, you may have missed configuring the managed identity in Exercise 0, Task 5, Step 5.
You can resolve the issue by going to the Azure portal and selecting your APIM resource. In the left navigation pane select Security and then select Managed Identities.
Set the value of Status to On and select Save.
Repeat the steps in this task.
-
Select the apim-@lab.LabInstance.Id resource that displays and then select Select.
-
Select Review + assign twice.
===
Task 2 - Add the managed identity of APIM to OpenAI-Instance2-@lab.LabInstance.Id.
In this task you will configure a managed identity to allow APIM access to the second Azure OpenAI instance.
[!KNOWLEDGE] If you want to learn more about backends in API Management, go to Backends in API Management
-
On the Home page of the Azure portal, search for and select +++Resource groups+++.
-
Select the @lab.CloudResourceGroup(rg-CSU).Name resource group from the list and then select OpenAI-Instance2-@lab.LabInstance.Id.
-
On the left navigation pane, select Access control (IAM).
-
Select +Add and then select Add role assignment.
-
Search for and select the +++Cognitive Services OpenAI User+++ role and then select Next.
-
Select Managed identity and then select +Select members.
-
On the Select managed identities pane, select API Management service (1) from the Managed identity drop-down list.
-
Select the apim-@lab.LabInstance.Id resource that displays and then select Select.
-
Select Review + assign twice.
===
Task 3 - Create a backend for OpenAI-Instance1-@lab.LabInstance.Id
An APIM backend represents an implementation for an operation that APIM manages. At this time, you can implement backends that implement the circuit breaker pattern or the load-balanced pools.
In this task, you create a backend for OpenAI-Instance1-@lab.LabInstance.Id that implements the circuit breaker pattern. If the backend policy reports HTTP status code 429 (too many requests), the Azure OpenAI instance will become unavailable. APIM will route requests to other healthy instances. After a timeout period (PT1M indicates a 1 minute timeout period) APIM will start sending requests to the instance again.
[!KNOWLEDGE] If you want to learn more about the circuit breaker pattern for backends, go to Circuit breaker
[!NOTE]It is not possible at this time to create a backend by using the Azure portal. The product team may add that capability in the future. Instead, you will issue a REST API call to create the pool backend.
-
Open a new browser tab and go to +++learn.microsoft.com/en-us/rest/api/apimanagement/backend/create-or-update?view=rest-apimanagement-2022-08-01&tabs=HTTP#code-try-0+++.
-
Sign in using the following credentials:
- Username: +++@lab.CloudPortalCredential(User1).Username+++
- Password: +++@lab.CloudPortalCredential(User1).AccessToken+++
[!NOTE] You will be redirected to a set of pages to set up a Microsoft Learn profile.
-
Select Next twice, select Skip, and then select Back to Learn.
-
On the Backend - Create or Update page, select Continue with Account.
-
Configure the pool backend using the values in the following table. Leave all other settings at their default values.
Parameter Value backendId +++BackendForOpenAI-Instance1-@lab.LabInstance.Id+++ resourceGroupName +++@lab.CloudResourceGroup(rg-CSU).Name+++ serviceName +++apim-@lab.LabInstance.Id+++ subscription +++@lab.CloudSubscription.Name+++ api-version +++2023-09-01-preview+++ -
In the Body, select and delete the existing JSON code. Paste the following code into the Body:
{ "properties": { "url":^quote@lab.Variable(OpenAIInstance1Endpoint)openai^quote, "protocol": "http", "circuitBreaker": { "rules": [ { "name": "breakerRule", "failureCondition": { "count": 1, "interval": "PT1M", "statusCodeRanges": [ { "min": 429, "max": 429 } ], "errorReasons": [ "timeout" ] }, "tripDuration": "PT1M", "acceptRetryAfter": true } ] } } }[!NOTE] The Azure OpenAI instance will report HTTP Status Code 429 when it has exceeded the rate limit for the number of tokens per minute. APIM will then divert all future requests to the other Azure OpenAI instance until this instance is available again.
-
Select Run.
-
On the right pane of the Backend - Create or Update page, scroll down. You should see Response Code: 201 if the backend was successfully created.
[!NOTE]Leave the Backend - Create or Update page open. You will create additional backends in Tasks 4 and 5.
===
Task 4 - Create a backend for OpenAI-Instance2-@lab.LabInstance.Id
In this task, you create a backend for OpenAI-Instance2-@lab.LabInstance.Id.
-
On the Backend - Create or Update page, change the value of the backendid parameter to +++BackendForOpenAI-Instance2-@lab.LabInstance.Id+++. Do not change the values for the other parameters.
-
In the Body, select and delete the existing JSON code. Paste the following code into the Body:
{ "properties": { "url":^quote@lab.Variable(OpenAIInstance2Endpoint)openai^quote, "protocol": "http", "circuitBreaker": { "rules": [ { "name": "breakerRule", "failureCondition": { "count": 1, "interval": "PT1M", "statusCodeRanges": [ { "min": 429, "max": 429 } ], "errorReasons": [ "timeout" ] }, "tripDuration": "PT1M", "acceptRetryAfter": true } ] } } } -
Select Run.
-
On the right pane of the Backend - Create or Update page, scroll down. You should see Response Code: 200 or Response Code: 201 if the backend was successfully created.
[!NOTE]Leave the Backend - Create or Update page open. You will create a pool backend in Task 5.
===
APIM uses a pool backend to control how the service distributes requests to the individual backend services (Azure Open AI instances). In this task, you will issue a REST API call to create the pool backend. After the backend is created you can view the backend in the Azure portal.
[!KNOWLEDGE] If you want to learn more about creating a load-balanced pool backend, go to Load-balanced pool
[!NOTE]It is not possible at this time to create a backend by using the Azure portal. The product team may add that capability in the future. Instead, you will issue a REST API call to create the pool backend.
-
On the Backend - Create or Update page, update all fields using values in the following table. Leave all other settings at their default values.
Setting Value backendId +++PoolBackend+++ -
In the Body, select and delete the existing JSON code. Paste the following code into the Body:
{ "properties": { "title": "PoolBackend", "type": "Pool", "protocol": null, "url": null, "pool": { "services": [ { "id": "/subscriptions/@lab.CloudSubscription.Id/resourceGroups/@lab.CloudResourceGroup(rg-CSU).Name/providers/Microsoft.ApiManagment/service/apim-@lab.LabInstance.Id/backends/BackendForOpenAI-Instance1-@lab.LabInstance.Id", "priority": 1 }, { "id": "/subscriptions/@lab.CloudSubscription.Id/resourceGroups/@lab.CloudResourceGroup(rg-CSU).Name/providers/Microsoft.ApiManagment/service/apim-@lab.LabInstance.Id/backends/BackendForOpenAI-Instance2-@lab.LabInstance.Id", "priority": 2 } ] } } }[!NOTE]This configuration file sets Azure OpenAI Instance 1 to have a higher priority than instance 2.
-
Select Run.
-
On the right pane of the Backend - Create or Update page, scroll down. You should see Response Code: 201.
===
An API abstracts the backend architecture from API consumers and provides consumers with a structured way to request and communicate with services. The API represents a contract that consumers use to communicate with APIM.
Microsoft maintains a GitHub reposity that includes generic specification files that you can use to create APIs. Visual Studio Code has an API Management extension that you can install to create and manage APIs.
[!KNOWLEDGE] If you want to learn more about creating APIs using Visual Studio Code, go to Import and publish your first API. This article is one in a series of 10 articles about working with APIs.
[!NOTE] In this task, you will import a generic API for Azure OpenAI Services from a GitHub repository. The repository includes stable and preview versions for APIs. You will use the 2024-06-01 stable API for this lab. This version represents the latest stable version of the API at the time of the writing of this lab.
-
Open a new browser tab and go to +++github.com/Azure/azure-rest-api-specs/tree/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/stable+++.
-
Select the 2024-06-01 folder and then select inference.json.
-
On the menu bar for the code window, select Download.
-
In the virtual machine (VM), open File Explorer and go to the Downloads folder.
-
Double-click inference.json to open the file in Visual Studio Code.
-
Locate the line of code
"url": "https://{endpoint}/openai",that appears in the servers section near the top of the file. -
Replace the code with the following line of code:
"url": "https://microsoft/openai", -
Save and close the file.
-
On the Home page of the Azure portal, search for and select +++Resource groups+++.
-
Select the @lab.CloudResourceGroup(rg-CSU).Name resource group from the list and then select apim-@lab.LabInstance.Id.
-
In the APIs section, select APIs and then select + Add API.
-
In the Create from definition section of the right hand pane, select OpenAPI.
-
In the Create from OpenAPI specification dialog, select Select a file.
-
In the Open dialog, go to the Downloads folder, select inference.json and then select Open.
-
Select Create.
===
Inbound policies help you secure an API. You can configure a policy to limit traffic to the API and manage authentication. Policies include the following sections:
- inbound
- backend
- outbound
- on-error
In the policy that you will implement, the inbound policy specifies that managaged identities are used for authentication. In addition, this section specifies that APIM should emit Subscription ID and Client IP information to Application Insights.
The backend section of the policy configures APIM to watch for HTTP status code 429 errors (too many requests) from Azure OpenAI instances. If APIM receives 2 status code 429 messages in a minute it will consider the instance unhealthy and temporarily stop sending requests to the instance.
There are no specific outbound or on-error policies defined.
[!KNOWLEDGE] If you want to learn more about creating APIs using Visual Studio Code, go to Import and publish your first API. This article is one in a series of 10 articles about working with APIs. Use the left navigation pane on the page to see other articles in the series.
In this task you will add an inbound policy to the API. The policy sets the required authentication method. The policy also emits subscription identifiers and client IP addresses as metrics. You can use these metrics in Application Insights to filter the data.
-
In the center pane of the apim-@lab.LabInstance.Id | APIs page, select Azure OpenAI Service API.
-
On the right pane of the page, locate the Inbound processing tile and select </>.
-
Replace the content with the following:
<policies> <inbound> <set-backend-service id="apim-generated-policy" backend-id="PoolBackend" /> <authentication-managed-identity resource="https://cognitiveservices.azure.com" output-token-variable-name="msi-access-token" ignore-error="false" /> <set-header name="Authorization" exists-action="override"> <value>@("Bearer " + (string)context.Variables["msi-access-token"])</value> </set-header> <azure-openai-emit-token-metric namespace="genaitest"> <dimension name="Subscription ID" /> <dimension name="Client IP" value="@(context.Request.IpAddress)" /> </azure-openai-emit-token-metric> <azure-openai-token-limit counter-key="@(context.Request.IpAddress)" tokens-per-minute="5000" estimate-prompt-tokens="false" remaining-tokens-variable-name="remainingTokens" /> <azure-openai-token-limit counter-key="@(context.Subscription.Id)" token-quota="100000" token-quota-period="Monthly" estimate-prompt-tokens="false" remaining-quota-tokens-variable-name="remainingQuotaTokens" /> <base /> </inbound> <backend> <retry condition="@(context.Response.StatusCode == 429)" count="2" interval="1" first-fast-retry="true"> <forward-request buffer-response="false" /> </retry> </backend> <outbound> <base /> </outbound> <on-error> <base /> </on-error> </policies>
-
Select Save.
[!NOTE] In the inbound section you specify the backend pool. APIM will use that pool to route requests. You also defined Subscription ID and Client IP as metrics. You will be able to use these metrics in Application Insights to select appropriate data.
===
APIM uses subscriptions to allow API consumers to access APIs published by APIM. In this task you will configure a subscription for the API. You will use the All APIs scope for the subscription. Using this option casues APIM to generate keys that consumers can use to connect to any APIs within the APIM instance.
[!KNOWLEDGE] If you want to learn more about creating APIs using Visual Studio Code, go to Subscriptions in Azure API Management.
-
In the leftmost pane, under the APIs section of apim-@lab.LabInstance.Id, select Subscriptions, then select + Add subscription.
-
Configure the new subscription using the values in the following table. Leave all other settings at their default values.
Setting Value Name +++APIM-subscription+++ Display Name +++APIM-subscription+++ Allow tracing Selected Scope All APIs -
Select Create.
-
Locate the row for APIM-Subscription, then select the ellipses on the right.
-
Select Show/hide keys.
-
In the Subscription with tracing dialog, select OK.
-
Copy the primary key value and paste the key into the following textbox:
@lab.TextBox(APIMSubscriptionPrimaryKey)
-
Go to the Overview page for APIM-@lab.LabInstance.Id.
-
Copy the Gateway URL and paste the URL into the following textbox:
@lab.TextBox(APIMSubscriptionGatewayURL)
===
In this task, you will configure the API in APIM. The URL suffix and header name help APIM ensure that it is processing the correct types of requests. You will see enable collection of diagnostic data. APIM will send diagnostic data to the Applications Insights instance that you created earlier in the lab. You will test the API to ensure that APIM sends requests to Azure OpenAI instances and responses are received.
-
In the APIs section, select APIs.
-
On the second pane from the left, under All APIs, select Azure OpenAI Service API.
-
On the right pane, select the Settings tab.
-
Configure the API using the values in the following table. Leave all other settings at their default values.
Section Setting Value General API URL suffix +++openai+++ Subscription Subscription required Selected Subscription Header name +++api-key+++ Diagnostic Logs Enable Selected Diagnostic Logs Destination app-insights-@lab.LabInstance.Id Diagnostic Logs Always log errors Selected Diagnostic Logs Support custom metrics Selected [!NOTE] Enabling custom metrics support here along with custom metrics in Application Insights (see next task) allows APIM to send information about the custom metrics you defined in the API policy to Application Insights.
-
Select Save.
[!NOTE] You can test the API you created before you reconfigure the solution to use APIM for managing requests to Azure OpenAI service instances. By using the Test tab for the API, you can select a prompt and send a request to APIM for processing. If everything is configured properly you will see a response to your prompt. In the next set of steps you will send multiple requests to APIM. You will review the output to ensure that a response is received. You also review other output data to learn more about the request and response.
-
Select the Test tab.
-
On the Test pane, select the POST request option Creates a completion for the chat message.
-
In the Console pane, under Template parameters, in the deployment-id box, enter +++gpt-4o+++.
-
In the api-version box, enter +++2024-06-01+++.
-
Delete the contents of the Request body and enter the following prompt:
{ "messages": [ { "role": "system", "content": "You are a helpful and concise assistant who writes well-organized summaries." }, { "role": "user", "content": "Create a one-page summary of tips for people planning to adopt an older dog." } ], "temperature": 0.7, "max_tokens": 1500 } -
Select Send. APIM will send the prompt to an Azure OpenAI instance.
-
In the Console pane, scroll down to the HTTP response section. The first line in the message should be HTTP/1.1 200 OK or a similar message.
[!NOTE] The HTTP response will also show the region that is hosting the Azure OpenAI instance that serviced the request, the number of remaining tokens and requests for the instance, and the response text.
-
Select Send 6 more times to send additional requests and collect additional transaction data.
[!NOTE] Remember that you configured APIM to send information about all transactions (requests, responses, errors) to Application Insights. You will review the transactions in the next task.
===
Transaction data includes information about requests received by APIM, responses provided by Azure OpenAI service instances, and information about erorrs. In this task you use Applications Insights to review information about APIM requests and responses.
[!KNOWLEDGE] If you want to learn more about Application Insights, go to Application Insights Overview.
-
On the Home page of the Azure portal, search for and select +++Resource groups+++.
-
Select the @lab.CloudResourceGroup(rg-CSU).Name resource group from the list and then select app-insights-@lab.LabInstance.Id.
-
On the left navigation menu, under Investigate, select Search.
-
Select See all data in the last 24 hours.
[!NOTE] Application Insights may not show all of the results from your requests through APIM to the Azure OpenAI instances. There is often a delay in reporting.
-
Select the first result in the list of results. On the right side of the screen, you should see information about the region which hosted the Azure OpenAI instance that serviced the request.
-
Return to the home page for app-insights@lab.LabInstance.Id.
-
On the left navigation menu, under Configure, select Usage and estimated costs.
-
On the top menu, select Custom metrics (Preview).
-
On the Custom metrics (Preview) pane, select With dimensions and then select OK.
[!NOTE] Enabling custom metrics along with selecting the option to support custom metrics in the API (see previous task) allows APIM to send information about the custom metrics you defined in the API policy to Application Insights.
-
On the left navigation menu, under Monitoring select Metrics.
-
On the center pane, select the Metric Namespace menu and then select genaitest.
[!ALERT] When you created the inbound policy rule for the API, you created a namespace named genaitest that includes two metrics. It takes some time (15-20 minutes is typical) for Application Insights to add the namespace to surface the metrics and make genaitest available in the Metric Namespace menu. Refresh the page periodically until you see the namespace available.
Continue with the lab and return later to review the chart.
===
#Knowledge Check
@lab.Activity(Question8)
@lab.Activity(Question10)
===
In this exercise, you will update the solution so that the microservice uses APIM to manage connections to multiple Azure OpenAI instances that are deployed to different regions.
Estimated time to complete: 15 minutes
After completing the tasks in this exercise, you will be able to:
- Reconfigure the microservice to use API instead of connecting directly to an Azure OpenAI instance
- Test the updated solution
===
Previously, you configured the ai-service.yaml file to target one specific Azure OpenAI instance. In this task, you will update the ai-service.yaml file to target APIM instead of a specific service. APIM will then manage requests.
In this task you will modify the same ai-service.yaml file that you downloaded and configured in an earlier exercise.
-
In the virtual machine, open File Explorer, then go to the Downloads folder.
-
Double-click ai-service.yaml to open the file in Visual Studio Code.
-
Locate the AZURE_OPENAI_ENDPOINT environment variable and set the value to +++@lab.Variable(APIMSubscriptionGatewayURL)+++.
-
Locate the OpenAI_API_KEY environment variable and set the value to +++@lab.Variable(APIMSubscriptionPrimaryKey)+++.
-
Save and close the ai-service.yaml file.
===
In this task you use Kubectl commands to deploy the updated microservice to the AKS cluster. You will then test the app to ensure that product descriptions display when you select the Ask AI Assistant button.
-
In the Azure portal, select the Cloud Shell icon on the top menu bar to open the Cloud Shell interface.
-
Enter the following command at the command prompt and press Enter:
+++az account set --subscription @lab.CloudSubscription.Id+++
-
Enter the following command at the command prompt and press Enter:
+++az aks get-credentials --resource-group @lab.CloudResourceGroup(rg-CSU).Name --name aks-cluster-@lab.LabInstance.Id --overwrite-existing+++
-
Enter the following command at the command prompt and press Enter. This command verifies your connection to the cluster.
+++kubectl get nodes+++
[!NOTE] The Status value for each node should show as Ready.
-
Enter the following command at the command prompt and press Enter. This command deletes the microservice that you deployed earlier in the lab.
+++kubectl delete deployment ai-service+++
-
On the Cloud Shell menu, select Manage Files and then select Upload.
-
In the Open dialog, go to the Downloads folder.
-
Select ai-service.yaml and then select Open.
-
After you see the confirmation message that the file is uploaded successfully, enter the following command at the Cloud Shell command prompt and press Enter. This command deploys the updated microservice to the cluster.
+++kubectl apply -f ai-service.yaml+++
[!NOTE] You should see a message that deployment.apps/ai-service is configured and service/ai-service is unchanged.
-
Enter the following command at the Cloud Shell command prompt and press Enter:
+++kubectl get pods+++
[!NOTE] Rerun the kubectl get pods command until you see 1/1 in the Ready column.
-
Open a web browser and go to +++http://@lab.Variable(StoreAdminExternalIP)+++. The admin portal for the app will be displayed.
-
Select Products from the app menu. A list of products will be displayed.
-
Select Add Product. The new product page will be displayed.
[!NOTE] If the AI service is running successfully, you should see a button with the text Ask AI Assistant to the right of the description field.
-
Enter the following values on the new product page:
Field Value Name +++Puppy Palace Dog Food+++ Price +++79.99+++ Keywords +++food, healthy, natural, no grains+++ -
Select Ask AI Assistant.
[!NOTE] Wait for the app to respond. The Description text field may display the text Thinking while the app contacts the service to retrieve a product description.
-
Select Save Product to save the new product record.
===
[!Alert] IMPORTANT: These labs are hosted on the Skillable platform. Completion data is collected and then exported to Success Factors every Monday. SF require another 1-3 days to process that data. The status for this lab will be visible in Viva and Learning Path next week.
Be sure to select "Submit" in the bottom right corner to get credit for completing this lab.
@lab.ActivityGroup(completionsurvey)
[!Alert] After answering the survey questions, select submit to complete and end the lab. This is required in order to receive credit for lab completion.

