Created
November 23, 2023 08:09
-
-
Save csiebler/7e8f1e1769660c8f4b1f636b34b43448 to your computer and use it in GitHub Desktop.
Example to use a single AAD token for multiple Azure OpenAI resources
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
from datetime import datetime | |
from azure.identity import DefaultAzureCredential, ChainedTokenCredential, ManagedIdentityCredential | |
# see https://learn.microsoft.com/en-us/azure/developer/python/sdk/authentication-on-premises-apps?tabs=azure-portal | |
# this Service Principal has the Cognitive Services OpenAI User role on the AOAI resources | |
os.environ["AZURE_CLIENT_ID"] = "xxxxx" | |
os.environ["AZURE_TENANT_ID"] = "xxxxx" | |
os.environ["AZURE_CLIENT_SECRET"] = "xxxxx" | |
# Define strategy which potential authentication methods should be tried to gain an access token | |
credential = ChainedTokenCredential(ManagedIdentityCredential(), DefaultAzureCredential()) | |
access_token = credential.get_token("https://cognitiveservices.azure.com/.default") | |
# convert unix timestamp access_token.expires_on to datetime | |
print(datetime.fromtimestamp(access_token.expires_on)) | |
token = access_token.token | |
endpoints = [ | |
"https://xxxxx.openai.azure.com/", | |
"https://xxxxx.openai.azure.com/" | |
] | |
API_VERSION = "2023-05-15" | |
DEPLOYMENT_NAME = "gpt-35-turbo" | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {token}" | |
} | |
data = {"max_tokens": 1, "messages":[{"role": "system", "content": ""},{"role": "user", "content": "Hi"}]} | |
for endpoint in endpoints: | |
url = f"{endpoint}/openai/deployments/{DEPLOYMENT_NAME}/chat/completions?api-version={API_VERSION}" | |
response = requests.post(url, headers=headers, json=data) | |
print(response.json()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment