Skip to content

Commit 5d0274a

Browse files
authored
Merge pull request #267765 from mrbullwinkle/mrb_02_29_2024_freshness
[Azure OpenAI] Freshness fixes
2 parents 838fd56 + 52e3b23 commit 5d0274a

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed

articles/ai-services/openai/how-to/managed-identity.md

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ description: Provides guidance on how to set managed identity with Microsoft Ent
55
ms.service: azure-ai-openai
66
ms.topic: how-to
77
ms.date: 02/29/2024
8-
author: ChrisHMSFT
9-
ms.author: chrhoder
8+
author: mrbullwinkle
9+
ms.author: mbullwin
1010
recommendations: false
1111
ms.custom: devx-track-azurecli
1212
---
@@ -15,7 +15,7 @@ ms.custom: devx-track-azurecli
1515

1616
More complex security scenarios require Azure role-based access control (Azure RBAC). This document covers how to authenticate to your OpenAI resource using Microsoft Entra ID.
1717

18-
In the following sections, you'll use the Azure CLI to assign roles, and obtain a bearer token to call the OpenAI resource. If you get stuck, links are provided in each section with all available options for each command in Azure Cloud Shell/Azure CLI.
18+
In the following sections, you'll use the Azure CLI to sign in, and obtain a bearer token to call the OpenAI resource. If you get stuck, links are provided in each section with all available options for each command in Azure Cloud Shell/Azure CLI.
1919

2020
## Prerequisites
2121

@@ -27,52 +27,71 @@ In the following sections, you'll use the Azure CLI to assign roles, and obtain
2727
../../cognitive-services-custom-subdomains.md)
2828

2929
- Azure CLI - [Installation Guide](/cli/azure/install-azure-cli)
30-
- The following Python libraries: os, requests, json
30+
- The following Python libraries: os, requests, json, openai, azure-identity
31+
32+
## Assign yourself to the Cognitive Services User role
33+
34+
Assign yourself the [Cognitive Services User](role-based-access-control.md#cognitive-services-contributor) role to allow you to use your account to make Azure OpenAI API calls rather than having to use key-based auth. After you make this change it can take up to 5 minutes before the change takes effect.
3135

3236
## Sign into the Azure CLI
3337

34-
To sign-in to the Azure CLI, run the following command and complete the sign-in. You may need to do it again if your session has been idle for too long.
38+
To sign-in to the Azure CLI, run the following command and complete the sign-in. You might need to do it again if your session has been idle for too long.
3539

3640
```azurecli
3741
az login
3842
```
3943

40-
## Assign yourself to the Cognitive Services User role
41-
42-
Assigning yourself to the "Cognitive Services User" role will allow you to use your account for access to the specific Azure AI services resource.
43-
44-
1. Get your user information
45-
46-
```azurecli
47-
export user=$(az account show --query "user.name" -o tsv)
48-
```
44+
## Chat Completions
45+
46+
```python
47+
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
48+
from openai import AzureOpenAI
49+
50+
token_provider = get_bearer_token_provider(
51+
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
52+
)
53+
54+
client = AzureOpenAI(
55+
api_version="2024-02-15-preview",
56+
azure_endpoint="https://{your-custom-endpoint}.openai.azure.com/",
57+
azure_ad_token_provider=token_provider
58+
)
59+
60+
response = client.chat.completions.create(
61+
model="gpt-35-turbo-0125", # model = "deployment_name".
62+
messages=[
63+
{"role": "system", "content": "You are a helpful assistant."},
64+
{"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},
65+
{"role": "assistant", "content": "Yes, customer managed keys are supported by Azure OpenAI."},
66+
{"role": "user", "content": "Do other Azure AI services support this too?"}
67+
]
68+
)
69+
70+
print(response.choices[0].message.content)
71+
```
4972

50-
2. Assign yourself to “Cognitive Services User” role.
73+
## Querying Azure OpenAI with the control plane API
5174

52-
```azurecli
53-
export resourceId=$(az group show -g $RG --query "id" -o tsv)
54-
az role assignment create --role "Cognitive Services User" --assignee $user --scope $resourceId
55-
```
75+
```python
76+
import requests
77+
import json
78+
from azure.identity import DefaultAzureCredential
5679

57-
> [!NOTE]
58-
> Role assignment change will take ~5 mins to become effective.
80+
region = "eastus"
81+
token_credential = DefaultAzureCredential()
82+
subscriptionId = "{YOUR-SUBSCRIPTION-ID}"
5983

60-
3. Acquire a Microsoft Entra access token. Access tokens expire in one hour. you'll then need to acquire another one.
6184

62-
```azurecli
63-
export accessToken=$(az account get-access-token --resource https://cognitiveservices.azure.com --query "accessToken" -o tsv)
64-
```
85+
token = token_credential.get_token('https://management.azure.com/.default')
86+
headers = {'Authorization': 'Bearer ' + token.token}
6587

66-
4. Make an API call
88+
url = f"https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.CognitiveServices/locations/{region}/models?api-version=2023-05-01"
6789

68-
Use the access token to authorize your API call by setting the `Authorization` header value.
90+
response = requests.get(url, headers=headers)
6991

92+
data = json.loads(response.text)
7093

71-
```bash
72-
curl ${endpoint%/}/openai/deployments/YOUR_DEPLOYMENT_NAME/completions?api-version=2023-05-15 \
73-
-H "Content-Type: application/json" \
74-
-H "Authorization: Bearer $accessToken" \
75-
-d '{ "prompt": "Once upon a time" }'
94+
print(json.dumps(data, indent=4))
7695
```
7796

7897
## Authorize access to managed identities

0 commit comments

Comments
 (0)