This repository contains code samples of different methods to authenticate to the Microsoft Foundry (and Azure OpenAI Service and Azure AI Service) using the Python openai library with both the legacy API and the Azure OpenAI v1 API, Python Azure AI Inference library, Python langchain-openai library, and Python requests library. Each example uses the ChatCompletions operation.
The samples assume the use of the python-dotenv library for storing sensitive variables.
The following authentication examples are provided:
- Managed Identity with Azure AI Inference SDK
- Service Principal with Azure AI Inference SDK
- API Key with LangChain
- Service Principal with LangChain
- API Key with OpenAI SDK with legacy API
- Managed Identity with OpenAI SDK with legacy API
- Service Principal with OpenAI SDK with legacy API using Client Credentials Flow)
- Service Principal with OpenAI SDK with legacy API using OBO Flow)
- API Key with OpenAI SDK with v1 API
- Managed Identity with OpenAI SDK with v1 API
- Service Principal with OpenAI SDK with v1 API using Client Credentials Flow)
- Service Principal with OpenAI SDK with v1 API using OBO Flow)
- API Key with REST API
- Service Principal with REST API
In each example you will find a requirements.txt file with the required libraries and a sample environmental variables file that can be used with the python-dotenv library. You will need to rename it from .env-sample to .env.
When using the on-behalf-of flow, you will need to property configure the application registration to support public client flows.