

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

# プロンプトを送信し、モデル推論を使用してレスポンスを生成する
<a name="inference"></a>

推論とは、モデルに提供された入力から出力を生成するプロセスを指します。

Amazon Bedrock には、一連の基盤モデルが用意されていて、以下のモダリティの出力を生成するために使用できます。基盤モデルが対応しているモダリティについては、「[Amazon Bedrock でサポートされている基盤モデル](models-supported.md)」を参照してください。


****  

| 出力モダリティ | 説明 | ユースケースの例 | 
| --- | --- | --- | 
| テキスト | テキスト入力を提供し、各種のテキストを生成する | チャット、質疑応答、ブレインストーミング、要約、コード生成、表作成、データの書式設定、書き換え | 
| Image | テキストまたは入力画像を提供し、画像を生成または変更する | 画像生成、画像編集、画像バリエーション | 
| 動画 | テキストまたはリファレンスイメージを提供して動画を生成する | 動画生成、イメージの動画への変換 | 
| 埋め込み | テキスト、画像、またはテキストと画像の両方を提供し、入力を表す数値のベクトルを生成する。出力ベクトルを他の埋め込みベクトルと比較して、セマンティック類似性 (テキストの場合) または視覚的類似性 (画像の場合) を決定できます。 | テキストと画像の検索、クエリ、分類、レコメンデーション、パーソナライズ、[ナレッジベースの作成](knowledge-base.md) | 

**Topics**
+ [さまざまなモデル推論方法のユースケースについて学ぶ](inference-methods.md)
+ [Amazon Bedrock での推論の仕組み](inference-how.md)
+ [推論パラメータでレスポンスの生成に影響を与える](inference-parameters.md)
+ [モデル推論をサポートするリージョンとモデル](inference-supported.md)
+ [モデル推論を実行するための前提条件](inference-prereq.md)
+ [Generate responses in the console using playgrounds](playgrounds.md)
+ [モデル推論を使用してモデルのレスポンスを強化する](inference-reasoning.md)
+ [レイテンシーのモデル推論を最適化する](latency-optimized-inference.md)
+ [OpenAI APIsを使用してレスポンスを生成する](bedrock-mantle.md)
+ [API を使用してプロンプトを送信し、レスポンスを生成する](inference-api.md)
+ [モデルから検証済みの JSON 結果を取得する](structured-output.md)
+ [Computer Use ツールを使用して Amazon Bedrock モデルレスポンスを完了する](computer-use.md)

# さまざまなモデル推論方法のユースケースについて学ぶ
<a name="inference-methods"></a>

モデル推論は、次の方法で直接実行できます。


****  

