翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Converse との会話を実行する
Amazon Bedrock Converse を使用してAPI、Amazon Bedrock モデルとの間でメッセージを送受信する会話アプリケーションを作成できます。例えば、多くのターンにわたって会話を維持し、役に立つテクニカルサポートアシスタントなど、ニーズに固有のペルソナやトーンのカスタマイズを使用するチャットボットを作成できます。
Converse を使用するにはAPI、Converse または (ストリーミングレスポンスの場合) オペレーションを使用してモデルにメッセージを送信します。 ConverseStream会話アプリケーションには、既存の推論オペレーション (InvokeModel または InvokeModelWithResponseStream) を使用できます。ただし、メッセージをサポートするすべての Amazon Bedrock モデルで動作する一貫した APIを提供するためAPI、Converse を使用することをお勧めします。つまり、コードを 1 回記述して、異なるモデルで使用できるということです。モデルに一意の推論パラメータがある場合、Converse では、モデル固有の構造でそれらの一意のパラメータを渡すAPIこともできます。
Converse を使用してAPI、ツールの使用とガードレールをアプリケーションに実装できます。
注記
で Mistral AI また、Meta モデルでは、会話を可能にするモデル固有のプロンプトテンプレートに Converse が入力をAPI埋め込みます。
サポートされているモデルとモデル機能
Converse は、以下の Amazon Bedrock モデルとモデル機能APIをサポートしています。Converse APIは、埋め込みモデル ( など Titan Embeddings G1 - Text) または画像生成モデル (例:Stability AI).
モデル | 会話 | ConverseStream | システムプロンプト | ドキュメントチャット | 視覚 | ツールの使用 | ストリーミングツールの使用 | ガードレール |
---|---|---|---|---|---|---|---|---|
AI21 Jamba-Instruct |
あり |
はい |
はい |
いいえ |
いいえ |
いいえ |
いいえ |
なし |
AI21 Labs Jurassic-2 (テキスト) |
制限あり。チャットはサポートされていません。 |
なし |
いいえ |
いいえ |
いいえ |
いいえ |
いいえ |
あり |
Amazon Titan モデル |
あり |
はい |
なし |
はい ( を除く Titan Text Premier) |
なし |
いいえ |
いいえ |
あり |
Anthropic Claude 2 以前 |
あり |
はい |
はい |
はい |
いいえ |
いいえ |
いいえ |
あり |
Anthropic Claude 3 |
あり |
はい |
はい |
はい |
はい |
はい |
はい |
あり |
Anthropic Claude 3.5 |
あり |
はい |
はい |
いいえ |
はい |
はい |
はい |
あり |
Cohere Command |
制限あり。チャットはサポートされていません。 |
制限あり。チャットはサポートされていません。 |
なし |
はい |
いいえ |
いいえ |
いいえ |
あり |
Cohere Command Light |
制限あり。チャットはサポートされていません。 |
制限あり。チャットはサポートされていません。 |
なし |
いいえ |
いいえ |
いいえ |
いいえ |
あり |
Cohere Command R また、Command R+ |
あり |
はい |
はい |
はい |
いいえ |
はい |
いいえ |
なし |
Meta Llama 2 また、Llama 3 |
あり |
はい |
はい |
はい |
いいえ |
いいえ |
いいえ |
はい |
Meta Llama 3.1 |
はい |
はい |
はい |
はい |
いいえ |
はい |
いいえ |
はい |
Mistral AI Instruct |
はい |
はい |
いいえ |
はい |
いいえ |
いいえ |
いいえ |
はい |
Mistral Large |
はい |
はい |
はい |
はい |
いいえ |
はい |
いいえ |
はい |
Mistral Large 2 (24.07) |
はい |
はい |
はい |
はい |
いいえ |
はい |
いいえ |
はい |
Mistral Small | はい | はい | はい | いいえ | いいえ | はい | いいえ | あり |
注記
Cohere Command (テキスト) と AI21 Labs Jurassic-2 (テキスト) は、会話 とのチャットをサポートしていませんAPI。モデルは一度に 1 つのユーザーメッセージしか処理できず、会話の履歴を維持できません。複数のメッセージを渡そうとすると、エラーが発生します。
Converse の使用 API
Converse を使用するにはAPI、 Converse
または ConverseStream
オペレーションを呼び出してモデルにメッセージを送信します。を呼び出すにはConverse
、 bedrock:InvokeModel
オペレーションのアクセス許可が必要です。を呼び出すにはConverseStream
、 bedrock:InvokeModelWithResponseStream
オペレーションのアクセス許可が必要です。
リクエスト
使用するモデルを指定するには、 modelId
フィールドを設定します。Amazon Bedrock がサポートIDsするモデルのリストについては、「」を参照してくださいAmazon Bedrock モデル IDs。
会話は、ユーザーとモデル間の一連のメッセージです。会話を開始するには、ユーザー (ユーザーロール) としてメッセージをモデルに送信します。モデルはアシスタント (アシスタントロール ) として機能し、メッセージで返されるレスポンスを生成します。必要に応じて、さらにユーザーロールメッセージをモデルに送信することで、会話を続けることができます。会話コンテキストを維持するには、モデルから受信したアシスタントロールメッセージを後続のリクエストに必ず含めてください。サンプルコードについては、「会話APIの例」を参照してください。
messages
フィールドにモデルに渡すメッセージを指定します。これは、Message オブジェクトの配列にマッピングされます。各メッセージには、メッセージのコンテンツと、会話でメッセージが再生するロールが含まれます。
注記
Amazon Bedrock は、コンテンツとして提供したテキスト、イメージ、またはドキュメントを保存しません。データはレスポンスの生成にのみ使用されます。Converse を使用する場合はAPI、サイズが 4.5 MB 未満の非圧縮およびデコードされたドキュメントを使用する必要があります。
メッセージのコンテンツは、 ContentBlock オブジェクトの配列にマッピングされる content
フィールドに保存します。各 内でContentBlock、次のいずれかのフィールドを指定できます (どのモデルがどのモダリティをサポートしているかを確認するには、「」を参照してくださいサポートされているモデルとモデル機能)。
の他のフィールドContentBlock
はツール用です。
role
フィールドでロールを指定します。ロールは次のいずれかになります。
-
user — モデルにメッセージを送信している人間。
-
Assistant — 人間のユーザーにメッセージを送信しているモデル。
注記
以下の制限は、 content
フィールドに関連しています。
-
最大 20 個のイメージを含めることができます。各イメージのサイズ、高さ、幅は、それぞれ 3.75 MB、8,000 px、8,000 px 以下にする必要があります。
-
最大 5 つのドキュメントを含めることができます。各ドキュメントのサイズは 4.5 MB 以下にする必要があります。
-
イメージとドキュメントを含めることができるの
role
は、 が の場合のみですuser
。
次の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" } ] } ]
システムプロンプトは、実行するタスク、または会話中に採用するペルソナに関する指示やコンテキストをモデルに提供するプロンプトの一種です。次の例に示すように、 system
(SystemContentBlock) フィールドでリクエストのシステムプロンプトのリストを指定できます。
[ { "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. " } ]
推論パラメータ
Converse は、 inferenceConfig
フィールド () で設定した推論パラメータの基本セットAPIをサポートしますInferenceConfiguration。推論パラメータの基本セットは次のとおりです。
maxTokens – 生成されたレスポンスで許可されるトークンの最大数。
stopSequences – 停止シーケンスのリスト。停止シーケンスは、モデルがレスポンスの生成を停止する一連の文字です。
temperature – モデルがレスポンスの生成中に確率の高いオプションを選択する可能性。
topP – モデルが次のトークンと見なす可能性が最も高い候補の割合。
詳細については、「推論パラメータによる影響レスポンスの生成」を参照してください。
次の例では、temperature
推論パラメータJSONを設定します。
{"temperature": 0.5}
使用しているモデルに追加の推論パラメータがある場合は、 JSONadditionalModelRequestFields
フィールドに と指定することで、それらのパラメータを設定できます。次の例はtop_k
、 で利用可能な を設定する方法JSONを示しています。Anthropic Claude はモデルですが、メッセージ のベース推論パラメータではありませんAPI。
{"top_k": 200}
次の例に示すように、 additionalModelResponseFieldPaths
フィールドで追加のモデルパラメータのパスを指定できます。
[ "/stop_sequence" ]
は、 フィールドでリクエストした追加のadditionalModelResponseFields
フィールドAPIを返します。
レスポンス
Converse から得られるレスポンスは、呼び出すオペレーション、Converse
または APIによって異なりますConverseStream
。
逆レスポンス
からのレスポンスではConverse
、 output
フィールド (ConverseOutput) には、モデルが生成するメッセージ (メッセージ ) が含まれます。メッセージの内容は content
(ContentBlock) フィールドにあり、メッセージに対応するロール (user
または assistant
) は role
フィールドにあります。
metrics
フィールド (ConverseMetrics) には、呼び出しのメトリクスが含まれます。モデルがコンテンツの生成を停止した理由を確認するには、 stopReason
フィールドを確認します。usage
フィールド () を確認することで、リクエストでモデルに渡されたトークンと、レスポンスで生成されたトークンに関する情報を取得できますTokenUsage。リクエストで追加のレスポンスフィールドを指定した場合、 はそれらを additionalModelResponseFields
フィールドJSONで としてAPI返します。
次の例は、「」で説明されているプロンプトに合格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 レスポンス
ConverseStream
を呼び出してモデルからレスポンスをストリーミングすると、ストリームはstream
レスポンスフィールドで返されます。ストリームは次の順序で次のイベントを出力します。
-
messageStart
(MessageStartEvent)。メッセージの開始イベント。メッセージのロールが含まれます。 -
contentBlockStart
(ContentBlockStartEvent)。コンテンツブロック開始イベント。ツールの使用のみ。 -
contentBlockDelta
(ContentBlockDeltaEvent)。コンテンツブロックの差分イベント。モデルが生成する部分的なテキスト、またはツール用の部分的な入力 JSON が含まれます。 -
contentBlockStop
(ContentBlockStopEvent)。コンテンツブロック停止イベント。 -
messageStop
(MessageStopEvent)。メッセージの停止イベント。モデルが出力の生成を停止した理由が含まれます。 -
metadata
(ConverseStreamMetadataEvent)。リクエストのメタデータ。メタデータには、usage
(TokenUsage) のトークン使用量と () の呼び出しのメトリクスが含まれますmetrics
ConverseStreamMetadataEvent。
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}}}
会話APIの例
次の例は、 Converse
および ConverseStream
オペレーションの使用方法を示しています。