View a markdown version of this page

モデルから検証済みの JSON 結果を取得する - Amazon Bedrock

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

モデルから検証済みの JSON 結果を取得する

構造化出力は Amazon Bedrock の機能であり、モデルレスポンスがユーザー定義の JSON スキーマとツール定義に準拠し、本番 AI デプロイでのカスタム解析および検証メカニズムの必要性が軽減されます。

利点

構造化出力は、本番稼働用 AI アプリケーションの重要な課題に対処します。

  • スキーマのコンプライアンスを確保 – プロンプトベースのアプローチからエラー率と再試行ループを排除

  • 開発の複雑さの軽減 – カスタム解析および検証ロジックが不要

  • 運用コストの削減 – 失敗したリクエストと再試行を削減

  • 本稼働の信頼性 – 予測可能で機械読み取り可能な出力を必要とする AI アプリケーションの信頼性の高いデプロイを可能にします。

仕組み

構造化出力は、特定のスキーマに従うようにモデルレスポンスを制限し、ダウンストリーム処理のために有効で解析可能な出力を確保します。構造化出力は、次の 2 つの補完的なメカニズムを通じて使用できます。

JSON スキーマ出力形式

Anthropic Claude モデルを使用した InvokeModel API の場合は、 output_config.formatリクエストフィールドを使用します。オープンウェイトモデルでは、 response_format リクエストフィールドを使用します。Converse APIs、 outputConfig.textFormatリクエストフィールドを使用します。モデルのレスポンスは、指定された JSON スキーマに準拠します。

厳格なツールの使用

ツール定義に strict: trueフラグを追加して、ツール名と入力のスキーマ検証を有効にします。その後、モデルのツール呼び出しは、定義されたツール入力スキーマに従います。

これらのメカニズムは、同じリクエストで個別に使用することも、一緒に使用することもできます。詳細については、Bedrock API ドキュメントを参照してください。

リクエストワークフロー

以下に、Amazon Bedrock が構造化された出力でリクエストを処理する方法について説明します。

  1. 初期リクエストoutputConfig.textFormat、、または response_formatパラメータを介して JSON スキーマを含めるかoutput_config.format、推論リクエストに strict: trueフラグを持つツール定義を含めます。

  2. スキーマの検証 – Amazon Bedrock は、サポートされている JSON Schema Draft 2020-12 サブセットに対して JSON スキーマ形式を検証します。スキーマにサポートされていない機能が含まれている場合、Amazon Bedrock はすぐに 400 エラーを返します。

  3. 初回コンパイル – 新しいスキーマの場合、Amazon Bedrock は文法をコンパイルします。これには数分かかる場合があります。

  4. キャッシュ — 正常にコンパイルされた文法は、最初のアクセスから 24 時間キャッシュされます。キャッシュされた文法は AWS が管理するキーで暗号化されます。

  5. 後続のリクエスト – 同じアカウントからの同じスキーマはキャッシュされた文法を使用するため、推論レイテンシーは最小限のオーバーヘッドで標準リクエストと同等になります。

  6. レスポンス – 厳密なスキーマコンプライアンスで標準の推論レスポンスを受け取ります。

サポートされている APIsまたは機能

構造化出力は、次の Amazon Bedrock 機能で使用できます。

Converse および ConverseStream APIs – 会話推論用の Converse および ConverseStream APIs で構造化出力を使用します。

InvokeModel API と InvokeModelWithResponseStream APIs シングルターン推論にはInvokeModel API と InvokeModelWithResponseStream APIs で構造化出力を使用します。

クロスリージョン推論 – 追加セットアップなしで、クロスリージョン推論内で構造化出力を使用します。

バッチ推論 – 追加のセットアップなしで、バッチ推論内で構造化出力を使用します。

注記

構造化出力は、Anthropic モデルの引用と互換性がありません。構造化出力の使用中に引用を有効にすると、モデルは 400 エラーを返します。

サポートされているモデル

構造化出力をサポートするモデルを確認するには、モデルを一目で確認し、関心のあるモデルを選択してください。

リクエストの例

JSON スキーマ出力形式

次の例は、構造化出力で JSON スキーマ出力形式を使用する方法を示しています。

Converse API

{ "messages": [ { "role": "user", "content": [ { "text": "Given the following unstructured data, extract it into the provided structure." }, { "text": "..." } ] } ], "outputConfig": { "textFormat": { "type": "json_schema", "structure": { "jsonSchema": { "schema": "{\"type\": \"object\", \"properties\": {\"title\": {\"type\": \"string\", \"description\": \"title\"}, \"summary\": {\"type\": \"string\", \"description\": \"summary\"}, \"next_steps\": {\"type\": \"string\", \"description\": \"next steps\"}}, \"required\": [\"title\", \"summary\", \"next_steps\"], \"additionalProperties\": false}", "name": "data_extraction", "description": "Extract structured data from unstructured text" } } } } }

InvokeModel (Anthropic Claude)

{ "anthropic_version": "bedrock-2023-05-31", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Given the following unstructured data, extract it into the provided structure." }, { "type": "text", "text": "..." } ] } ], "max_tokens": 3000, "temperature": 1.0, "output_config": { "format": { "type": "json_schema", "schema": { "type": "object", "properties": { "title": { "type": "string", "description": "title" }, "summary": { "type": "string", "description": "summary" }, "next_steps": { "type": "string", "description": "next steps" } }, "required": [ "title", "summary", "next_steps" ], "additionalProperties": false } } } }

InvokeModel (オープンウェイトモデル)

{ "messages": [ { "role": "user", "content": "Given the following unstructured data, extract it into the provided structure." }, { "role": "user", "content": "..." } ], "inferenceConfig": { "maxTokens": 3000, "temperature": 1.0 }, "response_format": { "json_schema": { "name": "summarizer", "schema": { "type": "object", "properties": { "title": { "type": "string", "description": "title" }, "summary": { "type": "string", "description": "summary" }, "next_steps": { "type": "string", "description": "next steps" } }, "required": [ "title", "summary", "next_steps" ], "additionalProperties": false } }, "type": "json_schema" } }

厳格なツールの使用

次の例は、ツールで strict フィールドを使用する方法を示しています。

Converse API

{ "messages": [ { "role": "user", "content": [ { "text": "What's the weather like in New York?" } ] } ], "toolConfig": { "tools": [ { "toolSpec": { "name": "get_weather", "description": "Get the current weather for a specified location", "strict": true, "inputSchema": { "json": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": [ "fahrenheit", "celsius" ], "description": "The temperature unit to use" } }, "required": [ "location", "unit" ] } } } } ] } }

InvokeModel (Anthropic Claude)

{ "anthropic_version": "bedrock-2023-05-31", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "What's the weather like in San Francisco?" } ] } ], "max_tokens": 3000, "temperature": 1.0, "tools": [ { "name": "get_weather", "description": "Get the current weather for a specified location", "strict": true, "input_schema": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": [ "fahrenheit", "celsius" ], "description": "The temperature unit to use" } }, "required": [ "location", "unit" ], "additionalProperties": false } } ] }

InvokeModel (オープンウェイトモデル)

{ "messages": [ { "role": "user", "content": "What's the weather like in San Francisco?" } ], "tools": [ { "type": "function", "function": { "name": "get_weather", "description": "Get the current weather for a specified location", "strict": true, "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": [ "fahrenheit", "celsius" ], "description": "The temperature unit to use" } }, "required": [ "location", "unit" ] } } } ], "tool_choice": "auto", "max_tokens": 2000, "temperature": 1.0 }