此頁面提供支援的驗證方法和用戶端的相關資訊。 它提供範例程式碼,可用來使用服務連接器將計算服務連線到 Foundry 模型中的 Azure OpenAI。 此頁面也會列出建立服務連線時取得的預設環境變數名稱和值。
支援的計算服務
服務連接器可用來將下列計算服務連線至 Azure OpenAI:
- Azure App Service
- Azure 容器應用程式
- Azure Functions
- Azure Kubernetes Service (AKS)
- Azure Spring 應用程式
支援的驗證類型和用戶端類型
下表顯示使用服務連接器將計算服務連線到 Azure OpenAI 時,支援哪些驗證方法和用戶端組合。 「是」表示支援組合,而「否」表示不支援。
| 用戶端類型 |
系統指派的受控識別 |
使用者指派的受控識別 |
祕密/連接字串 |
服務主體 |
| .NET |
是的 |
是的 |
是的 |
是的 |
| JAVA |
是的 |
是的 |
是的 |
是的 |
| Node.js |
是的 |
是的 |
是的 |
是的 |
| Python |
是的 |
是的 |
是的 |
是的 |
| None |
是的 |
是的 |
是的 |
是的 |
下表指出支源表格中所有用戶端類型和驗證方法組合。 所有用戶端類型都可以使用任何驗證方法,使用服務連接器連線到 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 |
您的用戶端識別碼 |
<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 |
您的用戶端識別碼 |
<client-ID> |
| AZURE_OPENAI_CLIENTSECRET |
您的用戶端密碼 |
<client-secret> |
| AZURE_OPENAI_TENANTID |
您的租用戶識別碼 |
<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);
相關內容