Azure AI Foundry にモデルをデプロイしたら、Azure AI Foundry API を使用してその機能を使用できます。 Azure AI Foundry モデルでモデルを使用するには、2 つの異なるエンドポイントと API があります。
推論エンドポイントをモデル化する
モデル推論エンドポイント (通常は https://<resource-name>.services.ai.azure.com/models
形式) を使用すると、同じ認証とスキーマを持つ単一のエンドポイントを使用して、リソースにデプロイされたモデルの推論を生成できます。 このエンドポイントは、Foundry Models のすべてのモデルがサポートする Azure AI モデル推論 API に従います。 次のモダリティがサポートされています。
経路選択
推論エンドポイントは、要求の内部の name
パラメーターをデプロイの名前と照合することで、要求を特定のデプロイにルーティングします。 つまり、"デプロイは、特定の構成下で特定のモデルのエイリアスとして機能する" ということです。 この柔軟性により、特定のモデルをサービスで複数回デプロイできますが、必要に応じて異なる構成でデプロイできます。
たとえば、Mistral-large
という名前のデプロイを作成した場合、そのようなデプロイを次のようにして呼び出すことができます。
pip のように、パッケージ マネージャーを使用してパッケージ azure-ai-inference
をインストールします。
pip install azure-ai-inference
その後、パッケージを使用してモデルを使用できます。 次の例では、チャット入力候補を使用してクライアントを作成する方法を示します。
import os
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential
client = ChatCompletionsClient(
endpoint="https://<resource>.services.ai.azure.com/models",
credential=AzureKeyCredential(os.environ["AZURE_INFERENCE_CREDENTIAL"]),
)
サンプルを確認し、API リファレンス ドキュメントを参照して、作業を開始してください。
npm を使用してパッケージ @azure-rest/ai-inference
をインストールします。
npm install @azure-rest/ai-inference
その後、パッケージを使用してモデルを使用できます。 次の例では、チャット入力候補を使用してクライアントを作成する方法を示します。
import ModelClient from "@azure-rest/ai-inference";
import { isUnexpected } from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";
const client = new ModelClient(
"https://<resource>.services.ai.azure.com/models",
new AzureKeyCredential(process.env.AZURE_INFERENCE_CREDENTIAL)
);
サンプルを確認し、API リファレンス ドキュメントを参照して、作業を開始してください。
次のコマンドを使用して Azure AI 推論ライブラリをインストールします:
dotnet add package Azure.AI.Inference --prerelease
次の名前空間をインポートします。
using Azure;
using Azure.Identity;
using Azure.AI.Inference;
その後、パッケージを使用してモデルを使用できます。 次の例では、チャット入力候補を使用してクライアントを作成する方法を示します。
ChatCompletionsClient client = new ChatCompletionsClient(
new Uri("https://<resource>.services.ai.azure.com/models"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_INFERENCE_CREDENTIAL"))
);
サンプルを確認し、API リファレンス ドキュメントを参照して、作業を開始してください。
パッケージをプロジェクトに追加します。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-inference</artifactId>
<version>1.0.0-beta.1</version>
</dependency>
その後、パッケージを使用してモデルを使用できます。 次の例では、チャット入力候補を使用してクライアントを作成する方法を示します。
ChatCompletionsClient client = new ChatCompletionsClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://<resource>.services.ai.azure.com/models")
.buildClient();
サンプルを確認し、API リファレンス ドキュメントを参照して、作業を開始してください。
リファレンス セクションを活用して、API の設計と使用可能なパラメーターを調べることができます。 たとえば、チャット補完のリファレンス セクションでは、ルート /chat/completions
を使用し、チャット形式の指示に基づいて予測を生成する方法について詳しく説明しています。 パス /models
が URL のルートに含まれていることに注目してください。
依頼
POST https://<resource>.services.ai.azure.com/models/chat/completions?api-version=2024-05-01-preview
api-key: <api-key>
Content-Type: application/json
from azure.ai.inference.models import SystemMessage, UserMessage
response = client.complete(
messages=[
SystemMessage(content="You are a helpful assistant."),
UserMessage(content="Explain Riemann's conjecture in 1 paragraph"),
],
model="mistral-large"
)
print(response.choices[0].message.content)
var messages = [
{ role: "system", content: "You are a helpful assistant" },
{ role: "user", content: "Explain Riemann's conjecture in 1 paragraph" },
];
var response = await client.path("/chat/completions").post({
body: {
messages: messages,
model: "mistral-large"
}
});
console.log(response.body.choices[0].message.content)
requestOptions = new ChatCompletionsOptions()
{
Messages = {
new ChatRequestSystemMessage("You are a helpful assistant."),
new ChatRequestUserMessage("Explain Riemann's conjecture in 1 paragraph")
},
Model = "mistral-large"
};
response = client.Complete(requestOptions);
Console.WriteLine($"Response: {response.Value.Content}");
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.complete(new ChatCompletionsOptions(chatMessages));
for (ChatChoice choice : chatCompletions.getChoices()) {
ChatResponseMessage message = choice.getMessage();
System.out.println("Response:" + message.getContent());
}
依頼
POST https://<resource>.services.ai.azure.com/models/chat/completions?api-version=2024-05-01-preview
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"
}
],
"model": "mistral-large"
}
ヒント
デプロイのルーティングでは、大文字と小文字は区別されません。
Azure OpenAI 推論エンドポイント
Azure AI Foundry では、Azure OpenAI API もサポートされています。 この API は、OpenAI モデルのすべての機能を公開し、アシスタント、スレッド、ファイル、バッチ推論などの追加機能をサポートします。 非 OpenAI モデルは、互換性のある機能にも使用できます。
Azure OpenAI エンドポイント (通常、フォーム https://<resource-name>.openai.azure.com
) はデプロイ レベルで動作し、それぞれに関連付けられた独自の URL を持ちます。 ただし、同じ認証メカニズムを使用してこれらを実行できます。 詳細については、Azure OpenAI API のリファレンス ページを参照してください
各デプロイには、Azure OpenAI のベース URL とルート /deployments/<model-deployment-name>
を連結した URL があります。
pip のように、パッケージ マネージャーを使用してパッケージ 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
は Azure AI 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"))
);
パッケージをプロジェクトに追加します。
<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
は Azure AI Foundry リソース内のモデル デプロイの名前です。
response = client.chat.completions.create(
model="deepseek-v3-0324", # Replace with your model dpeloyment 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
は Azure AI 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
は Azure AI Foundry リソース内のモデル デプロイの名前です。
次のステップ