このページでは、サポートされている認証方法およびクライアントに関する情報と、サービス コネクタを使用して Azure OpenAI Service を他のクラウド サービスに接続するためのサンプル コードを示します。 このページには、サービス接続を作成するときに取得する既定の環境変数の名前と値も示されています。
サポートされているコンピューティング サービス
サービス コネクタを使用して、次のコンピューティング サービスを Azure OpenAI Service に接続できます。
- Azure App Service
- Azure Container Apps
- Azure Functions
- Azure Kubernetes Service (AKS)
- Azure Spring Apps
サポートされている認証の種類とクライアントの種類
以下の表は、サービス コネクタを使用してコンピューティング サービスを Azure OpenAI Service に接続する場合にサポートされている認証方法とクライアントの組み合わせを示しています。 "はい" はその組み合わせがサポートされていることを示し、"いいえ" はサポートされていないことを示します。
クライアント タイプ |
システム割り当てマネージド ID |
ユーザー割り当てマネージド ID |
シークレット/接続文字列 |
サービス プリンシパル |
.NET |
はい |
はい |
はい |
はい |
Java |
はい |
はい |
はい |
はい |
Node.js |
はい |
はい |
はい |
はい |
Python |
はい |
はい |
はい |
はい |
なし |
有効 |
はい |
はい |
はい |
この表は、表内のクライアントの種類と認証方法のすべての組み合わせがサポートされていることを示しています。 すべてのクライアントの種類では、サービス コネクタを使って Azure OpenAI Service に接続するために、任意の認証方法を使用できます。
既定の環境変数名またはアプリケーション プロパティとサンプル コード
コンピューティング サービスを Azure OpenAI Service に接続するには、以下で示す接続の詳細を使います。 名前付け規則の詳細については、「Service Connector の内部構造」の記事を参照してください。
システム割り当てマネージド ID
既定の環境変数名 |
説明 |
サンプルの値 |
AZURE_OPENAI_BASE |
Azure OpenAI Service エンドポイント |
https://<Azure-OpenAI-name>.openai.azure.com/ |
サンプル コード
システム割り当てマネージド ID を使用して Azure OpenAI Service に接続するには、以下の手順とコードを参照してください。
依存関係をインストールします。
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
Azure ID ライブラリを使用して認証し、サービス コネクタによって追加された環境変数から 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);
ユーザー割り当てマネージド ID
既定の環境変数名 |
説明 |
サンプルの値 |
AZURE_OPENAI_BASE |
Azure OpenAI Service エンドポイント |
https://<Azure-OpenAI-name>.openai.azure.com/ |
AZURE_OPENAI_CLIENTID |
クライアント ID |
<client-ID> |
サンプル コード
ユーザー割り当てマネージド ID を使用して Azure OpenAI Service に接続するには、以下の手順とコードを参照してください。
依存関係をインストールします。
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
Azure ID ライブラリを使用して認証し、サービス コネクタによって追加された環境変数から 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 Service エンドポイント |
https://<Azure-OpenAI-name>.openai.azure.com/ |
AZURE_OPENAI_KEY |
Azure OpenAI Service API キー |
<api-key> |
サンプル コード
接続文字列を使用して Azure OpenAI Service に接続するには、以下の手順とコードを参照してください。
次の依存関係をインストールします。
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 Service エンドポイント |
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 Service に接続するには、以下の手順とコードを参照してください。
依存関係をインストールします。
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
Azure ID ライブラリを使用して認証し、サービス コネクタによって追加された環境変数から 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);
関連するコンテンツ