Microsoft Foundry Models は、1 つのエンドポイントと資格情報のセットを通じて、多くのプロバイダーからさまざまなモデルにアクセスできます。 この機能を使用すると、モデルを切り替えて、コードを変更せずにアプリケーションで使用できます。
Foundry リソースには、多数のモデル デプロイを含めることができます。 モデルデプロイで実行された推論に対してのみ課金されます。 デプロイはリソースAzureであるため、Azure ポリシーの対象となります。
Foundry リソースの API キーを使用して推論要求を認証できます。 API キーは簡単に設定できますが、リソースへの完全なアクセス権が付与され、特定のユーザーやアクションにスコープを設定することは困難であり、セキュリティを維持するには手動ローテーションが必要です。 運用環境のワークロードの場合は、代わりにMicrosoft Entra IDでキーレス認証を使用します。
pip を使用して openai パッケージをインストールします。
pip install openai --upgrade
Azure OpenAI v1 エンドポイントを指すクライアントを作成し、応答を生成します。
/openai/v1/ ルートでは暗黙的なバージョン管理が使用されるため、api-versionは渡されません。
model フィールドにデプロイ名を渡します。
import os
from openai import OpenAI
client = OpenAI(
base_url="https://<resource>.openai.azure.com/openai/v1/",
api_key=os.environ["AZURE_INFERENCE_CREDENTIAL"],
)
response = client.responses.create(
model="deepseek-v3-0324", # Replace with your model deployment name.
input="Explain the Riemann hypothesis in one paragraph.",
)
print(response.output_text)
npm を使用して openai パッケージをインストールします。
npm install openai
Azure OpenAI v1 エンドポイントを指すクライアントを作成し、応答を生成します。
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://<resource>.openai.azure.com/openai/v1/",
apiKey: process.env.AZURE_INFERENCE_CREDENTIAL,
});
const response = await client.responses.create({
model: "deepseek-v3-0324", // Replace with your model deployment name.
input: "Explain the Riemann hypothesis in one paragraph.",
});
console.log(response.output_text);
OpenAI ライブラリをインストールします。
dotnet add package OpenAI
Azure OpenAI v1 エンドポイントを指すクライアントを作成し、応答を生成します。
using System.ClientModel;
using OpenAI;
using OpenAI.Responses;
OpenAIClient client = new(
new ApiKeyCredential(Environment.GetEnvironmentVariable("AZURE_INFERENCE_CREDENTIAL")),
new OpenAIClientOptions
{
Endpoint = new Uri("https://<resource>.openai.azure.com/openai/v1/")
});
OpenAIResponseClient responseClient = client.GetResponsesClient("deepseek-v3-0324");
OpenAIResponse response = responseClient.CreateResponse(
"Explain the Riemann hypothesis in one paragraph.");
Console.WriteLine(response.GetOutputText());
OpenAI Java SDK をプロジェクトに追加します。
OpenAI Java リポジトリで最新バージョンを確認します。
Azure OpenAI v1 エンドポイントを指すクライアントを作成し、応答を生成します。
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
OpenAIClient client = OpenAIOkHttpClient.builder()
.baseUrl("https://<resource>.openai.azure.com/openai/v1/")
.apiKey(System.getenv("AZURE_INFERENCE_CREDENTIAL"))
.build();
Response response = client.responses().create(
ResponseCreateParams.builder()
.model("deepseek-v3-0324") // Replace with your model deployment name.
.input("Explain the Riemann hypothesis in one paragraph.")
.build());
// The Responses API has no single output-text accessor; concatenate the output items.
response.output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(outputText -> System.out.println(outputText.text()));
v1 ルートに直接要求を送信します。
/openai/v1/ パスでは暗黙的なバージョン管理が使用されるため、api-versionクエリ パラメーターは含まれません。
Authorization ヘッダーにベアラー トークンとしてキーを渡します。
curl -X POST https://<resource>.openai.azure.com/openai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_INFERENCE_CREDENTIAL" \
-d '{
"model": "deepseek-v3-0324",
"input": "Explain the Riemann hypothesis in one paragraph."
}'
デプロイされた Foundry モデルでは、Microsoft Entra IDを使用したキーレス承認がサポートされます。 キーレス承認により、セキュリティが強化され、ユーザー エクスペリエンスが簡素化され、運用の複雑さが軽減され、堅牢なコンプライアンス サポートが提供されます。 組織がセキュリティで保護されたスケーラブルな ID 管理ソリューションを使用している場合は、キーレス承認を使用します。
pip のようなパッケージ マネージャーを使用して OpenAI SDK をインストールします。
pip install openai
Microsoft Entra ID 認証の場合は、次もインストールします。
pip install azure-identity
パッケージを使用してモデルを消費します。 次の例では、Microsoft Entra IDとモデルデプロイを使用して、クライアントを作成し、Responses API のテスト呼び出しを行う方法を示します。
<resource>を Foundry リソース名に置き換えます。 Azure ポータルで、または az cognitiveservices account list を実行して検索します。
deepseek-v3-0324を実際のデプロイ名に置き換えます。
from openai import OpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(),
"https://ai.azure.com/.default"
)
client = OpenAI(
base_url="https://<resource>.openai.azure.com/openai/v1/",
api_key=token_provider,
)
response = client.responses.create(
model="deepseek-v3-0324", # Replace with your model deployment name.
input="What is Azure AI?",
)
print(response.output_text)
予期される出力
Azure AI is a comprehensive suite of artificial intelligence services and tools from Microsoft that enables developers to build intelligent applications. It includes services for natural language processing, computer vision, speech recognition, and machine learning capabilities.
リファレンス: OpenAI Python SDK および DefaultAzureCredential クラス。
OpenAI SDK をインストールします。
dotnet add package OpenAI
Microsoft Entra ID認証の場合は、Azure.Identity パッケージもインストールします。
dotnet add package Azure.Identity
次に、パッケージを使用してモデルを実行します。 次の例では、Microsoft Entra IDとモデルデプロイを使用して、クライアントを作成し、Responses API のテスト呼び出しを行う方法を示します。
<resource> を Foundry リソース名に置き換えます (Azure ポータルで見つけます)。
deepseek-v3-0324を実際のデプロイ名に置き換えます。
using Azure.Identity;
using OpenAI;
using OpenAI.Responses;
using System.ClientModel.Primitives;
#pragma warning disable OPENAI001
BearerTokenPolicy tokenPolicy = new(
new DefaultAzureCredential(),
"https://ai.azure.com/.default"
);
OpenAIResponseClient client = new(
model: "deepseek-v3-0324", // Replace with your model deployment name.
authenticationPolicy: tokenPolicy,
options: new OpenAIClientOptions()
{
Endpoint = new Uri("https://<resource>.openai.azure.com/openai/v1/")
}
);
OpenAIResponse response = client.CreateResponse("What is Azure AI?");
Console.WriteLine(response.GetOutputText());
予想される出力:
Azure AI is a comprehensive suite of artificial intelligence services and tools from Microsoft that enables developers to build intelligent applications. It includes services for natural language processing, computer vision, speech recognition, and machine learning capabilities.
リファレンス: OpenAI .NET SDK および DefaultAzureCredential クラス。
npm を使用して OpenAI SDK をインストールします。
npm install openai
Microsoft Entra ID 認証の場合は、次もインストールします。
npm install @azure/identity
次に、パッケージを使用してモデルを実行します。 次の例では、Microsoft Entra IDとモデルデプロイを使用して、クライアントを作成し、Responses API のテスト呼び出しを行う方法を示します。
<resource> を Foundry リソース名に置き換えます (Azure ポータルで検索するか、az cognitiveservices account list を実行します)。
deepseek-v3-0324を実際のデプロイ名に置き換えます。
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
import { OpenAI } from "openai";
const tokenProvider = getBearerTokenProvider(
new DefaultAzureCredential(),
'https://ai.azure.com/.default'
);
const client = new OpenAI({
baseURL: "https://<resource>.openai.azure.com/openai/v1/",
apiKey: tokenProvider
});
const response = await client.responses.create({
model: "deepseek-v3-0324", // Replace with your model deployment name.
input: "What is Azure AI?"
});
console.log(response.output_text);
予想される出力:
Azure AI is a comprehensive suite of artificial intelligence services and tools from Microsoft that enables developers to build intelligent applications. It includes services for natural language processing, computer vision, speech recognition, and machine learning capabilities.
リファレンス: OpenAI Node.js SDK と DefaultAzureCredential クラス。
OpenAI SDK をプロジェクトに追加します。 最新バージョンとインストール手順については、OpenAI Java GitHub リポジトリを確認してください。
Microsoft Entra ID の認証の場合は、次を追加してください。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.18.0</version>
</dependency>
次に、パッケージを使用してモデルを実行します。 次の例では、Microsoft Entra IDとモデルデプロイを使用して、クライアントを作成し、Responses API のテスト呼び出しを行う方法を示します。
<resource> を Foundry リソース名に置き換えます (Azure ポータルで見つけます)。
deepseek-v3-0324を実際のデプロイ名に置き換えます。
import com.azure.identity.AuthenticationUtil;
import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.credential.BearerTokenCredential;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
DefaultAzureCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
OpenAIClient client = OpenAIOkHttpClient.builder()
.baseUrl("https://<resource>.openai.azure.com/openai/v1/")
.credential(BearerTokenCredential.create(
AuthenticationUtil.getBearerTokenSupplier(
tokenCredential,
"https://ai.azure.com/.default"
)
))
.build();
ResponseCreateParams params = ResponseCreateParams.builder()
.model("deepseek-v3-0324") // Replace with your model deployment name.
.input("What is Azure AI?")
.build();
Response response = client.responses().create(params);
// The Responses API has no single output-text accessor; concatenate the output items.
response.output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(outputText -> System.out.println(outputText.text()));
予想される出力:
Azure AI is a comprehensive suite of artificial intelligence services and tools from Microsoft that enables developers to build intelligent applications. It includes services for natural language processing, computer vision, speech recognition, and machine learning capabilities.
リファレンス: OpenAI Java SDK および DefaultAzureCredential クラス。
参照セクションの API 設計を調べて、使用可能なパラメーターを確認します。
Authorization ヘッダーに認証 (ベアラー) トークンを挿入します。
たとえば、 Responses API リファレンス セクションでは、 /responses ルートを使用して予測を生成する方法について詳しく説明します。
/openai/v1/パスは、URL のルートに含まれています。
要求
<resource> を Foundry リソース名に置き換えます (Azure ポータルで検索するか、az cognitiveservices account list を実行します)。
deepseek-v3-0324を実際のデプロイ名に置き換えます。
ベース URL は、 https://<resource>.openai.azure.com/openai/v1/ 形式と https://<resource>.services.ai.azure.com/openai/v1/ 形式の両方を受け入れます。
curl -X POST https://<resource>.openai.azure.com/openai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "deepseek-v3-0324",
"input": "Explain what the bitter lesson is?"
}'
応答
認証が成功した場合は、応答結果が応答本文に含まれる 200 OK 応答を受け取ります。
{
"id": "resp_...",
"object": "response",
"created_at": 1738368234,
"model": "deepseek-v3-0324",
"status": "completed",
"output": [
{
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "The bitter lesson refers to a key insight in AI research that emphasizes the importance of general-purpose learning methods that leverage computation, rather than human-designed domain-specific approaches. It suggests that methods which scale with increased computation tend to be more effective in the long run."
}
]
}
],
"usage": {
"input_tokens": 28,
"output_tokens": 52,
"total_tokens": 80
}
}
トークンはスコープ https://ai.azure.com/.defaultで発行する必要があります。
テスト目的で、ユーザー アカウントの有効なトークンを取得する最も簡単な方法は、Azure CLIを使用することです。 コンソールで、次のAzure CLI コマンドを実行してサインインし、トークンを要求します。
az login
az account get-access-token --resource https://ai.azure.com --query "accessToken" --output tsv
このコマンドは、 $AZURE_OPENAI_AUTH_TOKEN 環境変数に格納できるアクセス トークンを出力します。
リファレンス: Responses API