本页提供有关受支持的身份验证方法和客户端的信息,还介绍用于使用服务连接器将 Azure OpenAI 服务连接到其他云服务的示例代码。 本页面还列出创建服务连接时获取的默认环境变量名称和值。
受支持的计算服务
服务连接器可用于将以下计算服务连接到 Azure OpenAI 服务:
- Azure 应用程序服务
- Azure Container Apps
- Azure Functions
- Azure Kubernetes 服务 (AKS)
- Azure Spring Apps
受支持的身份验证类型和客户端类型
下表显示了使用服务连接器将计算服务连接到 Azure OpenAI 服务时受支持的身份验证方法和客户端的组合。 “是”表示支持该组合,“否”表示不支持该组合。
客户端类型 |
系统分配的托管标识 |
用户分配的托管标识 |
机密/连接字符串 |
服务主体 |
.NET |
是 |
是 |
是 |
是 |
Java |
是 |
是 |
是 |
是 |
Node.js |
是 |
是 |
是 |
是 |
Python |
是 |
是 |
是 |
是 |
无 |
是 |
是 |
是 |
是 |
此表支持表中客户端类型和身份验证方法的所有组合都受到支持。 所有客户端类型都可通过服务连接器使用任何身份验证方法连接到 Azure OpenAI 服务。
默认环境变量名称或应用程序属性和示例代码
使用以下连接详细信息将计算服务连接到 Azure OpenAI 服务。 有关命名约定的详细信息,请参阅服务连接器深度解析一文。
系统分配的托管标识
默认环境变量名称 |
说明 |
示例值 |
AZURE_OPENAI_BASE |
Azure OpenAI 服务终结点 |
https://<Azure-OpenAI-name>.openai.azure.com/ |
代码示例
请参阅下面的步骤和代码,以使用系统分配的托管标识连接到 Azure OpenAI 服务。
安装依赖项。
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
使用 Azure 标识库进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
using Azure.AI.OpenAI;
using Azure.Identity;
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_BASE");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_OPENAI_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
AzureOpenAIClient openAIClient = new(
new Uri(endpoint),
credential
);
将以下依赖项添加到 pom.xml 文件:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-openai</artifactId>
<version>1.0.0-beta.6</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.11.4</version>
</dependency>
使用 azure-identity
进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_OPENAI_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_OPENAI_CLIENTID"))
// .clientSecret(System.getenv("AZURE_OPENAI_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_OPENAI_TENANTID"))
// .build();
String endpoint = System.getenv("AZURE_OPENAI_BASE");
OpenAIClient client = new OpenAIClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildClient();
- 安装依赖项。
pip install openai
pip install azure-identity
- 使用
azure-identity
进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
import os
import OpenAI
from azure.identity import ManagedIdentityCredential, ClientSecretCredential, get_bearer_token_provider
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_OPENAI_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_OPENAI_TENANTID')
# client_id = os.getenv('AZURE_OPENAI_CLIENTID')
# client_secret = os.getenv('AZURE_OPENAI_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
token_provider = get_bearer_token_provider(
cred, "https://cognitiveservices.azure.com/.default"
)
endpoint = os.getenv('AZURE_OPENAI_BASE')
client = AzureOpenAI(
api_version="2024-02-15-preview",
azure_endpoint=endpoint,
azure_ad_token_provider=token_provider
)
安装依赖项。
npm install --save @azure/identity
npm install @azure/openai
使用 @azure/identity
进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_OPENAI_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_OPENAI_TENANTID;
// const clientId = process.env.AZURE_OPENAI_CLIENTID;
// const clientSecret = process.env.AZURE_OPENAI_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const endpoint = process.env.AZURE_OPENAI_BASE;
const client = new OpenAIClient(endpoint, credential);
用户分配的托管标识
默认环境变量名称 |
说明 |
示例值 |
AZURE_OPENAI_BASE |
Azure OpenAI 服务终结点 |
https://<Azure-OpenAI-name>.openai.azure.com/ |
AZURE_OPENAI_CLIENTID |
客户端 ID |
<client-ID> |
代码示例
请参阅下面的步骤和代码,以使用用户分配的托管标识连接到 Azure OpenAI 服务。
安装依赖项。
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
使用 Azure 标识库进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
using Azure.AI.OpenAI;
using Azure.Identity;
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_BASE");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_OPENAI_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
AzureOpenAIClient openAIClient = new(
new Uri(endpoint),
credential
);
将以下依赖项添加到 pom.xml 文件:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-openai</artifactId>
<version>1.0.0-beta.6</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.11.4</version>
</dependency>
使用 azure-identity
进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_OPENAI_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_OPENAI_CLIENTID"))
// .clientSecret(System.getenv("AZURE_OPENAI_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_OPENAI_TENANTID"))
// .build();
String endpoint = System.getenv("AZURE_OPENAI_BASE");
OpenAIClient client = new OpenAIClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildClient();
- 安装依赖项。
pip install openai
pip install azure-identity
- 使用
azure-identity
进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
import os
import OpenAI
from azure.identity import ManagedIdentityCredential, ClientSecretCredential, get_bearer_token_provider
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_OPENAI_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_OPENAI_TENANTID')
# client_id = os.getenv('AZURE_OPENAI_CLIENTID')
# client_secret = os.getenv('AZURE_OPENAI_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
token_provider = get_bearer_token_provider(
cred, "https://cognitiveservices.azure.com/.default"
)
endpoint = os.getenv('AZURE_OPENAI_BASE')
client = AzureOpenAI(
api_version="2024-02-15-preview",
azure_endpoint=endpoint,
azure_ad_token_provider=token_provider
)
安装依赖项。
npm install --save @azure/identity
npm install @azure/openai
使用 @azure/identity
进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_OPENAI_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_OPENAI_TENANTID;
// const clientId = process.env.AZURE_OPENAI_CLIENTID;
// const clientSecret = process.env.AZURE_OPENAI_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const endpoint = process.env.AZURE_OPENAI_BASE;
const client = new OpenAIClient(endpoint, credential);
连接字符串
默认环境变量名称 |
说明 |
示例值 |
AZURE_OPENAI_BASE |
Azure OpenAI 服务终结点 |
https://<Azure-OpenAI-name>.openai.azure.com/ |
AZURE_OPENAI_KEY |
Azure OpenAI 服务 API 密钥 |
<api-key> |
代码示例
请参阅下面的步骤和代码,以使用连接字符串连接到 Azure OpenAI 服务。
安装以下依赖项。
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Core --version 1.40.0
从服务连接器添加的环境变量中获取 Azure OpenAI 终结点和 API 密钥。
using Azure.AI.OpenAI;
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_BASE")
string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
AzureOpenAIClient openAIClient = new(
new Uri(endpoint),
new AzureKeyCredential(key));
- 将以下依赖项添加到 pom.xml 文件:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.49.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-openai</artifactId>
<version>1.0.0-beta.6</version>
</dependency>
- 从服务连接器添加的环境变量中获取 Azure OpenAI 终结点和 API 密钥。
String endpoint = System.getenv("AZURE_OPENAI_BASE");
String key = System.getenv("AZURE_OPENAI_KEY");
OpenAIClient client = new OpenAIClientBuilder()
.credential(new AzureKeyCredential(key))
.endpoint(endpoint)
.buildClient();
- 安装以下依赖项。
pip install openai
pip install azure-core
- 从服务连接器添加的环境变量中获取 Azure OpenAI 终结点和 API 密钥。
import os
from openai import AzureOpenAI
from azure.core.credentials import AzureKeyCredential
key = os.environ['AZURE_OPENAI_KEY']
endpoint = os.environ['AZURE_OPENAI_BASE']
client = AzureOpenAI(
api_version="2024-02-15-preview",
azure_endpoint=endpoint,
api_key=key
)
安装以下依赖项。
npm install @azure/openai
npm install @azure/core-auth
从服务连接器添加的环境变量中获取 Azure OpenAI 终结点和 API 密钥。
import { OpenAIClient } from "@azure/openai";
import { AzureKeyCredential } from "@azure/core-auth";
const endpoint = process.env.AZURE_OPENAI_BASE;
const credential = new AzureKeyCredential(process.env.AZURE_OPENAI_KEY);
const client = new OpenAIClient(endpoint, credential);
服务主体
默认环境变量名称 |
说明 |
示例值 |
AZURE_OPENAI_BASE |
Azure OpenAI 服务终结点 |
https://<Azure-OpenAI-name>.openai.azure.com/ |
AZURE_OPENAI_CLIENTID |
客户端 ID |
<client-ID> |
AZURE_OPENAI_CLIENTSECRET |
客户端密码 |
<client-secret> |
AZURE_OPENAI_TENANTID |
租户 ID |
<tenant-ID> |
代码示例
请参阅下面的步骤和代码,以使用服务主体连接到 Azure OpenAI 服务。
安装依赖项。
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
使用 Azure 标识库进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
using Azure.AI.OpenAI;
using Azure.Identity;
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_BASE");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_OPENAI_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
AzureOpenAIClient openAIClient = new(
new Uri(endpoint),
credential
);
将以下依赖项添加到 pom.xml 文件:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-openai</artifactId>
<version>1.0.0-beta.6</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.11.4</version>
</dependency>
使用 azure-identity
进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_OPENAI_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_OPENAI_CLIENTID"))
// .clientSecret(System.getenv("AZURE_OPENAI_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_OPENAI_TENANTID"))
// .build();
String endpoint = System.getenv("AZURE_OPENAI_BASE");
OpenAIClient client = new OpenAIClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildClient();
- 安装依赖项。
pip install openai
pip install azure-identity
- 使用
azure-identity
进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
import os
import OpenAI
from azure.identity import ManagedIdentityCredential, ClientSecretCredential, get_bearer_token_provider
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_OPENAI_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_OPENAI_TENANTID')
# client_id = os.getenv('AZURE_OPENAI_CLIENTID')
# client_secret = os.getenv('AZURE_OPENAI_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
token_provider = get_bearer_token_provider(
cred, "https://cognitiveservices.azure.com/.default"
)
endpoint = os.getenv('AZURE_OPENAI_BASE')
client = AzureOpenAI(
api_version="2024-02-15-preview",
azure_endpoint=endpoint,
azure_ad_token_provider=token_provider
)
安装依赖项。
npm install --save @azure/identity
npm install @azure/openai
使用 @azure/identity
进行身份验证,并从服务连接器添加的环境变量中获取 Azure OpenAI 终结点。 使用下面的代码时,请对要使用的身份验证类型的代码片段的一部分取消评论。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_OPENAI_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_OPENAI_TENANTID;
// const clientId = process.env.AZURE_OPENAI_CLIENTID;
// const clientSecret = process.env.AZURE_OPENAI_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const endpoint = process.env.AZURE_OPENAI_BASE;
const client = new OpenAIClient(endpoint, credential);
相关内容