Microsoft Foundry Models を使用すると、1 つのエンドポイントと資格情報のセットを通じて、主要なモデル プロバイダーから最も強力なモデルをaccessできます。 この機能を使用すると、コードを変更することなく、モデルを切り替えてアプリケーションで使用できます。
Foundry リソースには、多数のモデル デプロイを含めることができます。 モデル デプロイで実行された推論に対してのみ支払いが発生します。 デプロイはリソースAzureであるため、Azure ポリシーの対象となります。
pip のように、package managerを使用してパッケージ openai をインストールします。
pip install openai --upgrade
その後、パッケージを使用してモデルを使用できます。 次の例では、チャット入力候補を使用してクライアントを作成する方法を示します。
import os
from openai import AzureOpenAI
client = AzureOpenAI(
azure_endpoint = "https://<resource>.services.ai.azure.com"
api_key=os.getenv("AZURE_INFERENCE_CREDENTIAL"),
api_version="2024-10-21",
)
npm を使用してパッケージ openai をインストールします。
npm install openai
その後、パッケージを使用してモデルを使用できます。 次の例では、チャット入力候補を使用してクライアントを作成する方法を示します。
import { AzureKeyCredential } from "@azure/openai";
const endpoint = "https://<resource>.services.ai.azure.com";
const apiKey = new AzureKeyCredential(process.env.AZURE_INFERENCE_CREDENTIAL);
const apiVersion = "2024-10-21"
const client = new AzureOpenAI({
endpoint,
apiKey,
apiVersion,
"deepseek-v3-0324"
});
ここでは、 deepseek-v3-0324 は Microsoft Foundry リソース内のモデル デプロイの名前です。
次のコマンドを使用して OpenAI ライブラリをインストールします。
dotnet add package Azure.AI.OpenAI --prerelease
パッケージを使用してモデルを実行できます。 次の例では、チャット入力候補を使用してクライアントを作成する方法を示します。
AzureOpenAIClient client = new(
new Uri("https://<resource>.services.ai.azure.com"),
new ApiKeyCredential(Environment.GetEnvironmentVariable("AZURE_INFERENCE_CREDENTIAL"))
);
projectにパッケージを追加します。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-openai</artifactId>
<version>1.0.0-beta.16</version>
</dependency>
その後、パッケージを使用してモデルを使用できます。 次の例では、チャット入力候補を使用してクライアントを作成する方法を示します。
OpenAIClient client = new OpenAIClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://<resource>.services.ai.azure.com")
.buildClient();
リファレンス セクションを活用して、API の設計と使用可能なパラメーターを調べることができます。 たとえば、チャット入力候補のリファレンス セクションでは、ルート /chat/completions を使用して、チャット形式の指示に基づいて予測を生成する方法について詳しく説明します。
依頼
POST https://<resource>.services.ai.azure.com/openai/deployments/deepseek-v3-0324/chat/completions?api-version=2024-10-21
api-key: <api-key>
Content-Type: application/json
ここで、 deepseek-v3-0324 は Foundry リソース内のモデル デプロイの名前です。
response = client.chat.completions.create(
model="deepseek-v3-0324", # Replace with your model deployment name.
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain Riemann's conjecture in 1 paragraph"}
]
)
print(response.model_dump_json(indent=2)
var messages = [
{ role: "system", content: "You are a helpful assistant" },
{ role: "user", content: "Explain Riemann's conjecture in 1 paragraph" },
];
const response = await client.chat.completions.create({ messages, model: "deepseek-v3-0324" });
console.log(response.choices[0].message.content)
ChatCompletion response = chatClient.CompleteChat(
[
new SystemChatMessage("You are a helpful assistant."),
new UserChatMessage("Explain Riemann's conjecture in 1 paragraph"),
]);
Console.WriteLine($"{response.Role}: {response.Content[0].Text}");
List<ChatRequestMessage> chatMessages = new ArrayList<>();
chatMessages.add(new ChatRequestSystemMessage("You are a helpful assistant"));
chatMessages.add(new ChatRequestUserMessage("Explain Riemann's conjecture in 1 paragraph"));
ChatCompletions chatCompletions = client.getChatCompletions("deepseek-v3-0324",
new ChatCompletionsOptions(chatMessages));
System.out.printf("Model ID=%s is created at %s.%n", chatCompletions.getId(), chatCompletions.getCreatedAt());
for (ChatChoice choice : chatCompletions.getChoices()) {
ChatResponseMessage message = choice.getMessage();
System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
System.out.println("Message:");
System.out.println(message.getContent());
}
ここでは、 deepseek-v3-0324 は Microsoft Foundry リソース内のモデル デプロイの名前です。
依頼
POST https://<resource>.services.ai.azure.com/openai/deployments/deepseek-v3-0324/chat/completions?api-version=2024-10-21
api-key: <api-key>
Content-Type: application/json
{
"messages": [
{
"role": "system",
"content": "You are a helpful assistant"
},
{
"role": "user",
"content": "Explain Riemann's conjecture in 1 paragraph"
}
]
}
ここで、 deepseek-v3-0324 は Foundry リソース内のモデル デプロイの名前です。
Foundry Tools の Foundry Models にデプロイされたモデルでは、Microsoft Entra IDを使用したキーレス承認がサポートされます。 キーレス承認により、セキュリティの強化、ユーザー エクスペリエンスの簡素化、運用の複雑さの軽減、最新の開発に対する堅牢なコンプライアンス サポートの提供が実現されます。 これにより、安全でスケーラブルな ID 管理ソリューションを導入する組織にとって、キーレス承認は強力な選択肢になります。
pip のようなpackage managerを使用して OpenAI SDK をインストールします。
pip install openai
Microsoft Entra ID認証のために、次をインストールしてください。
pip install azure-identity
パッケージを使用してモデルを消費します。 次の例では、クライアントを作成して、Microsoft Entra IDでチャットの完了を使用し、モデルデプロイでチャット完了エンドポイントにテスト呼び出しを行う方法を示します。
<resource>を Foundry リソース名に置き換えます。 Azure portalで、または az cognitiveservices account list を実行して見つけます。
DeepSeek-V3.1を実際のデプロイ名に置き換えます。
from openai import OpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default"
)
client = OpenAI(
base_url="https://<resource>.openai.azure.com/openai/v1/",
api_key=token_provider,
)
completion = client.chat.completions.create(
model="DeepSeek-V3.1", # Required: your deployment name
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is Azure AI?"}
]
)
print(completion.choices[0].message.content)
想定される出力
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
次の名前空間をインポートします。
using Azure.Identity;
using OpenAI;
using OpenAI.Chat;
using System.ClientModel.Primitives;
次に、パッケージを使用してモデルを実行します。 次の例は、Microsoft Entra IDでチャットの完了を使用するクライアントを作成し、モデルデプロイでチャット完了エンドポイントに対してテスト呼び出しを行う方法を示しています。
<resource> を Foundry リソース名に置き換えます (Azure portalで見つけます)。
gpt-4o-miniを実際のデプロイ名に置き換えます。
#pragma warning disable OPENAI001
BearerTokenPolicy tokenPolicy = new(
new DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default"
);
ChatClient client = new(
model: "gpt-4o-mini", // Your deployment name
authenticationPolicy: tokenPolicy,
options: new OpenAIClientOptions() {
Endpoint = new Uri("https://<resource>.openai.azure.com/openai/v1/")
}
);
ChatCompletion completion = client.CompleteChat(
new SystemChatMessage("You are a helpful assistant."),
new UserChatMessage("What is Azure AI?")
);
Console.WriteLine(completion.Content[0].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 .NET SDK および DefaultAzureCredential クラス。
npm を使用して OpenAI SDK をインストールします。
npm install openai
Microsoft Entra ID認証のために、次をインストールしてください。
npm install @azure/identity
次に、パッケージを使用してモデルを実行します。 次の例は、Microsoft Entra IDでチャットの完了を使用するクライアントを作成し、モデルデプロイでチャット完了エンドポイントに対してテスト呼び出しを行う方法を示しています。
<resource> を Foundry リソース名に置き換えます (Azure portalで検索するか、az cognitiveservices account list を実行します)。
DeepSeek-V3.1を実際のデプロイ名に置き換えます。
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
import { OpenAI } from "openai";
const tokenProvider = getBearerTokenProvider(
new DefaultAzureCredential(),
'https://cognitiveservices.azure.com/.default'
);
const client = new OpenAI({
baseURL: "https://<resource>.openai.azure.com/openai/v1/",
apiKey: tokenProvider
});
const completion = await client.chat.completions.create({
model: "DeepSeek-V3.1", // Required: your deployment name
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What is Azure AI?" }
]
});
console.log(completion.choices[0].message.content);
予想される出力:
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 クラス。
Projectに 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でチャットの完了を使用するクライアントを作成し、モデルデプロイでチャット完了エンドポイントに対してテスト呼び出しを行う方法を示しています。
<resource> を Foundry リソース名に置き換えます (Azure portalで見つけます)。
DeepSeek-V3.1を実際のデプロイ名に置き換えます。
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.models.chat.completions.*;
DefaultAzureCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
OpenAIClient client = OpenAIOkHttpClient.builder()
.baseUrl("https://<resource>.openai.azure.com/openai/v1/")
.credential(BearerTokenCredential.create(
AuthenticationUtil.getBearerTokenSupplier(
tokenCredential,
"https://cognitiveservices.azure.com/.default"
)
))
.build();
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
.addSystemMessage("You are a helpful assistant.")
.addUserMessage("What is Azure AI?")
.model("DeepSeek-V3.1") // Required: your deployment name
.build();
ChatCompletion completion = client.chat().completions().create(params);
System.out.println(completion.choices().get(0).message().content());
予想される出力:
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に認証トークンを示します。 たとえば、 チャット完了 のリファレンス セクションでは、 /chat/completions ルートを使用して、チャット形式の指示に基づいて予測を生成する方法について詳しく説明します。
/modelsパスは、URL のルートに含まれています。
依頼
<resource> を Foundry リソース名に置き換えます (Azure portalで検索するか、az cognitiveservices account list を実行します)。
MAI-DS-R1を実際のデプロイ名に置き換えます。
base_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/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "MAI-DS-R1",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Explain what the bitter lesson is?"
}
]
}'
応答
認証が成功すると、応答本文にチャット完了の結果を含む 200 OK 応答が表示されます。
{
"id": "chatcmpl-...",
"object": "chat.completion",
"created": 1738368234,
"model": "MAI-DS-R1",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "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."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 28,
"completion_tokens": 52,
"total_tokens": 80
}
}
トークンはスコープ https://cognitiveservices.azure.com/.default で発行する必要があります。
テスト目的で、ユーザー アカウントの有効なトークンを取得する最も簡単な方法は、Azure CLIを使用することです。 コンソールで、次のAzure CLI コマンドを実行します。
az account get-access-token --resource https://cognitiveservices.azure.com --query "accessToken" --output tsv
このコマンドは、$AZURE_OPENAI_AUTH_TOKEN 環境変数に格納できるaccess トークンを出力します。
リファレンス: チャット完了 API