Aad Integration Example Sdk
Azure OpenAI Service API with OpenAI Python SDK
This notebook shows how to use the Azure OpenAI (AOAI) Service API with the OpenAI Python SDK.
Prerequisites
- Setup for Azure Active Directory (AAD) authentication.
- A Python environment setup with all the requirements.
- See the setup_python_env.md page for instructions on setting up your environment.
Configuring OpenAI SDK
The OpenAI SDK can be configured via config file.
| SDK Variable | Description |
|---|---|
api_type | API Type. Use azure_ad for AAD authentication. |
api_version | API Version. You can use 2022-12-01. |
api_base | Base URL for the API. This is the endpoint URL for your team. |
Setup Parameters
Here we will load the configurations from config.json file to setup deployment_name, openai.api_base and openai.api_version.
Set up AAD authentication
DefaultAzureCredential can read your credentials from the environment after doing az login.
In VS Code, you can use the Azure Account extension to log in with your Azure account. If you are running this notebook in VS Code, be sure to restart VS Code after you do az login.
This article gives details on what identity DefaultAzureCredential uses: https://docs.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python
If you get an error similar to the following, you can try using AzureCliCredential instead of DefaultAzureCredential:
DefaultAzureCredential failed to retrieve a token from the included credentials. Attempted credentials: EnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured. Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot.this issue. ManagedIdentityCredential: ManagedIdentityCredential authentication unavailable, no response from the IMDS endpoint. SharedTokenCacheCredential: Azure Active Directory error '(invalid_scope) AADSTS70011: The provided request must include a 'scope' input parameter. The provided value for the input parameter 'scope' is not valid. The scope https://cognitiveservices.azure.com is not valid. The scope format is invalid. Scope must be in a valid URI form <https://example/scope> or a valid Guid <guid/scope>.
Define method to get a prompt completion using AAD authentication
The refresh_token function below is used to get a new token when the current token expires. The refresh_token method is called by the get_completion to get the token if it is not already set or if the token has expired.
Test the API
The payload parameters in the get_completion call are for illustration only and can be changed for your use case as described in the reference.
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"text": "\nthere was a little girl named Emma. She lived in a small village with her parents and two brothers. Emma was a very curious girl and loved to explore the world around her.\n\nOne day, Emma decided to go on an adventure. She packed her bag with food and water and set off into the forest. As she walked, she noticed many different animals and plants. She also saw a lot of strange and mysterious things.\n\nEmma eventually came across a small cottage in the middle of the forest. She knocked on the door and a kindly old woman answered. The woman welcomed Emma inside and offered her a warm meal and a place to sleep.\n\nThe old woman told Emma about the magical creatures that lived in the forest. She also told her about a powerful wizard who lived nearby. Emma was so excited to learn about all of these things and she decided to stay with the old woman for a while.\n\nThe old woman taught Emma many things about the forest and its creatures. She also gave Emma a magical necklace that would protect her from harm.\n\nEmma thanked the old woman for her kindness and hospitality and set off on her journey. She explored the forest and encountered many more magical creatures. Eventually, she made it to the wizard's castle and was granted three wishes.\n\nEmma used her wishes to help her family and the people of her village. She also used her wishes to protect the magical creatures of the forest.\n\nEmma's adventure changed her life forever. She returned home with a newfound appreciation for the world around her and a newfound respect for the magical creatures that lived in the forest."
}
],
"created": 1680086585,
"id": "cmpl-6zNYfdtf0KC5ArhdPJsUctgI7Ng3C",
"model": "text-davinci-003",
"object": "text_completion",
"usage": {
"completion_tokens": 333,
"prompt_tokens": 5,
"total_tokens": 338
}
}