W
wernerrall
Introduction
Deploying an Azure OpenAI instance integrated with a Search Index and a Storage Account can significantly enhance your applications' capabilities. In this guide, I will walk you through the process step-by-step.
Prerequisites
- An active Azure subscription
- Azure CLI installed
- Basic understanding of Azure services
Step 1: Setting Up the Azure OpenAI Instance
Create a Resource Group. In our case I called it DeployOpenAI.
Next we will deploy the Open AI instance by going to All Services and Searching for Open AI
Create the Instance by placing it in your same resource Group. Accept all the defaults and press next until Resource is deployed.
Click "Go to resource" and Enable the System assigned Managed Identity. Copy the Object ID of the created managed identity for later use.
Step 2: Setting Up the Azure Search Index
In All Services we are searching for AI Search
For this example we will be using the Basic pricing tier and a different region. We are using the basic pricing tier because it supports managed identities. We are using UK South region at time of posting this article because Semantic Ranker is only available in certain regions.
Click "Go to resource" and ensure Semantic Ranker shows "Selected Plan"
Enable the use of RBAC and API keys by setting the Keys to both.
Enable the System assigned Managed Identity. Copy the Object ID of the created managed identity for later use.
Step 3: Creating the Storage Account
In All Services we search for Storage
We can technically create the storage account anywhere but I prefer it close to my search index so I will choose UK South as my region. I then adjust the Redundancy to LRS to save some costs. We can keep the default settings for the rest of the Storage Account and go next until my resource is created.
Go to Resource and click on Containers
Create a new container where we will be storing our knowledge documents or uploading our files
az ad signed-in-user show --query id -o tsv
Upload a file you want the Language model to be able to interpret for you later. I will be uploading the Microsoft Azure SLA Documentation in PDF format. Supported Formats are: "Txt, .md, .html, .docx, .pptx, and .pdf"
Step 4: Granting Permissions
The permissions can sometimes be confusing, especially if you don't normally work in Azure. Copy the script from my GitHub Repository --> RallTheory/OpenAIBYODPermissions/OpenAIStudioPermissionsRequired.sh at main · WernerRall147/RallTheory (github.com)
Code:
# Variables
resourceGroup="#TODO"
userObjectId="#TODO"
managedIdentityObjectId1="#TODO"
managedIdentityObjectId2="#TODO"
subscriptionId="#TODO"
# Assign Cognitive Services OpenAI Contributor
az role assignment create --assignee $userObjectId --role "Cognitive Services OpenAI Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
az role assignment create --assignee $managedIdentityObjectId1 --role "Cognitive Services OpenAI Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
az role assignment create --assignee $managedIdentityObjectId2 --role "Cognitive Services OpenAI Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
# Assign Cognitive Services Contributor
az role assignment create --assignee $userObjectId --role "Cognitive Services Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
az role assignment create --assignee $managedIdentityObjectId1 --role "Cognitive Services Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
az role assignment create --assignee $managedIdentityObjectId2 --role "Cognitive Services Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
# Assign Search Index Data Reader
az role assignment create --assignee $userObjectId --role "Search Index Data Reader" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
az role assignment create --assignee $managedIdentityObjectId1 --role "Search Index Data Reader" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
az role assignment create --assignee $managedIdentityObjectId2 --role "Search Index Data Reader" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
# Assign Storage Blob Data Contributor
az role assignment create --assignee $userObjectId --role "Storage Blob Data Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
az role assignment create --assignee $managedIdentityObjectId1 --role "Storage Blob Data Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
az role assignment create --assignee $managedIdentityObjectId2 --role "Storage Blob Data Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
# Assign Search Service Contributor
az role assignment create --assignee $userObjectId --role "Search Service Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
az role assignment create --assignee $managedIdentityObjectId1 --role "Search Service Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
az role assignment create --assignee $managedIdentityObjectId2 --role "Search Service Contributor" --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroup
Only replace the Variables Section with the #TODO labels. The 2 Managed Identity Objects we copied from earlier steps. To get your own user Object Id you can run the below script by launching the Azure Cloud Shell.
az ad signed-in-user show --query id -o tsv
Our variables should now look something like this (I have hidden some of the characters)
Code:
# Variables
resourceGroup="DeployOpenAI"
userObjectId="6167fxxxxxxxxxxxxxxxxxx"
managedIdentityObjectId1="f24cbcxxxxxxxxxxxxxxxxxxx"
managedIdentityObjectId2="542bxxxxxxxxxxxxxxxxxxxxxx"
subscriptionId="2910xxxxxxxxxxxxxxxxxxxxxxxxx"
Now we are ready to apply the permissions. Copy the entire script including the variables section and paste it as is in the Azure Cloud Shell. Then press Enter. Your output should look similar to below.
If all permissions applied successfully, we should have no problem in the Open AI Studio.
Step 5: Open AI Studio Deployment
Head over to our Open AI deployment and click "Go to Azure OpenAI Studio"
On the landing page click on Deployments and Deploy Model
We will be deploying 2 models. The Base "text-embedding-ada-002" and the "gpt-4o" models. Let's start with text-embedding-ada-002.
Next we deploy the GPT-4o model
You should now see both your deployments
Now we need to load our data from the Storage Account into the AI Search Index. In Chat, we can click "Add your data".
We will use Hybrid + semantic search
Click next on System Assigned Managed Identity. If our permissions applied correctly, we shouldn't get any errors here and we can continue to Save and Close.
You will see your data getting processed and Indexed.
And lastly, we need to create a deployment to a Web App so we can interact with all of these pieces. Let's go to Chat and click "Deploy to a web app"
We can fill in all the details here. If this is the second or third time I am deploying I can also choose "Update an Existing Web App"
To see if my app is deploying, I can go back to the Azure Portal and take a look in the Resource Group under Deployments
Once my website is ready I can simply go to Web Apps or find my new Web App in the Resource Group.
Now we can finally enjoy our newly deployed Large Language Model.
You might get this from time to time
To fix this we can either:
1. Increase our models Rate Limits or request more Quota, this may require a redeploy to your existing web app. If we still encounter the error, we can deploy other models with more quota available out-of-the-box or we can request quota for our required model from Azure Support.
2. Decrease the size of our documents by converting them to easier formats like ".txt" or "md". Converting file formats like PDF to TXT can significantly help reduce token usage when working with Azure OpenAI or any other text-based AI model.
In this blog I chose to deploy gpt-4 as a different model to try assist in solving our problem
After adding some quota and redeploying the app with gpt-4 we are now getting better responses.
I hope this has been a helpful walkthrough!
Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts or Power BI Dashboards are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts or Power BI Dashboards be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages. This blog post was written with the help of generative AI.
Continue reading...