# Learning APIM Series (3) - Configuring multiple Azure OpenAI instances with APIM and managed identity
# Enable APIM managed identity
- Enable APIM managed identity
Ensure you have enabled APIM managed identity on APIM resource.
# Prepare Azure OpenAI resource
- Add role assignment to APIM managed identity
(1) Add role assignment with Cognitive Services OpenAI User role
(2) Assign to APIM managed identity
Note: You have to add role assignment on all Azure OpenAI resources
# Configure APIM with Azure OpenAI backends
- Add Azure OpenAI backend
# Add Azure API with OpenAI
- Add Azure API with OpenAI
(1) Add OpenAPI with Add API on APIM API pane
- Create from Open API specification
(1) Select Full
on Create from OpenAPI specification
(2) Download the OpenAPI specification from the URL https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/stable/2023-05-15/inference.json
(3) Modify the Server setting to the OpenAI endpoint url
(4) Upload the json file to OpenAPI setting
(5) Add openai
as API URL suffix
(6) Add Starter
and Unlimited
as products
# Set the Inbound policy to add the managed identity token
- Add the following policy to the Inbound policy
1 | <authentication-managed-identity resource="https://cognitiveservices.azure.com" output-token-variable-name="msi-access-token" ignore-error="false" /> |
# Add Inbound policy for Azure OpenAI redundancy
- Add the following policy to the Inbound policy
1 | <set-variable name="urlId" value="@{ |
# Add retry Backend policy
- Add the following policy to the Backend policy
1 | <retry condition="@(context.Response.StatusCode == 429)" count="2" interval="1" max-interval="10" delta="1" first-fast-retry="true"> |
# Configure APIM subscription to accept api-key
header and query parameter
# Test the APIM in a VM with VNet integration
- Get the subscription key from APIM
- Add APIM fqdn to hosts file
1 | vi /etc/hosts |
- Using curl to test the APIM
1 | curl -X POST "https://apim-jpe-20230818.azure-api.net/openai/deployments/gpt-35-turbo/chat/completions?api-version=2023-05-15" \ |
# Reference
- Azure OpenAI API 2023-05-15 Swagger spec
- Authenticate with managed identity
- api-management-policy-snippets-Back-end API redundancy.policy.xml