使用 AAD 身份验证的 Azure OpenAI#

本指南将向您展示如何使用 Azure Active Directory (AAD) 身份验证的 Azure OpenAI 客户端。

使用的标识必须分配有认知服务 OpenAI 用户角色。

安装 Azure 标识客户端#

Azure 标识客户端用于使用 Azure Active Directory 进行身份验证。

pip install azure-identity

使用模型客户端#

from autogen_ext.models.openai import AzureOpenAIChatCompletionClient
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

# Create the token provider
token_provider = get_bearer_token_provider(
    DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)

client = AzureOpenAIChatCompletionClient(
    azure_deployment="{your-azure-deployment}",
    model="{model-name, such as gpt-4o}",
    api_version="2024-02-01",
    azure_endpoint="https://{your-custom-endpoint}.openai.azure.com/",
    azure_ad_token_provider=token_provider,
)

注意

有关如何直接使用 Azure 客户端或了解更多信息,请参见此处