| Method | ユースケース | 
| --- | --- | 
| [Amazon Bedrock コンソールのプレイグラウンド](playgrounds.md) | ユーザーフレンドリーなグラフィカルインターフェイスで推論を実行します。探索に便利です。 | 
| [Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) または [ConverseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html) | モデル入力用の統合 API を使用して、会話型アプリケーションを実装します。 | 
| [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) または [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) | 単一のプロンプトを送信し、レスポンスを同期的に生成します。レスポンスをリアルタイムで生成する場合や検索クエリに便利です。 | 
| [StartAsyncInvoke](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_StartAsyncInvoke.html) | 単一のプロンプトを送信し、レスポンスを非同期的に生成します。レスポンスを大規模に生成する場合に役立ちます。 | 
| [CreateModelInvocationJob](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_CreateModelInvocationJob.html) | プロンプトのデータセットを準備し、レスポンスをバッチで生成します。 | 
| [レスポンス API](https://platform.openai.com/docs/api-reference/responses) | 組み込みツールの使用 (検索、コードインタープリタ）、マルチモーダル入力、ステートフルな会話を必要とする最新のエージェントアプリケーションには、 Responses API を使用します。 | 
| [チャットの完了](https://platform.openai.com/docs/api-reference/chat) | Chat Completions API は、チャット履歴管理と低レイテンシーを完全に制御する必要がある、軽量でステートレスなテキストに焦点を当てたタスクに使用します。 | 

次の Amazon Bedrock の機能も、より大きなワークフローの一部としてモデル推論を使用します。
+ [モデル評価](evaluation.md)では、[CreateEvaluationJob](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_CreateEvaluationJob.html) リクエストを送信した後、モデル呼び出しプロセスを使用して異なるモデルのパフォーマンスを評価します。
+ [ナレッジベース](knowledge-base.md)は、[https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) API を使用してナレッジベースから得た検索結果に基づいてレスポンスを生成するときに、モデル呼び出しを使用します。
+ [エージェント](agents.md)は、[https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) リクエストの処理中のさまざまな段階で、モデル呼び出しを使用してレスポンスを生成します。
+ [フロー](flows.md)には、プロンプト、ナレッジベース、エージェントなど、モデルの呼び出しを使用する Amazon Bedrock リソースが含まれています。

異なるプロンプトや推論パラメータでさまざまな基盤モデルをテストした後、必要な仕様でこれらの API を呼び出すようにアプリケーションを設定できます。

# Amazon Bedrock での推論の仕組み
<a name="inference-how"></a>

モデルに入力を送信すると、モデルはその後に続く可能性のあるトークンのシーケンスを予測し、そのシーケンスを出力として返します。Amazon Bedrock により、選択した基盤モデルで推論を実行できるようになります。推論を実行する場合は、次の入力を提供します。
+ **プロンプト** — レスポンスを生成するためにモデルに提供される入力。プロンプトの書き方については、「[プロンプトエンジニアリングの概念](prompt-engineering-guidelines.md)」を参照してください。プロンプトインジェクション攻撃の対策については、「[プロンプトインジェクションのセキュリティ](prompt-injection.md)」を参照してください。
+ **モデル** – プロンプトで推論を実行するようにモデルにリクエストします。選択したモデルによって、スループットのレベルも指定されます。つまり、処理できる入出力トークンの数とレートが定義されます。次のタイプのモデルにリクエストを行うことができます。
  + **ベースモデル** – 推論の実行に使用する基盤モデル。リクエストは 1 つの AWS リージョンに送信されます。モデル ID については、「[Amazon Bedrock でサポートされている基盤モデル](models-supported.md)」を参照してください。Amazon Bedrock で使用できる基盤モデルの詳細については、「[Amazon Bedrock 基盤モデルの情報](foundation-models-reference.md)」を参照してください。
  + **推論プロファイル** – 推論の実行に使用する基盤モデル。リクエストは複数の AWS リージョンのモデルに対して行われます。推論プロファイルについては、「[推論プロファイルでサポートされているリージョンおよびモデル](inference-profiles-support.md)」を参照してください。
**注記**  
モデルは、リージョンと API メソッドによって、ベースモデルと推論プロファイルの可用性が異なります。詳細については、「[Amazon Bedrock でサポートされている基盤モデル](models-supported.md)」、および「[基盤モデルリファレンス](foundation-models-reference.md)」の個々のモデルのページを参照してください。
  + **プロビジョンドスループット** – 専用スループットを購入した基盤モデル。詳細については、[Amazon Bedrock のプロビジョンドスループットでモデル呼び出し容量を増やす](prov-throughput.md)を参照してください。
  + **カスタムモデル** – モデルのカスタマイズによって重みが変更された基盤モデル。詳細については、「[モデルをカスタマイズしてユースケースのパフォーマンスを向上させる](custom-models.md)」を参照してください。
+ **推論パラメータ** — モデルレスポンスを制限したり影響を与えたりするように調整できる値のセット。推論パラメータの詳細については、「[推論パラメータでレスポンスの生成に影響を与える](inference-parameters.md)」および「[Inference request parameters and response fields for foundation models](model-parameters.md)」を参照してください。

## 異なる AWS リージョンでのモデルの呼び出し
<a name="inference-how-regions"></a>

モデルを呼び出すときは、そのモデルを呼び出す AWS リージョンを選択します。実行できるリクエストの頻度とサイズに対応するクォータは、リージョンによって異なります。これらのクォータは、「[Amazon Bedrock サービスクォータ](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#limits_bedrock)」で次のクォータを検索することで確認できます。
+ *\$1\$1Model\$1* の 1 分あたりのオンデマンドモデル推論のリクエスト数
+ *\$1\$1model\$1* の 1 分あたりのオンデマンド InvokeModel のトークン数

基盤モデル自体ではなく、推論プロファイルを呼び出すこともできます。推論プロファイルは、モデルと、その推論プロファイルによるモデル呼び出しリクエストのルーティング先となる 1 つ以上のリージョンを定義します。複数のリージョンを含む推論プロファイルを呼び出すことで、スループットを向上させることができます。詳細については、「[クロスリージョン推論によりスループットを向上させる](cross-region-inference.md)」を参照してください。推論プロファイルで実行できるリクエストの頻度とサイズに対応するクォータを確認するには、「[Amazon Bedrock サービスクォータ](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#limits_bedrock)」で次のクォータを検索します。
+ *\$1\$1Model\$1* の 1 分あたりのクロスリージョン InvokeModel のリクエスト数
+ *\$1\$1Model\$1* の 1 分あたりのクロスリージョン InvokeModel のトークン数
+ *\$1\$1Model\$1* の 1 分あたりのグローバルクロスリージョン InvokeModel のリクエスト数
+ *\$1\$1Model\$1* の 1 分あたりのグローバルクロスリージョン InvokeModel のトークン数

リージョンへのリクエストは、同じ親リージョンを共有するローカルゾーンから提供される場合があります。例えば、米国東部 (バージニア北部) (us-east-1) へのリクエストは、米国アトランタ (us-east-1-atl-2a) など、それに関連付けられている任意のローカルゾーンから提供される場合があります。

クロスリージョン推論の使用時にも同じ原則が適用されます。例えば、米国の Anthropic Claude 3 Haiku 推論プロファイルへのリクエストは、親リージョンが米国シアトル (us-west-2-sea-1a) など米国にある任意のローカルゾーンから提供される場合があります。新しいローカルゾーンは、AWS に追加されると、対応するクロスリージョン推論エンドポイントにも追加されます。

ローカルエンドポイントとそれらが関連付けられている親リージョンのリストを確認するには、「[AWS ローカルゾーンの場所](https://aws.amazon.com/about-aws/global-infrastructure/localzones/locations/)」を参照してください。

Amazon Bedrock でクロスリージョン推論プロファイルを呼び出すと、リクエストはソースリージョンから発信され、そのプロファイルで定義されている送信先リージョンの 1 つに自動的にルーティングされて、パフォーマンスが最適化されます。グローバルクロスリージョン推論プロファイルの送信リージョンには、商用リージョンがすべて含まれます。

特定のモデルに対するグローバルクロスリージョン推論プロファイルは、AWS で追加される商用リージョン (リクエストを処理できるリージョン) の数の増加に伴い、時間の経過とともに変化する可能性があります。ただし、推論プロファイルが地域 (米国、欧州、アジア太平洋など) に関連付けられている場合、その送信先リージョンリストは変更されません。AWS では、新しいリージョンを組み込む新しい推論プロファイルが作成される場合があります。これらの推論プロファイルを使用するようにシステムを更新するには、セットアップ内の ID を新しいものに変更します。

**注記**  
クロスリージョン推論プロファイルの送信先リージョンには、**オプトインリージョン**を含めることができます。オプトインリージョンは、AWS アカウントまたは組織レベルで明示的に有効にする必要があるリージョンです。詳細については、「[アカウントで AWS リージョンを有効化または無効化する](https://docs.aws.amazon.com/accounts/latest/reference/manage-acct-regions.html)」を参照してください。クロスリージョン推論プロファイルを使用する場合、プロファイル内の任意の送信先リージョンをアカウントでオプトインしなかった場合でも、推論リクエストはその送信先リージョンにルーティングされる可能性があります。

サービスコントロールポリシー (SCP) と AWS Identity and Access Management (IAM) ポリシーは連携して、クロスリージョン推論が許可されている場所を制御します。SCP を使用すると、Amazon Bedrock で推論に使用できるリージョンを制御でき、IAM ポリシーを使用すると、推論を実行するためのアクセス許可を持つユーザーまたはロールを定義できます。クロスリージョン推論プロファイルの任意の送信先リージョンが SCP でブロックされている場合、他のリージョンが許可されていてもリクエストは失敗します。クロスリージョン推論による効率的なオペレーションを確保するために、選択した推論プロファイルに含まれるすべての送信先リージョンで、必要なすべての Amazon Bedrock 推論アクション (`bedrock:InvokeModel*` や `bedrock:CreateModelInvocationJob` など) を許可するように SCP と IAM ポリシーを更新できます。詳細については、「[https://aws.amazon.com/blogs/machine-learning/enable-amazon-bedrock-cross-region-inference-in-multi-account-environments/](https://aws.amazon.com/blogs/machine-learning/enable-amazon-bedrock-cross-region-inference-in-multi-account-environments/)Enabling Amazon Bedrock cross-Region inference in multi-account environments」を参照してください。

# 推論パラメータでレスポンスの生成に影響を与える
<a name="inference-parameters"></a>

モデル推論を実行するときは、推論パラメータを調整して、モデルレスポンスに影響を与えることができます。推論パラメータによって、モデルが生成中に検討する出力候補のプールを変更したり、最終レスポンスを制限したりできます。

推論パラメータのデフォルト値と範囲はモデルによって異なります。さまざまなモデルの推論パラメータについては、「[Inference request parameters and response fields for foundation models](model-parameters.md)」を参照してください。

以下のカテゴリのパラメータは、さまざまなモデルに共通しています。

**Topics**
+ [ランダム性と多様性](#inference-randomness)
+ [Length](#inference-length)

## ランダム性と多様性
<a name="inference-randomness"></a>

どのシーケンスでも、モデルはシーケンス内の次のトークンのオプションの確率分布を決定します。出力で各トークンを生成するために、モデルはこの分布からサンプリングします。ランダム性と多様性とは、モデルのレスポンスにおける変動量を指します。これらの要因は、分布を制限または調整することで制御できます。基盤モデルでは通常、レスポンスのランダム性と多様性を制御するための以下のパラメータがサポートされています。
+ **[温度]** — 予測出力の確率分布の形状に影響し、モデルがより確率の低い出力を選択する可能性にも影響します。
  + 確率の高い出力を選択するには、モデルに影響する値を低く設定します。
  + 確率の低い出力を選択するには、モデルに影響する値を高く設定します。

  専門用語で言うと、温度は次のトークンの確率質量関数を変調します。温度が低いほど関数が急勾配になり、レスポンスがより決定論的になります。一方、温度が高いほど関数が平坦になり、ランダムなレスポンスが多くなります。
+ **[トップ K]** — モデルが次のトークンについて検討する最も可能性の高い候補の数。
  + 小さい値を選択するとプールのサイズが小さくなり、選択肢がより可能性の高い出力に限定されます。
  + 大きい値を選択するとプールのサイズが大きくなり、モデルが可能性の低い出力を考慮できるようになります。

  例えば、[トップ K] に 50 の値を選択した場合、モデルはシーケンスにおいて次に来る可能性が最も高い 50 個のトークンの中から選択します。
+ **[トップ P]** — モデルが次のトークンについて考慮する最も可能性の高い候補のパーセンテージ。
  + 小さい値を選択するとプールのサイズが小さくなり、選択肢がより可能性の高い出力に限定されます。
  + 大きい値を選択するとプールのサイズが大きくなり、モデルが可能性の低い出力を考慮できるようになります。

  専門用語で言うと、このモデルはレスポンスのセットの累積確率分布を計算し、分布の上位 P% のみを考慮します。

  例えば、[トップ P] に 0.8 の値を選択した場合、モデルはシーケンスにおいて次に来る可能性が最も高い 80% のトークンの確率分布から選択します。

次の表は、これらのパラメータの効果をまとめたものです。


****  

| パラメータ | 低い値の効果 | 高い値の効果 | 
| --- | --- | --- | 
| 温度 | 高い確率のトークンの可能性を向上する 低い確率のトークンの可能性を低下する | 低い確率のトークンの可能性を向上する高い確率のトークンの可能性を低下する | 
| トップ K | 低い確率のトークンを削除する | 低い確率のトークンを許可する | 
| トップ P | 低い確率のトークンを削除する | 低い確率のトークンを許可する | 

これらのパラメータを理解するための例として、プロンプト **I hear the hoof beats of "** の例について考えてみましょう。モデルが、次の 3 つの単語を次のトークンの候補として決定したとします。このモデルでは、各単語に確率も割り当てられます。

```
{
    "horses": 0.7,
    "zebras": 0.2,
    "unicorns": 0.1
}
```
+ **[温度]** を高く設定すると、確率分布が平坦になり、確率の差が小さくなるため、「ユニコーン」を選ぶ確率は上がり、「馬」を選ぶ確率は下がります。
+ **[トップ K]** を 2 に設定すると、モデルは最も可能性の高い候補の上位 2 つ、つまり「馬」と「シマウマ」のみを考慮します。
+ **[トップ P]** を 0.7 に設定した場合、モデルは「horses」のみを考慮します。確率分布の上位 70% に入る候補は「horses」だけだからです。**[トップ P]** を 0.9 に設定した場合は、「horses」と「zebras」が考慮されます。いずれも確率分布の上位 90% に入っているからです。

## Length
<a name="inference-length"></a>

基盤モデルでは通常、レスポンスの長さを制限するパラメータがサポートされています。これらのパラメータの例を以下に示します。
+ **[レスポンスの長さ]** — 生成されたレスポンスで返されるトークンの最小数または最大数を指定する正確な値。
+ **[ペナルティ]** — レスポンス内の出力にどの程度ペナルティを課すかを指定します。次に例を示します。
  + レスポンスの長さ。
  + レスポンスで繰り返されるトークン。
  + レスポンス内のトークンの頻度。
  + レスポンス内のトークンのタイプ。
+ **[停止シーケンス]** — モデルがそれ以上トークンを生成しないようにする文字シーケンスを指定します。指定した停止シーケンスをモデルが生成すると、そのシーケンスの後に生成が停止します。

# モデル推論をサポートするリージョンとモデル
<a name="inference-supported"></a>

基盤モデルを使用したモデル推論は、すべてのリージョンおよび Amazon Bedrock でサポートされているすべてのモデルでサポートされています。Amazon Bedrock でサポートされているリージョンとモデルを確認するには、「[Amazon Bedrock でサポートされている基盤モデル](models-supported.md)」を参照してください。

基盤モデル以外の Amazon Bedrock リソースを使用してモデル推論を実行することもできます。さまざまなリソースのリージョンとモデルの可用性については、以降のページを参照してください。
+ [推論プロファイルでサポートされているリージョンおよびモデル](inference-profiles-support.md)
+ [プロンプト管理でサポートされているリージョンおよびモデル](prompt-management-supported.md)
**注記**  
[InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) と [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) は、設定で Anthropic Claude または Meta Llama モデルが指定されているプロンプト管理からのプロンプトでのみ機能します。
+ [ファインチューニングでサポートされているモデルとリージョン](custom-model-fine-tuning.md#custom-model-supported)
+ [カスタムモデルのインポートを使用して、カスタマイズされたオープンソースモデルを Amazon Bedrock にインポートする](model-customization-import-model.md)
+ [Amazon Bedrock ガードレールをサポートするリージョンとモデル](guardrails-supported.md)

# モデル推論を実行するための前提条件
<a name="inference-prereq"></a>

ロールでモデル推論を実行するには、モデルの呼び出し API アクションの実行を許可する必要があります。ロールに [AmazonBedrockFullAccess](security-iam-awsmanpol.md#security-iam-awsmanpol-AmazonBedrockFullAccess)AWS 管理ポリシーがアタッチされている場合は、このセクションをスキップできます。アタッチされていない場合は、次のアクセス許可をロールにアタッチして、Amazon Bedrock でサポートされているすべてのリソースで [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) アクション、[InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) アクション、[Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) アクション、[ConverseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html) アクションの使用を許可します。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "ModelInvocationPermissions",
            "Effect": "Allow",
            "Action": [
                "bedrock:InvokeModel",
                "bedrock:InvokeModelWithResponseStream",
                "bedrock:GetInferenceProfile",
                "bedrock:ListInferenceProfiles",
                "bedrock:RenderPrompt",
                "bedrock:GetCustomModel",
                "bedrock:ListCustomModels",
                "bedrock:GetImportedModel",
                "bedrock:ListImportedModels",
                "bedrock:GetProvisionedModelThroughput",
                "bedrock:ListProvisionedModelThroughputs",
                "bedrock:GetGuardrail",
                "bedrock:ListGuardrails",
                "bedrock:ApplyGuardrail"
            ],
            "Resource": "*"
        }
    ]
}
```

------

アクセス許可をさらに制限するには、アクションを省略するか、アクセス許可をフィルタリングするためのリソースや条件キーを指定できます。アクション、リソース、条件キーの詳細については、「*サービス認可リファレンス*」の以下のトピックを参照してください。
+ [Amazon Bedrock で定義されるアクション](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonbedrock.html#amazonbedrock-actions-as-permissions) – アクション、`Resource` フィールドで範囲を定義できるリソースタイプ、`Condition` フィールドでアクセス許可をフィルタリングできる条件キーについて説明しています。
+ [Amazon Bedrock で定義されるリソースタイプ](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonbedrock.html#amazonbedrock-resources-for-iam-policies) – Amazon Bedrock のリソースタイプについて説明しています。
+ [Amazon Bedrock の条件キー](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonbedrock.html#amazonbedrock-policy-keys) – Amazon Bedrock の条件キーについて説明しています。

次のリストでは、ユースケースに応じてアクションが必要かどうかをまとめています。
+ `bedrock:InvokeModel` – モデルの呼び出しを実行するために必要です。ロールが [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) API オペレーションと [Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) API オペレーションを呼び出すことを許可します。
+ `bedrock:InvokeModelWithResponseStream` – モデルの呼び出しを実行し、ストリーミングレスポンスを返すために必要です。ロールが [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) API オペレーションと [Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html)Stream API オペレーションを呼び出すことを許可します。
+ 次のアクションは、ロールに基盤モデル以外の Amazon Bedrock リソースを使用して推論を実行することを許可します。
  + `bedrock:GetInferenceProfile` – [推論プロファイル](inference-profiles.md)を使用して推論を実行するために必要です。
  + `bedrock:RenderPrompt` – [プロンプト管理](prompt-management.md)からプロンプトを呼び出すために必要です。
  + `bedrock:GetCustomModel` – [カスタムモデル](custom-models.md)を使用して推論を実行するために必要です。
  + `bedrock:GetImportedModel` – [インポートしたモデル](model-customization-import-model.md)を使用して推論を実行するために必要です。
  + `bedrock:GetProvisionedModelThroughput` – [プロビジョンドスループット](prov-throughput.md)を使用して推論を実行するために必要です。
+ 次のアクションは、Amazon Bedrock コンソールで基盤モデル以外の Amazon Bedrock リソースを表示し、選択することをロールに許可します。
  + `bedrock:ListInferenceProfiles` – Amazon Bedrock コンソールで[推論プロファイル](custom-models.md)を選択するために必要です。
  + `bedrock:ListCustomModels` – Amazon Bedrock コンソールで[カスタムモデル](custom-models.md)を選択するために必要です。
  + `bedrock:ListImportedModels` – Amazon Bedrock コンソールで[インポートしたモデル](model-customization-import-model.md)を選択するために必要です。
  + `bedrock:ListProvisionedModelThroughputs` – Amazon Bedrock コンソールで[プロビジョンドスループット](prov-throughput.md)を選択するために必要です。
+ 次のアクションは、モデルの呼び出し中に [Amazon Bedrock のガードレール](guardrails.md)からガードレールにアクセスして適用することをロールに許可します。
  + `bedrock:GetGuardrail` – モデルの呼び出し中にガードレールを使用するために必要です。
  + `bedrock:ApplyGuardrail` – モデルの呼び出し中にガードレールを適用するために必要です。
  + `bedrock:ListGuardrails` – Amazon Bedrock コンソールでガードレールを選択するために必要です。

# Generate responses in the console using playgrounds
<a name="playgrounds"></a>

Amazon Bedrock プレイグラウンドは、さまざまなモデルで推論を実行し、さまざまな設定を使用するためのビジュアルインターフェイス AWS マネジメントコンソール を提供する のツールです。プレイグラウンドを使用して、アプリケーションに統合する前にさまざまなモデルと値をテストできます。

プレイグラウンドでプロンプトを実行することは、API で [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html)、[InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html)、[Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html)、または [ConverseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html) リクエストを行うのと同じです。

Amazon Bedrock では、以下を試すプレイグラウンドがあります。
+ **チャット/テキスト** – テキストプロンプトを送信し、レスポンスを生成するか、または音声とやり取りします。次のいずれかのモードを選択できます。
  + **チャット** – テキストプロンプトを送信するか、音声とやり取りします。テキストプロンプトには、プロンプトを補足するイメージやドキュメントも含めることができます。送信するそれ以降のプロンプトには、プロンプトとレスポンスのシーケンスが会話に似るように、以前のプロンプトがコンテキストとして含まれます。
  + **単一プロンプト** – 単一テキストプロンプトを送信し、それに対するレスポンスを生成します。
**注記**  
Amazon Nova Sonic などの音声から音声へのモデルは、チャットモードでのみ使用できます。音声から音声へのモデルでは、比較モードはサポートされていません。
+ **イメージ** – テキストプロンプトを送信してイメージを生成します。イメージプロンプトを送信し、編集するか、バリエーションを生成するかを指定することもできます。
+ **マルチモーダル (プレビュー)** – テキストプロンプトを送信し、マルチモーダルコンテンツを生成します。また、チャットモードとシングルプロンプトモードもサポートしています。

次の手順では、プレイグラウンドでプロンプトを送信する方法、調整できるオプション、およびモデルがレスポンスを生成した後に実行できるアクションについて説明します。

**プレイグラウンドを使用するには**

1. まだ設定していない場合は、使用するモデルへのアクセスをリクエストします。詳細については、「[Access Amazon Bedrock foundation models](model-access.md)」を参照してください。

1. Amazon Bedrock コンソールを使用するアクセス許可を持つ IAM ID AWS マネジメントコンソール を使用して にサインインします。Amazon Bedrock コンソール ([https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock)) を開きます。

1. ナビゲーションペインのテストで、**プレイグラウンド**を選択します。

1. **[チャット/テキスト]** プレイグラウンドにいる場合は、**[モード]** を選択します。

1. **[モデルを選択]** を選択し、使用するプロバイダー、モデル、スループットを選択します。スループットの向上の詳細については、「[クロスリージョン推論によりスループットを向上させる](cross-region-inference.md)」および「[Amazon Bedrock のプロビジョンドスループットでモデル呼び出し容量を増やす](prov-throughput.md)」を参照してください。

1. 以下の情報を送信してレスポンスを生成します。
   + プロンプト - モデルのシナリオ、質問、またはタスクをセットアップする 1 つ以上のテキスト文です。プロンプトの作成の詳細については、「[プロンプトエンジニアリングの概念](prompt-engineering-guidelines.md)」を参照してください。

     一部のモデル (「」を参照[サポートされているモデルとモデルの機能](conversation-inference-supported-models-features.md)) では、次の方法でファイルを含めることができます。
     + 添付ファイルアイコンを選択し、アップロードするファイルを選択します。
     + アタッチメントアイコンを選択し、アップロードする Amazon S3 オブジェクトを選択します。
     + ファイルをプロンプトにドラッグします。

     プロンプトを補完するファイルを含めます。プロンプトテキストでファイルを参照できます。例えば、**Summarize this document for me** または **Tell me what's in this image** を記述できます。以下のファイルタイプを定義できます。
     + **ドキュメント** – プロンプトを補完するドキュメントを追加します。サポートされているファイルタイプのリストについては、[DocumentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_DocumentBlock.html) の `format` フィールドを参照してください。
**警告**  
モデルが誤って指示として解釈する可能性があるため、ドキュメント名は、プロンプトインジェクションに対して脆弱です。したがって、中立的な名前を指定することが推奨されます。
     + **イメージ** – モデルがイメージとテキストのマルチモーダル入力をサポートしている場合は、プロンプトを補完するイメージを追加します。サポートされているファイルタイプのリストについては、[ImageBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ImageBlock.html) の `format` フィールドを参照してください。
     + **ビデオ** – モデルがビデオとテキストのマルチモーダル入力をサポートしている場合は、プロンプトを補完するビデオを追加します。サポートされているファイルタイプのリストについては、[VideoBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_VideoBlock.html) の `format` フィールドを参照してください。
**注記**  
コンテンツの制限は、基盤となる API オペレーションとモデルによって異なります。詳細については、[「 API の制限](inference-api-restrictions.md)」を参照してください。
   + 設定 – モデルレスポンスを変更するために調整する設定です。設定には以下が含まれます。
     + 推論パラメータ – モデルがレスポンスを生成する方法に影響を与えたり制限したりする値です。詳細については、「[推論パラメータでレスポンスの生成に影響を与える](inference-parameters.md)」を参照してください。特定のモデルの推論パラメータを確認するには、「[Inference request parameters and response fields for foundation models](model-parameters.md)」を参照してください。
     + システムプロンプト – 実行するタスクまたは採用するペルソナに関する指示またはコンテキストをモデルに指定するプロンプトです。システムプロンプトをサポートするモデルの詳細とリストについては、「[Converse API オペレーションを使用して会話を実行する](conversation-inference.md)」を参照してください。
     + ガードレール – プロンプトとモデルレスポンスで有害または不要なコンテンツをフィルタリングします。詳細については、「[Amazon Bedrock ガードレールを使用して有害なコンテンツを検出してフィルタリングする](guardrails.md)」を参照してください。

1. (オプション) モデルがストリーミングをサポートしている場合、デフォルトの動作はレスポンスをストリーミングすることです。ストリーミングをオフにするには、オプションアイコン (![\[Vertical ellipsis icon representing a menu or more options.\]](http://docs.aws.amazon.com/ja_jp/bedrock/latest/userguide/images/icons/vertical-ellipsis.png)) を選択し、**[ストリーミング設定]** オプションを変更します。

1. (オプション) 一部のテキスト生成モデルは比較評価をサポートしており、次のようにしてさまざまなモデルからのレスポンスを比較できます。

   1. **[比較モード]** をオンにします。

   1. **[モデルを選択]** を選択し、使用するプロバイダー、モデル、スループットを選択します。

   1. 設定アイコン (![\[Three horizontal sliders with adjustable circular controls for settings or parameters.\]](http://docs.aws.amazon.com/ja_jp/bedrock/latest/userguide/images/icons/configurations.png)) を選択して、使用する設定を変更します。

   1. 比較するモデルを追加するには、右側の [\$1] アイコンを選択し、モデルを選択し、必要に応じて設定を変更します。

1. (オプション) モデルがプロンプトキャッシュをサポートしている場合は、**[設定]** パネルを開き、**[プロンプトキャッシュ]** をオンにしして入力とモデルレスポンスのキャッシュを有効にして、コストとレイテンシーを削減できます。詳細については、「[モデル推論を高速化するためのプロンプトキャッシュ](prompt-caching.md)」を参照してください。

1. クエリを実行するには、**[実行]** を選択します。Amazon Bedrock は、指定したテキスト、画像、ドキュメントを保存しません。データはレスポンスの生成にのみ使用されます。
**注記**  
レスポンスがコンテンツモデレーションポリシーに違反している場合、Amazon Bedrock はそのレスポンスを表示しません。ストリーミングを有効にしている場合、Amazon Bedrock は、ポリシーに違反するコンテンツを生成すると、レスポンス全体をクリアします。詳細については、Amazon Bedrock コンソールに移動し、**[プロバイダー]** を選択して、**[Content limitations]** セクションの説明をお読みください。

1. モデルはレスポンスを返します。プレイグラウンドのチャットモードを使用している場合は、レスポンスに応答するようにプロンプトを送信し、別のレスポンスを生成できます。

1. レスポンスを生成したら、次のオプションを使用できます。
   + レスポンスを JSON ファイルとしてエクスポートするには、オプションアイコン (![\[Vertical ellipsis icon representing a menu or more options.\]](http://docs.aws.amazon.com/ja_jp/bedrock/latest/userguide/images/icons/vertical-ellipsis.png)) を選択し、**[JSON としてエクスポート]** を選択します。
   + 作成した API リクエストを表示するには、オプションアイコン (![\[Vertical ellipsis icon representing a menu or more options.\]](http://docs.aws.amazon.com/ja_jp/bedrock/latest/userguide/images/icons/vertical-ellipsis.png)) を選択し、**[API リクエストを表示]** を選択します。
   + プレイグラウンドのチャットモードでは、モデルメトリクスセクションで**メトリクス**を表示できます。以下のメトリクスが利用可能です。
     + **レイテンシー** — Amazon Bedrock がリクエストを受信してからレスポンスが返されるまで (非ストリーミングレスポンスの場合)、またはレスポンスストリームが完了するまで (ストリーミングレスポンスの場合) にかかる時間。
     + **[入力トークン数]** — 推論中に入力としてモデルに入力されるトークンの数。
     + **[出力トークン数]** — プロンプトに応答して生成されたトークンの数。レスポンスが長く、会話が多いほど、必要なトークンは多くなります。
     + **[コスト]** — 入力トークンの処理と出力トークンの生成にかかるコスト。

     レスポンスを一致させるメトリクス基準を設定するには、**[メトリクス基準を定義]** を選択し、一致するモデルの条件を定義します。条件を適用すると、**[モデルメトリクス]** セクションには、レスポンスが満たした数と条件が表示されます。

     基準が満たされていない場合は、別のモデルを選択するか、プロンプトを書き換えるか、設定を変更してプロンプトを再実行できます。

# モデル推論を使用してモデルのレスポンスを強化する
<a name="inference-reasoning"></a>

基盤モデルによっては、モデル推論を実行できます。モデル推論では、より大規模で複雑なタスクを、より小規模でシンプルなステップに分割できます。このプロセスは多くの場合、思考の連鎖 (CoT) 推論と呼ばれます。思考の連鎖推論は、モデルが応答する前に考察する機会を与えることで、モデルの正解率が向上することがよくあります。モデル推論は、マルチステップ分析、数値問題、複雑な推論タスクなどにおいて最も有用です。

例えば、数学的な単語問題を解く場合、モデルはまず関連する変数を特定したら、与えられた情報に基づいて方程式を構築し、最後にそれらの方程式を解いて解決できます。このような戦略の場合、エラーを最小限に抑えるのみでなく、推論プロセスをより明確化して理解しやすくするため、基盤モデルの品質向上につながります。

モデルの推論はすべてのタスクに必要であるとは限らず、レイテンシーの増大や出力トークン数の増加など、追加のオーバーヘッドを伴います。追加の説明を必要としないシンプルなタスクは、CoT 推論に適していません。

モデルによっては、モデルの推論に割り当てられる出力トークン数を設定できるわけではないことに注意が必要です。

モデル推論は、次のモデルで使用できます。


| 基盤モデル | モデル ID | トークンの数 | 推論設定 | 
| --- | --- | --- | --- | 
| Anthropic Claude Opus 4 | anthropic.claude-opus-4-20250514-v1:0 | このモデルには 32,768 個のトークンがあり、出力トークンと推論トークンの両方が含まれます。 | このモデルでは、設定可能なトークン予算を使用して、推論を有効または無効にすることができます。デフォルトでは、推論は無効になっています。 | 
| Anthropic Claude Sonnet 4 | anthropic.claude-sonnet-4-20250514-v1:0 | このモデルには 65,536 個のトークンがあり、出力トークンと推論トークンの両方が含まれます。 | このモデルでは、設定可能なトークン予算を使用して、推論を有効または無効にすることができます。デフォルトでは、推論は無効になっています。 | 
| Anthropic Claude 3.7 Sonnet | anthropic.claude-3-7-sonnet-20250219-v1:0 | このモデルには 65,536 個のトークンがあり、出力トークンと推論トークンの両方が含まれます。 | このモデルでは、設定可能なトークン予算を使用して、推論を有効または無効にすることができます。デフォルトでは、推論は無効になっています。 | 
| DeepSeek DeepSeek-R1 | deepseek.r1-v1:0 | このモデルには 8,192 個のトークンがあり、出力トークンと推論トークンの両方が含まれます。思考トークンの数は設定できません。出力トークンの最大数は 8192 以下にする必要があります。 | このモデルでは、推論は常に有効になっています。このモデルは、推論機能のオンとオフの切り替えをサポートしていません。 | 

# レイテンシーのモデル推論を最適化する
<a name="latency-optimized-inference"></a>

**注記**  
レイテンシー最適化推論機能は のプレビューリリースであり Amazon Bedrock 、変更される可能性があります。

の基盤モデルのレイテンシー最適化推論 Amazon Bedrock は、AI アプリケーションの応答時間を短縮し、応答性を向上させます。[https://docs.aws.amazon.com/nova/latest/userguide/what-is-nova.html](https://docs.aws.amazon.com/nova/latest/userguide/what-is-nova.html) の最適化バージョン、[Anthropic の Claude 3.5 Haiku モデル](https://aws.amazon.com/bedrock/claude/)、[Meta の Llama 3.1 405B モデルと 70B モデル](https://aws.amazon.com/bedrock/llama/)は、正解率を損なうことなく、レイテンシーを大幅に短縮します。

レイテンシー最適化機能の利用には追加のセットアップやモデルのファインチューニングは必要なく、レスポンス時間を短縮して既存のアプリケーションを直ちに強化できます。Amazon Bedrock ランタイム API を呼び出す際に、「Latency」パラメータを「optimized」に設定できます。呼び出しオプションとして「標準」を選択した場合、リクエストは標準推論で処理されます。デフォルトでは、すべてのリクエストは「標準」経由でルーティングされます。

```
"performanceConfig" : {
    "latency" : "standard | optimized" 
}
```

モデルのレイテンシー最適化の使用クォータに達すると、リクエストの処理は標準レイテンシーで試行されます。このような場合、リクエストには標準レイテンシー料金が適用されます。サービス対象のリクエストのレイテンシー設定は、API レスポンスと AWS CloudTrail ログに表示されます。また、「model-id\$1latency-optimized」の Amazon CloudWatch ログにレイテンシー最適化リクエストのメトリクスを表示することもできます。

レイテンシー最適化推論は、Meta の Llama 3.1 70B および 405B、Anthropic の Claude 3.5 Haiku で、米国東部 (オハイオ) リージョンおよび米国西部 (オレゴン) リージョンにおいて、[クロスリージョン推論](https://docs.aws.amazon.com/bedrock/latest/userguide/cross-region-inference.html)を介して利用できます。

レイテンシー最適化推論は、Amazon Nova Pro で、米国東部 (バージニア北部) リージョン、米国東部 (オハイオ) リージョン、米国西部 (オレゴン) リージョンにおいて、[クロスリージョン推論](https://docs.aws.amazon.com/bedrock/latest/userguide/cross-region-inference.html)を介して利用できます。

料金の詳細については、[料金](https://aws.amazon.com/bedrock/pricing/)ページを参照してください。

**注記**  
 現時点では Llama 3.1 405B のレイテンシー最適化推論は、入力トークンと出力トークンの合計数が最大 11K までのリクエストをサポートしています。トークン数が多いリクエストの場合、標準モードにフォールバックします。

次の表は、レイテンシーの最適化をサポートする推論プロファイルを示しています。


| プロバイダー | モデル | モデル ID | クロスリージョン推論プロファイルのサポート | 
| --- | --- | --- | --- | 
| Amazon | Nova Pro | amazon.nova-pro-v1:0 |  us–east–1 us-east-2  | 
| Anthropic | Claude 3.5 Haiku | anthropic.claude-3-5-haiku-20241022-v1:0 |  us-east-2 us-west-2  | 
| Meta | Llama 3.1 405B 指示 | meta.llama3-1-405b-instruct-v1:0 |  us-east-2  | 
| Meta | Llama 3.1 70B Instruct | meta.llama3-1-70b-instruct-v1:0 |  us-east-2 us-west-2  | 

推論プロファイルの詳細については、「[推論プロファイルでサポートされているリージョンおよびモデル](inference-profiles-support.md)」を参照してください。

# OpenAI APIsを使用してレスポンスを生成する
<a name="bedrock-mantle"></a>

Amazon Bedrock は、大規模な機械学習モデル提供用の分散推論エンジンである Mantle を搭載した、モデル推論用のOpenAI互換性のある API エンドポイントを提供します。これらのエンドポイントを使用すると、Amazon Bedrock モデルで使い慣れた OpenAI SDK とツールを使用できるため、最小限のコード変更で既存のアプリケーションを移行できます。基本 URL と API キーを更新するだけです。 SDKs 

その他の主な利点には以下が含まれます:
+ **非同期推論** — Responses API による長時間実行される推論ワークロードのサポート
+ **ステートフル会話管理** – 各リクエストで会話履歴を手動で渡すことなく、コンテキストを自動的に再構築します。
+ **ツールの使用の簡素化** – エージェントワークフローの統合の合理化
+ **柔軟なレスポンスモード** – ストリーミングレスポンスと非ストリーミングレスポンスの両方のサポート
+ **簡単な移行** – 既存の OpenAI SDK コードベースと互換性があります

## サポートされているリージョンとエンドポイント
<a name="bedrock-mantle-supported"></a>

Amazon Bedrock は、次の AWS リージョンで使用できます。


| リージョン名 | リージョン | エンドポイント | 
| --- | --- | --- | 
| 米国東部 (オハイオ) | us-east-2 | bedrock-mantle.us-east-2.api.aws | 
| 米国東部 (バージニア北部) | us–east–1 | bedrock-mantle.us-east-1.api.aws | 
| 米国西部 (オレゴン) | us-west-2 | bedrock-mantle.us-west-2.api.aws | 
| アジアパシフィック (ジャカルタ) | ap-southeast-3 | bedrock-mantle.ap-southeast-3.api.aws | 
| アジアパシフィック (ムンバイ) | ap-south-1 | bedrock-mantle.ap-south-1.api.aws | 
| アジアパシフィック (東京) | ap-northeast-1 | bedrock-mantle.ap-northeast-1.api.aws | 
| 欧州 (フランクフルト) | eu-central-1 | bedrock-mantle.eu-central-1.api.aws | 
| 欧州 (アイルランド) | eu-west-1 | bedrock-mantle.eu-west-1.api.aws | 
| 欧州 (ロンドン) | eu-west-2 | bedrock-mantle.eu-west-2.api.aws | 
| 欧州 (ミラノ) | eu-south-1 | bedrock-mantle.eu-south-1.api.aws | 
| 欧州 (ストックホルム) | eu-north-1 | bedrock-mantle.eu-north-1.api.aws | 
| 南米 (サンパウロ） | sa-east-1 | bedrock-mantle.sa-east-1.api.aws | 

## 前提条件
<a name="bedrock-mantle-prereq"></a>

OpenAI APIsを使用する前に、以下があることを確認してください。
+ **認証** – 以下を使用して認証できます。
  + Amazon Bedrock API キー ( OpenAI SDK に必要)
  + AWS 認証情報 (HTTP リクエストでサポート)
+ **OpenAI SDK** (オプション) – SDK OpenAI ベースのリクエストを使用している場合は、Python SDK をインストールします。
+ **環境変数** – 次の環境変数を設定します。
  + `OPENAI_API_KEY` – Amazon Bedrock API キーに設定する
  + `OPENAI_BASE_URL` – リージョンの Amazon Bedrock エンドポイントに設定します (例: ` https://bedrock-mantle.us-east-1.api.aws/v1`)

## モデル API
<a name="bedrock-mantle-models"></a>

Models API を使用すると、Mantle を搭載した Amazon Bedrock で利用可能なモデルを検出できます。この API を使用して、 Responses API および Chat Completions API で使用できるモデルのリストを取得します。API の詳細については、[OpenAI「 Models ドキュメント](https://platform.openai.com/docs/api-reference/models)」を参照してください。

### 使用可能なモデルを一覧表示する
<a name="bedrock-mantle-models-list"></a>

使用可能なモデルを一覧表示するには、任意の方法のタブを選択し、ステップに従います。

------
#### [ OpenAI SDK (Python) ]

```
# List all available models using the OpenAI SDK
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables

from openai import OpenAI

client = OpenAI()

models = client.models.list()

for model in models.data:
    print(model.id)
```

------
#### [ HTTP request ]

に GET リクエストを行います`/v1/models`。

```
# List all available models
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables

curl -X GET $OPENAI_BASE_URL/models \
   -H "Authorization: Bearer $OPENAI_API_KEY"
```

------

## レスポンス API
<a name="bedrock-mantle-responses"></a>

Responses API は、ストリーミング、バックグラウンド処理、マルチターンインタラクションをサポートするステートフルな会話管理を提供します。API の詳細については、[OpenAI「レスポンスドキュメント](https://platform.openai.com/docs/api-reference/responses)」を参照してください。

### 基本リクエスト
<a name="bedrock-mantle-responses-create"></a>

レスポンスを作成するには、任意の方法のタブを選択し、ステップに従います。

------
#### [ OpenAI SDK (Python) ]

```
# Create a basic response using the OpenAI SDK
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables

from openai import OpenAI

client = OpenAI()

response = client.responses.create(
    model="openai.gpt-oss-120b",
    input=[
        {"role": "user", "content": "Hello! How can you help me today?"}
    ]
)

print(response)
```

------
#### [ HTTP request ]

に POST リクエストを行います`/v1/responses`。

```
# Create a basic response
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables

curl -X POST $OPENAI_BASE_URL/responses \
   -H "Content-Type: application/json" \
   -H "Authorization: Bearer $OPENAI_API_KEY" \
   -d '{
    "model": "openai.gpt-oss-120b",
    "input": [
        {"role": "user", "content": "Hello! How can you help me today?"}
    ]
}'
```

------

### ストリームレスポンス
<a name="bedrock-mantle-responses-streaming"></a>

レスポンスイベントを段階的に受信するには、任意の方法のタブを選択し、ステップに従います。

------
#### [ OpenAI SDK (Python) ]

```
# Stream response events incrementally using the OpenAI SDK
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables

from openai import OpenAI

client = OpenAI()

stream = client.responses.create(
    model="openai.gpt-oss-120b",
    input=[{"role": "user", "content": "Tell me a story"}],
    stream=True
)

for event in stream:
    print(event)
```

------
#### [ HTTP request ]

を `stream`に設定`/v1/responses`して、 に POST リクエストを行います`true`。

```
# Stream response events incrementally
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables

curl -X POST $OPENAI_BASE_URL/responses \
   -H "Content-Type: application/json" \
   -H "Authorization: Bearer $OPENAI_API_KEY" \
   -d '{
    "model": "openai.gpt-oss-120b",
    "input": [
        {"role": "user", "content": "Tell me a story"}
    ],
    "stream": true
}'
```

------

## チャット完了 API
<a name="bedrock-mantle-chat-completions"></a>

Chat Completions API は会話レスポンスを生成します。API の詳細については、[OpenAIチャット完了ドキュメント](https://platform.openai.com/docs/api-reference/chat/create)を参照してください。

### チャット完了を作成する
<a name="bedrock-mantle-chat-completions-create"></a>

チャット完了を作成するには、任意の方法のタブを選択し、ステップに従います。

------
#### [ OpenAI SDK (Python) ]

環境変数を使用してOpenAIクライアントを設定します。

```
# Create a chat completion using the OpenAI SDK
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables

from openai import OpenAI

client = OpenAI()

completion = client.chat.completions.create(
    model="openai.gpt-oss-120b",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)

print(completion.choices[0].message)
```

------
#### [ HTTP request ]

に POST リクエストを行います`/v1/chat/completions`。

```
# Create a chat completion
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables

curl -X POST $OPENAI_BASE_URL/chat/completions \
   -H "Content-Type: application/json" \
   -H "Authorization: Bearer $OPENAI_API_KEY" \
   -d '{
    "model": "openai.gpt-oss-120b",
    "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
}'
```

------

### ストリーミングを有効にする
<a name="bedrock-mantle-chat-completions-streaming"></a>

レスポンスを段階的に受信するには、任意の方法のタブを選択し、ステップに従います。

------
#### [ OpenAI SDK (Python) ]

```
# Stream chat completion responses incrementally using the OpenAI SDK
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables

from openai import OpenAI

client = OpenAI()

stream = client.chat.completions.create(
    model="openai.gpt-oss-120b",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="")
```

------
#### [ HTTP request ]

を ` stream`に設定`/v1/chat/completions`して、 に POST リクエストを行います`true`。

```
# Stream chat completion responses incrementally
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables

curl -X POST $OPENAI_BASE_URL/chat/completions \
   -H "Content-Type: application/json" \
   -H "Authorization: Bearer $OPENAI_API_KEY" \
   -d '{
    "model": "openai.gpt-oss-120b",
    "messages": [
        {"role": "user", "content": "Tell me a story"}
    ],
    "stream": true
}'
```

------

# API を使用してプロンプトを送信し、レスポンスを生成する
<a name="inference-api"></a>

Amazon Bedrock には、モデル推論を実行するための followingAPI オペレーションが用意されています。
+ [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) – プロンプトを送信し、レスポンスを生成します。リクエスト本文はモデル固有です。ストリーミングレスポンスを生成するには、[InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) を使用します。
+ [Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) – プロンプトを送信し、すべてのモデルにわたって統合された構造でレスポンスを生成します。モデル固有のリクエストフィールドを `additionalModelRequestFields` フィールドで指定できます。コンテキストがわかるように、システムプロンプトと以前の会話を含めることもできます。ストリーミングレスポンスを生成するには、[ConverseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html) を使用します。
+ [StartAsyncInvoke](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_StartAsyncInvoke.html) – プロンプトを送信し、後で取得できるレスポンスを非同期的に生成します。動画の生成に使用されます。
+ [InvokeModelWithBidirectionalStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithBidirectionalStream.html) – 
+ OpenAI Chat completions API – Amazon Bedrock でサポートされているモデルで [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat/create) を使用してレスポンスを生成します。

**注記**  
制限は、`InvokeModel`、、`InvokeModelWithResponseStream``Converse`、および のオペレーションに適用されます`ConverseStream`。詳細については、[「API の制限](inference-api-restrictions.md)」を参照してください。

モデル推論では、次のパラメータを決定する必要があります。
+ モデル ID – モデルまたは推論プロファイルの ID または Amazon リソースネーム (ARN)。推論の `modelId` フィールドで使用します。次の表は、各種リソースの ID の調べ方をまとめたものです。  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/bedrock/latest/userguide/inference-api.html)
+ リクエスト本文 – モデルの推論パラメータおよびその他の設定が含まれます。各ベースモデルには、独自の推論パラメータがあります。カスタムモデルまたはプロビジョニングモデルの推論パラメータは、作成元のベースモデルによって異なります。詳細については、「[Inference request parameters and response fields for foundation models](model-parameters.md)」を参照してください。

トピックを選択して、モデル呼び出し API の使い方を確認してください。

**Topics**
+ [InvokeModel で 1 つのプロンプトを送信する](inference-invoke.md)
+ [OpenAI Chat Completions API を使用してモデルを呼び出す](inference-chat-completions.md)
+ [Converse API オペレーションを使用して会話を実行する](conversation-inference.md)
+ [API の制限](inference-api-restrictions.md)

# InvokeModel で 1 つのプロンプトを送信する
<a name="inference-invoke"></a>

[InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) および [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) API オペレーションを使用してモデルを指定し、単一のプロンプトで推論を実行します。Amazon Bedrock モデルは、テキスト、画像、または動画入力を受け入れるかどうか、およびテキスト、イメージ、または埋め込みの出力を生成できるかどうかによって異なります。一部のモデルはストリームでレスポンスを返すことができます。入力、出力、ストリーミングのサポートについてモデルのサポートを確認するには、次のいずれかを実行します。
+ [Amazon Bedrock でサポートされている基盤モデル](models-supported.md) でモデルの**入力モダリティ**、**出力モダリティ**、または**ストリーミングでサポートされている**列の値を確認します。
+ モデル ID を使用して [GetFoundationModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_GetFoundationModel.html) リクエストを送信し、`inputModalities`、`outputModalities`、`responseStreamingSupported` フィールドの値を確認します。

[Amazon Bedrock ランタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt)で [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) または [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) リクエストを送信して、プロンプトでモデル推論を実行します。

**注記**  
制限は、`InvokeModel`、、`InvokeModelWithResponseStream`、`Converse`および のオペレーションに適用されます`ConverseStream`。詳細については、[「API の制限](inference-api-restrictions.md)」を参照してください。

以下のフィールドが必要です。


****  

| フィールド | ユースケース | 
| --- | --- | 
| modelId | 使用するプロンプト管理からモデル、推論プロファイル、またはプロンプトを指定します。この値の調べ方については、「[API を使用してプロンプトを送信し、レスポンスを生成する](inference-api.md)」を参照してください。 | 
| body | モデルの推論パラメータを指定します。さまざまなモデルの推論パラメータを確認するには、「[Inference request parameters and response fields for foundation models](model-parameters.md)」を参照してください。modelId フィールドでプロンプト管理からプロンプトを指定する場合は、このフィールドを省略します (含めると無視されます)。 | 

次のフィールドはオプションです。


****  

| フィールド | ユースケース | 
| --- | --- | 
| accept | リクエスト本文のメディアタイプを指定します。詳細については、[Swagger ウェブサイト](https://swagger.io/specification/)の「Media Types」を参照してください。 | 
| contentType | レスポンス本文のメディアタイプを指定します。詳細については、[Swagger ウェブサイト](https://swagger.io/specification/)の「Media Types」を参照してください。 | 
| performanceConfigLatency | レイテンシーに合わせてモデルを最適化するかどうかを指定します。詳細については、「[レイテンシーのモデル推論を最適化する](latency-optimized-inference.md)」を参照してください。 | 
| guardrailIdentifier | プロンプトとレスポンスに適用するガードレールを指定します。詳細については、「[ガードレールをテストする](guardrails-test.md)」を参照してください。 | 
| guardrailVersion | プロンプトとレスポンスに適用するガードレールを指定します。詳細については、「[ガードレールをテストする](guardrails-test.md)」を参照してください。 | 
| trace | 指定したガードレールのトレースを返すかどうかを指定します。詳細については、「[ガードレールをテストする](guardrails-test.md)」を参照してください。 | 
| serviceTier | リクエストのサービス層を指定するには。詳細については、「[パフォーマンスとコストを最適化するためのサービス階層](service-tiers-inference.md)」を参照してください。 | 

## モデルコードの呼び出しの例
<a name="inference-example-invoke"></a>

このトピックでは、[InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) API で単一のプロンプトを使用して推論を実行するための基本的な例をいくつか紹介します。さまざまなモデルを使用したその他の例については、次のリソースを参照してください。
+ [AWS SDKsコード例](service_code_examples_bedrock-runtime.md) トピックで例を選択します。
+ 目的のモデルの推論パラメータリファレンスについては、「[Inference request parameters and response fields for foundation models](model-parameters.md)」を参照してください。

次の例では、これらの例を実行する AWS リージョン ときに、デフォルトで AWS CLI と SDK for Python (Boto3) に対して自動的に認証されるようにプログラムによるアクセスを設定していることを前提としています。プログラムによるアクセスの設定については、「[API の使用を開始する](getting-started-api.md)」を参照してください。

**注記**  
例を試す前に、次の点を確認してください。  
これらの例は、例で使用されているすべてのモデルをサポートする米国東部 (バージニア北部) (us-east-1) でテストする必要があります。
`body` パラメータは大きい場合があるため、一部の CLI 例では、コマンドラインで指定するのではなく、JSON ファイルを作成し、そのファイルを `--body` 引数に提供するように求められます。
画像と動画の例については、独自のイメージと動画を使用するように求められます。この例では、画像ファイルの名前が *image.png* で、動画ファイルの名前が *video.mp4* であることを前提としています。
画像や動画を base64 でエンコードされた文字列に変換するか、Amazon S3 の場所にアップロードする必要がある場合があります。この例では、プレースホルダーを実際の base64 でエンコードされた文字列または S3 の場所に置き換える必要があります。

セクションを展開して、いくつかの基本的なコード例を試してください。

### テキストプロンプトを使用してテキストを生成する
<a name="w2aac13c32c33c17c19c13b1"></a>

次の例では、Amazon Titan Text Premier モデルを使用してテキストプロンプトへのテキストレスポンスを生成します。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

ターミナルで次のコマンドを実行し、*invoke-model-output.txt* というファイルで生成されたレスポンスを見つけます。

```
aws bedrock-runtime invoke-model \
    --model-id amazon.titan-text-premier-v1:0 \
    --body '{
        "inputText": "Describe the purpose of a 'hello world' program in one line.",
        "textGenerationConfig": {
            "maxTokenCount": 512,
            "temperature": 0.5
        }
    }' \
    --cli-binary-format raw-in-base64-out \
    invoke-model-output.txt
```

------
#### [ Python ]

次の Python コード例を実行して、テキストレスポンスを生成します。

```
# Use the native inference API to send a text message to Amazon Titan Text.

import boto3
import json

from botocore.exceptions import ClientError

# Create a Bedrock Runtime client in the AWS Region of your choice.
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Set the model ID, e.g., Titan Text Premier.
model_id = "amazon.titan-text-premier-v1:0"

# Define the prompt for the model.
prompt = "Describe the purpose of a 'hello world' program in one line."

# Format the request payload using the model's native structure.
native_request = {
    "inputText": prompt,
    "textGenerationConfig": {
        "maxTokenCount": 512,
        "temperature": 0.5,
    },
}

# Convert the native request to JSON.
request = json.dumps(native_request)

try:
    # Invoke the model with the request.
    response = client.invoke_model(modelId=model_id, body=request)

except (ClientError, Exception) as e:
    print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
    exit(1)

# Decode the response body.
model_response = json.loads(response["body"].read())

# Extract and print the response text.
response_text = model_response["results"][0]["outputText"]
print(response_text)
```

------

### サービス層を使用してテキストプロンプトでテキストを生成する
<a name="w2aac13c32c33c17c19c13b3"></a>

次の例では、OpenAIGPT モデルとサービス層を使用してテキストプロンプトへのテキストレスポンスを生成し、リクエストに優先順位を付けます。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

ターミナルで次のコマンドを実行し、レスポンスでサービス層を検証します。

```
aws bedrock-runtime invoke-model \
    --model-id openai.gpt-oss-120b-1:0 \
    --body '{
        "messages": [
            {
                "role": "user",
                "content": "Describe the purpose of a '\''hello world'\'' program in one line."
            }
        ],
        "max_tokens": 512,
        "temperature": 0.7
    }' \
    --content-type application/json \
    --accept application/json \
    --service-tier priority \
    --cli-binary-format raw-in-base64-out
```

------
#### [ Python ]

次の Python コード例を実行して、サービス層を含むテキストレスポンスを生成します。

```
import boto3
import json

# Create a Bedrock Runtime client
bedrock_runtime = boto3.client(
    service_name="bedrock-runtime",
    region_name="us-east-1"
)

# Define the model ID and request body
model_id = "openai.gpt-oss-120b-1:0"
body = json.dumps({
    "messages": [
        {
            "role": "user",
            "content": "Describe the purpose of a 'hello world' program in one line."
        }
    ],
    "max_tokens": 512,
    "temperature": 0.7
})

# Make the request with service tier
response = bedrock_runtime.invoke_model(
    modelId=model_id,
    body=body,
    contentType="application/json",
    accept="application/json",
    serviceTier="priority"
)

# Parse and print the response
response_body = json.loads(response["body"])
print(response_body)
```

------

### テキストプロンプトを使用して画像を生成します。
<a name="w2aac13c32c33c17c19c13b5"></a>

次のコード例では、Stable Diffusion XL 1.0 モデルでテキストプロンプトを使用して画像を生成します。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

ターミナルで次のコマンドを実行し、*invoke-model-output.txt* というファイルで生成されたレスポンスを見つけます。画像を表すバイトは、レスポンスの `base64` フィールドにあります。

```
aws bedrock-runtime invoke-model \
    --model-id stability.stable-diffusion-xl-v1 \
    --body '{
        "text_prompts": [{"text": "A stylized picture of a cute old steampunk robot."}],
        "style_preset": "photographic",
        "seed": 0,
        "cfg_scale": 10,
        "steps": 30
    }' \
    --cli-binary-format raw-in-base64-out \
    invoke-model-output.txt
```

------
#### [ Python ]

次の Python コード例を実行して画像を生成し、*output* というフォルダで *stability\$11.png* イメージファイルを見つけます。

```
# Use the native inference API to create an image with Amazon Titan Image Generator

import base64
import boto3
import json
import os
import random

# Create a Bedrock Runtime client in the AWS Region of your choice.
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Set the model ID, e.g., Titan Image Generator G1.
model_id = "amazon.titan-image-generator-v2:0"

# Define the image generation prompt for the model.
prompt = "A stylized picture of a cute old steampunk robot."

# Generate a random seed.
seed = random.randint(0, 2147483647)

# Format the request payload using the model's native structure.
native_request = {
    "taskType": "TEXT_IMAGE",
    "textToImageParams": {"text": prompt},
    "imageGenerationConfig": {
        "numberOfImages": 1,
        "quality": "standard",
        "cfgScale": 8.0,
        "height": 512,
        "width": 512,
        "seed": seed,
    },
}

# Convert the native request to JSON.
request = json.dumps(native_request)

# Invoke the model with the request.
response = client.invoke_model(modelId=model_id, body=request)

# Decode the response body.
model_response = json.loads(response["body"].read())

# Extract the image data.
base64_image_data = model_response["images"][0]

# Save the generated image to a local folder.
i, output_dir = 1, "output"
if not os.path.exists(output_dir):
    os.makedirs(output_dir)
while os.path.exists(os.path.join(output_dir, f"titan_{i}.png")):
    i += 1

image_data = base64.b64decode(base64_image_data)

image_path = os.path.join(output_dir, f"titan_{i}.png")
with open(image_path, "wb") as file:
    file.write(image_data)

print(f"The generated image has been saved to {image_path}")
```

------

### テキストから埋め込みを生成する
<a name="w2aac13c32c33c17c19c13b9"></a>

次の例では、Amazon Titan Text Embeddings V2 モデルを使用して、テキスト入力のバイナリ埋め込みを生成します。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

ターミナルで次のコマンドを実行し、*invoke-model-output.txt* というファイルで生成されたレスポンスを見つけます。結果として生じる埋め込みは `binary` フィールドにあります。

```
aws bedrock-runtime invoke-model \
    --model-id amazon.titan-embed-text-v2:0 \
    --body '{
        "inputText": "What are the different services that you offer?",
        "embeddingTypes": ["binary"]
    }' \
    --cli-binary-format raw-in-base64-out \
    invoke-model-output.txt
```

------
#### [ Python ]

次の Python コード例を実行して、指定されたテキストの埋め込みを生成します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to generate an embedding with the Amazon Titan Text Embeddings V2 Model
"""

import json
import logging
import boto3


from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_embedding(model_id, body):
    """
    Generate an embedding with the vector representation of a text input using Amazon Titan Text Embeddings G1 on demand.
    Args:
        model_id (str): The model ID to use.
        body (str) : The request body to use.
    Returns:
        response (JSON): The embedding created by the model and the number of input tokens.
    """

    logger.info("Generating an embedding with Amazon Titan Text Embeddings V2 model %s", model_id)

    bedrock = boto3.client(service_name='bedrock-runtime')

    accept = "application/json"
    content_type = "application/json"

    response = bedrock.invoke_model(
        body=body, modelId=model_id, accept=accept, contentType=content_type
    )

    response_body = json.loads(response.get('body').read())

    return response_body


def main():
    """
    Entrypoint for Amazon Titan Embeddings V2 - Text example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "amazon.titan-embed-text-v2:0"
    input_text = "What are the different services that you offer?"


    # Create request body.
    body = json.dumps({
        "inputText": input_text,
        "embeddingTypes": ["binary"]
    })


    try:

        response = generate_embedding(model_id, body)

        print(f"Generated an embedding: {response['embeddingsByType']['binary']}") # returns binary embedding
        print(f"Input text: {input_text}")
        print(f"Input Token count:  {response['inputTextTokenCount']}")

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))

    else:
        print(f"Finished generating an embedding with Amazon Titan Text Embeddings V2 model {model_id}.")


if __name__ == "__main__":
    main()
```

------

### 画像から埋め込みを生成する
<a name="w2aac13c32c33c17c19c13c11"></a>

次の例では、Amazon Titan Multimodal Embeddings G1 モデルを使用して画像入力の埋め込みを生成します。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

次の操作を行って、ターミナルを開きます。

1. 現在のフォルダの *image.png* というタイトルの画像を base64 でエンコードされた文字列に変換し、次のコマンドを実行して *image.txt* というタイトルのファイルに書き込みます。

   ```
   base64 -i image.png -o image.txt
   ```

1. *image-input-embeddings-output.json* という JSON ファイルを作成し、次の JSON を貼り付けます。*\$1\$1image-base64\$1* を *image.txt* ファイルの内容に置き換えます (文字列の末尾に新しい行がないことを確認します)。

   ```
   {
       "inputImage": "${image-base64}",
       "embeddingConfig": {
           "outputEmbeddingLength": 256
       }
   }
   ```

1. 本文として *image-input-embeddings-output.json* ファイルを指定して、次のコマンドを実行します。

   ```
   aws bedrock-runtime invoke-model \
       --model-id amazon.titan-embed-image-v1 \
       --body file://image-input-embeddings-output.json \
       --cli-binary-format raw-in-base64-out \
       invoke-model-output.txt
   ```

1. *invoke-model-output.txt* ファイルで結果の埋め込みを見つけます。

------
#### [ Python ]

次の Python スクリプトで、*/path/to/image* を実際の画像へのパスに置き換えます。次に、スクリプトを実行して埋め込みを生成します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to generate embeddings from an image with the Amazon Titan Multimodal Embeddings G1 model (on demand).
"""

import base64
import json
import logging
import boto3

from botocore.exceptions import ClientError

class EmbedError(Exception):
    "Custom exception for errors returned by Amazon Titan Multimodal Embeddings G1"

    def __init__(self, message):
        self.message = message

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_embeddings(model_id, body):
    """
    Generate a vector of embeddings for an image input using Amazon Titan Multimodal Embeddings G1 on demand.
    Args:
        model_id (str): The model ID to use.
        body (str) : The request body to use.
    Returns:
        response (JSON): The embeddings that the model generated, token information, and the
        reason the model stopped generating embeddings.
    """

    logger.info("Generating embeddings with Amazon Titan Multimodal Embeddings G1 model %s", model_id)

    bedrock = boto3.client(service_name='bedrock-runtime')

    accept = "application/json"
    content_type = "application/json"

    response = bedrock.invoke_model(
        body=body, modelId=model_id, accept=accept, contentType=content_type
    )

    response_body = json.loads(response.get('body').read())

    finish_reason = response_body.get("message")

    if finish_reason is not None:
        raise EmbedError(f"Embeddings generation error: {finish_reason}")

    return response_body


def main():
    """
    Entrypoint for Amazon Titan Multimodal Embeddings G1 example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    # Read image from file and encode it as base64 string.
    with open("/path/to/image", "rb") as image_file:
        input_image = base64.b64encode(image_file.read()).decode('utf8')

    model_id = 'amazon.titan-embed-image-v1'
    output_embedding_length = 256

    # Create request body.
    body = json.dumps({
        "inputImage": input_image,
        "embeddingConfig": {
            "outputEmbeddingLength": output_embedding_length
        }
    })


    try:

        response = generate_embeddings(model_id, body)

        print(f"Generated image embeddings of length {output_embedding_length}: {response['embedding']}")

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))
        
    except EmbedError as err:
        logger.error(err.message)
        print(err.message)

    else:
        print(f"Finished generating image embeddings with Amazon Titan Multimodal Embeddings G1 model {model_id}.")


if __name__ == "__main__":
    main()
```

------

### 付随するテキストプロンプトを使用して画像へのテキストレスポンスを生成する
<a name="w2aac13c32c33c17c19c13c13"></a>

任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

次の例では、画像と画像の内容を尋ねるテキストプロンプトに基づいて、Anthropic Claude 3 Haiku モデルを使用してレスポンスを生成します。次の操作を行って、ターミナルを開きます。

1. 現在のフォルダの *image.png* というタイトルの画像を base64 でエンコードされた文字列に変換し、次のコマンドを実行して *image.txt* というタイトルのファイルに書き込みます。

   ```
   base64 -i image.png -o image.txt
   ```

1. *image-text-input.json* という JSON ファイルを作成し、次の JSON を貼り付けます。*\$1\$1image-base64\$1* を *image.txt* ファイルの内容に置き換えます (文字列の末尾に新しい行がないことを確認します)。

   ```
   {
       "anthropic_version": "bedrock-2023-05-31",
       "max_tokens": 1000,
       "messages": [
           {               
               "role": "user",
               "content": [
                   {
                       "type": "image",
                       "source": {
                           "type": "base64",
                           "media_type": "image/png", 
                           "data": "${image-base64}"
                       }
                   },
                   {
                       "type": "text",
                       "text": "What's in this image?"
                   }
               ]
           }
       ]
   }
   ```

1. 次のコマンドを実行して、画像とそれに付随するテキストプロンプトに基づいて *invoke-model-output.txt* というファイルにテキスト出力を生成します。

   ```
   aws bedrock-runtime invoke-model \
       --model-id anthropic.claude-3-haiku-20240307-v1:0 \
       --body file://image-text-input.json \
       --cli-binary-format raw-in-base64-out \
       invoke-model-output.txt
   ```

1. 現在のフォルダの *invoke-model-output.txt* ファイルで出力を見つけます。

------
#### [ Python ]

次の Python スクリプトでは、スクリプトを実行する前に */path/to/image.png* を画像への実際のパスに置き換えます。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to run a multimodal prompt with Anthropic Claude (on demand) and InvokeModel.
"""

import json
import logging
import base64
import boto3

from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens):
    """
    Invokes a model with a multimodal prompt.
    Args:
        bedrock_runtime: The Amazon Bedrock boto3 client.
        model_id (str): The model ID to use.
        messages (JSON) : The messages to send to the model.
        max_tokens (int) : The maximum  number of tokens to generate.
    Returns:
        None.
    """



    body = json.dumps(
        {
            "anthropic_version": "bedrock-2023-05-31",
            "max_tokens": max_tokens,
            "messages": messages
        }
    )

    response = bedrock_runtime.invoke_model(
        body=body, modelId=model_id)
    response_body = json.loads(response.get('body').read())

    return response_body


def main():
    """
    Entrypoint for Anthropic Claude multimodal prompt example.
    """

    try:

        bedrock_runtime = boto3.client(service_name='bedrock-runtime')

        model_id = 'anthropic.claude-3-sonnet-20240229-v1:0'
        max_tokens = 1000
        input_text = "What's in this image?"
        input_image = "/path/to/image" # Replace with actual path to image file
 
        # Read reference image from file and encode as base64 strings.
        image_ext = input_image.split(".")[-1]
        with open(input_image, "rb") as image_file:
            content_image = base64.b64encode(image_file.read()).decode('utf8')

        message = {
            "role": "user",
            "content": [
                {
                    "type": "image", 
                    "source": {
                        "type": "base64",
                        "media_type": f"image/{image_ext}", 
                        "data": content_image
                    }
                },
                {
                    "type": "text", 
                    "text": input_text
                }
            ]
        }

    
        messages = [message]

        response = run_multi_modal_prompt(
            bedrock_runtime, model_id, messages, max_tokens)
        print(json.dumps(response, indent=4))

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occurred: " +
              format(message))


if __name__ == "__main__":
    main()
```

------

### Amazon S3 にアップロードされたビデオに付随するテキストプロンプトでテキストレスポンスを生成する
<a name="w2aac13c32c33c17c19c13c15"></a>

次の例は、S3 バケットにアップロードした動画とそれに付随するテキストプロンプトに基づいて、Amazon Nova Lite モデルを使用してレスポンスを生成する方法を示しています。

**前提条件:** 「Amazon Simple Storage Service ユーザーガイド」の「[オブジェクトのアップロード](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html#upload-objects-procedure)」の手順に従って、*video.mp4* というタイトルの動画をアカウントの Amazon S3 バケットにアップロードします。動画の S3 URI を書き留めます。

任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

ターミナルを開き、次のコマンドを実行します。*s3://amzn-s3-demo-bucket/video.mp4* を動画の実際の S3 の場所に置き換えます。

```
aws bedrock-runtime invoke-model \
    --model-id amazon.nova-lite-v1:0 \
    --body '{
        "messages": [          
            {               
                "role": "user",
                "content": [      
                    {                       
                        "video": {     
                            "format": "mp4",   
                            "source": {
                                "s3Location": {
                                    "uri": "s3://amzn-s3-demo-bucket/video.mp4"
                                }
                            }
                        }                                    
                    },
                    {
                        "text": "What happens in this video?"
                    }
                ]
            }                              
        ]                  
    }' \
    --cli-binary-format raw-in-base64-out \
    invoke-model-output.txt
```

現在のフォルダの *invoke-model-output.txt* ファイルで出力を見つけます。

------
#### [ Python ]

次の Python スクリプトで、*s3://amzn-s3-demo-bucket/video.mp4* を動画の実際の S3 の場所に置き換えます。次に、スクリプトを実行します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to run a multimodal prompt with Nova Lite (on demand) and InvokeModel.
"""

import json
import logging
import base64
import boto3

from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens):
    """
    Invokes a model with a multimodal prompt.
    Args:
        bedrock_runtime: The Amazon Bedrock boto3 client.
        model_id (str): The model ID to use.
        messages (JSON) : The messages to send to the model.
        max_tokens (int) : The maximum  number of tokens to generate.
    Returns:
        None.
    """

    body = json.dumps(
        {
            "messages": messages,
            "inferenceConfig": {
                "maxTokens": max_tokens
            }
        }
    )

    response = bedrock_runtime.invoke_model(
        body=body, modelId=model_id)
    response_body = json.loads(response.get('body').read())

    return response_body


def main():
    """
    Entrypoint for Nova Lite video prompt example.
    """

    try:

        bedrock_runtime = boto3.client(service_name='bedrock-runtime')

        model_id = "amazon.nova-lite-v1:0"
        max_tokens = 1000
        input_video_s3_uri = "s3://amzn-s3-demo-bucket/video.mp4" # Replace with real S3 URI
        video_ext = input_video_s3_uri.split(".")[-1]
        input_text = "What happens in this video?"

        message = {
            "role": "user",
            "content": [
                {
                    "video": {
                        "format": video_ext,
                        "source": {
                            "s3Location": {
                                "uri": input_video_s3_uri
                            }
                        }
                    }
                },
                {
                    "text": input_text
                }
            ]
        }

    
        messages = [message]

        response = run_multi_modal_prompt(
            bedrock_runtime, model_id, messages, max_tokens)
        print(json.dumps(response, indent=4))

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))


if __name__ == "__main__":
    main()
```

------

### テキストプロンプトが付随する、base64 でエンコードされた文字列に変換された動画へのテキストレスポンスを生成する
<a name="w2aac13c32c33c17c19c13c17"></a>

次の例は、base64 でエンコードされた文字列に変換された動画とそれに付随するテキストプロンプトに基づいて、Amazon Nova Lite モデルを使用してレスポンスを生成する方法を示しています。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

以下の操作を実行します。

1. 次のコマンドを実行して、現在のフォルダの *video.mp4* というタイトルの動画を base64 に変換します。

   ```
   base64 -i video.mp4 -o video.txt
   ```

1. *video-text-input.json* という JSON ファイルを作成し、次の JSON を貼り付けます。*\$1\$1video-base64\$1* を `video.txt` ファイルの内容に置き換えます (末尾に新しい行がないことを確認します)。

   ```
   {
       "messages": [          
           {               
               "role": "user",
               "content": [      
                   {                       
                       "video": {     
                           "format": "mp4",   
                           "source": {
                               "bytes": ${video-base64}
                           }
                       }                                    
                   },
                   {
                       "text": "What happens in this video?"
                   }
               ]
           }                              
       ]                  
   }
   ```

1. 次のコマンドを実行して、動画とそれに付随するテキストプロンプトに基づいて *invoke-model-output.txt* というファイルへのテキスト出力を生成します。

   ```
   aws bedrock-runtime invoke-model \
       --model-id amazon.nova-lite-v1:0 \
       --body file://video-text-input.json \
       --cli-binary-format raw-in-base64-out \
       invoke-model-output.txt
   ```

1. 現在のフォルダの *invoke-model-output.txt* ファイルで出力を見つけます。

------
#### [ Python ]

次の Python スクリプトで、*/path/to/video.mp4* を動画への実際のパスに置き換えます。次に、スクリプトを実行します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to run a multimodal prompt with Nova Lite (on demand) and InvokeModel.
"""

import json
import logging
import base64
import boto3

from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens):
    """
    Invokes a model with a multimodal prompt.
    Args:
        bedrock_runtime: The Amazon Bedrock boto3 client.
        model_id (str): The model ID to use.
        messages (JSON) : The messages to send to the model.
        max_tokens (int) : The maximum  number of tokens to generate.
    Returns:
        None.
    """

    body = json.dumps(
        {
            "messages": messages,
            "inferenceConfig": {
                "maxTokens": max_tokens
            }
        }
    )

    response = bedrock_runtime.invoke_model(
        body=body, modelId=model_id)
    response_body = json.loads(response.get('body').read())

    return response_body


def main():
    """
    Entrypoint for Nova Lite video prompt example.
    """

    try:

        bedrock_runtime = boto3.client(service_name='bedrock-runtime')

        model_id = "amazon.nova-lite-v1:0"
        max_tokens = 1000
        input_video = "/path/to/video.mp4" # Replace with real path to video
        video_ext = input_video.split(".")[-1]
        input_text = "What happens in this video?"

        # Read reference video from file and encode as base64 string.
        with open(input_video, "rb") as video_file:
            content_video = base64.b64encode(video_file.read()).decode('utf8')\

        message = {
            "role": "user",
            "content": [
                {
                    "video": {
                        "format": video_ext,
                        "source": {
                            "bytes": content_video
                        }
                    }
                },
                {
                    "text": input_text
                }
            ]
        }

    
        messages = [message]

        response = run_multi_modal_prompt(
            bedrock_runtime, model_id, messages, max_tokens)
        print(json.dumps(response, indent=4))

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))


if __name__ == "__main__":
    main()
```

------

## ストリーミングコードによるモデル呼び出しの例
<a name="inference-examples-stream"></a>

**注記**  
 AWS CLI はストリーミングをサポートしていません。

次の例は、「*火星での生活に関する小論文を 1,000 語で書いてください*」というプロンプトを使用して、[InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) API で Python でストリーミングテキストを生成する方法を示しています。

```
import boto3
import json

brt = boto3.client(service_name='bedrock-runtime')

body = json.dumps({
    'prompt': '\n\nHuman: write an essay for living on mars in 1000 words\n\nAssistant:',
    'max_tokens_to_sample': 4000
})
                   
response = brt.invoke_model_with_response_stream(
    modelId='anthropic.claude-v2', 
    body=body
)
    
stream = response.get('body')
if stream:
    for event in stream:
        chunk = event.get('chunk')
        if chunk:
            print(json.loads(chunk.get('bytes').decode()))
```

# OpenAI Chat Completions API を使用してモデルを呼び出す
<a name="inference-chat-completions"></a>

Amazon Bedrock モデルで[OpenAI Create chat completion API](https://platform.openai.com/docs/api-reference/chat/create) を使用してモデル推論を実行できます。

以下の方法で Create chat completion API を呼び出すことができます。
+ Amazon Bedrock ランタイムエンドポイントを使用して HTTP リクエストを行う。
+ Amazon Bedrock ランタイムエンドポイントで OpenAI SDK リクエストを使用する。

詳細情報については、トピックを選択してください。

**Topics**
+ [OpenAI Chat Completions API でサポートされているモデルとリージョン](#inference-chat-completions-supported)
+ [Chat Completions API を使用するための前提条件](#inference-chat-completions-prereq)
+ [チャット完了を作成する](#inference-chat-completions-create)
+ [チャット完了にガードレールを含める](#inference-chat-completions-guardrails)

## OpenAI Chat Completions API でサポートされているモデルとリージョン
<a name="inference-chat-completions-supported"></a>

Create chat completion API は、Amazon Bedrock およびこれらのOpenAIモデルをサポートする AWS リージョンでサポートされているすべてのモデルで使用できます。サポートされているモデルとリージョンの詳細については、「[Amazon Bedrock でサポートされている基盤モデル](models-supported.md)」を参照してください。

## Chat Completions API を使用するための前提条件
<a name="inference-chat-completions-prereq"></a>

Chat Completions API を使用するための前提条件を確認するには、任意の方法のタブを選択し、そのステップに従います。

------
#### [ OpenAI SDK ]
+ **認証** – OpenAI SDK は Amazon Bedrock API キーを使用した認証のみをサポートします。Amazon Bedrock API キーを生成してリクエストを認証します。Amazon Bedrock API キーとその生成方法については、「ビルド」章の「API キー」セクションを参照してください。
+ **エンドポイント** – Amazon Bedrock ランタイムエンドポイントとクォータで使用する AWS リージョンに対応するエンドポイントを見つけます。 [https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt) AWS SDK を使用する場合は、クライアントの設定時にエンドポイント全体ではなく、リージョンコードを指定するだけで済みます。
+ **OpenAI SDK のインストール** – 詳細については、「OpenAI ドキュメント」の「[ライブラリ](https://platform.openai.com/docs/libraries)」を参照してください。

------
#### [ HTTP request ]
+ **認証** – AWS 認証情報または Amazon Bedrock API キーを使用して認証できます。

   AWS 認証情報を設定するか、Amazon Bedrock API キーを生成してリクエストを認証します。
  +  AWS 認証情報の設定については、[AWS 「セキュリティ認証情報を使用したプログラムによるアクセス](https://docs.aws.amazon.com/IAM/latest/UserGuide/security-creds-programmatic-access.html)」を参照してください。
  + Amazon Bedrock API キーとその生成方法については、「ビルド」章の「API キー」セクションを参照してください。
+ **エンドポイント** – Amazon Bedrock ランタイムエンドポイントとクォータで使用する AWS リージョンに対応するエンドポイントを見つけます。 [https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt) AWS SDK を使用する場合は、クライアントの設定時にエンドポイント全体ではなく、リージョンコードを指定するだけで済みます。

------

## チャット完了を作成する
<a name="inference-chat-completions-create"></a>

Create chat completion API の詳細については、OpenAI ドキュメントの以下のリソースを参照してください。
+ [リクエストボディパラメータ](https://platform.openai.com/docs/api-reference/chat/create)
+ [レスポンス本文パラメータ](https://platform.openai.com/docs/api-reference/chat/object)

**注記**  
Amazon Bedrock では現在、他のOpenAI Chat completion API オペレーションがサポートされていません。

OpenAI Create chat completion API の使用方法を確認するには、任意の方法のタブを選択し、そのステップに従ってください。

------
#### [ OpenAI SDK (Python) ]

OpenAI SDK でチャット完了を作成するには、次の手順を実行します。

1. OpenAI SDK をインポートし、次のフィールドを使用してクライアントを設定します。
   + `base_url` – 次の形式のように、Amazon Bedrock ランタイムエンドポイントのプレフィックスを `/openai/v1` に付けます。

     ```
     https://${bedrock-runtime-endpoint}/openai/v1
     ```
   + `api_key` – Amazon Bedrock API キーを指定します。
   + `default_headers` – ヘッダーを含める必要がある場合は、キーと値のペアとしてこのオブジェクトに含めることができます。または、特定の API コールを行うときに `extra_headers` でヘッダーを指定することもできます。

1. クライアントで `chat.completions.create()` メソッドを使用し、リクエスト本文で少なくとも `model` と `messages` を指定します。

次の例では、`us-west-2` で Create chat completion API を呼び出します。*\$1AWS\$1BEARER\$1TOKEN\$1BEDROCK* を実際の API キーに置き換えます。

```
from openai import OpenAI

client = OpenAI(
    base_url="https://bedrock-runtime.us-west-2.amazonaws.com/openai/v1", 
    api_key="$AWS_BEARER_TOKEN_BEDROCK" # Replace with actual API key
)

completion = client.chat.completions.create(
    model="openai.gpt-oss-20b-1:0",
    messages=[
        {
            "role": "developer",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "Hello!"
        }
    ]
)

print(completion.choices[0].message)
```

------
#### [ HTTP request ]

直接 HTTP リクエストを使用してチャット完了を作成するには、次の手順を実行します。

1. 次の形式で、Amazon Bedrock ランタイムエンドポイントのプレフィックスを `/openai/v1/chat/completions` に付け加えて URL を指定します。

   ```
   https://${bedrock-runtime-endpoint}/openai/v1/chat/completions
   ```

1. `Authorization` ヘッダーで AWS 認証情報または Amazon Bedrock API キーを指定します。

1. リクエスト本文で、リクエスト本文に少なくとも `model` と `messages` を指定します。

次の例では、`us-west-2` で curl を使用して Create chat completion API を呼び出します。*\$1AWS\$1BEARER\$1TOKEN\$1BEDROCK* を実際の API キーに置き換えます。

```
curl -X POST https://bedrock-runtime.us-west-2.amazonaws.com/openai/v1/chat/completions \
   -H "Content-Type: application/json" \
   -H "Authorization: Bearer $AWS_BEARER_TOKEN_BEDROCK" \
   -d '{
    "model": "openai.gpt-oss-20b-1:0",
    "messages": [
        {
            "role": "developer",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "Hello!"
        }
    ]
}'
```

------

## チャット完了にガードレールを含める
<a name="inference-chat-completions-guardrails"></a>

モデルの入力とレスポンスに保護対策を追加するには、モデル呼び出しを実行するとき、次の[追加のパラメータ](https://github.com/openai/openai-python#undocumented-request-params)をリクエスト本文にフィールドとして含めることにより[ガードレール](guardrails.md)を適用します。
+ `extra_headers` – リクエストで追加のヘッダーを指定する次のフィールドを含むオブジェクトにマッピングします。
  + `X-Amzn-Bedrock-GuardrailIdentifier` (必須) – ガードレールの ID。
  + `X-Amzn-Bedrock-GuardrailVersion` (必須) – ガードレールのバージョン。
  + `X-Amzn-Bedrock-Trace` (オプション) – ガードレールトレースを有効にするかどうか。
+ `extra_body` – オブジェクトにマッピングします。そのオブジェクトには、次のフィールドを含むオブジェクトにマッピングされる `amazon-bedrock-guardrailConfig` フィールドを含めることができます。
  + `tagSuffix` (オプション) – [入力タグ付け](guardrails-tagging.md)にこのフィールドを含めます。

Amazon Bedrock ガードレールのこれらのパラメータの詳細については、「[ガードレールをテストする](guardrails-test.md)」を参照してください。

OpenAI chat completion を使用したガードレールの使用例を確認するには、任意の方法のタブを選択し、そのステップに従ってください。

------
#### [ OpenAI SDK (Python) ]

```
import openai
from openai import OpenAIError

# Endpoint for Amazon Bedrock Runtime
bedrock_endpoint = "https://bedrock-runtime.us-west-2.amazonaws.com/openai/v1"

# Model ID
model_id = "openai.gpt-oss-20b-1:0"

# Replace with actual values
bedrock_api_key = "$AWS_BEARER_TOKEN_BEDROCK"
guardrail_id = "GR12345"
guardrail_version = "DRAFT"

client = openai.OpenAI(
    api_key=bedrock_api_key,
    base_url=bedrock_endpoint,
)

try:
    response = client.chat.completions.create(
        model=model_id,
        # Specify guardrail information in the header
        extra_headers={
            "X-Amzn-Bedrock-GuardrailIdentifier": guardrail_id,
            "X-Amzn-Bedrock-GuardrailVersion": guardrail_version,
            "X-Amzn-Bedrock-Trace": "ENABLED",
        },
        # Additional guardrail information can be specified in the body
        extra_body={
            "amazon-bedrock-guardrailConfig": {
                "tagSuffix": "xyz"  # Used for input tagging
            }
        },
        messages=[
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "assistant", 
                "content": "Hello! How can I help you today?"
            },
            {
                "role": "user",
                "content": "What is the weather like today?"
            }
        ]
    )

    request_id = response._request_id
    print(f"Request ID: {request_id}")
    print(response)
    
except OpenAIError as e:
    print(f"An error occurred: {e}")
    if hasattr(e, 'response') and e.response is not None:
        request_id = e.response.headers.get("x-request-id")
        print(f"Request ID: {request_id}")
```

------
#### [ OpenAI SDK (Java) ]

```
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.HttpResponseFor;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

// Endpoint for Amazon Bedrock Runtime
String bedrockEndpoint = "http://bedrock-runtime.us-west-2.amazonaws.com/openai/v1"

// Model ID
String modelId = "openai.gpt-oss-20b-1:0"

// Replace with actual values
String bedrockApiKey = "$AWS_BEARER_TOKEN_BEDROCK"
String guardrailId = "GR12345"
String guardrailVersion = "DRAFT"

OpenAIClient client = OpenAIOkHttpClient.builder()
        .apiKey(bedrockApiKey)
        .baseUrl(bedrockEndpoint)
        .build()

ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
        .addUserMessage("What is the temperature in Seattle?")
        .model(modelId)
        // Specify additional headers for the guardrail
        .putAdditionalHeader("X-Amzn-Bedrock-GuardrailIdentifier", guardrailId)
        .putAdditionalHeader("X-Amzn-Bedrock-GuardrailVersion", guardrailVersion)
        // Specify additional body parameters for the guardrail
        .putAdditionalBodyProperty(
                "amazon-bedrock-guardrailConfig",
                JsonValue.from(Map.of("tagSuffix", JsonValue.of("xyz"))) // Allows input tagging
        )
        .build();
        
HttpResponseFor<ChatCompletion> rawChatCompletionResponse =
        client.chat().completions().withRawResponse().create(request);

final ChatCompletion chatCompletion = rawChatCompletionResponse.parse();

System.out.println(chatCompletion);
```

------

# Converse API オペレーションを使用して会話を実行する
<a name="conversation-inference"></a>

Amazon Bedrock Converse API を使用すると、Amazon Bedrock モデルとの間でメッセージを送受信する会話型アプリケーションを作成できます。例えば、多くのターンにわたって会話を維持し、役に立つテクニカルサポートアシスタントなど、ニーズに固有のペルソナまたはトーンをカスタマイズしたチャットボットを作成できます。

Converse API を使用するには、[Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) または [ConverseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html) (ストリーミングレスポンス用) オペレーションを使用して、モデルにメッセージを送信します。会話アプリケーションには、既存のベース推論オペレーション ([InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) または [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html)) を使用できます。ただし、メッセージをサポートするすべての Amazon Bedrock モデルで動作する一貫した API を提供する Converse API を使用することをお勧めします。そうすることで、コードを 1 回だけ記述し、それをさまざまなモデルで使用できます。Converse API では、モデルに一意の推論パラメータがある場合、モデル固有の構造でこれらの一意のパラメータを渡すこともできます。

Converse API を使用すると、アプリケーションで[ツールの使用](tool-use.md)と[ガードレール](guardrails-use-converse-api.md)を実装できます。

**注記**  
Mistral AI モデルと Meta モデルでは、Converse API は入力内容をモデル固有のプロンプトテンプレートに埋め込み、会話ができるようにします。
制限は、`InvokeModel`、、`InvokeModelWithResponseStream``Converse`、および のオペレーションに適用されます`ConverseStream`。詳細については、[「API の制限](inference-api-restrictions.md)」を参照してください。

コード例については、以下を参照してください。
+ このトピックの Python の例 - [Converse API の例](conversation-inference-examples.md)
+ さまざまな言語とモデル – [AWS SDKsコード例](service_code_examples_bedrock-runtime.md)
+ Java チュートリアル – [Bedrock の新しい Converse API](https://community.aws/content/2hUiEkO83hpoGF5nm3FWrdfYvPt/amazon-bedrock-converse-api-java-developer-guide) に関する Java デベロッパーガイド
+ JavaScript チュートリアル – [Bedrock の新しい Converse API](https://community.aws/content/2dtauBCeDa703x7fDS9Q30MJoBA/amazon-bedrock-converse-api-developer-guide) に関するデベロッパーガイド

**Topics**
+ [サポートされているモデルとモデルの機能](conversation-inference-supported-models-features.md)
+ [Converse API を使用する場合](conversation-inference-call.md)
+ [Converse API の例](conversation-inference-examples.md)

# サポートされているモデルとモデルの機能
<a name="conversation-inference-supported-models-features"></a>

Converse API は、以下の Amazon Bedrock モデルとモデルの機能をサポートしています。Converse API は、いずれの埋め込みモデルやイメージ生成モデルもサポートしていません。


| モデル | Converse | ConverseStream | システムプロンプト | ドキュメントチャット | 視覚 | ツールの使用 | ストリーミングツールの使用 | ガードレール | マルチメディア用の Amazon S3 リンク | 
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | 
| AI21 Jamba-Instruct | はい  | はい | あり | なし | なし | なし | なし | なし | いいえ | 
| AI21 Labs Jurassic-2 (テキスト) | 制限あり。チャットはサポートされていません。 | いいえ | なし | なし | なし | なし | なし | あり | なし | 
| AI21 Labs Jamba 1.5 Large | はい | はい | はい | あり | なし | はい | はい | あり | なし | 
| AI21 Labs Jamba 1.5 Mini | はい | はい | はい | あり | なし | はい | はい | あり | なし | 
| Amazon Nova Premier | はい | はい | はい | はい | はい | はい | はい | はい | はい | 
| Amazon Nova Pro | はい | はい | はい | はい | はい | はい | はい | はい | はい | 
| Amazon Nova Lite | はい | はい | はい | はい | はい | はい | はい | はい | はい | 
| Amazon Nova Micro | はい | はい | あり | なし | なし | はい | はい | あり | なし | 
| Amazon Titan モデル | はい  | あり | いいえ | はい (Titan Text Premier を除く) | いいえ | なし | なし | あり | なし | 
| Anthropic Claude 2.x 以前のモデル | はい  | はい | はい | あり | なし | なし | なし | あり | なし | 
| Anthropic Claude 3 モデル | はい  | はい | はい | はい | はい | はい | はい | あり | なし | 
| Anthropic Claude 3.5 Sonnet | はい | はい | はい | はい | はい | はい | はい | あり | なし | 
| Anthropic Claude 3.5 Sonnet v2 | はい  | はい | はい | はい | はい | はい | はい | あり | なし | 
| Anthropic Claude 3.7 Sonnet | はい | はい | はい | はい | はい | はい | はい | あり | なし | 
| Anthropic Claude 3.5 Haiku | はい | はい | はい | あり | なし | はい | あり | なし | なし | 
| Anthropic Claude Sonnet 4 | はい | はい | はい | はい | はい | はい | あり | なし | いいえ | 
| Anthropic Claude Opus 4 | はい  | はい | はい | はい | はい | はい | あり | なし | なし | 
| Anthropic Claude Sonnet 4.5 | はい | はい | はい | はい | はい | はい | あり | なし | なし | 
| Anthropic Claude Haiku 4.5 | はい | はい | はい | はい | はい | はい | あり | なし | いいえ | 
| Anthropic Claude Opus 4.1 | はい  | はい | はい | はい | はい | はい | あり | なし | いいえ | 
| Claude Opus 4.5 | はい  | はい | はい | はい | はい | はい | あり | なし | いいえ | 
| Cohere Command | 制限あり。チャットはサポートされていません。 | 制限あり。チャットはサポートされていません。 | いいえ | あり | なし | なし | なし | あり | いいえ | 
| Cohere Command Light | 制限あり。チャットはサポートされていません。 | 制限あり。チャットはサポートされていません。 | いいえ | なし | なし | なし | なし | あり | いいえ | 
| Cohere および Command R Command R\$1 | はい  | はい | はい | あり | なし | はい | あり | なし | なし | 
| DeepSeek-R1 | はい | はい | はい | あり | なし | なし | なし | あり | いいえ | 
| Meta および Llama 2 Llama 3 | はい  | はい | はい | あり | なし | なし | なし | あり | なし | 
| Meta Llama 3.1 | はい | はい | はい | あり | なし | あり | なし | あり | なし | 
| Meta Llama 3.2 1b and Llama 3.2 3b | はい | はい | はい | あり | なし | なし | なし | あり | なし | 
| Meta Llama 3.2 11b and Llama 3.2 90b | はい | はい | はい | はい | はい | あり | なし | あり | なし | 
| Meta Llama 4 Maverick 17B and Llama 4.0 Scout 17B | はい | はい | はい | はい | はい | あり | なし | あり | なし | 
| Mistral AI Instruct | はい | あり | なし | あり | なし | なし | なし | あり | なし | 
| Mistral Large | はい | はい | はい | あり | なし | あり | なし | あり | なし | 
| Mistral Large 2 (24.07) | はい | はい | はい | あり | なし | あり | なし | あり | なし | 
| Mistral Small | はい | はい | あり | なし | なし | あり | なし | あり | なし | 
| Pixtral Large (25.02) | はい | はい | はい | あり | なし | あり | なし | あり | なし | 
| Writer Palmyra X4 | はい | はい | はい | あり | なし | あり | なし | あり | なし | 
| Writer Palmyra X5 | はい | はい | はい | あり | なし | あり | なし | あり | なし | 

各モデルをサポートするリージョンの表については、「[Amazon Bedrock AWS リージョン での によるモデルサポート](models-regions.md)」を参照してください。

**注記**  
Cohere Command (テキスト) と AI21 Labs Jurassic-2 (テキスト) は、Converse API とのチャットをサポートしていません。モデルは一度に 1 つのユーザーメッセージのみを処理でき、会話の履歴を維持することはできません。複数のメッセージを渡そうとすると、エラーが発生します。

# Converse API を使用する場合
<a name="conversation-inference-call"></a>

Converse API を使用するには、`Converse` または `ConverseStream` オペレーションを呼び出してモデルにメッセージを送信します。`Converse` を呼び出すには、`bedrock:InvokeModel` オペレーションを呼び出す許可も必要です。`ConverseStream` を呼び出すには、`bedrock:InvokeModelWithResponseStream` オペレーションを呼び出す許可も必要です。

**Topics**
+ [リクエスト](#conversation-inference-call-request)
+ [応答](#conversation-inference-call-response)

**注記**  
制限は、InvokeModel、InvokeModelWithResponseStream、Converse、ConverseStream の各オペレーションに適用されます。詳細については、[「API の制限](inference-api-restrictions.md)」を参照してください。

## リクエスト
<a name="conversation-inference-call-request"></a>

[Amazon Bedrock ランタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt)で [Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) リクエストを行うときは、次のフィールドを含めることができます。
+ **modelId** – 推論に使用するリソースを指定できるヘッダー内の必須パラメータ。
+ 次のフィールドでは、プロンプトをカスタマイズできます。
  + **messages** – プロンプトの内容とロールを指定するために使用します。
  + **system** – モデルの指示またはコンテキストを定義するシステムプロンプトを指定するために使用します。
  + **inferenceConfig** – すべてのモデルに共通の推論パラメータを指定するために使用します。推論パラメータはレスポンスの生成に影響を与えます。
  + **additionalModelRequestFields** – 推論を実行するモデルに固有の推論パラメータを指定するために使用します。
  + **promptVariables** – (プロンプト管理のプロンプトを使用する場合) このフィールドを使用して、入力するプロンプト内の変数と入力する値を定義します。
+ 次のフィールドでは、レスポンスが返される方法をカスタマイズできます。
  + **guardrailConfig** – このフィールドを使用して、プロンプト全体に適用するガードレールを含めます。
  + **toolConfig** – このフィールドを使用して、モデルがレスポンスを生成するのに役立つツールを含めます。
  + **additionalModelResponseFieldPaths** – このフィールドを使用して、JSON ポインタオブジェクトとして返すフィールドを指定します。
  + **serviceTier** – このフィールドを使用して、特定のリクエストのサービス層を指定します。
+ **requestMetadata** – このフィールドを使用して、呼び出しログを使用するときにフィルタリングできるメタデータを含めます。

**注記**  
`Converse` または `ConverseStream` でプロンプト管理プロンプトを使用する場合、次の制限が適用されます。  
`additionalModelRequestFields`、`inferenceConfig`、`system`、`toolConfig` フィールドを含めることはできません。
`messages` フィールドを含めると、プロンプトで定義されたメッセージの後にメッセージが追加されます。
`guardrailConfig` フィールドを含めると、プロンプト全体にガードレールが適用されます。[ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) フィールドに `guardContent` ブロックを含めると、ガードレールはそれらのブロックにのみ適用されます。

セクションを展開すると、`Converse` リクエスト本文のフィールドの詳細を確認できます。

### メッセージ
<a name="converse-messages"></a>

`messages` フィールドは [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトの配列であり、それぞれがユーザーとモデル間のメッセージを定義します。`Message` オブジェクトには、以下のフィールドが含まれています。
+ **role** – メッセージの送信元が `user` (モデルに送信されたプロンプト) か `assistant` (モデルレスポンス) かを定義します。
+ **content** – プロンプト内のコンテンツを定義します。
**注記**  
Amazon Bedrock は、ユーザーがコンテンツとして提供したテキスト、画像、ドキュメントを保存しません。データはレスポンスの生成にのみ使用されます。

会話コンテキストを維持するには、後続の `Converse` リクエストに会話のすべてのメッセージを含め、`role` フィールドを使用して、メッセージがユーザーからのものであるかモデルからのものであるかを指定します。

`content` フィールドは [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) オブジェクトの配列にマッピングされます。各 [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) 内で、次のいずれかのフィールドを指定できます (どのモデルがどのブロックをサポートしているかを確認するには、「[サポートされているモデルとモデルの機能](conversation-inference-supported-models-features.md)」を参照)。

------
#### [ text ]

`text` フィールドは、プロンプトを指定する文字列にマッピングされます。`text` フィールドは、同じ [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) で指定された他のフィールドとともに解釈されます。

以下は、テキスト [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) のみを含む `content` 配列を持つ [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "text": "string"
        }
    ]
}
```

------
#### [ image ]

`image` フィールドは [ImageBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ImageBlock.html) にマッピングされます。base64 でエンコードされた raw バイトを、`bytes` フィールド内の画像に渡します。 AWS SDK を使用する場合、base64 のバイトをエンコードする必要はありません。

`text` フィールドを除外すると、モデルは画像を記述します。

以下は、画像 [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) のみを含む `content` 配列を持つサンプル [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "image": {
                "format": "png",
                "source": {
                    "bytes": "image in bytes"
                }
            }
        }
    ]
}
```

リクエスト本文でバイトを直接渡す代わりに、Amazon S3 URI を指定することもできます。以下は、Amazon S3 URI を通過するソースを含むコンテンツ配列を持つサンプル `Message` オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "image": {
                "format": "png",
                "source": {
                    "s3Location": {
                        "uri": "s3://amzn-s3-demo-bucket/myImage",
                        "bucketOwner": "111122223333"
                    }
                }
            }
        }
    ]
}
```

------
#### [ document ]

`document` フィールドは [DocumentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_DocumentBlock.html) にマッピングされます。`DocumentBlock` を含める場合は、リクエストが次の制限に準拠していることを確認します。
+ [メッセージ](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html)オブジェクトの `content` フィールドに、ドキュメントに関連するプロンプトを含む `text` フィールドを含める必要があります。
+ base64 でエンコードされた raw バイトを、`bytes` フィールド内のドキュメントに渡します。 AWS SDK を使用する場合、base64 のドキュメントのバイトをエンコードする必要はありません。
+ `name` フィールドには以下の文字のみ含めることができます。
  + アルファベットの文字
  + 空白文字 (連続した空白文字は使用不可)
  + ハイフン
  + 括弧
  + 角括弧
**注記**  
モデルが誤って指示として解釈する可能性があるため、`name` フィールドはプロンプトインジェクションに対して脆弱です。したがって、中立的な名前を指定することが推奨されます。

ドキュメントを使用する場合、`citations` タグを有効にできます。これにより、API コールのレスポンスでドキュメント固有の引用が提供されます。詳細については、「[DocumentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_DocumentBlock.html)」 API を参照してください。

以下は、ドキュメント [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) と必須の付随テキスト [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) のみを含む `content` 配列を持つサンプル [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "text": "string"
        },
        {
            "document": {
                "format": "pdf",
                "name": "MyDocument",
                "source": {
                    "bytes": "document in bytes"
                }
            }
        }
    ]
}
```

リクエスト本文でバイトを直接渡す代わりに、Amazon S3 URI を指定することもできます。以下は、Amazon S3 URI を通過するソースを含むコンテンツ配列を持つサンプル `Message` オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "text": "string"
        },
        {
            "document": {
                "format": "pdf",
                "name": "MyDocument",
                "source": {
                    "s3Location": {
                      "uri": "s3://amzn-s3-demo-bucket/myDocument",
                      "bucketOwner": "111122223333"
                    }
                }
            }
        }
    ]
}
```

------
#### [ video ]

`video` フィールドは [VideoBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_VideoBlock.html) オブジェクトにマッピングされます。base64 でエンコードされた `bytes` フィールド内の raw バイトを渡します。 AWS SDK を使用する場合、base64 でバイトをエンコードする必要はありません。

`text` フィールドを含めない場合、モデルは動画を記述します。

以下は、画像 [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) のみを含む `content` 配列を持つサンプル [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "video": {
                "format": "mp4",
                "source": {
                    "bytes": "video in bytes"
                }
            }
        }
    ]
}
```

リクエスト本文でバイトを直接渡す代わりに、Amazon S3 URI を指定することもできます。以下は、Amazon S3 URI を通過するソースを含むコンテンツ配列を持つサンプル `Message` オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "video": {
                "format": "mp4",
                "source": {
                    "s3Location": {
                        "uri": "s3://amzn-s3-demo-bucket/myVideo",
                        "bucketOwner": "111122223333"
                    }
                }
            }
        }
    ]
}
```

**注記**  
引き受けたロールには、Amazon S3 URI に対する `s3:GetObject` アクセス許可が必要です。`bucketOwner` フィールドはオプションですが、リクエストを行うアカウントが Amazon S3 URI が見つかるバケットを所有していない場合は、指定する必要があります。詳細については、「[Amazon S3 バケットへのアクセスを設定する](s3-bucket-access.md)」を参照してください。

------
#### [ cachePoint ]

`cachePoint` フィールドを使用してプロンプトキャッシュを利用することで、キャッシュチェックポイントをメッセージ内のブロックとして付随するプロンプトとともに追加できます。プロンプトキャッシュは、会話のコンテキストのキャッシュを開始してコストとレイテンシーを節約できる機能です。詳細については、「[モデル推論を高速化するためのプロンプトキャッシュ](prompt-caching.md)」を参照してください。

以下は、ドキュメント [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) と付随する必須のテキスト [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) を含む `content` 配列を持つ [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトの例と、ドキュメントとテキストの両方の内容をキャッシュに追加する **cachePoint** を示しています。

```
{
    "role": "user",
    "content": [
        {
            "text": "string"
        },
        {
            "document": {
                "format": "pdf",
                "name": "string",
                "source": {
                    "bytes": "document in bytes"
                }
            }
        },
        {
            "cachePoint": {
                "type": "default"
            }
        }
    ]
}
```

------
#### [ guardContent ]

`guardContent` フィールドは [GuardrailConverseContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailConverseContentBlock.html) オブジェクトにマッピングされます。このフィールドを使用して、`guardrailConfig` フィールドで定義されたガードレールによって評価される入力をターゲットにできます。このフィールドを指定しない場合、ガードレールはリクエスト本文内のすべてのメッセージを評価します。`GuardBlock` では、次のタイプのコンテンツを渡すことができます。
+ **text** – 以下は、テキスト [GuardrailConverseContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailConverseContentBlock.html) のみを含む `content` 配列を持つ [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトの例を示しています。

  ```
  {
      "role": "user",
      "content": [
          {
              "text": "Tell me what stocks to buy.",
              "qualifiers": [
                  "guard_content"
              ]
          }
      ]
  }
  ```

  評価するテキストを定義し、[コンテキストグラウンディング](guardrails-contextual-grounding-check.md)に使用する修飾子を含めます。
+ **image** – 以下は、画像 [GuardrailConverseContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailConverseContentBlock.html) のみを含む `content` 配列を持つ [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトの例を示しています。

  ```
  {
      "role": "user",
      "content": [
          {
              "format": "png",
              "source": {
                  "bytes": "image in bytes"
              }
          }
      ]
  }
  ```

  画像の形式を指定し、画像をバイト単位で定義します。

ガードレールの使用方法の詳細については、「[Amazon Bedrock ガードレールを使用して有害なコンテンツを検出してフィルタリングする](guardrails.md)」を参照してください。

------
#### [ reasoningContent ]

`reasoningContent` フィールドは [ReasoningContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ReasoningContentBlock.html) にマッピングされます。このブロックには、付随する `ContentBlock` でレスポンスを生成するためにモデルが実行した推論に関するコンテンツが含まれています。

以下は、`ReasoningContentBlock` とそれに付随するテキスト `ContentBlock` のみを含む `content` 配列を持つ `Message` オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "text": "string"
        },
        {
            "reasoningContent": {
                "reasoningText": {
                    "text": "string",
                    "signature": "string"
                }
                "redactedContent": "base64-encoded binary data object"
            }
        }
    ]
}
```

`ReasoningContentBlock` には、`reasoningText` フィールド内に付随するコンテンツを生成するために使用する推論と、`redactedContent` フィールドの信頼と安全の理由でモデルプロバイダーが暗号化した推論内のコンテンツが含まれます。

`reasoningText` フィールド内で、`text` フィールドは推論を記述します。`signature` フィールドは会話内のすべてのメッセージのハッシュであり、モデルで使用される推論の改ざんに対するセーフガードです。署名と以前のすべてのメッセージを後続の `Converse` リクエストに含める必要があります。メッセージのいずれかが変更されると、レスポンスはエラーをスローします。

------
#### [ toolUse ]

モデルが使用するツールに関する情報が含まれています。詳細については、「[ツールを使用して Amazon Bedrock のモデルレスポンスを完成させる](tool-use.md)」を参照してください。

------
#### [ toolResult ]

ツールを使用するモデルの結果に関する情報が含まれます。詳細については、「[ツールを使用して Amazon Bedrock のモデルレスポンスを完成させる](tool-use.md)」を参照してください。

------

次の `messages` 例では、ユーザーはポップの曲を 3 曲含むリストを要求し、モデルは曲のリストを生成します。

```
[
    {
        "role": "user",
        "content": [
            {
                "text": "Create a list of 3 pop songs."
            }
        ]
    },
    {
        "role": "assistant",
        "content": [
            {
                "text": "Here is a list of 3 pop songs by artists from the United Kingdom:\n\n1. \"As It Was\" by Harry Styles\n2. \"Easy On Me\" by Adele\n3. \"Unholy\" by Sam Smith and Kim Petras"
            }
        ]
    }
]
```

### アタッチ
<a name="converse-system"></a>

システムプロンプトは、会話中に実行するタスク、または採用するペルソナに関する指示やコンテキストをモデルに提供するプロンプトの一種です。次の例に示すように、`system` ([SystemContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_SystemContentBlock.html)) フィールドでリクエストのシステムプロンプトのリストを指定できます。

```
[
    {
        "text": "You are an app that creates play lists for a radio station that plays rock and pop music. Only return song names and the artist. "
    }
]
```

### inferenceConfig
<a name="converse-inference"></a>

Converse API は、`inferenceConfig` フィールド ([InferenceConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InferenceConfiguration.html)) で設定した推論パラメータの基本セットをサポートします。推論パラメータの基本セットは次のとおりです。
+ **maxTokens** – 生成されたレスポンスで許可するトークンの最大数。
+ **stopSequences** – ストップシーケンスのリスト。停止シーケンスは、モデルがレスポンスの生成を停止する一連の文字です。
+ **temperature** – モデルがレスポンスの生成中に確率の高いオプションを選択する可能性。
+ **topP** — モデルが次のトークンについて考慮する最も可能性の高い候補のパーセンテージ。

詳細については、「[推論パラメータでレスポンスの生成に影響を与える](inference-parameters.md)」を参照してください。

次の JSON の例では、`temperature` 推論パラメータを設定します。

```
{"temperature": 0.5}
```

### additionalModelRequestFields
<a name="converse-additional-model-request-fields"></a>

使用しているモデルに追加の推論パラメータがある場合は、`additionalModelRequestFields` フィールドに JSON として指定することで、これらのパラメータを設定できます。次の JSON の例は、`top_k` を設定する方法を示しています。これは Anthropic Claude モデルで使用できますが、メッセージ API のベース推論パラメータではありません。

```
{"top_k": 200}
```

### promptVariables
<a name="converse-prompt-variables"></a>

推論を実行するリソースとして `modelId` の[プロンプト管理](prompt-management.md)からプロンプトを指定する場合は、このフィールドを使用してプロンプト変数に実際の値を入力します。`promptVariables` フィールドは、プロンプトで定義された変数に対応するキーと、変数を置き換える値を持つ JSON オブジェクトにマップされます。

例えば **Make me a *\$1\$1genre\$1\$1* playlist consisting of the following number of songs: *\$1\$1number\$1\$1*.** というプロンプトがあるとします。プロンプトの ID は `PROMPT12345` で、バージョンは `1` です。変数を置き換えるには、次の `Converse` リクエストを送信できます。

```
POST /model/arn:aws:bedrock:us-east-1:111122223333:prompt/PROMPT12345:1/converse HTTP/1.1
Content-type: application/json

{
   "promptVariables": { 
      "genre" : "pop",
      "number": 3
   }
}
```

### guardrailConfig
<a name="converse-guardrail"></a>

このフィールドを含めることで、[Amazon Bedrock ガードレール](guardrails.md)で作成したガードレールを適用できます。会話内の特定のメッセージにガードレールを適用するには、[GuardrailConverseContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailConverseContentBlock.html) にメッセージを含めます。リクエスト本文に `GuardrailConverseContentBlock` を含めない場合、ガードレールは `messages` フィールド内のすべてのメッセージに適用されます。例については、[Converse API にガードレールを含める](guardrails-use-converse-api.md)を参照してください。

### toolConfig
<a name="converse-tool"></a>

このフィールドでは、モデルがレスポンスを生成するのに役立つツールを定義できます。詳細については、「[ツールを使用して Amazon Bedrock のモデルレスポンスを完成させる](tool-use.md)」を参照してください。

### additionalModelResponseFieldPaths
<a name="converse-additional-model-response-field-paths"></a>

次の例に示すように、`additionalModelResponseFieldPaths` フィールドで追加のモデルパラメータのパスを指定できます。

```
[ "/stop_sequence" ]
```

API は、`additionalModelResponseFields` フィールドでリクエストした追加のフィールドを返します。

### requestMetadata
<a name="converse-request-metadata"></a>

このフィールドは JSON オブジェクトにマッピングされます。このオブジェクト内でマッピングするメタデータキーと値を指定できます。リクエストメタデータを使用して、モデル呼び出しログをフィルタリングできます。

### serviceTier
<a name="inference-service-tiers"></a>

このフィールドは JSON オブジェクトにマッピングされます。特定のリクエストのサービス層を指定できます。

次の例は、 `serviceTier`構造を示しています。

```
"serviceTier": {
  "type": "reserved" | "priority" | "default" | "flex"
}
```

料金やパフォーマンス特性など、サービス階層の詳細については、「」を参照してください[パフォーマンスとコストを最適化するためのサービス階層](service-tiers-inference.md)。

オプションでキャッシュチェックポイントを `system` または `tools` フィールドに追加して、使用しているモデルに応じてプロンプトキャッシュを使用することもできます。詳細については、「[モデル推論を高速化するためのプロンプトキャッシュ](prompt-caching.md)」を参照してください。

## 応答
<a name="conversation-inference-call-response"></a>

Converse API から得られるレスポンスは、呼び出すオペレーション、`Converse`、または `ConverseStream` によって異なります。

**Topics**
+ [会話レスポンス](#conversation-inference-call-response-converse)
+ [ConverseStream レスポンス](#conversation-inference-call-response-converse-stream)

### 会話レスポンス
<a name="conversation-inference-call-response-converse"></a>

`Converse` からのレスポンスでは、`output` フィールド ([ConverseOutput](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseOutput.html)) には、モデルが生成するメッセージ ([Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html)) が含まれます。メッセージの内容は `content` ([ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html)) フィールドにあり、メッセージが対応するロール (`user` または `assistant`) は `role` フィールドにあります。

[プロンプトキャッシュ](prompt-caching.md)を使用した場合は、使用量フィールドの `cacheReadInputTokensCount` と`cacheWriteInputTokensCount` はキャッシュから読み取られたトークンの合計数とキャッシュに書き込まれたトークンの合計数をそれぞれ示します。

[サービス層](#inference-service-tiers)を使用した場合、レスポンスフィールドで、 `service tier`はリクエストに使用されたサービス層を示します。

`metrics` フィールド ([ConverseMetrics](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseMetrics.html)) には、呼び出しのメトリクスが含まれます。モデルがコンテンツの生成を停止した理由を確認するには、`stopReason` フィールドを確認します。`usage` フィールド ([TokenUsage](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_TokenUsage.html)) を確認することで、リクエストでモデルに渡されたトークンと、レスポンスで生成されたトークンに関する情報を取得できます。リクエストで追加のレスポンスフィールドを指定した場合、API は `additionalModelResponseFields` フィールドに JSON として返します。

次の例は、[リクエスト](#conversation-inference-call-request) で説明されているプロンプトを渡す際の `Converse` からのレスポンスを示しています。

```
{
    "output": {
        "message": {
            "role": "assistant",
            "content": [
                {
                    "text": "Here is a list of 3 pop songs by artists from the United Kingdom:\n\n1. \"Wannabe\" by Spice Girls\n2. \"Bitter Sweet Symphony\" by The Verve \n3. \"Don't Look Back in Anger\" by Oasis"
                }
            ]
        }
    },
    "stopReason": "end_turn",
    "usage": {
        "inputTokens": 125,
        "outputTokens": 60,
        "totalTokens": 185
    },
    "metrics": {
        "latencyMs": 1175
    }
}
```

### ConverseStream レスポンス
<a name="conversation-inference-call-response-converse-stream"></a>

モデルからレスポンスをストリーミングするために `ConverseStream` を呼び出すと、`stream` レスポンスフィールドにストリームが返されます。ストリームは、以下の順序で次のイベントを出力します。

1. `messageStart` ([MessageStartEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_MessageStartEvent.html))。メッセージの開始イベント。メッセージのロールが含まれます。

1. `contentBlockStart` ([ContentBlockStartEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlockStartEvent.html))。コンテンツブロックの開始イベント。ツールの使用のみ。

1. `contentBlockDelta` ([ContentBlockDeltaEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlockDeltaEvent.html))。コンテンツブロックのデルタイベント。次のいずれかが含まれます。
   + `text` – モデルが生成する部分的なテキスト。
   + `reasoningContent` – レスポンスを生成するためにモデルが実行する部分的な推論。後続の `Converse` リクエストで、以前のすべてのメッセージに加えて、返された `signature` を送信する必要があります。メッセージのいずれかが変更されると、レスポンスはエラーをスローします。
   + `toolUse` – ツール使用のための部分的な入力 JSON オブジェクト。

1. `contentBlockStop` ([ContentBlockStopEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlockStopEvent.html))。コンテンツブロックの停止イベント。

1. `messageStop` ([MessageStopEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_MessageStopEvent.html))。メッセージの停止イベント。モデルが出力の生成を停止した理由が含まれます。

1. `metadata` ([ConverseStreamMetadataEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStreamMetadataEvent.html))。リクエストのメタデータ。メタデータには、`usage` ([TokenUsage](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_TokenUsage.html)) のトークン使用量と `metrics` ([ConverseStreamMetadataEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStreamMetadataEvent.html)) の呼び出しのメトリクスが含まれます。

ConverseStream は、完全なコンテンツブロックを `ContentBlockStartEvent` イベント、1 つ以上の `ContentBlockDeltaEvent` イベント、および `ContentBlockStopEvent` イベントとしてストリーミングします。`contentBlockIndex` フィールドをインデックスとして使用して、コンテンツブロックを構成するイベントを関連付けます。

次の例は、`ConverseStream` からのレスポンスの一部です。

```
{'messageStart': {'role': 'assistant'}}
{'contentBlockDelta': {'delta': {'text': ''}, 'contentBlockIndex': 0}}
{'contentBlockDelta': {'delta': {'text': ' Title'}, 'contentBlockIndex': 0}}
{'contentBlockDelta': {'delta': {'text': ':'}, 'contentBlockIndex': 0}}
.
.
.
{'contentBlockDelta': {'delta': {'text': ' The'}, 'contentBlockIndex': 0}}
{'messageStop': {'stopReason': 'max_tokens'}}
{'metadata': {'usage': {'inputTokens': 47, 'outputTokens': 20, 'totalTokens': 67}, 'metrics': {'latencyMs': 100.0}}}
```

# Converse API の例
<a name="conversation-inference-examples"></a>

次の例では、`Converse` および `ConverseStream` オペレーションの使用方法を示します。

------
#### [ Text ]

この例では、*Anthropic Claude 3 Sonnet* モデルで `Converse` オペレーションを呼び出す方法を示しています。この例では、入力テキスト、推論パラメータ、およびモデルに固有の追加のパラメータを送信する方法を示しています。コードは、モデルに曲のリストを作成するように依頼することで会話を開始します。その後、英国のアーティストによる曲であることを尋ねることで会話を続けます。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to use the <noloc>Converse</noloc> API with Anthropic Claude 3 Sonnet (on demand).
"""

import logging
import boto3

from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_conversation(bedrock_client,
                          model_id,
                          system_prompts,
                          messages):
    """
    Sends messages to a model.
    Args:
        bedrock_client: The Boto3 Bedrock runtime client.
        model_id (str): The model ID to use.
        system_prompts (JSON) : The system prompts for the model to use.
        messages (JSON) : The messages to send to the model.

    Returns:
        response (JSON): The conversation that the model generated.

    """

    logger.info("Generating message with model %s", model_id)

    # Inference parameters to use.
    temperature = 0.5
    top_k = 200

    # Base inference parameters to use.
    inference_config = {"temperature": temperature}
    # Additional inference parameters to use.
    additional_model_fields = {"top_k": top_k}

    # Send the message.
    response = bedrock_client.converse(
        modelId=model_id,
        messages=messages,
        system=system_prompts,
        inferenceConfig=inference_config,
        additionalModelRequestFields=additional_model_fields
    )

    # Log token usage.
    token_usage = response['usage']
    logger.info("Input tokens: %s", token_usage['inputTokens'])
    logger.info("Output tokens: %s", token_usage['outputTokens'])
    logger.info("Total tokens: %s", token_usage['totalTokens'])
    logger.info("Stop reason: %s", response['stopReason'])

    return response

def main():
    """
    Entrypoint for Anthropic Claude 3 Sonnet example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "anthropic.claude-3-sonnet-20240229-v1:0"

    # Setup the system prompts and messages to send to the model.
    system_prompts = [{"text": "You are an app that creates playlists for a radio station that plays rock and pop music. Only return song names and the artist."}]
    message_1 = {
        "role": "user",
        "content": [{"text": "Create a list of 3 pop songs."}]
    }
    message_2 = {
        "role": "user",
        "content": [{"text": "Make sure the songs are by artists from the United Kingdom."}]
    }
    messages = []

    try:

        bedrock_client = boto3.client(service_name='bedrock-runtime')

        # Start the conversation with the 1st message.
        messages.append(message_1)
        response = generate_conversation(
            bedrock_client, model_id, system_prompts, messages)

        # Add the response message to the conversation.
        output_message = response['output']['message']
        messages.append(output_message)

        # Continue the conversation with the 2nd message.
        messages.append(message_2)
        response = generate_conversation(
            bedrock_client, model_id, system_prompts, messages)

        output_message = response['output']['message']
        messages.append(output_message)

        # Show the complete conversation.
        for message in messages:
            print(f"Role: {message['role']}")
            for content in message['content']:
                print(f"Text: {content['text']}")
            print()

    except ClientError as err:
        message = err.response['Error']['Message']
        logger.error("A client error occurred: %s", message)
        print(f"A client error occured: {message}")

    else:
        print(
            f"Finished generating text with model {model_id}.")


if __name__ == "__main__":
    main()
```

------
#### [ Image ]

この例では、メッセージの一部として画像を送信する方法と、モデルが画像を記述するリクエストを示しています。この例では、`Converse` オペレーションと *Anthropic Claude 3 Sonnet* モデルを使用します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to send an image with the <noloc>Converse</noloc> API with an accompanying text prompt to Anthropic Claude 3 Sonnet (on demand).
"""

import logging
import boto3


from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_conversation(bedrock_client,
                          model_id,
                          input_text,
                          input_image):
    """
    Sends a message to a model.
    Args:
        bedrock_client: The Boto3 Bedrock runtime client.
        model_id (str): The model ID to use.
        input text : The text prompt accompanying the image.
        input_image : The path to the input image.

    Returns:
        response (JSON): The conversation that the model generated.

    """

    logger.info("Generating message with model %s", model_id)

    # Get image extension and read in image as bytes
    image_ext = input_image.split(".")[-1]
    with open(input_image, "rb") as f:
        image = f.read()

    message = {
        "role": "user",
        "content": [
            {
                "text": input_text
            },
            {
                "image": {
                    "format": image_ext,
                    "source": {
                        "bytes": image
                    }
                }
            }
        ]
    }

    messages = [message]

    # Send the message.
    response = bedrock_client.converse(
        modelId=model_id,
        messages=messages
    )

    return response


def main():
    """
    Entrypoint for Anthropic Claude 3 Sonnet example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
    input_text = "What's in this image?"
    input_image = "path/to/image"

    try:

        bedrock_client = boto3.client(service_name="bedrock-runtime")

        response = generate_conversation(
            bedrock_client, model_id, input_text, input_image)

        output_message = response['output']['message']

        print(f"Role: {output_message['role']}")

        for content in output_message['content']:
            print(f"Text: {content['text']}")

        token_usage = response['usage']
        print(f"Input tokens:  {token_usage['inputTokens']}")
        print(f"Output tokens:  {token_usage['outputTokens']}")
        print(f"Total tokens:  {token_usage['totalTokens']}")
        print(f"Stop reason: {response['stopReason']}")

    except ClientError as err:
        message = err.response['Error']['Message']
        logger.error("A client error occurred: %s", message)
        print(f"A client error occured: {message}")

    else:
        print(
            f"Finished generating text with model {model_id}.")


if __name__ == "__main__":
    main()
```

------
#### [ Document ]

この例では、メッセージの一部としてドキュメントを送信する方法と、ドキュメントの内容を説明するようモデルにリクエストする方法を示しています。この例では、`Converse` オペレーションと *Anthropic Claude 3 Sonnet* モデルを使用します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to send an document as part of a message to Anthropic Claude 3 Sonnet (on demand).
"""

import logging
import boto3


from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_message(bedrock_client,
                     model_id,
                     input_text,
                     input_document_path):
    """
    Sends a message to a model.
    Args:
        bedrock_client: The Boto3 Bedrock runtime client.
        model_id (str): The model ID to use.
        input text : The input message.
        input_document_path : The path to the input document.

    Returns:
        response (JSON): The conversation that the model generated.

    """

    logger.info("Generating message with model %s", model_id)

    # Get format from path and read the path
    input_document_format = input_document_path.split(".")[-1]
    with open(input_document_path, 'rb') as input_document_file:
        input_document = input_document_file.read()

    # Message to send.
    message = {
        "role": "user",
        "content": [
            {
                "text": input_text
            },
            {
                "document": {
                    "name": "MyDocument",
                    "format": input_document_format,
                    "source": {
                        "bytes": input_document
                    }
                }
            }
        ]
    }

    messages = [message]

    # Send the message.
    response = bedrock_client.converse(
        modelId=model_id,
        messages=messages
    )

    return response


def main():
    """
    Entrypoint for Anthropic Claude 3 Sonnet example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
    input_text = "What's in this document?"
    input_document_path = "path/to/document"

    try:

        bedrock_client = boto3.client(service_name="bedrock-runtime")


        response = generate_message(
            bedrock_client, model_id, input_text, input_document_path)

        output_message = response['output']['message']

        print(f"Role: {output_message['role']}")

        for content in output_message['content']:
            print(f"Text: {content['text']}")

        token_usage = response['usage']
        print(f"Input tokens:  {token_usage['inputTokens']}")
        print(f"Output tokens:  {token_usage['outputTokens']}")
        print(f"Total tokens:  {token_usage['totalTokens']}")
        print(f"Stop reason: {response['stopReason']}")

    except ClientError as err:
        message = err.response['Error']['Message']
        logger.error("A client error occurred: %s", message)
        print(f"A client error occured: {message}")

    else:
        print(
            f"Finished generating text with model {model_id}.")


if __name__ == "__main__":
    main()
```

------
#### [ Streaming ]

この例では、*Anthropic Claude 3 Sonnet* モデルで `ConverseStream` オペレーションを呼び出す方法を示しています。この例では、入力テキスト、推論パラメータ、およびモデルに固有の追加のパラメータを送信する方法を示しています。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to use the <noloc>Converse</noloc> API to stream a response from Anthropic Claude 3 Sonnet (on demand).
"""

import logging
import boto3


from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def stream_conversation(bedrock_client,
                    model_id,
                    messages,
                    system_prompts,
                    inference_config,
                    additional_model_fields):
    """
    Sends messages to a model and streams the response.
    Args:
        bedrock_client: The Boto3 Bedrock runtime client.
        model_id (str): The model ID to use.
        messages (JSON) : The messages to send.
        system_prompts (JSON) : The system prompts to send.
        inference_config (JSON) : The inference configuration to use.
        additional_model_fields (JSON) : Additional model fields to use.

    Returns:
        Nothing.

    """

    logger.info("Streaming messages with model %s", model_id)

    response = bedrock_client.converse_stream(
        modelId=model_id,
        messages=messages,
        system=system_prompts,
        inferenceConfig=inference_config,
        additionalModelRequestFields=additional_model_fields
    )

    stream = response.get('stream')
    if stream:
        for event in stream:

            if 'messageStart' in event:
                print(f"\nRole: {event['messageStart']['role']}")

            if 'contentBlockDelta' in event:
                print(event['contentBlockDelta']['delta']['text'], end="")

            if 'messageStop' in event:
                print(f"\nStop reason: {event['messageStop']['stopReason']}")

            if 'metadata' in event:
                metadata = event['metadata']
                if 'usage' in metadata:
                    print("\nToken usage")
                    print(f"Input tokens: {metadata['usage']['inputTokens']}")
                    print(
                        f":Output tokens: {metadata['usage']['outputTokens']}")
                    print(f":Total tokens: {metadata['usage']['totalTokens']}")
                if 'metrics' in event['metadata']:
                    print(
                        f"Latency: {metadata['metrics']['latencyMs']} milliseconds")


def main():
    """
    Entrypoint for streaming message API response example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
    system_prompt = """You are an app that creates playlists for a radio station
      that plays rock and pop music. Only return song names and the artist."""

    # Message to send to the model.
    input_text = "Create a list of 3 pop songs."

    message = {
        "role": "user",
        "content": [{"text": input_text}]
    }
    messages = [message]
    
    # System prompts.
    system_prompts = [{"text" : system_prompt}]

    # inference parameters to use.
    temperature = 0.5
    top_k = 200
    # Base inference parameters.
    inference_config = {
        "temperature": temperature
    }
    # Additional model inference parameters.
    additional_model_fields = {"top_k": top_k}

    try:
        bedrock_client = boto3.client(service_name='bedrock-runtime')

        stream_conversation(bedrock_client, model_id, messages,
                        system_prompts, inference_config, additional_model_fields)

    except ClientError as err:
        message = err.response['Error']['Message']
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))

    else:
        print(
            f"Finished streaming messages with model {model_id}.")


if __name__ == "__main__":
    main()
```

------
#### [ Video ]

この例は、メッセージの一部として動画を送信し、モデルに動画の説明をリクエストする方法を説明しています。この例では、`Converse` オペレーションと Amazon Nova Pro モデルを使用します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to send a video with the <noloc>Converse</noloc> API to Amazon Nova Pro (on demand).
"""

import logging
import boto3


from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_conversation(bedrock_client,
                          model_id,
                          input_text,
                          input_video):
    """
    Sends a message to a model.
    Args:
        bedrock_client: The Boto3 Bedrock runtime client.
        model_id (str): The model ID to use.
        input text : The input message.
        input_video : The input video.

    Returns:
        response (JSON): The conversation that the model generated.

    """

    logger.info("Generating message with model %s", model_id)

    # Message to send.

    with open(input_video, "rb") as f:
        video = f.read()

    message = {
        "role": "user",
        "content": [
            {
                "text": input_text
            },
            {
                    "video": {
                        "format": 'mp4',
                        "source": {
                            "bytes": video
                        }
                    }
            }
        ]
    }

    messages = [message]

    # Send the message.
    response = bedrock_client.converse(
        modelId=model_id,
        messages=messages
    )

    return response


def main():
    """
    Entrypoint for Amazon Nova Pro example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "amazon.nova-pro-v1:0"
    input_text = "What's in this video?"
    input_video = "path/to/video"

    try:

        bedrock_client = boto3.client(service_name="bedrock-runtime")

        response = generate_conversation(
            bedrock_client, model_id, input_text, input_video)

        output_message = response['output']['message']

        print(f"Role: {output_message['role']}")

        for content in output_message['content']:
            print(f"Text: {content['text']}")

        token_usage = response['usage']
        print(f"Input tokens:  {token_usage['inputTokens']}")
        print(f"Output tokens:  {token_usage['outputTokens']}")
        print(f"Total tokens:  {token_usage['totalTokens']}")
        print(f"Stop reason: {response['stopReason']}")

    except ClientError as err:
        message = err.response['Error']['Message']
        logger.error("A client error occurred: %s", message)
        print(f"A client error occured: {message}")

    else:
        print(
            f"Finished generating text with model {model_id}.")


if __name__ == "__main__":
    main()
```

------

# API の制限
<a name="inference-api-restrictions"></a>

、`InvokeModel`、`InvokeModelWithResponseStream`、および `ConverseStream`オペレーションには`Converse`、次の制限が適用されます。一部の制限は、以下に示すように、オペレーションまたはモデルによって異なります。
+ これらのオペレーションを使用する場合、 `role`が の場合にのみイメージとドキュメントを含めることができます`user`。
+ **動画生成:** 動画生成は、 `InvokeModel`および ではサポートされていません`InvokeModelWithResponseStream`。代わりに、[StartAsyncInvoke](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_StartAsyncInvoke.html) オペレーションを使用できます。例については、「[Amazon Nova Reel を使用してテキストプロンプトで動画を生成する](https://docs.aws.amazon.com/bedrock/latest/userguide/bedrock-runtime_example_bedrock-runtime_Scenario_AmazonNova_TextToVideo_section.html)」を参照してください。
+ **リクエスト本文でのドキュメントのサポート:** `InvokeModel`および を使用する場合、リクエスト本文にドキュメントを含めることはサポートされていません`InvokeModelWithResponseStream`。推論中にドキュメントを含めるには、 で[チャット/テキストプレイグラウンド](playgrounds.md)を使用する AWS マネジメントコンソール か、 `Converse` または `ConverseStream`オペレーションを使用します。
+ **ドキュメント数とサイズ:** リクエストごとに最大 5 つのドキュメントを含めることができます。各ドキュメントのサイズは 4.5 MB 以下にする必要があります。Claude 4 以降のバージョンでは、4.5 MB のドキュメントサイズ制限は `PDF`形式には適用されません。Nova モデルの場合、4.5 MB のドキュメントサイズ制限は `PDF`および `DOCX`形式には適用されません。これらの制限は Bedrock コンソールで引き続き適用されます。個々のモデルには、Amazon Bedrock によって適用されるもの以外の追加のコンテンツ制限がある場合があります。詳細については、**「サードパーティーのモデルプロバイダーの要件**」を参照してください。
+ **イメージの数とサイズ**: Amazon Bedrock では、イメージの数とサイズに制限はありません。ただし、個々のモデルには特定のイメージ要件がある場合があります。詳細については、**「サードパーティーのモデルプロバイダーの要件**」を参照してください。
+ **サードパーティーモデルプロバイダーの要件:** 、`InvokeModel`、、および `ConverseStream`オペレーションを使用する場合`InvokeModelWithResponseStream``Converse`、サードパーティーモデルプロバイダーの要件が適用され、満たされない場合にエラーが発生する可能性があります。Amazon Bedrock を介してサードパーティーモデル (Anthropic Claude など) を使用する場合は、予期しないエラーを避けるためにプロバイダーのユーザーガイドと API ドキュメントを確認してください。たとえば、Anthropic Messages 標準エンドポイントは、最大リクエストサイズ 32 MB をサポートしています。Claude には、リクエストあたり最大 100 `PDF`ページ、最大イメージサイズ 8000x8000 ピクセルなど、特定のコンテンツ要件もあります。リクエストサイズやコンテンツ要件など、Anthropic Claude Messages のリクエストとレスポンスに関する最新情報については、Anthropic Claude ドキュメント[「Anthropic Claude API Overview](https://platform.claude.com/docs/en/api/overview)」、[「Anthropic Claude Messages API Reference](https://docs.anthropic.com/claude/reference/messages_post)」、[「Build with Claude: Vision](https://platform.claude.com/docs/en/build-with-claude/vision) and [Build with Claude: PDF Support](https://platform.claude.com/docs/en/build-with-claude/pdf-support)」を参照してください。

**ヒント**  
Claude では、PDF ドキュメントをリクエストごとに最大 100 ページにする必要があります。大きな PDF ドキュメントがある場合は、それぞれ 100 ページ未満の複数の PDFs に分割するか、より多くのテキストを少数のページに統合することをお勧めします。

# モデルから検証済みの JSON 結果を取得する
<a name="structured-output"></a>

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

## 利点
<a name="structured-output-benefits"></a>

構造化出力は、本番稼働用 AI アプリケーションの重要な課題に対処します。
+ **スキーマのコンプライアンスを確保** — プロンプトベースのアプローチからエラー率と再試行ループを排除
+ **開発の複雑さの軽減** – カスタム解析および検証ロジックが不要
+ **運用コストの削減** – 失敗したリクエストと再試行を削減
+ **本番稼働の信頼性** – 予測可能で機械読み取り可能な出力を必要とする AI アプリケーションの確実なデプロイを可能にします。

## 仕組み
<a name="structured-output-how-it-works"></a>

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

### JSON スキーマ出力形式
<a name="structured-output-json-schema"></a>

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

### 厳格なツールの使用
<a name="structured-output-strict-tool-use"></a>

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

これらのメカニズムは、個別に使用することも、同じリクエストで一緒に使用することもできます。詳細については、[Bedrock API ドキュメント](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)を参照してください。

### リクエストワークフロー
<a name="structured-output-request-workflow"></a>

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

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

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

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

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

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

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

## サポートされている APIsまたは機能
<a name="structured-output-supported-apis"></a>

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

**Converse および ConverseStream APIs** – Converse および ConverseStream APIs で構造化された出力を使用して会話推論を行います。

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

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

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

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

## サポートされているモデル
<a name="structured-output-supported-models"></a>

構造化出力は、一部の Amazon Bedrock サーバーレスモデルのすべての商用 AWS リージョンで一般利用可能です。サポートされているモデルのリストについては、「機能別のモデルサポート」を参照してください。

### サポートされているすべてのモデルを表示する
<a name="w2aac13c32c35c11b5b1"></a>

Anthropic  
+ Claude Haiku 4.5 (`anthropic.claude-haiku-4-5-20251001-v1:0`)
+ Claude Sonnet 4.5 (`anthropic.claude-sonnet-4-5-20250929-v1:0`)
+ Claude Opus 4.5 (`anthropic.claude-opus-4-5-20251101-v1:0`)
+ Claude Opus 4.6 (`anthropic.claude-opus-4-6-v1`)

Qwen  
+ Qwen3 235B A22B 2507 (`qwen.qwen3-235b-a22b-2507-v1:0`)
+ Qwen3 32B (高密度) (`qwen.qwen3-32b-v1:0`)
+ Qwen3-Coder-30B-A3B-Instruct (`qwen.qwen3-coder-30b-a3b-v1:0`)
+ Qwen3 Coder 480B A35B 指示 (`qwen.qwen3-coder-480b-a35b-v1:0`)
+ Qwen3 Next 80B A3B (`qwen.qwen3-next-80b-a3b`)
+ Qwen3 VL 235B A22B (`qwen.qwen3-vl-235b-a22b`)

OpenAI  
+ gpt-oss-120b (`openai.gpt-oss-120b-1:0`)
+ gpt-oss-20b (`openai.gpt-oss-20b-1:0`)
+ GPT OSS セーフガード 120B (`openai.gpt-oss-safeguard-120b`)
+ GPT OSS セーフガード 20B (`openai.gpt-oss-safeguard-20b`)

DeepSeek  
+ DeepSeek-V3.1 (`deepseek.v3-v1:0`)

Google  
+ Gemma 3 12B IT (`google.gemma-3-12b-it`)
+ Gemma 3 27B PT (`google.gemma-3-27b-it`)

MiniMax  
+ MiniMax M2 (`minimax.minimax-m2`)

Mistral AI  
+ Magistral Small 2509 (`mistral.magistral-small-2509`)
+ 最小 3B (`mistral.ministral-3-3b-instruct`)
+ ミニストラル 3 8B (`mistral.ministral-3-8b-instruct`)
+ 最小 14B 3.0 (`mistral.ministral-3-14b-instruct`)
+ Mistral Large 3 (`mistral.mistral-large-3-675b-instruct`)
+ Voxtral Mini 3B 2507 (`mistral.voxtral-mini-3b-2507`)
+ Voxtral Small 24B 2507 (`mistral.voxtral-small-24b-2507`)

ムーンショット AI  
+ Kimi K2 思考 (`moonshot.kimi-k2-thinking`)

NVIDIA  
+ NVIDIA Nemotron Nano 12B v2 VL BF16 (`nvidia.nemotron-nano-12b-v2`)
+ NVIDIA Nemotron Nano 9B v2 (`nvidia.nemotron-nano-9b-v2`)

## リクエストの例
<a name="structured-output-examples"></a>

### JSON スキーマ出力形式
<a name="structured-output-json-schema-examples"></a>

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

#### Converse API
<a name="json-schema-converse"></a>

##### 例を表示する
<a name="w2aac13c32c35c13b3b5b3b1"></a>

```
{
  "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)
<a name="json-schema-invokemodel-claude"></a>

##### 例を表示する
<a name="w2aac13c32c35c13b3b7b3b1"></a>

```
{
  "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 (オープンウェイトモデル)
<a name="json-schema-invokemodel-openweight"></a>

##### 例を表示する
<a name="w2aac13c32c35c13b3b9b3b1"></a>

```
{
  "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"
  }
}
```

### 厳格なツールの使用
<a name="structured-output-strict-tool-examples"></a>

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

#### Converse API
<a name="strict-tool-converse"></a>

##### 例を表示する
<a name="w2aac13c32c35c13b5b5b3b1"></a>

```
{
  "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)
<a name="strict-tool-invokemodel-claude"></a>

##### 例を表示する
<a name="w2aac13c32c35c13b5b7b3b1"></a>

```
{
  "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 (オープンウェイトモデル)
<a name="strict-tool-invokemodel-openweight"></a>

##### 例を表示する
<a name="w2aac13c32c35c13b5b9b3b1"></a>

```
{
  "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
}
```

# Computer Use ツールを使用して Amazon Bedrock モデルレスポンスを完了する
<a name="computer-use"></a>

Computer Use は、Anthropic Claude 3.7 Sonnet および Claude 3.5 Sonnet v2 でのみ利用可能な Anthropic Claude モデル機能 (ベータ版) です。Computer Use を使用すると、Claude は基本的な GUI アクションを通じてタスクを自動化するのに役立ちます。

**警告**  
コンピュータ使用機能は、 AWS サービス条件に定義されている「ベータサービス」として利用できます。本ライセンス条項には、 AWS および AWS サービス条件、および該当するモデル EULA とのお客様の契約が適用されます。Computer Use API には、標準の API 機能やチャットインターフェイスとは異なる固有のリスクがあることに注意してください。これらのリスクは、Computer Use API を使用してインターネットと通信するときに高くなります。リスクを最小限に抑えるには、次のような予防策を検討します。  
Computer Use 機能は、システムへの直接攻撃や事故を防ぐために、最小限の権限で専用の仮想マシンまたはコンテナで操作します。
情報の盗難を防ぐため、Computer Use API に機密アカウントまたはデータへのアクセスを許可しないでください。
Computer Use API のインターネットアクセスを必要なドメインに制限すると、悪意のあるコンテンツにさらされる状況を軽減できます。
適切な監視を行うには、機密タスク (現実の世界に重大な結果を与える意思決定など) や、肯定的合意が必要なもの (Cookie の承諾、金融取引の実行または利用規約への同意など) のループに人間を関与させます。
Claude が閲覧またはアクセスできるようにしたコンテンツは、指示をオーバーライドしたり、Claude による間違いを招いたり、意図しない動作を引き起こす場合があります。Claude を機密事項から隔離するなど、適切な予防措置を講じることは、プロンプトインジェクション関連のリスクを回避するためにも不可欠です。独自の製品で Computer Use 機能に必要なアクセス許可を有効化またはリクエストする前に、エンドユーザーに関連するリスクを通知したり、必要に応じて、エンドユーザーから合意を得ます。

Computer Use API には、ユーザーが使用できる事前定義された Computer Use ツール (*computer\$120241022*、*bash\$120241022* および *text\$1editor\$120241022*) がいくつか用意されています。その後、「前回の会議メモを含む E メールを Ben に送信する」やスクリーンショット (必要な場合) などのリクエストを含むプロンプトを作成できます。レスポンスには、JSON 形式の `tool_use` アクションのリストが含まれます (例: scroll\$1down、left\$1button\$1press、スクリーンショット)。コードはコンピュータアクションを実行し、出力を示すスクリーンショットとともに Claude を提供します (リクエストされた場合)。

多相ツール型を承認するようにツールパラメータが更新されました。これらを区別するために新しい `tool.type` プロパティが追加されました。`type` は、オプションですが、省略された場合、ツールは、カスタムツールとしてみなされます (以前唯一サポートされていたツールタイプ)。さらに、対応する列挙値 `computer-use-2024-10-22` を持つ新しいパラメータ `anthropic_beta` が追加されました。このパラメータと列挙で行われたリクエストのみが、新しい Computer Use ツールを使用できます。`"anthropic_beta": ["computer-use-2024-10-22"] ` のように指定できます。

Anthropic Claude 3.5 Sonnet v2 で Computer Use を使用するには、Converse API ([Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) または [ConverseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html)) を使用できます。`additionalModelRequestFields` フィールドに Computer Use 固有のフィールドを指定します。Converse API の呼び出しに関する全般的な情報については、「[Converse API オペレーションを使用して会話を実行する](conversation-inference.md)」を参照してください。

ベース推論オペレーション ([InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) または [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html)) でツールを使用できます。リクエスト本文で渡す推論パラメータを確認するには、「[Anthropic Claude Messages API](model-parameters-anthropic-claude-messages.md)」を参照してください。

詳細については、「Anthropic ドキュメント」の「[Computer Use (ベータ版)](https://docs.anthropic.com/en/docs/build-with-claude/computer-use)」を参照してください。

**Topics**
+ [コードの例](#computer-use-example-code)
+ [レスポンスの例](#example-response)

## コードの例
<a name="computer-use-example-code"></a>

次のコードは、Computer Use API を呼び出す方法を示しています。入力はコンソールの画像です AWS 。

```
with open('test_images/console.png', 'rb') as f:
        png = f.read()

    response = bedrock.converse(
        modelId='anthropic.claude-3-5-sonnet-20241022-v2:0',
        messages=[
            {
                'role': 'user',
                'content': [
                    {
                        'text': 'Go to the bedrock console'
                    },
                    {
                        'image': {
                            'format': 'png',
                            'source': {
                                'bytes': png
                            }
                        }
                    }
                ]
            }
        ],
        additionalModelRequestFields={
            "tools": [
                {
                    "type": "computer_20241022",
                    "name": "computer",
                    "display_height_px": 768,
                    "display_width_px": 1024,
                    "display_number": 0
                },
                {
                    "type": "bash_20241022",
                    "name": "bash",

                },
                {
                    "type": "text_editor_20241022",
                    "name": "str_replace_editor",
                }
            ],
            "anthropic_beta": ["computer-use-2024-10-22"]
        },
        toolConfig={
            'tools': [
                {
                    'toolSpec': {
                        'name': 'get_weather',
                        'inputSchema': {
                            'json': {
                                'type': 'object'
                            }
                        }
                    }
                }
            ]
        })

    print(json.dumps(response, indent=4))
```

## レスポンスの例
<a name="example-response"></a>

このコードの例では、以下のような出力を得ることができます。

```
{
   "id": "msg_bdrk_01Ch8g9MF3A9FTrmeywrwfMZ",
   "type": "message",
   "role": "assistant",
   "content": [
        {
            "type": "text",
            "text": "I can see from the screenshot that we're already in the AWS Console. To go to the Amazon Bedrock console specifically, I'll click on the Amazon Bedrock service from the \"Recently Visited\" section."
        },
        {
            "type": "tool_use",
            "id": "toolu_bdrk_013sAzs1gsda9wLrfD8bhYQ3",
            "name": "computer",
            "input": {
                "action": "screenshot"
            }
        }
   ],
   "stop_reason": "tool_use",
   "stop_sequence": null,
   "usage": {
       "input_tokens": 3710,
       "output_tokens": 97
   }
}
```