この記事では、Responses API を使用して、Microsoft AI、DeepSeek、Grok モデルなどの Foundry モデルのテキスト応答を生成する方法について説明します。 Responses API の使用をサポートする Foundry モデルの完全な一覧については、「 サポートされている Foundry モデル」を参照してください。
前提 条件
アプリケーションでデプロイされたモデルで Responses API を使用するには、次のものが必要です。
Azure サブスクリプション。
Foundry プロジェクト。 この種のプロジェクトは Foundry リソースで管理されます。 Foundry プロジェクトがない場合は、「Microsoft Foundry プロジェクトを作成する」を参照してください。
Foundry プロジェクトのエンドポイント URL。これは、 https://YOUR-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR_PROJECT_NAME形式です。
Foundry モデルのデプロイ (この記事で使用する DeepSeek-R1-0528 モデルなど)。 デプロイがまだない場合は、「 Foundry Models を追加して構成して、 モデル デプロイをリソースに追加する」を参照してください。
AI モデル スターター キットを使用する
この記事のコード スニペットは、 AI モデル スターター キットのコード スニペットです。 このスターター キットは、Foundry Models を呼び出すために必要な完全なクラウド インフラストラクチャとコードを、Responses API で安定した OpenAI ライブラリを使用して簡単に開始する方法として使用します。
Responses API を使用してテキストを生成する
このセクションのコードを使用して、Foundry Models の Responses API 呼び出しを行います。 コード サンプルでは、モデルを使用するクライアントを作成し、基本的な要求を送信します。
ヒント
Foundry ポータルでモデルをデプロイするときは、デプロイ名を割り当てます。 API 呼び出しの model パラメーターで、このデプロイ名 (モデル カタログ ID ではなく) を使用します。
Azure ID クライアント ライブラリを含むライブラリをインストールします。
pip install azure-identity
pip install -U openai
次のコードを使用して、プロジェクト ルートで OpenAI クライアント オブジェクトを構成し、デプロイを指定し、応答を生成します。
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from openai import OpenAI
project_endpoint = "https://YOUR-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR_PROJECT_NAME"
# Build the base URL: project_endpoint + /openai/v1 (no api-version needed)
base_url = project_endpoint.rstrip("/") + "/openai/v1"
# Use get_bearer_token_provider for automatic token refresh
credential = DefaultAzureCredential()
client = OpenAI(
base_url=base_url,
api_key=get_bearer_token_provider(credential, "https://ai.azure.com/.default"),
)
response = client.responses.create(
model="DeepSeek-R1-0528", # Replace with your deployment name, not the model ID
input="What are the top 3 benefits of cloud computing? Be concise.",
max_output_tokens=2000,
)
print(f"Response: {response.output_text}")
print(f"Status: {response.status}")
print(f"Output tokens: {response.usage.output_tokens}")
Azure ID クライアント ライブラリをインストールします。
dotnet add package Azure.Identity
dotnet add package OpenAI
次のコードを使用して、プロジェクト ルートで OpenAI クライアント オブジェクトを構成し、デプロイを指定し、応答を生成します。
using System.ClientModel;
using Azure.Identity;
using OpenAI;
using OpenAI.Responses;
var deploymentName = "DeepSeek-R1-0528"; // Replace with your deployment name, not the model ID
var project_endpoint = "https://YOUR-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR_PROJECT_NAME";
// Get EntraID token for keyless auth
var credential = new DefaultAzureCredential();
var token = await credential.GetTokenAsync(
new Azure.Core.TokenRequestContext(["https://ai.azure.com/.default"])
);
// Standard OpenAI client — no AzureOpenAI wrapper (no api-version needed with /v1 path)
var baseUrl = project_endpoint.TrimEnd('/') + "/openai/v1";
var client = new OpenAIClient(
new ApiKeyCredential(token.Token),
new OpenAIClientOptions { Endpoint = new Uri(baseUrl) });
// GetResponsesClient takes no parameter; model goes in CreateResponseOptions
var responseClient = client.GetResponsesClient(deploymentName);
var result = await responseClient.CreateResponseAsync(new CreateResponseOptions(
[ResponseItem.CreateUserMessageItem("What are the top 3 benefits of cloud computing? Be concise.")])
{ MaxOutputTokenCount = 500 }
);
Console.WriteLine($"Response: {result.Value.GetOutputText()}");
Console.WriteLine($"Status: {result.Value.Status}");
Console.WriteLine($"Output tokens: {result.Value.Usage.OutputTokenCount}");
DefaultAzureCredentialを使用する前に、Azure ID クライアント ライブラリをインストールします。
npm install @azure/identity
npm install openai
次のコードを使用して、プロジェクト ルートで OpenAI クライアント オブジェクトを構成し、デプロイを指定し、応答を生成します。
import OpenAI from "openai";
import { DefaultAzureCredential } from "@azure/identity";
async function getToken() {
const credential = new DefaultAzureCredential();
const tokenResponse = await credential.getToken(
"https://ai.azure.com/.default"
);
return tokenResponse.token;
}
async function main() {
const projectEndpoint = "https://YOUR-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR_PROJECT_NAME";
const deploymentName = "DeepSeek-R1-0528"; // Replace with your deployment name, not the model ID
const baseURL = projectEndpoint.replace(/\/+$/, "") + "/openai/v1";
const token = await getToken();
const client = new OpenAI({
baseURL,
apiKey: token,
});
const response = await client.responses.create({
model: deploymentName,
input: "What are the top 3 benefits of cloud computing? Be concise.",
max_output_tokens: 500,
});
console.log(`Response: ${response.output_text}`);
console.log(`Status: ${response.status}`);
console.log(`Output tokens: ${response.usage?.output_tokens}`);
}
main();
Microsoft Entra IDを使用した認証には、いくつかの初期セットアップが必要です。 まず、Azure ID クライアント ライブラリをインストールします。 このライブラリのインストール方法についての詳細は、Azure Identity クライアント ライブラリ for Javaを参照してください。
Azure ID クライアント ライブラリを追加します。
<dependencies>
<dependency>
<groupId>com.openai</groupId>
<artifactId>openai-java</artifactId>
<version>4.22.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.18.4</version>
</dependency>
</dependencies>
セットアップ後、使用する azure.identity から資格情報の種類を選択します。 たとえば、 DefaultAzureCredential を使用してクライアントを認証します。
DefaultAzureCredential は、実行中の環境で使用するのに最適な資格情報が見つかるため、最も簡単なオプションです。
次のコードを使用して、プロジェクト ルートで OpenAI クライアント オブジェクトを構成し、デプロイを指定し、応答を生成します。
import com.azure.core.credential.TokenRequestContext;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
public class Sample {
// Return the final assistant message text from a Responses API result.
static String getOutputText(Response response) {
var sb = new StringBuilder();
response.output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(outputText -> sb.append(outputText.text()));
return sb.toString();
}
public static void main(String[] args) {
String endpoint = "https://YOUR-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR_PROJECT_NAME";
String deploymentName = "DeepSeek-R1-0528"; // Replace with your deployment name, not the model ID
// Get EntraID token for keyless auth
var credential = new DefaultAzureCredentialBuilder().build();
var context = new TokenRequestContext().addScopes("https://ai.azure.com/.default");
String token = credential.getToken(context).block().getToken();
// Standard OpenAI client — no Azure wrapper
// Java SDK uses /openai/v1 path (no api-version needed; SDK manages versioning internally)
String baseUrl = endpoint.replaceAll("/+$", "") + "/openai/v1";
OpenAIClient client = OpenAIOkHttpClient.builder()
.baseUrl(baseUrl)
.apiKey(token)
.build();
var response = client.responses().create(
ResponseCreateParams.builder()
.model(deploymentName)
.input("What are the top 3 benefits of cloud computing? Be concise.")
.maxOutputTokens(500)
.build()
);
System.out.printf("Response: %s%n", getOutputText(response));
System.out.printf("Status: %s%n", response.status());
response.usage().ifPresent(u ->
System.out.printf("Output tokens: %d%n", u.outputTokens()));
}
}
サンプルを実行する前に、必要な Go モジュールをインストールします。
go get github.com/Azure/azure-sdk-for-go/sdk/azcore@v1.21.0
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity@v1.13.1
go get github.com/openai/openai-go/v3@v3.22.0
次のコードを使用して、プロジェクト ルートで OpenAI クライアント オブジェクトを構成し、デプロイを指定し、応答を生成します。
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
"github.com/openai/openai-go/v3/responses"
)
func main() {
projectEndpoint := "https://YOUR-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR_PROJECT_NAME"
deploymentName := "DeepSeek-R1-0528" // Replace with your deployment name, not the model ID
ctx := context.Background()
// Get EntraID token for keyless auth
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create credential: %v\n", err)
os.Exit(1)
}
token, err := credential.GetToken(ctx, policy.TokenRequestOptions{
Scopes: []string{"https://ai.azure.com/.default"},
})
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get token: %v\n", err)
os.Exit(1)
}
// Standard OpenAI client — no Azure wrapper (no api-version needed with /v1 path)
baseURL := strings.TrimRight(projectEndpoint, "/") + "/openai/v1"
client := openai.NewClient(
option.WithBaseURL(baseURL),
option.WithAPIKey(token.Token),
)
resp, err := client.Responses.New(ctx, responses.ResponseNewParams{
Model: deploymentName,
Input: responses.ResponseNewParamsInputUnion{
OfString: openai.String("What are the top 3 benefits of cloud computing? Be concise."),
},
MaxOutputTokens: openai.Int(500),
})
if err != nil {
fmt.Fprintf(os.Stderr, "API error: %v\n", err)
os.Exit(1)
}
fmt.Printf("Response: %s\n", resp.OutputText())
fmt.Printf("Status: %s\n", resp.Status)
fmt.Printf("Output tokens: %d\n", resp.Usage.OutputTokens)
}
応答には、生成されたテキストと、モデルと使用状況のメタデータが含まれます。
サポートされている Foundry モデル
Foundry モデルの選択は、Responses API で使用するためにサポートされています。
Foundry ポータルでサポートされているモデルを表示する
Foundry ポータルでサポートされているモデルの完全な一覧を表示するには:
-
Microsoft Foundry にサインイン>。
新しいファウンドリーのトグルがオンになっていることを確認します。 これらの手順は Foundry (新規) を参照します。
- 右上のナビゲーションで [検出 ] を選択し、左側のウィンドウで [モデル ] を選択します。
-
[機能] ドロップダウンを開き、エージェントでサポートされているフィルターを選択します。
サポートされているモデルの一覧
このセクションでは、Responses API で使用するためにサポートされている Foundry モデルの一部を示します。 サポートされている Azure OpenAI モデルについては、「Available Azure OpenAI モデル」を参照してください。
Azure が販売する Foundry Models:
-
MAI-DS-R1: 決定論的で、精度に重点を置いた推論。
-
grok-4: 複雑な複数ステップの問題解決のためのフロンティア規模の推論。
-
grok-4-fast-reasoning: ワークフロー自動化用に最適化された高速エージェント推論。
-
grok-4-fast-non-reasoning: 高スループット、低遅延生成、システム ルーティング。
-
grok-3: 複雑なシステム レベルのワークフローの強力な推論。
-
grok-3-mini: インタラクティブで大量のユース ケース向けに最適化された軽量モデル。
-
Llama-3.3-70B-Instruct: エンタープライズ Q&A、意思決定サポート、およびシステム オーケストレーションのための多様なモデル。
-
Llama-4-Maverick-17B-128E-Instruct-FP8: 高速でコスト効率の高い推論を提供する FP8 最適化モデル。
-
DeepSeek-V3-0324: テキストと画像全体のマルチモーダルな理解。
-
DeepSeek-V3.1: マルチモーダル推論と接地検索が強化されました。
-
DeepSeek-V3.2: 高い計算効率と優れた推論とエージェントのパフォーマンスを調和させるモデル。
-
DeepSeek-V3.2-Speciale: Specialized DeepSeek-V3.2 variant.
-
DeepSeek-R1-0528: 高度な長い形式と複数ステップの推論。
-
gpt-oss-120b: 透明性と再現性をサポートするオープン エコシステム モデル。
一般的なエラーのトラブルシューティング
| エラー |
原因 |
解決方法 |
| 401 未認証 |
無効な資格情報または有効期限が切れた資格情報 |
リソースに DefaultAzureCredential ロールが割り当てられているを確認します。 |
| 404 見つかりません |
エンドポイントまたはデプロイ名が正しくありません |
エンドポイント URL に /api/projects/YOUR_PROJECT_NAME が含まれていることを確認し、デプロイ名が Foundry ポータルと一致します。 |
| 400 モデルはサポートされていません |
モデルは Responses API をサポートしていません |
サポートされているモデルの一覧を確認し、デプロイで互換性のあるモデルが使用されていることを確認します。 |
関連コンテンツ