推論モデルのクエリ
この記事では、クエリ要求を記述する方法を学習します 基盤モデル 推論タスクに最適化され、それらを 基盤モデル API エンドポイントに送信します。
Mosaic AI 基盤モデル API は、推論モデルを含むすべての基盤モデルと対話するための統一された API を提供します。 推論により、基盤モデルは複雑なタスクに取り組むための強化された能力を得ることができます。 また、一部のモデルは、最終的な答えを出す前に、段階的な思考プロセスを明らかにすることで透明性を提供します。
推論モデルの種類
モデルには、推論のみとハイブリッドの 2 種類があります。次の表は、異なるモデルが推論を制御するために異なるアプローチを使用する方法を示しています。
推論モデルの種類 | 詳細 | モデル例 | パラメータ |
---|---|---|---|
ハイブリッド推論 | 必要に応じて、迅速で即時の返信とより深い推論の両方をサポートします。 | クロードは | ハイブリッド推論を使用するには、次のパラメーターを含めます。
|
推論のみ | これらのモデルは、応答に常に内部推論を使用します。 |
| リクエストで次のパラメーターを使用します。
|
クエリの例
すべての推論モデルは、 チャット完了 エンドポイントを介してアクセスされます。
- Claude model example
- GPT OSS model example
from openai import OpenAI
import base64
import httpx
client = OpenAI(
api_key=os.environ.get('YOUR_DATABRICKS_TOKEN'),
base_url=os.environ.get('YOUR_DATABRICKS_BASE_URL')
)
response = client.chat.completions.create(
model="databricks-claude-3-7-sonnet",
messages=[{"role": "user", "content": "Why is the sky blue?"}],
max_tokens=20480,
extra_body={
"thinking": {
"type": "enabled",
"budget_tokens": 10240
}
}
)
msg = response.choices[0].message
reasoning = msg.content[0]["summary"][0]["text"]
answer = msg.content[1]["text"]
print("Reasoning:", reasoning)
print("Answer:", answer)
reasoning_effort
パラメーターは、"low"
、"medium"
(デフォルト)、または"high"
値を受け入れます。推論の努力が高まると、より思慮深く正確な応答が得られる可能性がありますが、レイテンシーとトークンの使用量が増加する可能性があります。
curl -X POST "https://<workspace_host>/serving-endpoints/databricks-gpt-oss-120b/invocations" \
-H "Authorization: Bearer $DATABRICKS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "user",
"content": "Why is the sky blue?"
}
],
"max_tokens": 4096,
"reasoning_effort": "high"
}'
API レスポンスには、思考コンテンツブロックとテキストコンテンツブロックの両方が含まれます。
ChatCompletionMessage(
role="assistant",
content=[
{
"type": "reasoning",
"summary": [
{
"type": "summary_text",
"text": ("The question is asking about the scientific explanation for why the sky appears blue... "),
"signature": ("EqoBCkgIARABGAIiQAhCWRmlaLuPiHaF357JzGmloqLqkeBm3cHG9NFTxKMyC/9bBdBInUsE3IZk6RxWge...")
}
]
},
{
"type": "text",
"text": (
"# Why the Sky Is Blue\n\n"
"The sky appears blue because of a phenomenon called Rayleigh scattering. Here's how it works..."
)
}
],
refusal=None,
annotations=None,
audio=None,
function_call=None,
tool_calls=None
)
複数のターンにわたる推論の管理
このセクションは、 databricks-claude-3-7-sonnet model
に固有のものです。
マルチターンの会話では、最後のアシスタントターンまたはツール使用セッションに関連付けられた推論ブロックのみがモデルに表示され、入力トークンとしてカウントされます。
推論トークンをモデルに戻さない場合 (たとえば、前の手順で推論する必要がない場合) は、推論ブロックを完全に省略できます。例えば:
response = client.chat.completions.create(
model="databricks-claude-3-7-sonnet",
messages=[
{"role": "user", "content": "Why is the sky blue?"},
{"role": "assistant", "content": text_content},
{"role": "user", "content": "Can you explain in a way that a 5-year-old child can understand?"}
],
max_tokens=20480,
extra_body={
"thinking": {
"type": "enabled",
"budget_tokens": 10240
}
}
)
answer = response.choices[0].message.content[1]["text"]
print("Answer:", answer)
ただし、モデルが前の推論プロセスを推論する必要がある場合 (たとえば、中間推論を表示するエクスペリエンスを構築している場合) は、前のターンの推論ブロックを含む、変更されていない完全なアシスタント メッセージを含める必要があります。完全なアシスタントメッセージでスレッドを続ける方法は次のとおりです。
assistant_message = response.choices[0].message
response = client.chat.completions.create(
model="databricks-claude-3-7-sonnet",
messages=[
{"role": "user", "content": "Why is the sky blue?"},
{"role": "assistant", "content": text_content},
{"role": "user", "content": "Can you explain in a way that a 5-year-old child can understand?"},
assistant_message,
{"role": "user", "content": "Can you simplify the previous answer?"}
],
max_tokens=20480,
extra_body={
"thinking": {
"type": "enabled",
"budget_tokens": 10240
}
}
)
answer = response.choices[0].message.content[1]["text"]
print("Answer:", answer)
推論モデルはどのように機能しますか?
推論モデルでは、標準の入力トークンと出力トークンに加えて、特別な推論トークンが導入されます。これらのトークンにより、モデルはプロンプトを通じて「考え」、プロンプトを分解し、さまざまな応答方法を検討できます。この内部推論プロセスの後、モデルは最終的な回答を目に見える出力トークンとして生成します。databricks-claude-3-7-sonnet
のような一部のモデルは、これらの推論トークンをユーザーに表示する一方で、OpenAI o シリーズなどの他のモデルはそれらを破棄し、最終出力で公開しません。