翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
レスポンスストリームで Invoke Model API を使用して Amazon Bedrock で Mistral AI モデルを呼び出す
次のコード例は、Invoke Model API を使用して Mistral AI モデルにテキストメッセージを送信し、レスポンスストリームを出力する方法を示しています。
- Python
-
- SDK for Python (Boto3)
-
については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。
モデル呼び出し API を使用してテキストメッセージを送信し、レスポンスストリームを出力します。
# Use the native inference API to send a text message to Mistral AI
# and print the response stream.
import boto3
import json
# 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., Mistral Large.
model_id = "mistral.mistral-large-2402-v1:0"
# Define the message to send.
user_message = "Describe the purpose of a 'hello world' program in one line."
# Embed the message in Mistral's prompt format.
prompt = f"<s>[INST] {user_message} [/INST]"
# Format the request payload using the model's native structure.
native_request = {
"prompt": prompt,
"max_tokens": 512,
"temperature": 0.5,
}
# Convert the native request to JSON.
request = json.dumps(native_request)
# Invoke the model with the request.
streaming_response = client.invoke_model_with_response_stream(
modelId=model_id, body=request
)
# Extract and print the response text in real-time.
for event in streaming_response["body"]:
chunk = json.loads(event["chunk"]["bytes"])
if "outputs" in chunk:
print(chunk["outputs"][0]["text"], end="")
AWS SDK デベロッパーガイドとコード例の完全なリストについては、「」を参照してくださいAWS SDK でこのサービスを使用する。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。