Skip to content

Instantly share code, notes, and snippets.

@csiebler
Created November 23, 2023 08:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save csiebler/7e8f1e1769660c8f4b1f636b34b43448 to your computer and use it in GitHub Desktop.
Save csiebler/7e8f1e1769660c8f4b1f636b34b43448 to your computer and use it in GitHub Desktop.
Example to use a single AAD token for multiple Azure OpenAI resources
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