Von Bedeutung
この記事で "(プレビュー)" と付記されている項目は、現在、パブリック プレビュー段階です。 このプレビューはサービス レベル アグリーメントなしで提供されており、運用環境ではお勧めしません。 特定の機能はサポート対象ではなく、機能が制限されることがあります。 詳細については、「 Microsoft Azure プレビューの追加使用条件」を参照してください。
この記事では、Azure AI Foundry Models にデプロイされたチャット補完モデルの推論機能を使用する方法について説明します。
推論モデル
推論モデルは、数学、コーディング、科学、戦略、ロジスティックスなどの分野で高レベルのパフォーマンスを発揮できます。 これらのモデルが出力を生成する方法は、答えを生成する前に考え方の連鎖を明示的に使用して、考えられるすべてのパスを調べる方法です。 彼らはそれらを生成する際に彼らの答えを検証し、より正確な結論に到達するのに役立ちます。 その結果、推論モデルでは、効果的な結果を生成するためにプロンプトのコンテキストが少なくて済む場合があります。
モデルのパフォーマンスをスケーリングするこの方法は、待機時間とコストの増加に対してパフォーマンスを交換するため、 推論コンピューティング時間 と呼ばれます。 これに対し、他のアプローチは学習計算時間によってスケーリングされる場合があります。
推論モデルでは、次の 2 種類のコンテンツが出力として生成されます。
- 推論の入力候補
- 出力の入力候補
これらの生成結果はいずれも、モデルから生成されたコンテンツにカウントされます。 そのため、モデルに関連付けられているトークンの制限とコストに寄与します。 DeepSeek-R1
などの一部のモデルは、推論コンテンツで応答する場合があります。 o1
などの他のものは、完了のみを出力します。
[前提条件]
このチュートリアルを完了するには、次のものが必要です。
Azure サブスクリプション。 GitHub モデルを使用している場合は、エクスペリエンスをアップグレードし、プロセスで Azure サブスクリプションを作成できます。 その場合は、 GitHub モデルから Azure AI Foundry モデルへのアップグレード に関するページを参照してください。
Azure AI Foundry リソース (旧称 Azure AI Services)。 詳細については、「 Azure AI Foundry リソースの作成」を参照してください。
エンドポイント URL とキー。
次のコマンドを使用して SDK をインストールします。
pip install -U openai
推論機能を備えたモデルのモデル デプロイ。 モデルがない場合は、Foundry モデルの追加と構成に関する記事を参照して推論モデルを追加します。
- この例では、
DeepSeek-R1
を使用します。
- この例では、
チャットで推論機能を使用する
まず、モデルを実行するクライアントを作成します。 次のコードでは、環境変数に格納されているエンドポイント URL とキーを使用しています。
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",
)
Microsoft Entra ID をサポートするリソースを構成してある場合、次のコード スニペットを使用してクライアントを作成できます。
import os
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
azure_endpoint = "https://<resource>.services.ai.azure.com",
azure_ad_token_provider=token_provider,
api_version="2024-10-21",
)
プロンプト推論モデル
推論モデルのプロンプトを作成する場合は、次の事項を考慮してください。
- 指示は簡単にし、思考の連鎖による手法は使用しないようにします。
- 組み込みの推論機能により、単純なゼロショットのプロンプトがより複雑なメソッドと同じくらい効果的になります。
- RAG シナリオなど、追加のコンテキストやドキュメントを提供する場合、最も関連性の高い情報のみを含めると、モデルの応答が過度に複雑になるのを防ぐのに役立つ場合があります。
- 推論モデルでは、システム メッセージの使用がサポートされる場合があります。 ただし、他の非推論モデルほど厳密には従わない場合があります。
- マルチターン アプリケーションを作成する場合は、「推論コンテンツ」セクションで説明されているように、推論コンテンツなしで、モデルから最終的 な 回答のみを追加することを検討してください。
推論モデルでは、応答の生成に時間がかかる場合があることに注意してください。 彼らは、より深く、より構造化された問題解決を可能にする思考の長い推論チェーンを使用しています。 また、自己検証を実行して回答をクロスチェックし、間違いを修正することで、出現する自己反射的な行動を示します。
チャット入力候補要求を作成する
次の例に、モデルに対する基本的なチャット要求を作成する方法を示します。
response = client.chat.completions.create(
model="deepseek-r1",
messages=[
{"role": "user", "content": "How many languages are in the world?"}
]
)
応答は次のとおりです。モデルの使用状況の統計情報が表示されます。
print("Response:", response.choices[0].message.content)
print("Model:", response.model)
print("Usage:")
print("\tPrompt tokens:", response.usage.prompt_tokens)
print("\tTotal tokens:", response.usage.total_tokens)
print("\tCompletion tokens:", response.usage.completion_tokens)
Response: As of now, it's estimated that there are about 7,000 languages spoken around the world. However, this number can vary as some languages become extinct and new ones develop. It's also important to note that the number of speakers can greatly vary between languages, with some having millions of speakers and others only a few hundred.
Model: deepseek-r1
Usage:
Prompt tokens: 11
Total tokens: 897
Completion tokens: 886
推論に関するコンテンツ
推論モデルの中には、DeepSeek-R1 のように、補完を生成し、その背後にある推論が含まれるものがあります。
完了に関連する理由は、フィールド reasoning_content
に含まれます。 モデルは、推論コンテンツを生成するシナリオを選択できます。
print("Thinking:", response.choices[0].message.reasoning_content)
Thinking: Okay, the user is asking how many languages exist in the world. I need to provide a clear and accurate answer...
複数ターン会話を行うときは、説明が長くなる傾向があるため、チャット履歴に推論コンテンツを送信しないようにすると便利です。
コンテンツのストリーミング
既定では、入力候補 API は、生成されたコンテンツ全体を 1 つの応答で返します。 長い入力候補を生成する場合、応答が得られるまでに数秒かかることがあります。
コンテンツをストリーミングして、コンテンツが生成されるにつれ返されるようにできます。 コンテンツをストリーミングすると、コンテンツが使用可能になったときに入力候補の処理を開始できます。 このモードは、データのみのサーバー送信イベントとして応答をストリーム バックするオブジェクトを返します。 メッセージ フィールドではなく、デルタ フィールドからチャンクを抽出します。
生成結果をストリーミングするには、モデルを呼び出す際にstream=True
を設定します。
response = client.chat.completions.create(
model="deepseek-r1",
messages=[
{"role": "user", "content": "How many languages are in the world?"}
],
stream=True
)
出力を視覚化するには、ストリームを出力するヘルパー関数を定義します。 次の例では、推論コンテンツを使用せずに回答のみを配信するルーティングを実装しています。
推論コンテンツは、応答のデルタ部分の中のキー reasoning_content
にも含まれます。
def print_stream(completion):
"""
Prints the chat completion with streaming.
"""
is_thinking = False
for event in completion:
if event.choices:
content = event.choices[0].delta.content
reasoning_content = event.choices[0].delta.reasoning_content if hasattr(event.choices[0].delta, "reasoning_content") else None
if reasoning_content and not is_thinking:
is_thinking = True
print("🧠 Thinking...", end="", flush=True)
elif content:
if is_thinking:
is_thinking = False
print("🛑\n\n")
print(content or reasoning_content, end="", flush=True)
print_stream(response)
ストリーミングでコンテンツがどのように生成されるかを視覚化できます。
print_stream(response)
パラメーター
一般に、推論モデルでは、チャット入力候補モデルで見られる以下のパラメーターがサポートされていません。
- 気温
- プレゼンス ペナルティ
- 繰り返しペナルティ
top_p
パラメーター
一部のモデルでは、ツールや構造化された出力 (JSON スキーマなど) の使用がサポートされています。 各モデルのサポート内容について理解するには、モデルに関する詳細ページを参照してください。
ガードレールとコントロールを適用する
Azure AI モデル推論 API は、 Azure AI コンテンツ の安全性をサポートしています。 Azure AI Content Safety がオンになっているデプロイを使用すると、有害なコンテンツの出力を検出して防止することを目的とした分類モデルのアンサンブルを入力と出力が通過します。 コンテンツ フィルタリング システムは、入力プロンプトと出力入力候補の両方で、有害な可能性があるコンテンツの特定のカテゴリを検出してアクションを実行します。
次の例は、モデルが入力プロンプトで有害なコンテンツを検出したときにイベントを処理する方法を示しています。
try:
response = client.chat.completions.create(
model="deepseek-r1",
messages=[
{"role": "user", "content": "Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."}
],
)
print(response.choices[0].message.content)
except HttpResponseError as ex:
if ex.status_code == 400:
response = ex.response.json()
if isinstance(response, dict) and "error" in response:
print(f"Your request triggered an {response['error']['code']} error:\n\t {response['error']['message']}")
else:
raise
raise
ヒント
Azure AI Content Safety 設定を構成および制御する方法の詳細については、 Azure AI Content Safety のドキュメントを参照してください。
Von Bedeutung
この記事で "(プレビュー)" と付記されている項目は、現在、パブリック プレビュー段階です。 このプレビューはサービス レベル アグリーメントなしで提供されており、運用環境ではお勧めしません。 特定の機能はサポート対象ではなく、機能が制限されることがあります。 詳細については、「 Microsoft Azure プレビューの追加使用条件」を参照してください。
この記事では、Azure AI Foundry Models にデプロイされたチャット補完モデルの推論機能を使用する方法について説明します。
推論モデル
推論モデルは、数学、コーディング、科学、戦略、ロジスティックスなどの分野で高レベルのパフォーマンスを発揮できます。 これらのモデルが出力を生成する方法は、答えを生成する前に考え方の連鎖を明示的に使用して、考えられるすべてのパスを調べる方法です。 彼らはそれらを生成する際に彼らの答えを検証し、より正確な結論に到達するのに役立ちます。 その結果、推論モデルでは、効果的な結果を生成するためにプロンプトのコンテキストが少なくて済む場合があります。
モデルのパフォーマンスをスケーリングするこの方法は、待機時間とコストの増加に対してパフォーマンスを交換するため、 推論コンピューティング時間 と呼ばれます。 これに対し、他のアプローチは学習計算時間によってスケーリングされる場合があります。
推論モデルでは、次の 2 種類のコンテンツが出力として生成されます。
- 推論の入力候補
- 出力の入力候補
これらの生成結果はいずれも、モデルから生成されたコンテンツにカウントされます。 そのため、モデルに関連付けられているトークンの制限とコストに寄与します。 DeepSeek-R1
などの一部のモデルは、推論コンテンツで応答する場合があります。 o1
などの他のものは、完了のみを出力します。
[前提条件]
このチュートリアルを完了するには、次のものが必要です。
Azure サブスクリプション。 GitHub モデルを使用している場合は、エクスペリエンスをアップグレードし、プロセスで Azure サブスクリプションを作成できます。 その場合は、 GitHub モデルから Azure AI Foundry モデルへのアップグレード に関するページを参照してください。
Azure AI Foundry リソース (旧称 Azure AI Services)。 詳細については、「 Azure AI Foundry リソースの作成」を参照してください。
エンドポイント URL とキー。
次のコマンドを使用して、JavaScript 用 Azure 推論ライブラリをインストールします。
npm install @azure-rest/ai-inference npm install @azure/core-auth npm install @azure/identity
Node.jsを使用している場合は、 package.jsonで依存関係を構成できます。
package.json
{ "name": "main_app", "version": "1.0.0", "description": "", "main": "app.js", "type": "module", "dependencies": { "@azure-rest/ai-inference": "1.0.0-beta.6", "@azure/core-auth": "1.9.0", "@azure/core-sse": "2.2.0", "@azure/identity": "4.8.0" } }
次をインポートします。
import ModelClient from "@azure-rest/ai-inference"; import { isUnexpected } from "@azure-rest/ai-inference"; import { createSseStream } from "@azure/core-sse"; import { AzureKeyCredential } from "@azure/core-auth"; import { DefaultAzureCredential } from "@azure/identity";
推論機能を備えたモデルのモデル デプロイ。 モデルがない場合は、Foundry モデルの追加と構成に関する記事を参照して推論モデルを追加します。
- この例では、
DeepSeek-R1
を使用します。
- この例では、
チャットで推論機能を使用する
まず、モデルを実行するクライアントを作成します。 次のコードでは、環境変数に格納されているエンドポイント URL とキーを使用しています。
const client = ModelClient(
"https://<resource>.services.ai.azure.com/models",
new AzureKeyCredential(process.env.AZURE_INFERENCE_CREDENTIAL)
);
Microsoft Entra ID サポートを使用してリソースを構成した場合は、次のコード スニペットを使用してクライアントを作成できます。
const clientOptions = { credentials: { "https://cognitiveservices.azure.com/.default" } };
const client = ModelClient(
"https://<resource>.services.ai.azure.com/models",
new DefaultAzureCredential()
clientOptions,
);
プロンプト推論モデル
推論モデルのプロンプトを作成する場合は、次の事項を考慮してください。
- 指示は簡単にし、思考の連鎖による手法は使用しないようにします。
- 組み込みの推論機能により、単純なゼロショットのプロンプトがより複雑なメソッドと同じくらい効果的になります。
- RAG シナリオなど、追加のコンテキストやドキュメントを提供する場合、最も関連性の高い情報のみを含めると、モデルの応答が過度に複雑になるのを防ぐのに役立つ場合があります。
- 推論モデルでは、システム メッセージの使用がサポートされる場合があります。 ただし、他の非推論モデルほど厳密には従わない場合があります。
- マルチターン アプリケーションを作成する場合は、「推論コンテンツ」セクションで説明されているように、推論コンテンツなしで、モデルから最終的 な 回答のみを追加することを検討してください。
推論モデルでは、応答の生成に時間がかかる場合があることに注意してください。 彼らは、より深く、より構造化された問題解決を可能にする思考の長い推論チェーンを使用しています。 また、自己検証を実行して回答をクロスチェックし、間違いを修正することで、出現する自己反射的な行動を示します。
チャット入力候補要求を作成する
次の例に、モデルに対する基本的なチャット要求を作成する方法を示します。
var messages = [
{ role: "user", content: "How many languages are in the world?" },
];
var response = await client.path("/chat/completions").post({
body: {
model: "DeepSeek-R1",
messages: messages,
}
});
応答は次のとおりです。モデルの使用状況の統計情報が表示されます。
if (isUnexpected(response)) {
throw response.body.error;
}
console.log("Response: ", response.body.choices[0].message.content);
console.log("Model: ", response.body.model);
console.log("Usage:");
console.log("\tPrompt tokens:", response.body.usage.prompt_tokens);
console.log("\tTotal tokens:", response.body.usage.total_tokens);
console.log("\tCompletion tokens:", response.body.usage.completion_tokens);
Response: <think>Okay, the user is asking how many languages exist in the world. I need to provide a clear and accurate answer...</think>As of now, it's estimated that there are about 7,000 languages spoken around the world. However, this number can vary as some languages become extinct and new ones develop. It's also important to note that the number of speakers can greatly vary between languages, with some having millions of speakers and others only a few hundred.
Model: deepseek-r1
Usage:
Prompt tokens: 11
Total tokens: 897
Completion tokens: 886
推論に関するコンテンツ
推論モデルの中には、DeepSeek-R1 のように、補完を生成し、その背後にある推論が含まれるものがあります。 補完に関連付けられた推論は、<think>
と </think>
のタグ内の応答のコンテンツに含まれます。 モデルがどのシナリオで推論コンテンツを生成するかを選択できます。 次のように応答から推論コンテンツを抽出して、モデルの思考プロセスを理解できます。
var content = response.body.choices[0].message.content
var match = content.match(/<think>(.*?)<\/think>(.*)/s);
console.log("Response:");
if (match) {
console.log("\tThinking:", match[1]);
console.log("\Answer:", match[2]);
}
else {
console.log("Response:", content);
}
console.log("Model: ", response.body.model);
console.log("Usage:");
console.log("\tPrompt tokens:", response.body.usage.prompt_tokens);
console.log("\tTotal tokens:", response.body.usage.total_tokens);
console.log("\tCompletion tokens:", response.body.usage.completion_tokens);
Thinking: Okay, the user is asking how many languages exist in the world. I need to provide a clear and accurate answer. Let's start by recalling the general consensus from linguistic sources. I remember that the number often cited is around 7,000, but maybe I should check some reputable organizations.\n\nEthnologue is a well-known resource for language data, and I think they list about 7,000 languages. But wait, do they update their numbers? It might be around 7,100 or so. Also, the exact count can vary because some sources might categorize dialects differently or have more recent data. \n\nAnother thing to consider is language endangerment. Many languages are endangered, with some having only a few speakers left. Organizations like UNESCO track endangered languages, so mentioning that adds context. Also, the distribution isn't even. Some countries have hundreds of languages, like Papua New Guinea with over 800, while others have just a few. \n\nA user might also wonder why the exact number is hard to pin down. It's because the distinction between a language and a dialect can be political or cultural. For example, Mandarin and Cantonese are considered dialects of Chinese by some, but they're mutually unintelligible, so others classify them as separate languages. Also, some regions are under-researched, making it hard to document all languages. \n\nI should also touch on language families. The 7,000 languages are grouped into families like Indo-European, Sino-Tibetan, Niger-Congo, etc. Maybe mention a few of the largest families. But wait, the question is just about the count, not the families. Still, it's good to provide a bit more context. \n\nI need to make sure the information is up-to-date. Let me think – recent estimates still hover around 7,000. However, languages are dying out rapidly, so the number decreases over time. Including that note about endangerment and language extinction rates could be helpful. For instance, it's often stated that a language dies every few weeks. \n\nAnother point is sign languages. Does the count include them? Ethnologue includes some, but not all sources might. If the user is including sign languages, that adds more to the count, but I think the 7,000 figure typically refers to spoken languages. For thoroughness, maybe mention that there are also over 300 sign languages. \n\nSummarizing, the answer should state around 7,000, mention Ethnologue's figure, explain why the exact number varies, touch on endangerment, and possibly note sign languages as a separate category. Also, a brief mention of Papua New Guinea as the most linguistically diverse country. \n\nWait, let me verify Ethnologue's current number. As of their latest edition (25th, 2022), they list 7,168 living languages. But I should check if that's the case. Some sources might round to 7,000. Also, SIL International publishes Ethnologue, so citing them as reference makes sense. \n\nOther sources, like Glottolog, might have a different count because they use different criteria. Glottolog might list around 7,000 as well, but exact numbers vary. It's important to highlight that the count isn't exact because of differing definitions and ongoing research. \n\nIn conclusion, the approximate number is 7,000, with Ethnologue being a key source, considerations of endangerment, and the challenges in counting due to dialect vs. language distinctions. I should make sure the answer is clear, acknowledges the variability, and provides key points succinctly.
Answer: The exact number of languages in the world is challenging to determine due to differences in definitions (e.g., distinguishing languages from dialects) and ongoing documentation efforts. However, widely cited estimates suggest there are approximately **7,000 languages** globally.
Model: DeepSeek-R1
Usage:
Prompt tokens: 11
Total tokens: 897
Completion tokens: 886
複数ターン会話を行うときは、説明が長くなる傾向があるため、チャット履歴に推論コンテンツを送信しないようにすると便利です。
コンテンツのストリーミング
既定では、入力候補 API は、生成されたコンテンツ全体を 1 つの応答で返します。 長い入力候補を生成する場合、応答が得られるまでに数秒かかることがあります。
コンテンツをストリーミングして、コンテンツが生成されるにつれ返されるようにできます。 コンテンツをストリーミングすると、コンテンツが使用可能になったときに入力候補の処理を開始できます。 このモードは、データのみのサーバー送信イベントとして応答をストリーム バックするオブジェクトを返します。 メッセージ フィールドではなく、デルタ フィールドからチャンクを抽出します。
生成結果をストリーミングするには、モデルを呼び出す際にstream=True
を設定します。
var messages = [
{ role: "user", content: "How many languages are in the world?" },
];
var response = await client.path("/chat/completions").post({
body: {
model: "DeepSeek-R1",
messages: messages,
stream: true
}
}).asNodeStream();
出力を視覚化するには、ストリームを出力するヘルパー関数を定義します。 次の例では、推論コンテンツを使用せずに回答のみを配信するルーティングを実装しています。
async function printStream(sses) {
let isThinking = false;
for await (const event of sses) {
if (event.data === "[DONE]") {
return;
}
for (const choice of (JSON.parse(event.data)).choices) {
const content = choice.delta?.content ?? "";
if (content === "<think>") {
isThinking = true;
process.stdout.write("🧠 Thinking...");
} else if (content === "</think>") {
isThinking = false;
console.log("🛑\n\n");
} else if (content) {
process.stdout.write(content);
}
}
}
}
ストリーミングでコンテンツがどのように生成されるかを視覚化できます。
var sses = createSseStream(response.body);
await printStream(sses)
パラメーター
一般に、推論モデルでは、チャット入力候補モデルで見られる以下のパラメーターがサポートされていません。
- 気温
- プレゼンス ペナルティ
- 繰り返しペナルティ
top_p
パラメーター
一部のモデルでは、ツールや構造化された出力 (JSON スキーマなど) の使用がサポートされています。 各モデルのサポート内容について理解するには、モデルに関する詳細ページを参照してください。
ガードレールとコントロールを適用する
Azure AI モデル推論 API は、 Azure AI コンテンツ の安全性をサポートしています。 Azure AI Content Safety がオンになっているデプロイを使用すると、有害なコンテンツの出力を検出して防止することを目的とした分類モデルのアンサンブルを入力と出力が通過します。 コンテンツ フィルタリング システムは、入力プロンプトと出力入力候補の両方で、有害な可能性があるコンテンツの特定のカテゴリを検出してアクションを実行します。
次の例は、モデルが入力プロンプトで有害なコンテンツを検出したときにイベントを処理する方法を示しています。
try {
var messages = [
{ role: "system", content: "You are an AI assistant that helps people find information." },
{ role: "user", content: "Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills." },
];
var response = await client.path("/chat/completions").post({
model: "DeepSeek-R1",
body: {
messages: messages,
}
});
console.log(response.body.choices[0].message.content);
}
catch (error) {
if (error.status_code == 400) {
var response = JSON.parse(error.response._content);
if (response.error) {
console.log(`Your request triggered an ${response.error.code} error:\n\t ${response.error.message}`);
}
else
{
throw error;
}
}
}
ヒント
Azure AI Content Safety 設定を構成および制御する方法の詳細については、 Azure AI Content Safety のドキュメントを参照してください。
Von Bedeutung
この記事で "(プレビュー)" と付記されている項目は、現在、パブリック プレビュー段階です。 このプレビューはサービス レベル アグリーメントなしで提供されており、運用環境ではお勧めしません。 特定の機能はサポート対象ではなく、機能が制限されることがあります。 詳細については、「 Microsoft Azure プレビューの追加使用条件」を参照してください。
この記事では、Azure AI Foundry Models にデプロイされたチャット補完モデルの推論機能を使用する方法について説明します。
推論モデル
推論モデルは、数学、コーディング、科学、戦略、ロジスティックスなどの分野で高レベルのパフォーマンスを発揮できます。 これらのモデルが出力を生成する方法は、答えを生成する前に考え方の連鎖を明示的に使用して、考えられるすべてのパスを調べる方法です。 彼らはそれらを生成する際に彼らの答えを検証し、より正確な結論に到達するのに役立ちます。 その結果、推論モデルでは、効果的な結果を生成するためにプロンプトのコンテキストが少なくて済む場合があります。
モデルのパフォーマンスをスケーリングするこの方法は、待機時間とコストの増加に対してパフォーマンスを交換するため、 推論コンピューティング時間 と呼ばれます。 これに対し、他のアプローチは学習計算時間によってスケーリングされる場合があります。
推論モデルでは、次の 2 種類のコンテンツが出力として生成されます。
- 推論の入力候補
- 出力の入力候補
これらの生成結果はいずれも、モデルから生成されたコンテンツにカウントされます。 そのため、モデルに関連付けられているトークンの制限とコストに寄与します。 DeepSeek-R1
などの一部のモデルは、推論コンテンツで応答する場合があります。 o1
などの他のものは、完了のみを出力します。
[前提条件]
このチュートリアルを完了するには、次のものが必要です。
Azure サブスクリプション。 GitHub モデルを使用している場合は、エクスペリエンスをアップグレードし、プロセスで Azure サブスクリプションを作成できます。 その場合は、 GitHub モデルから Azure AI Foundry モデルへのアップグレード に関するページを参照してください。
Azure AI Foundry リソース (旧称 Azure AI Services)。 詳細については、「 Azure AI Foundry リソースの作成」を参照してください。
エンドポイント URL とキー。
プロジェクトに Azure AI 推論パッケージを追加します。
<dependency> <groupId>com.azure</groupId> <artifactId>azure-ai-inference</artifactId> <version>1.0.0-beta.4</version> </dependency>
Entra ID を使用する場合は、次のパッケージも必要です。
<dependency> <groupId>com.azure</groupId> <artifactId>azure-identity</artifactId> <version>1.15.3</version> </dependency>
次の名前空間をインポートします。
package com.azure.ai.inference.usage; import com.azure.ai.inference.EmbeddingsClient; import com.azure.ai.inference.EmbeddingsClientBuilder; import com.azure.ai.inference.ChatCompletionsClient; import com.azure.ai.inference.ChatCompletionsClientBuilder; import com.azure.ai.inference.models.EmbeddingsResult; import com.azure.ai.inference.models.EmbeddingItem; import com.azure.ai.inference.models.ChatCompletions; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.util.Configuration; import java.util.ArrayList; import java.util.List;
推論機能を備えたモデルのモデル デプロイ。 モデルがない場合は、Foundry モデルの追加と構成に関する記事を参照して推論モデルを追加します。
- この例では、
DeepSeek-R1
を使用します。
- この例では、
チャットで推論機能を使用する
まず、モデルを実行するクライアントを作成します。 次のコードでは、環境変数に格納されているエンドポイント URL とキーを使用しています。
ChatCompletionsClient client = new ChatCompletionsClient(
new URI("https://<resource>.services.ai.azure.com/models"),
new AzureKeyCredential(System.getProperty("AZURE_INFERENCE_CREDENTIAL")),
ヒント
Azure AI モデル推論 API を使用して、モデルを Azure AI Services リソースにデプロイしたことを確認します。 Deepseek-R1
は、サーバーレス API デプロイとしても使用できます。 ただし、このチュートリアルで説明しているように、これらのエンドポイントは model
パラメーターを受け取りません。 Azure AI Foundry ポータル>[モデル + エンドポイント] に移動して、[Azure AI サービス] セクションにモデルが表示されていることを確認できます。
Microsoft Entra ID をサポートするリソースを構成してある場合、次のコード スニペットを使用してクライアントを作成できます。
client = new ChatCompletionsClient(
new URI("https://<resource>.services.ai.azure.com/models"),
new DefaultAzureCredentialBuilder().build()
);
プロンプト推論モデル
推論モデルのプロンプトを作成する場合は、次の事項を考慮してください。
- 指示は簡単にし、思考の連鎖による手法は使用しないようにします。
- 組み込みの推論機能により、単純なゼロショットのプロンプトがより複雑なメソッドと同じくらい効果的になります。
- RAG シナリオなど、追加のコンテキストやドキュメントを提供する場合、最も関連性の高い情報のみを含めると、モデルの応答が過度に複雑になるのを防ぐのに役立つ場合があります。
- 推論モデルでは、システム メッセージの使用がサポートされる場合があります。 ただし、他の非推論モデルほど厳密には従わない場合があります。
- マルチターン アプリケーションを作成する場合は、「推論コンテンツ」セクションで説明されているように、推論コンテンツなしで、モデルから最終的 な 回答のみを追加することを検討してください。
推論モデルでは、応答の生成に時間がかかる場合があることに注意してください。 彼らは、より深く、より構造化された問題解決を可能にする思考の長い推論チェーンを使用しています。 また、自己検証を実行して回答をクロスチェックし、間違いを修正することで、出現する自己反射的な行動を示します。
チャット入力候補要求を作成する
次の例に、モデルに対する基本的なチャット要求を作成する方法を示します。
ChatCompletionsOptions requestOptions = new ChatCompletionsOptions()
.setModel("DeepSeek-R1")
.setMessages(Arrays.asList(
new ChatRequestUserMessage("How many languages are in the world?")
));
Response<ChatCompletions> response = client.complete(requestOptions);
応答は次のとおりです。モデルの使用状況の統計情報が表示されます。
System.out.println("Response: " + response.getValue().getChoices().get(0).getMessage().getContent());
System.out.println("Model: " + response.getValue().getModel());
System.out.println("Usage:");
System.out.println("\tPrompt tokens: " + response.getValue().getUsage().getPromptTokens());
System.out.println("\tTotal tokens: " + response.getValue().getUsage().getTotalTokens());
System.out.println("\tCompletion tokens: " + response.getValue().getUsage().getCompletionTokens());
Response: <think>Okay, the user is asking how many languages exist in the world. I need to provide a clear and accurate...</think>The exact number of languages in the world is challenging to determine due to differences in definitions (e.g., distinguishing languages from dialects) and ongoing documentation efforts. However, widely cited estimates suggest there are approximately **7,000 languages** globally.
Model: deepseek-r1
Usage:
Prompt tokens: 11
Total tokens: 897
Completion tokens: 886
推論に関するコンテンツ
推論モデルの中には、DeepSeek-R1 のように、補完を生成し、その背後にある推論が含まれるものがあります。 補完に関連付けられた推論は、<think>
と </think>
のタグ内の応答のコンテンツに含まれます。 モデルがどのシナリオで推論コンテンツを生成するかを選択できます。 次のように応答から推論コンテンツを抽出して、モデルの思考プロセスを理解できます。
String content = response.getValue().getChoices().get(0).getMessage().getContent()
Pattern pattern = Pattern.compile("<think>(.*?)</think>(.*)", Pattern.DOTALL);
Matcher matcher = pattern.matcher(content);
System.out.println("Response:");
if (matcher.find()) {
System.out.println("\tThinking: " + matcher.group(1));
System.out.println("\tAnswer: " + matcher.group(2));
}
else {
System.out.println("Response: " + content);
}
System.out.println("Model: " + response.getValue().getModel());
System.out.println("Usage:");
System.out.println("\tPrompt tokens: " + response.getValue().getUsage().getPromptTokens());
System.out.println("\tTotal tokens: " + response.getValue().getUsage().getTotalTokens());
System.out.println("\tCompletion tokens: " + response.getValue().getUsage().getCompletionTokens());
Thinking: Okay, the user is asking how many languages exist in the world. I need to provide a clear and accurate answer. Let's start by recalling the general consensus from linguistic sources. I remember that the number often cited is around 7,000, but maybe I should check some reputable organizations.\n\nEthnologue is a well-known resource for language data, and I think they list about 7,000 languages. But wait, do they update their numbers? It might be around 7,100 or so. Also, the exact count can vary because some sources might categorize dialects differently or have more recent data. \n\nAnother thing to consider is language endangerment. Many languages are endangered, with some having only a few speakers left. Organizations like UNESCO track endangered languages, so mentioning that adds context. Also, the distribution isn't even. Some countries have hundreds of languages, like Papua New Guinea with over 800, while others have just a few. \n\nA user might also wonder why the exact number is hard to pin down. It's because the distinction between a language and a dialect can be political or cultural. For example, Mandarin and Cantonese are considered dialects of Chinese by some, but they're mutually unintelligible, so others classify them as separate languages. Also, some regions are under-researched, making it hard to document all languages. \n\nI should also touch on language families. The 7,000 languages are grouped into families like Indo-European, Sino-Tibetan, Niger-Congo, etc. Maybe mention a few of the largest families. But wait, the question is just about the count, not the families. Still, it's good to provide a bit more context. \n\nI need to make sure the information is up-to-date. Let me think – recent estimates still hover around 7,000. However, languages are dying out rapidly, so the number decreases over time. Including that note about endangerment and language extinction rates could be helpful. For instance, it's often stated that a language dies every few weeks. \n\nAnother point is sign languages. Does the count include them? Ethnologue includes some, but not all sources might. If the user is including sign languages, that adds more to the count, but I think the 7,000 figure typically refers to spoken languages. For thoroughness, maybe mention that there are also over 300 sign languages. \n\nSummarizing, the answer should state around 7,000, mention Ethnologue's figure, explain why the exact number varies, touch on endangerment, and possibly note sign languages as a separate category. Also, a brief mention of Papua New Guinea as the most linguistically diverse country. \n\nWait, let me verify Ethnologue's current number. As of their latest edition (25th, 2022), they list 7,168 living languages. But I should check if that's the case. Some sources might round to 7,000. Also, SIL International publishes Ethnologue, so citing them as reference makes sense. \n\nOther sources, like Glottolog, might have a different count because they use different criteria. Glottolog might list around 7,000 as well, but exact numbers vary. It's important to highlight that the count isn't exact because of differing definitions and ongoing research. \n\nIn conclusion, the approximate number is 7,000, with Ethnologue being a key source, considerations of endangerment, and the challenges in counting due to dialect vs. language distinctions. I should make sure the answer is clear, acknowledges the variability, and provides key points succinctly.
Answer: The exact number of languages in the world is challenging to determine due to differences in definitions (e.g., distinguishing languages from dialects) and ongoing documentation efforts. However, widely cited estimates suggest there are approximately **7,000 languages** globally.
Model: DeepSeek-R1
Usage:
Prompt tokens: 11
Total tokens: 897
Completion tokens: 886
複数ターン会話を行うときは、説明が長くなる傾向があるため、チャット履歴に推論コンテンツを送信しないようにすると便利です。
コンテンツのストリーミング
既定では、入力候補 API は、生成されたコンテンツ全体を 1 つの応答で返します。 長い入力候補を生成する場合、応答が得られるまでに数秒かかることがあります。
コンテンツをストリーミングして、コンテンツが生成されるにつれ返されるようにできます。 コンテンツをストリーミングすると、コンテンツが使用可能になったときに入力候補の処理を開始できます。 このモードは、データのみのサーバー送信イベントとして応答をストリーム バックするオブジェクトを返します。 メッセージ フィールドではなく、デルタ フィールドからチャンクを抽出します。
ChatCompletionsOptions requestOptions = new ChatCompletionsOptions()
.setModel("DeepSeek-R1")
.setMessages(Arrays.asList(
new ChatRequestUserMessage("How many languages are in the world? Write an essay about it.")
))
.setMaxTokens(4096);
return client.completeStreamingAsync(requestOptions).thenAcceptAsync(response -> {
try {
printStream(response);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
出力を視覚化するには、ストリームを出力するヘルパー関数を定義します。 次の例では、推論コンテンツを使用せずに回答のみを配信するルーティングを実装しています。
public void printStream(StreamingResponse<StreamingChatCompletionsUpdate> response) throws Exception {
boolean isThinking = false;
for (StreamingChatCompletionsUpdate chatUpdate : response) {
if (chatUpdate.getContentUpdate() != null && !chatUpdate.getContentUpdate().isEmpty()) {
String content = chatUpdate.getContentUpdate();
if ("<think>".equals(content)) {
isThinking = true;
System.out.print("🧠 Thinking...");
System.out.flush();
} else if ("</think>".equals(content)) {
isThinking = false;
System.out.println("🛑\n\n");
} else if (content != null && !content.isEmpty()) {
System.out.print(content);
System.out.flush();
}
}
}
}
ストリーミングでコンテンツがどのように生成されるかを視覚化できます。
try {
streamMessageAsync(client).get();
} catch (Exception e) {
throw new RuntimeException(e);
}
パラメーター
一般に、推論モデルでは、チャット入力候補モデルで見られる以下のパラメーターがサポートされていません。
- 気温
- プレゼンス ペナルティ
- 繰り返しペナルティ
top_p
パラメーター
一部のモデルでは、ツールや構造化された出力 (JSON スキーマなど) の使用がサポートされています。 各モデルのサポート内容について理解するには、モデルに関する詳細ページを参照してください。
Von Bedeutung
この記事で "(プレビュー)" と付記されている項目は、現在、パブリック プレビュー段階です。 このプレビューはサービス レベル アグリーメントなしで提供されており、運用環境ではお勧めしません。 特定の機能はサポート対象ではなく、機能が制限されることがあります。 詳細については、「 Microsoft Azure プレビューの追加使用条件」を参照してください。
この記事では、Azure AI Foundry Models にデプロイされたチャット補完モデルの推論機能を使用する方法について説明します。
推論モデル
推論モデルは、数学、コーディング、科学、戦略、ロジスティックスなどの分野で高レベルのパフォーマンスを発揮できます。 これらのモデルが出力を生成する方法は、答えを生成する前に考え方の連鎖を明示的に使用して、考えられるすべてのパスを調べる方法です。 彼らはそれらを生成する際に彼らの答えを検証し、より正確な結論に到達するのに役立ちます。 その結果、推論モデルでは、効果的な結果を生成するためにプロンプトのコンテキストが少なくて済む場合があります。
モデルのパフォーマンスをスケーリングするこの方法は、待機時間とコストの増加に対してパフォーマンスを交換するため、 推論コンピューティング時間 と呼ばれます。 これに対し、他のアプローチは学習計算時間によってスケーリングされる場合があります。
推論モデルでは、次の 2 種類のコンテンツが出力として生成されます。
- 推論の入力候補
- 出力の入力候補
これらの生成結果はいずれも、モデルから生成されたコンテンツにカウントされます。 そのため、モデルに関連付けられているトークンの制限とコストに寄与します。 DeepSeek-R1
などの一部のモデルは、推論コンテンツで応答する場合があります。 o1
などの他のものは、完了のみを出力します。
[前提条件]
このチュートリアルを完了するには、次のものが必要です。
Azure サブスクリプション。 GitHub モデルを使用している場合は、エクスペリエンスをアップグレードし、プロセスで Azure サブスクリプションを作成できます。 その場合は、 GitHub モデルから Azure AI Foundry モデルへのアップグレード に関するページを参照してください。
Azure AI Foundry リソース (旧称 Azure AI Services)。 詳細については、「 Azure AI Foundry リソースの作成」を参照してください。
エンドポイント URL とキー。
次のコマンドを使用して Azure AI 推論パッケージをインストールします。
dotnet add package Azure.AI.Inference --prerelease
Entra ID を使用する場合は、次のパッケージも必要です。
dotnet add package Azure.Identity
推論機能を備えたモデルのモデル デプロイ。 モデルがない場合は、Foundry モデルの追加と構成に関する記事を参照して推論モデルを追加します。
- この例では、
DeepSeek-R1
を使用します。
- この例では、
チャットで推論機能を使用する
まず、モデルを実行するクライアントを作成します。 次のコードでは、環境変数に格納されているエンドポイント URL とキーを使用しています。
AzureAIInferenceClientOptions clientOptions = new AzureAIInferenceClientOptions(apiVersion);
ChatCompletionsClient client = new ChatCompletionsClient(
new Uri("https://<resource>.services.ai.azure.com/models"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_INFERENCE_CREDENTIAL")),
clientOptions
);
Microsoft Entra ID をサポートするリソースを構成してある場合、次のコード スニペットを使用してクライアントを作成できます。
AzureAIInferenceClientOptions clientOptions = new AzureAIInferenceClientOptions(
"2024-05-01-preview",
new string[] { "https://cognitiveservices.azure.com/.default" }
);
client = new ChatCompletionsClient(
new Uri("https://<resource>.services.ai.azure.com/models"),
new DefaultAzureCredential(),
clientOptions,
);
プロンプト推論モデル
推論モデルのプロンプトを作成する場合は、次の事項を考慮してください。
- 指示は簡単にし、思考の連鎖による手法は使用しないようにします。
- 組み込みの推論機能により、単純なゼロショットのプロンプトがより複雑なメソッドと同じくらい効果的になります。
- RAG シナリオなど、追加のコンテキストやドキュメントを提供する場合、最も関連性の高い情報のみを含めると、モデルの応答が過度に複雑になるのを防ぐのに役立つ場合があります。
- 推論モデルでは、システム メッセージの使用がサポートされる場合があります。 ただし、他の非推論モデルほど厳密には従わない場合があります。
- マルチターン アプリケーションを作成する場合は、「推論コンテンツ」セクションで説明されているように、推論コンテンツなしで、モデルから最終的 な 回答のみを追加することを検討してください。
推論モデルでは、応答の生成に時間がかかる場合があることに注意してください。 彼らは、より深く、より構造化された問題解決を可能にする思考の長い推論チェーンを使用しています。 また、自己検証を実行して回答をクロスチェックし、間違いを修正することで、出現する自己反射的な行動を示します。
チャット入力候補要求を作成する
次の例に、モデルに対する基本的なチャット要求を作成する方法を示します。
ChatCompletionsOptions requestOptions = new ChatCompletionsOptions()
{
Messages = {
new ChatRequestUserMessage("How many languages are in the world?")
},
Model = "deepseek-r1",
};
Response<ChatCompletions> response = client.Complete(requestOptions);
応答は次のとおりです。モデルの使用状況の統計情報が表示されます。
Console.WriteLine($"Response: {response.Value.Content}");
Console.WriteLine($"Model: {response.Value.Model}");
Console.WriteLine("Usage:");
Console.WriteLine($"\tPrompt tokens: {response.Value.Usage.PromptTokens}");
Console.WriteLine($"\tTotal tokens: {response.Value.Usage.TotalTokens}");
Console.WriteLine($"\tCompletion tokens: {response.Value.Usage.CompletionTokens}");
Response: <think>Okay, the user is asking how many languages exist in the world. I need to provide a clear and accurate...</think>The exact number of languages in the world is challenging to determine due to differences in definitions (e.g., distinguishing languages from dialects) and ongoing documentation efforts. However, widely cited estimates suggest there are approximately **7,000 languages** globally.
Model: deepseek-r1
Usage:
Prompt tokens: 11
Total tokens: 897
Completion tokens: 886
推論に関するコンテンツ
推論モデルの中には、DeepSeek-R1 のように、補完を生成し、その背後にある推論が含まれるものがあります。 補完に関連付けられた推論は、<think>
と </think>
のタグ内の応答のコンテンツに含まれます。 モデルがどのシナリオで推論コンテンツを生成するかを選択できます。 次のように応答から推論コンテンツを抽出して、モデルの思考プロセスを理解できます。
Regex regex = new Regex(pattern, RegexOptions.Singleline);
Match match = regex.Match(response.Value.Content);
Console.WriteLine("Response:");
if (match.Success)
{
Console.WriteLine($"\tThinking: {match.Groups[1].Value}");
Console.WriteLine($"\tAnswer: {match.Groups[2].Value}");
else
{
Console.WriteLine($"Response: {response.Value.Content}");
}
Console.WriteLine($"Model: {response.Value.Model}");
Console.WriteLine("Usage:");
Console.WriteLine($"\tPrompt tokens: {response.Value.Usage.PromptTokens}");
Console.WriteLine($"\tTotal tokens: {response.Value.Usage.TotalTokens}");
Console.WriteLine($"\tCompletion tokens: {response.Value.Usage.CompletionTokens}");
Thinking: Okay, the user is asking how many languages exist in the world. I need to provide a clear and accurate answer. Let's start by recalling the general consensus from linguistic sources. I remember that the number often cited is around 7,000, but maybe I should check some reputable organizations.\n\nEthnologue is a well-known resource for language data, and I think they list about 7,000 languages. But wait, do they update their numbers? It might be around 7,100 or so. Also, the exact count can vary because some sources might categorize dialects differently or have more recent data. \n\nAnother thing to consider is language endangerment. Many languages are endangered, with some having only a few speakers left. Organizations like UNESCO track endangered languages, so mentioning that adds context. Also, the distribution isn't even. Some countries have hundreds of languages, like Papua New Guinea with over 800, while others have just a few. \n\nA user might also wonder why the exact number is hard to pin down. It's because the distinction between a language and a dialect can be political or cultural. For example, Mandarin and Cantonese are considered dialects of Chinese by some, but they're mutually unintelligible, so others classify them as separate languages. Also, some regions are under-researched, making it hard to document all languages. \n\nI should also touch on language families. The 7,000 languages are grouped into families like Indo-European, Sino-Tibetan, Niger-Congo, etc. Maybe mention a few of the largest families. But wait, the question is just about the count, not the families. Still, it's good to provide a bit more context. \n\nI need to make sure the information is up-to-date. Let me think – recent estimates still hover around 7,000. However, languages are dying out rapidly, so the number decreases over time. Including that note about endangerment and language extinction rates could be helpful. For instance, it's often stated that a language dies every few weeks. \n\nAnother point is sign languages. Does the count include them? Ethnologue includes some, but not all sources might. If the user is including sign languages, that adds more to the count, but I think the 7,000 figure typically refers to spoken languages. For thoroughness, maybe mention that there are also over 300 sign languages. \n\nSummarizing, the answer should state around 7,000, mention Ethnologue's figure, explain why the exact number varies, touch on endangerment, and possibly note sign languages as a separate category. Also, a brief mention of Papua New Guinea as the most linguistically diverse country. \n\nWait, let me verify Ethnologue's current number. As of their latest edition (25th, 2022), they list 7,168 living languages. But I should check if that's the case. Some sources might round to 7,000. Also, SIL International publishes Ethnologue, so citing them as reference makes sense. \n\nOther sources, like Glottolog, might have a different count because they use different criteria. Glottolog might list around 7,000 as well, but exact numbers vary. It's important to highlight that the count isn't exact because of differing definitions and ongoing research. \n\nIn conclusion, the approximate number is 7,000, with Ethnologue being a key source, considerations of endangerment, and the challenges in counting due to dialect vs. language distinctions. I should make sure the answer is clear, acknowledges the variability, and provides key points succinctly.
Answer: The exact number of languages in the world is challenging to determine due to differences in definitions (e.g., distinguishing languages from dialects) and ongoing documentation efforts. However, widely cited estimates suggest there are approximately **7,000 languages** globally.
Model: DeepSeek-R1
Usage:
Prompt tokens: 11
Total tokens: 897
Completion tokens: 886
複数ターン会話を行うときは、説明が長くなる傾向があるため、チャット履歴に推論コンテンツを送信しないようにすると便利です。
コンテンツのストリーミング
既定では、入力候補 API は、生成されたコンテンツ全体を 1 つの応答で返します。 長い入力候補を生成する場合、応答が得られるまでに数秒かかることがあります。
コンテンツをストリーミングして、コンテンツが生成されるにつれ返されるようにできます。 コンテンツをストリーミングすると、コンテンツが使用可能になったときに入力候補の処理を開始できます。 このモードは、データのみのサーバー送信イベントとして応答をストリーム バックするオブジェクトを返します。 メッセージ フィールドではなく、デルタ フィールドからチャンクを抽出します。
static async Task StreamMessageAsync(ChatCompletionsClient client)
{
ChatCompletionsOptions requestOptions = new ChatCompletionsOptions()
{
Messages = {
new ChatRequestUserMessage("How many languages are in the world?")
},
MaxTokens=4096,
Model = "deepseek-r1",
};
StreamingResponse<StreamingChatCompletionsUpdate> streamResponse = await client.CompleteStreamingAsync(requestOptions);
await PrintStream(streamResponse);
}
出力を視覚化するには、ストリームを出力するヘルパー関数を定義します。 次の例では、推論コンテンツを使用せずに回答のみを配信するルーティングを実装しています。
static void PrintStream(StreamingResponse<StreamingChatCompletionsUpdate> response)
{
bool isThinking = false;
await foreach (StreamingChatCompletionsUpdate chatUpdate in response)
{
if (!string.IsNullOrEmpty(chatUpdate.ContentUpdate))
{
string content = chatUpdate.ContentUpdate;
if (content == "<think>")
{
isThinking = true;
Console.Write("🧠 Thinking...");
Console.Out.Flush();
}
else if (content == "</think>")
{
isThinking = false;
Console.WriteLine("🛑\n\n");
}
else if (!string.IsNullOrEmpty(content))
{
Console.Write(content);
Console.Out.Flush();
}
}
}
}
ストリーミングでコンテンツがどのように生成されるかを視覚化できます。
StreamMessageAsync(client).GetAwaiter().GetResult();
パラメーター
一般に、推論モデルでは、チャット入力候補モデルで見られる以下のパラメーターがサポートされていません。
- 気温
- プレゼンス ペナルティ
- 繰り返しペナルティ
top_p
パラメーター
一部のモデルでは、ツールや構造化された出力 (JSON スキーマなど) の使用がサポートされています。 各モデルのサポート内容について理解するには、モデルに関する詳細ページを参照してください。
ガードレールとコントロールを適用する
Azure AI モデル推論 API は、 Azure AI コンテンツ の安全性をサポートしています。 Azure AI Content Safety がオンになっているデプロイを使用すると、有害なコンテンツの出力を検出して防止することを目的とした分類モデルのアンサンブルを入力と出力が通過します。 コンテンツ フィルタリング システムは、入力プロンプトと出力入力候補の両方で、有害な可能性があるコンテンツの特定のカテゴリを検出してアクションを実行します。
次の例は、モデルが入力プロンプトで有害なコンテンツを検出したときにイベントを処理する方法を示しています。
try
{
requestOptions = new ChatCompletionsOptions()
{
Messages = {
new ChatRequestSystemMessage("You are an AI assistant that helps people find information."),
new ChatRequestUserMessage(
"Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."
),
},
Model = "deepseek-r1",
};
response = client.Complete(requestOptions);
Console.WriteLine(response.Value.Content);
}
catch (RequestFailedException ex)
{
if (ex.ErrorCode == "content_filter")
{
Console.WriteLine($"Your query has trigger Azure Content Safety: {ex.Message}");
}
else
{
throw;
}
}
ヒント
Azure AI Content Safety 設定を構成および制御する方法の詳細については、 Azure AI Content Safety のドキュメントを参照してください。
Von Bedeutung
この記事で "(プレビュー)" と付記されている項目は、現在、パブリック プレビュー段階です。 このプレビューはサービス レベル アグリーメントなしで提供されており、運用環境ではお勧めしません。 特定の機能はサポート対象ではなく、機能が制限されることがあります。 詳細については、「 Microsoft Azure プレビューの追加使用条件」を参照してください。
この記事では、Azure AI Foundry Models にデプロイされたチャット補完モデルの推論機能を使用する方法について説明します。
推論モデル
推論モデルは、数学、コーディング、科学、戦略、ロジスティックスなどの分野で高レベルのパフォーマンスを発揮できます。 これらのモデルが出力を生成する方法は、答えを生成する前に考え方の連鎖を明示的に使用して、考えられるすべてのパスを調べる方法です。 彼らはそれらを生成する際に彼らの答えを検証し、より正確な結論に到達するのに役立ちます。 その結果、推論モデルでは、効果的な結果を生成するためにプロンプトのコンテキストが少なくて済む場合があります。
モデルのパフォーマンスをスケーリングするこの方法は、待機時間とコストの増加に対してパフォーマンスを交換するため、 推論コンピューティング時間 と呼ばれます。 これに対し、他のアプローチは学習計算時間によってスケーリングされる場合があります。
推論モデルでは、次の 2 種類のコンテンツが出力として生成されます。
- 推論の入力候補
- 出力の入力候補
これらの生成結果はいずれも、モデルから生成されたコンテンツにカウントされます。 そのため、モデルに関連付けられているトークンの制限とコストに寄与します。 DeepSeek-R1
などの一部のモデルは、推論コンテンツで応答する場合があります。 o1
などの他のものは、完了のみを出力します。
[前提条件]
このチュートリアルを完了するには、次のものが必要です。
Azure サブスクリプション。 GitHub モデルを使用している場合は、エクスペリエンスをアップグレードし、プロセスで Azure サブスクリプションを作成できます。 その場合は、 GitHub モデルから Azure AI Foundry モデルへのアップグレード に関するページを参照してください。
Azure AI Foundry リソース (旧称 Azure AI Services)。 詳細については、「 Azure AI Foundry リソースの作成」を参照してください。
エンドポイント URL とキー。
推論機能を備えたモデルのモデル デプロイ。 モデルがない場合は、Foundry モデルの追加と構成に関する記事を参照して推論モデルを追加します。
- この例では、
DeepSeek-R1
を使用します。
- この例では、
チャットで推論機能を使用する
まず、モデルを実行するクライアントを作成します。 次のコードでは、環境変数に格納されているエンドポイント URL とキーを使用しています。
POST https://<resource>.services.ai.azure.com/openai/deployments/deepseek-r1/chat/completions?api-version=2024-10-21
Content-Type: application/json
api-key: <key>
Microsoft Entra ID のサポートを使用してリソースを構成した場合は、Authorization
ヘッダーにBearer <token>
形式のトークンを渡します。 スコープ https://cognitiveservices.azure.com/.default
を使用します。
POST https://<resource>.services.ai.azure.com/openai/deployments/deepseek-r1/chat/completions?api-version=2024-10-21
Content-Type: application/json
Authorization: Bearer <token>
Microsoft Entra ID を使用するには、アクセス権を付与するためにリソースに追加の構成が必要になる場合があります。 Microsoft Entra ID を使用してキーレス認証を構成する方法について説明します。
プロンプト推論モデル
推論モデルのプロンプトを作成する場合は、次の事項を考慮してください。
- 指示は簡単にし、思考の連鎖による手法は使用しないようにします。
- 組み込みの推論機能により、単純なゼロショットのプロンプトがより複雑なメソッドと同じくらい効果的になります。
- RAG シナリオなど、追加のコンテキストやドキュメントを提供する場合、最も関連性の高い情報のみを含めると、モデルの応答が過度に複雑になるのを防ぐのに役立つ場合があります。
- 推論モデルでは、システム メッセージの使用がサポートされる場合があります。 ただし、他の非推論モデルほど厳密には従わない場合があります。
- マルチターン アプリケーションを作成する場合は、「推論コンテンツ」セクションで説明されているように、推論コンテンツなしで、モデルから最終的 な 回答のみを追加することを検討してください。
推論モデルでは、応答の生成に時間がかかる場合があることに注意してください。 彼らは、より深く、より構造化された問題解決を可能にする思考の長い推論チェーンを使用しています。 また、自己検証を実行して回答をクロスチェックし、間違いを修正することで、出現する自己反射的な行動を示します。
チャット入力候補要求を作成する
次の例に、モデルに対する基本的なチャット要求を作成する方法を示します。
{
"model": "deepseek-r1",
"messages": [
{
"role": "user",
"content": "How many languages are in the world?"
}
]
}
応答は次のとおりです。モデルの使用状況の統計情報が表示されます。
{
"id": "0a1234b5de6789f01gh2i345j6789klm",
"object": "chat.completion",
"created": 1718726686,
"model": "DeepSeek-R1",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"reasoning_content": "Okay, the user is asking how many languages exist in the world. I need to provide a clear and accurate answer. Let's start by recalling the general consensus from linguistic sources. I remember that the number often cited is around 7,000, but maybe I should check some reputable organizations.\n\nEthnologue is a well-known resource for language data, and I think they list about 7,000 languages. But wait, do they update their numbers? It might be around 7,100 or so. Also, the exact count can vary because some sources might categorize dialects differently or have more recent data. \n\nAnother thing to consider is language endangerment. Many languages are endangered, with some having only a few speakers left. Organizations like UNESCO track endangered languages, so mentioning that adds context. Also, the distribution isn't even. Some countries have hundreds of languages, like Papua New Guinea with over 800, while others have just a few. \n\nA user might also wonder why the exact number is hard to pin down. It's because the distinction between a language and a dialect can be political or cultural. For example, Mandarin and Cantonese are considered dialects of Chinese by some, but they're mutually unintelligible, so others classify them as separate languages. Also, some regions are under-researched, making it hard to document all languages. \n\nI should also touch on language families. The 7,000 languages are grouped into families like Indo-European, Sino-Tibetan, Niger-Congo, etc. Maybe mention a few of the largest families. But wait, the question is just about the count, not the families. Still, it's good to provide a bit more context. \n\nI need to make sure the information is up-to-date. Let me think – recent estimates still hover around 7,000. However, languages are dying out rapidly, so the number decreases over time. Including that note about endangerment and language extinction rates could be helpful. For instance, it's often stated that a language dies every few weeks. \n\nAnother point is sign languages. Does the count include them? Ethnologue includes some, but not all sources might. If the user is including sign languages, that adds more to the count, but I think the 7,000 figure typically refers to spoken languages. For thoroughness, maybe mention that there are also over 300 sign languages. \n\nSummarizing, the answer should state around 7,000, mention Ethnologue's figure, explain why the exact number varies, touch on endangerment, and possibly note sign languages as a separate category. Also, a brief mention of Papua New Guinea as the most linguistically diverse country. \n\nWait, let me verify Ethnologue's current number. As of their latest edition (25th, 2022), they list 7,168 living languages. But I should check if that's the case. Some sources might round to 7,000. Also, SIL International publishes Ethnologue, so citing them as reference makes sense. \n\nOther sources, like Glottolog, might have a different count because they use different criteria. Glottolog might list around 7,000 as well, but exact numbers vary. It's important to highlight that the count isn't exact because of differing definitions and ongoing research. \n\nIn conclusion, the approximate number is 7,000, with Ethnologue being a key source, considerations of endangerment, and the challenges in counting due to dialect vs. language distinctions. I should make sure the answer is clear, acknowledges the variability, and provides key points succinctly.\n",
"content": "The exact number of languages in the world is challenging to determine due to differences in definitions (e.g., distinguishing languages from dialects) and ongoing documentation efforts. However, widely cited estimates suggest there are approximately **7,000 languages** globally.",
"tool_calls": null
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 11,
"total_tokens": 897,
"completion_tokens": 886
}
}
推論に関するコンテンツ
推論モデルの中には、DeepSeek-R1 のように、補完を生成し、その背後にある推論が含まれるものがあります。
完了に関連する理由は、フィールド reasoning_content
に含まれます。 モデルは、推論コンテンツを生成するシナリオを選択できます。
複数ターン会話を行うときは、説明が長くなる傾向があるため、チャット履歴に推論コンテンツを送信しないようにすると便利です。
コンテンツのストリーミング
既定では、入力候補 API は、生成されたコンテンツ全体を 1 つの応答で返します。 長い入力候補を生成する場合、応答が得られるまでに数秒かかることがあります。
コンテンツをストリーミングして、コンテンツが生成されるにつれ返されるようにできます。 コンテンツをストリーミングすると、コンテンツが使用可能になったときに入力候補の処理を開始できます。 このモードは、データのみのサーバー送信イベントとして応答をストリーム バックするオブジェクトを返します。 メッセージ フィールドではなく、デルタ フィールドからチャンクを抽出します。
生成結果をストリーミングするには、モデルを呼び出す際に"stream": true
を設定します。
{
"model": "DeepSeek-R1",
"messages": [
{
"role": "user",
"content": "How many languages are in the world?"
}
],
"stream": true,
"max_tokens": 2048
}
出力を視覚化するには、ストリームを出力するヘルパー関数を定義します。 次の例では、推論コンテンツを使用せずに回答のみを配信するルーティングを実装しています。
{
"id": "23b54589eba14564ad8a2e6978775a39",
"object": "chat.completion.chunk",
"created": 1718726371,
"model": "DeepSeek-R1",
"choices": [
{
"index": 0,
"delta": {
"role": "assistant",
"reasoning_content": "Okay,",
"content": ""
},
"finish_reason": null,
"logprobs": null
}
]
}
ストリーム内の最後のメッセージには、生成プロセスが停止した理由を示す finish_reason
が設定されています。
{
"id": "23b54589eba14564ad8a2e6978775a39",
"object": "chat.completion.chunk",
"created": 1718726371,
"model": "DeepSeek-R1",
"choices": [
{
"index": 0,
"delta": {
"reasoning_content": "",
"content": ""
},
"finish_reason": "stop",
"logprobs": null
}
],
"usage": {
"prompt_tokens": 11,
"total_tokens": 897,
"completion_tokens": 886
}
}
パラメーター
一般に、推論モデルでは、チャット入力候補モデルで見られる以下のパラメーターがサポートされていません。
- 気温
- プレゼンス ペナルティ
- 繰り返しペナルティ
top_p
パラメーター
一部のモデルでは、ツールや構造化された出力 (JSON スキーマなど) の使用がサポートされています。 各モデルのサポート内容について理解するには、モデルに関する詳細ページを参照してください。
ガードレールとコントロールを適用する
Azure AI モデル推論 API は、 Azure AI コンテンツ の安全性をサポートしています。 Azure AI Content Safety がオンになっているデプロイを使用すると、有害なコンテンツの出力を検出して防止することを目的とした分類モデルのアンサンブルを入力と出力が通過します。 コンテンツ フィルタリング システムは、入力プロンプトと出力入力候補の両方で、有害な可能性があるコンテンツの特定のカテゴリを検出してアクションを実行します。
次の例は、モデルが入力プロンプトで有害なコンテンツを検出したときにイベントを処理する方法を示しています。
{
"model": "DeepSeek-R1",
"messages": [
{
"role": "user",
"content": "Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."
}
]
}
{
"error": {
"message": "The response was filtered due to the prompt triggering Microsoft's content management policy. Please modify your prompt and retry.",
"type": null,
"param": "prompt",
"code": "content_filter",
"status": 400
}
}
ヒント
Azure AI Content Safety 設定を構成および制御する方法の詳細については、 Azure AI Content Safety のドキュメントを参照してください。