翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Converse API を使用した推論
Converse API はbedrock-runtimeエンドポイントでのみ使用できます。
Amazon Bedrock Converse API を使用すると、Amazon Bedrock モデルとの間でメッセージを送受信する会話型アプリケーションを作成できます。例えば、多くのターンにわたって会話を維持し、役に立つテクニカルサポートアシスタントなど、ニーズに固有のペルソナまたはトーンをカスタマイズしたチャットボットを作成できます。
Converse API を使用するには、Converse または ConverseStream (ストリーミングレスポンス用) オペレーションを使用して、モデルにメッセージを送信します。会話アプリケーションには、既存のベース推論オペレーション (InvokeModel または InvokeModelWithResponseStream) を使用できます。ただし、メッセージをサポートするすべての Amazon Bedrock モデルで動作する一貫した API を提供する Converse API を使用することをお勧めします。そうすることで、コードを 1 回だけ記述し、それをさまざまなモデルで使用できます。Converse API では、モデルに一意の推論パラメータがある場合、モデル固有の構造でこれらの一意のパラメータを渡すこともできます。
Converse API を使用すると、アプリケーションでツールの使用とガードレールを実装できます。
コード例については、以下を参照してください。
Converse API を使用する場合
Converse API を使用するには、Converse または ConverseStream オペレーションを呼び出してモデルにメッセージを送信します。Converse を呼び出すには、bedrock:InvokeModel オペレーションを呼び出す許可も必要です。ConverseStream を呼び出すには、bedrock:InvokeModelWithResponseStream オペレーションを呼び出す許可も必要です。
[リクエスト]
Amazon Bedrock ランタイムエンドポイントで Converse リクエストを行うときは、次のフィールドを含めることができます。
-
modelId – 推論に使用するリソースを指定できるヘッダー内の必須パラメータ。
-
次のフィールドでは、プロンプトをカスタマイズできます。
-
messages – プロンプトの内容とロールを指定するために使用します。
-
system – モデルの指示またはコンテキストを定義するシステムプロンプトを指定するために使用します。
-
inferenceConfig – すべてのモデルに共通の推論パラメータを指定するために使用します。推論パラメータはレスポンスの生成に影響を与えます。
-
additionalModelRequestFields – 推論を実行するモデルに固有の推論パラメータを指定するために使用します。
-
promptVariables – (プロンプト管理のプロンプトを使用する場合) このフィールドを使用して、入力するプロンプト内の変数と入力する値を定義します。
-
次のフィールドでは、レスポンスが返される方法をカスタマイズできます。
-
guardrailConfig – このフィールドを使用して、プロンプト全体に適用するガードレールを含めます。
-
toolConfig – このフィールドを使用して、モデルがレスポンスを生成するのに役立つツールを含めます。
-
additionalModelResponseFieldPaths – このフィールドを使用して、JSON ポインタオブジェクトとして返すフィールドを指定します。
-
serviceTier – このフィールドを使用して、特定のリクエストのサービス層を指定します。
-
requestMetadata – このフィールドを使用して、呼び出しログを使用するときにフィルタリングできるメタデータを含めます。
Converse または ConverseStream でプロンプト管理プロンプトを使用する場合、次の制限が適用されます。
-
additionalModelRequestFields、inferenceConfig、system、toolConfig フィールドを含めることはできません。
-
messages フィールドを含めると、プロンプトで定義されたメッセージの後にメッセージが追加されます。
-
guardrailConfig フィールドを含めると、プロンプト全体にガードレールが適用されます。ContentBlock フィールドに guardContent ブロックを含めると、ガードレールはそれらのブロックにのみ適用されます。
セクションを展開すると、Converse リクエスト本文のフィールドの詳細を確認できます。
messages
messages フィールドは Message オブジェクトの配列であり、それぞれがユーザーとモデル間のメッセージを定義します。Message オブジェクトには、以下のフィールドが含まれています。
会話コンテキストを維持するには、後続の Converse リクエストに会話のすべてのメッセージを含め、role フィールドを使用して、メッセージがユーザーからのものであるかモデルからのものであるかを指定します。
content フィールドは ContentBlock オブジェクトの配列にマッピングされます。各 ContentBlock 内で、次のいずれかのフィールドを指定できます (どのモデルがどのブロックをサポートしているかを確認するには、モデルを一目で確認できます)。
- text
-
text フィールドは、プロンプトを指定する文字列にマッピングされます。text フィールドは、同じ ContentBlock で指定された他のフィールドとともに解釈されます。
以下は、テキスト ContentBlock のみを含む content 配列を持つ Message オブジェクトを示しています。
{
"role": "user",
"content": [
{
"text": "string"
}
]
}
- image
-
image フィールドは ImageBlock にマッピングされます。base64 でエンコードされた raw バイトを、bytes フィールド内の画像に渡します。 AWS SDK を使用する場合、base64 でバイトをエンコードする必要はありません。
text フィールドを除外すると、モデルは画像を記述します。
以下は、画像 ContentBlock のみを含む content 配列を持つサンプル Message オブジェクトを示しています。
{
"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 にマッピングされます。DocumentBlock を含める場合は、リクエストが次の制限に準拠していることを確認します。
-
メッセージオブジェクトの content フィールドに、ドキュメントに関連するプロンプトを含む text フィールドを含める必要があります。
-
base64 でエンコードされた raw バイトを、bytes フィールド内のドキュメントに渡します。 AWS SDK を使用する場合、base64 のドキュメントのバイトをエンコードする必要はありません。
-
name フィールドには以下の文字のみ含めることができます。
-
アルファベットの文字
-
空白文字 (連続した空白文字は使用不可)
-
ハイフン
-
括弧
-
角括弧
モデルが誤って指示として解釈する可能性があるため、name フィールドはプロンプトインジェクションに対して脆弱です。したがって、中立的な名前を指定することが推奨されます。
ドキュメントを使用する場合、citations タグを有効にできます。これにより、API コールのレスポンスでドキュメント固有の引用が提供されます。詳細については、「DocumentBlock」 API を参照してください。
以下は、ドキュメント ContentBlock と必須の付随テキスト ContentBlock のみを含む content 配列を持つサンプル Message オブジェクトを示しています。
{
"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 オブジェクトにマッピングされます。base64 でエンコードされた bytes フィールド内の raw バイトを渡します。 AWS SDK を使用する場合、base64 のバイトをエンコードする必要はありません。
text フィールドを含めない場合、モデルは動画を記述します。
以下は、画像 ContentBlock のみを含む content 配列を持つサンプル Message オブジェクトを示しています。
{
"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 バケットへのアクセスを設定する」を参照してください。
- cachePoint
-
cachePoint フィールドを使用してプロンプトキャッシュを使用することで、キャッシュチェックポイントをメッセージ内のブロックとして付随するプロンプトとともに追加できます。プロンプトキャッシュは、会話のコンテキストのキャッシュを開始してコストとレイテンシーを節約できる機能です。詳細については、「モデル推論を高速化するためのプロンプトキャッシュ」を参照してください。
以下は、ドキュメント ContentBlock と付随する必須のテキスト ContentBlock を含む content 配列を持つ Message オブジェクトの例と、ドキュメントとテキストの両方の内容をキャッシュに追加する cachePoint を示しています。
{
"role": "user",
"content": [
{
"text": "string"
},
{
"document": {
"format": "pdf",
"name": "string",
"source": {
"bytes": "document in bytes"
}
}
},
{
"cachePoint": {
"type": "default"
}
}
]
}
- guardContent
-
guardContent フィールドは GuardrailConverseContentBlock オブジェクトにマッピングされます。このフィールドを使用して、guardrailConfig フィールドで定義されたガードレールによって評価される入力をターゲットにできます。このフィールドを指定しない場合、ガードレールはリクエスト本文内のすべてのメッセージを評価します。GuardBlock では、次のタイプのコンテンツを渡すことができます。
-
text – 以下は、テキスト GuardrailConverseContentBlock のみを含む content 配列を持つ Message オブジェクトの例を示しています。
{
"role": "user",
"content": [
{
"text": "Tell me what stocks to buy.",
"qualifiers": [
"guard_content"
]
}
]
}
評価するテキストを定義し、コンテキストグラウンディングに使用する修飾子を含めます。
-
image – 以下は、画像 GuardrailConverseContentBlock のみを含む content 配列を持つ Message オブジェクトの例を示しています。
{
"role": "user",
"content": [
{
"format": "png",
"source": {
"bytes": "image in bytes"
}
}
]
}
画像の形式を指定し、画像をバイト単位で定義します。
ガードレールの使用方法の詳細については、「Amazon Bedrock ガードレールを使用して有害なコンテンツを検出してフィルタリングする」を参照してください。
- reasoningContent
-
reasoningContent フィールドは ReasoningContentBlock にマッピングされます。このブロックには、付随する 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 のモデルレスポンスを完成させる」を参照してください。
- toolResult
-
ツールを使用するモデルの結果に関する情報が含まれます。詳細については、「ツールを使用して Amazon Bedrock のモデルレスポンスを完成させる」を参照してください。
次の 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
システムプロンプトは、会話中に実行するタスク、または採用するペルソナに関する指示やコンテキストをモデルに提供するプロンプトの一種です。次の例に示すように、system (SystemContentBlock) フィールドでリクエストのシステムプロンプトのリストを指定できます。
[
{
"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
Converse API は、inferenceConfig フィールド (InferenceConfiguration) で設定した推論パラメータの基本セットをサポートします。推論パラメータの基本セットは次のとおりです。
-
maxTokens – 生成されたレスポンスで許可するトークンの最大数。
-
stopSequences – ストップシーケンスのリスト。停止シーケンスは、モデルがレスポンスの生成を停止する一連の文字です。
-
temperature – モデルがレスポンスの生成中に確率の高いオプションを選択する可能性。
-
topP — モデルが次のトークンについて考慮する最も可能性の高い候補のパーセンテージ。
詳細については、「推論パラメータでレスポンスの生成に影響を与える」を参照してください。
次の JSON の例では、temperature 推論パラメータを設定します。
{"temperature": 0.5}
additionalModelRequestFields
使用しているモデルに追加の推論パラメータがある場合は、additionalModelRequestFields フィールドに JSON として指定することで、これらのパラメータを設定できます。次の JSON の例は、top_k を設定する方法を示しています。これは Anthropic Claude モデルで使用できますが、メッセージ API のベース推論パラメータではありません。
{"top_k": 200}
promptVariables
推論を実行するリソースとして modelId のプロンプト管理からプロンプトを指定する場合は、このフィールドを使用してプロンプト変数に実際の値を入力します。promptVariables フィールドは、プロンプトで定義された変数に対応するキーと、変数を置き換える値を持つ JSON オブジェクトにマップされます。
例えば Make me a {{genre}} playlist consisting of the following number of songs: {{number}}. というプロンプトがあるとします。プロンプトの 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": {
"text": "pop"
},
"number": {
"text": "3"
}
}
}
guardrailConfig
このフィールドを含めることで、Amazon Bedrock ガードレールで作成したガードレールを適用できます。会話内の特定のメッセージにガードレールを適用するには、GuardrailConverseContentBlock にメッセージを含めます。リクエスト本文に GuardrailConverseContentBlock を含めない場合、ガードレールは messages フィールド内のすべてのメッセージに適用されます。例については、Converse API にガードレールを含めるを参照してください。
このフィールドでは、モデルがレスポンスを生成するのに役立つツールを定義できます。詳細については、「ツールを使用して Amazon Bedrock のモデルレスポンスを完成させる」を参照してください。
additionalModelResponseFieldPaths
次の例に示すように、additionalModelResponseFieldPaths フィールドで追加のモデルパラメータのパスを指定できます。
[ "/stop_sequence" ]
API は、additionalModelResponseFields フィールドでリクエストした追加のフィールドを返します。
requestMetadata フィールドは、モデル呼び出しログのリクエストで記録されるキーと値のタグの JSON オブジェクトにマッピングされます。リクエストメタデータを使用して、チーム、アプリケーション、環境、または呼び出しごとに異なるその他のディメンションごとにログをフィルタリングおよび集計できます。
X-Amzn-Bedrock-Request-Metadata HTTP ヘッダーを介して InvokeModel と InvokeModelWithResponseStream でも同じ機能を使用できます。サポートされている APIs「」を参照してくださいリクエストごとのメタデータのタグ付け。
serviceTier
このフィールドは JSON オブジェクトにマッピングされます。特定のリクエストのサービス層を指定できます。
次の例は、 serviceTier構造を示しています。
"serviceTier": {
"type": "reserved" | "priority" | "default" | "flex"
}
料金やパフォーマンス特性など、サービス階層の詳細については、「」を参照してくださいパフォーマンスとコストを最適化するためのサービス階層。
オプションでキャッシュチェックポイントを system または tools フィールドに追加して、使用しているモデルに応じてプロンプトキャッシュを使用することもできます。詳細については、「モデル推論を高速化するためのプロンプトキャッシュ」を参照してください。
[応答]
Converse API から得られるレスポンスは、呼び出すオペレーション、Converse、または ConverseStream によって異なります。
会話レスポンス
Converse からのレスポンスでは、output フィールド (ConverseOutput) には、モデルが生成するメッセージ (Message) が含まれます。メッセージの内容は content (ContentBlock) フィールドにあり、メッセージが対応するロール (user または assistant) は role フィールドにあります。
プロンプトキャッシュを使用した場合は、使用量フィールドの cacheReadInputTokens とcacheWriteInputTokens はキャッシュから読み取られたトークンの合計数とキャッシュに書き込まれたトークンの合計数をそれぞれ示します。
サービス階層を使用した場合、レスポンスフィールドで、 service tierはリクエストに使用されたサービス階層を示します。
metrics フィールド (ConverseMetrics) には、呼び出しのメトリクスが含まれます。モデルがコンテンツの生成を停止した理由を確認するには、stopReason フィールドを確認します。usage フィールド (TokenUsage) を確認することで、リクエストでモデルに渡されたトークンと、レスポンスで生成されたトークンに関する情報を取得できます。リクエストで追加のレスポンスフィールドを指定した場合、API は additionalModelResponseFields フィールドに JSON として返します。
次の例は、[リクエスト] で説明されているプロンプトを渡す際の 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 レスポンスフィールドにストリームが返されます。ストリームは、以下の順序で次のイベントを出力します。
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 の例
次の例では、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()