SDK for Python (Boto3) を使用する Amazon Bedrock ランタイムの例 - AWS SDK コードの例

Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 AWS

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

SDK for Python (Boto3) を使用する Amazon Bedrock ランタイムの例

次のコード例は、Amazon Bedrock ランタイム AWS SDK for Python (Boto3) で を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

「シナリオ」は、1 つのサービス内から、または他の AWS のサービスと組み合わせて複数の関数を呼び出し、特定のタスクを実行する方法を示すコード例です。

各例には完全なソースコードへのリンクが含まれており、コードの設定方法と実行方法に関する手順を確認できます。

開始方法

次のコード例は、Amazon Bedrock の使用を開始する方法を示しています。

SDK for Python (Boto3)
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

InvokeModel オペレーションを使用してモデルにプロンプトを送信します。

""" Uses the Amazon Bedrock runtime client InvokeModel operation to send a prompt to a model. """ import logging import json import boto3 from botocore.exceptions import ClientError logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def invoke_model(brt, model_id, prompt): """ Invokes the specified model with the supplied prompt. param brt: A bedrock runtime boto3 client param model_id: The model ID for the model that you want to use. param prompt: The prompt that you want to send to the model. :return: The text response from the model. """ # Format the request payload using the model's native structure. native_request = { "inputText": prompt, "textGenerationConfig": { "maxTokenCount": 512, "temperature": 0.5, "topP": 0.9 } } # Convert the native request to JSON. request = json.dumps(native_request) try: # Invoke the model with the request. response = brt.invoke_model(modelId=model_id, body=request) # Decode the response body. model_response = json.loads(response["body"].read()) # Extract and print the response text. response_text = model_response["results"][0]["outputText"] return response_text except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") raise def main(): """Entry point for the example. Uses the AWS SDK for Python (Boto3) to create an Amazon Bedrock runtime client. Then sends a prompt to a model in the region set in the callers profile and credentials. """ # Create an Amazon Bedrock Runtime client. brt = boto3.client("bedrock-runtime") # Set the model ID, e.g., Amazon Titan Text G1 - Express. model_id = "amazon.titan-text-express-v1" # Define the prompt for the model. prompt = "Describe the purpose of a 'hello world' program in one line." # Send the prompt to the model. response = invoke_model(brt, model_id, prompt) print(f"Response: {response}") logger.info("Done.") if __name__ == "__main__": main()

Converse オペレーションを使用してモデルにユーザーメッセージを送信します。

""" Uses the Amazon Bedrock runtime client Converse operation to send a user message to a model. """ import logging import boto3 from botocore.exceptions import ClientError logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def converse(brt, model_id, user_message): """ Uses the Converse operation to send a user message to the supplied model. param brt: A bedrock runtime boto3 client param model_id: The model ID for the model that you want to use. param user message: The user message that you want to send to the model. :return: The text response from the model. """ # Format the request payload using the model's native structure. conversation = [ { "role": "user", "content": [{"text": user_message}], } ] try: # Send the message to the model, using a basic inference configuration. response = brt.converse( modelId=model_id, messages=conversation, inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9}, ) # Extract and print the response text. response_text = response["output"]["message"]["content"][0]["text"] return response_text except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") raise def main(): """Entry point for the example. Uses the AWS SDK for Python (Boto3) to create an Amazon Bedrock runtime client. Then sends a user message to a model in the region set in the callers profile and credentials. """ # Create an Amazon Bedrock Runtime client. brt = boto3.client("bedrock-runtime") # Set the model ID, e.g., Amazon Titan Text G1 - Express. model_id = "amazon.titan-text-express-v1" # Define the message for the model. message = "Describe the purpose of a 'hello world' program in one line." # Send the message to the model. response = converse(brt, model_id, message) print(f"Response: {response}") logger.info("Done.") if __name__ == "__main__": main()
  • API の詳細については、AWS SDK for Python (Boto3) API リファレンスの「InvokeModel」を参照してください。

シナリオ

次のコード例は、さまざまな方法で Amazon Bedrock 基盤モデルと相互作用するプレイグラウンドを作成する方法を示しています。

SDK for Python (Boto3)

Python 基盤モデル (FM) プレイグラウンドは Python/FastAPI のサンプルアプリケーションで、Python で Amazon Bedrock を使用する方法を紹介しています。この例は、Python 開発者が Amazon Bedrock を使用して生成 AI 対応アプリケーションを構築する方法を示しています。次の 3 つのプレイグラウンドを使用して Amazon Bedrock 基盤モデルをテストしたり操作したりできます。

  • テキストプレイグラウンド。

  • チャットプレイグラウンド。

  • イメージプレイグラウンド。

この例には、アクセスできる基盤モデルとその特性が一覧表示されています。ソースコードとデプロイ手順については、GitHub のプロジェクトを参照してください。

この例で使用されているサービス
  • Amazon Bedrock ランタイム

次のコードサンプルは、以下の操作方法を示しています。

  • マネージドプロンプトを作成します。

  • プロンプトのバージョンを作成します。

  • バージョンを使用してプロンプトを呼び出します。

  • リソースをクリーンアップします (オプション)。

SDK for Python (Boto3)
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

マネージドプロンプトを作成して呼び出します。

import argparse import boto3 import logging import time # Now import the modules from prompt import create_prompt, create_prompt_version, delete_prompt from run_prompt import invoke_prompt logging.basicConfig( level=logging.INFO, format='%(levelname)s: %(message)s' ) logger = logging.getLogger(__name__) def run_scenario(bedrock_client, bedrock_runtime_client, model_id, cleanup=True): """ Runs the Amazon Bedrock managed prompt scenario. Args: bedrock_client: The Amazon Bedrock Agent client. bedrock_runtime_client: The Amazon Bedrock Runtime client. model_id (str): The model ID to use for the prompt. cleanup (bool): Whether to clean up resources at the end of the scenario. Returns: dict: A dictionary containing the created resources. """ prompt_id = None try: # Step 1: Create a prompt print("\n=== Step 1: Creating a prompt ===") prompt_name = f"PlaylistGenerator-{int(time.time())}" prompt_description = "Playlist generator" prompt_template = """ Make me a {{genre}} playlist consisting of the following number of songs: {{number}}.""" create_response = create_prompt( bedrock_client, prompt_name, prompt_description, prompt_template, model_id ) prompt_id = create_response['id'] print(f"Created prompt: {prompt_name} with ID: {prompt_id}") # Create a version of the prompt print("\n=== Creating a version of the prompt ===") version_response = create_prompt_version( bedrock_client, prompt_id, description="Initial version of the product description generator" ) prompt_version_arn = version_response['arn'] prompt_version = version_response['version'] print(f"Created prompt version: {prompt_version}") print(f"Prompt version ARN: {prompt_version_arn}") # Step 2: Invoke the prompt directly print("\n=== Step 2: Invoking the prompt ===") input_variables = { "genre": "pop", "number": "2", } # Use the ARN from the create_prompt_version response result = invoke_prompt( bedrock_runtime_client, prompt_version_arn, input_variables ) # Display the playlist print(f"\n{result}") # Step 3: Clean up resources (optional) if cleanup: print("\n=== Step 3: Cleaning up resources ===") # Delete the prompt print(f"Deleting prompt {prompt_id}...") delete_prompt(bedrock_client, prompt_id) print("Cleanup complete") else: print("\n=== Resources were not cleaned up ===") print(f"Prompt ID: {prompt_id}") except Exception as e: logger.exception("Error in scenario: %s", str(e)) # Attempt to clean up if an error occurred and cleanup was requested if cleanup and prompt_id: try: print("\nCleaning up resources after error...") # Delete the prompt try: delete_prompt(bedrock_client, prompt_id) print("Cleanup after error complete") except Exception as cleanup_error: logger.error("Error during cleanup: %s", str(cleanup_error)) except Exception as final_error: logger.error("Final error during cleanup: %s", str(final_error)) # Re-raise the original exception raise def main(): """ Entry point for the Amazon Bedrock managed prompt scenario. """ parser = argparse.ArgumentParser( description="Run the Amazon Bedrock managed prompt scenario." ) parser.add_argument( '--region', default='us-east-1', help="The AWS Region to use." ) parser.add_argument( '--model-id', default='anthropic.claude-v2', help="The model ID to use for the prompt." ) parser.add_argument( '--cleanup', action='store_true', default=True, help="Clean up resources at the end of the scenario." ) parser.add_argument( '--no-cleanup', action='store_false', dest='cleanup', help="Don't clean up resources at the end of the scenario." ) args = parser.parse_args() bedrock_client = boto3.client('bedrock-agent', region_name=args.region) bedrock_runtime_client = boto3.client('bedrock-runtime', region_name=args.region) print("=== Amazon Bedrock Managed Prompt Scenario ===") print(f"Region: {args.region}") print(f"Model ID: {args.model_id}") print(f"Cleanup resources: {args.cleanup}") try: run_scenario( bedrock_client, bedrock_runtime_client, args.model_id, args.cleanup ) except Exception as e: logger.exception("Error running scenario: %s", str(e)) if __name__ == "__main__": main()

次のコード例は、Amazon Bedrock と Step Functions を使用して生成 AI アプリケーションを構築およびオーケストレーションする方法を示しています。

SDK for Python (Boto3)

Amazon Bedrock Serverless のプロンプトチェイニングシナリオは、AWS Step FunctionsAmazon Bedrock、および https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html を使用して、複雑でサーバーレス、高度にスケーラブルな生成 AI アプリケーションを構築およびオーケストレーションする方法を示しています。これには、次の実際の例が含まれています。

  • 文学ブログの特定の小説の分析を行う。この例では、プロンプトのシンプルでシーケンシャルなチェーンを示しています。

  • 特定のトピックに関する短いストーリーを生成する。この例では、AI が以前に生成した項目のリストを繰り返し処理する方法を示しています。

  • 特定の目的地への週末の旅程を作成する。この例では、複数の個別のプロンプトを並列化する方法を示しています。

  • 映画のプロデューサーに映画のアイデアを提案する。この例では、異なる推論パラメータを使用して同じプロンプトを並列化する方法、チェーン内の前のステップにバックトラックする方法、ワークフローの一部として人間の入力を含める方法を示しています。

  • ユーザーの手元にある材料に基づいて料理を計画する。この例では、プロンプトチェーンが 2 つの異なる AI 会話を組み込んで、2 つの AI ペルソナが相互に議論を行い、最終的な結果を改善する方法を示しています。

  • 当日中で最も人気のある GitHub リポジトリを検索して要約する。この例では、外部 API とやり取りする複数の AI エージェントをチェーンさせる方法を示しています。

完全なソースコードと設定および実行の手順については、GitHub で完全なプロジェクトを参照してください。

この例で使用されているサービス
  • Amazon Bedrock

  • Amazon Bedrock ランタイム

  • Amazon Bedrock エージェント

  • Amazon Bedrock エージェントランタイム

  • Step Functions

次のコード例は、アプリケーション、生成 AI モデル、接続されたツールまたは API 間の一般的なインタラクションを構築し、AI と外部世界のインタラクションを仲介する方法を示しています。外部気象 API を AI モデルに接続する例を使用して、ユーザー入力に基づいてリアルタイムの気象情報を提供します。

SDK for Python (Boto3)
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

デモのプライマリ実行スクリプト。このスクリプトは、ユーザー、Amazon Bedrock Converse API、および気象ツール間の会話を調整します。

""" This demo illustrates a tool use scenario using Amazon Bedrock's Converse API and a weather tool. The script interacts with a foundation model on Amazon Bedrock to provide weather information based on user input. It uses the Open-Meteo API (https://open-meteo.com) to retrieve current weather data for a given location. """ import boto3 import logging from enum import Enum import utils.tool_use_print_utils as output import weather_tool logging.basicConfig(level=logging.INFO, format="%(message)s") AWS_REGION = "us-east-1" # For the most recent list of models supported by the Converse API's tool use functionality, visit: # https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html class SupportedModels(Enum): CLAUDE_OPUS = "anthropic.claude-3-opus-20240229-v1:0" CLAUDE_SONNET = "anthropic.claude-3-sonnet-20240229-v1:0" CLAUDE_HAIKU = "anthropic.claude-3-haiku-20240307-v1:0" COHERE_COMMAND_R = "cohere.command-r-v1:0" COHERE_COMMAND_R_PLUS = "cohere.command-r-plus-v1:0" # Set the model ID, e.g., Claude 3 Haiku. MODEL_ID = SupportedModels.CLAUDE_HAIKU.value SYSTEM_PROMPT = """ You are a weather assistant that provides current weather data for user-specified locations using only the Weather_Tool, which expects latitude and longitude. Infer the coordinates from the location yourself. If the user provides coordinates, infer the approximate location and refer to it in your response. To use the tool, you strictly apply the provided tool specification. - Explain your step-by-step process, and give brief updates before each step. - Only use the Weather_Tool for data. Never guess or make up information. - Repeat the tool use for subsequent requests if necessary. - If the tool errors, apologize, explain weather is unavailable, and suggest other options. - Report temperatures in °C (°F) and wind in km/h (mph). Keep weather reports concise. Sparingly use emojis where appropriate. - Only respond to weather queries. Remind off-topic users of your purpose. - Never claim to search online, access external data, or use tools besides Weather_Tool. - Complete the entire process until you have all required data before sending the complete response. """ # The maximum number of recursive calls allowed in the tool_use_demo function. # This helps prevent infinite loops and potential performance issues. MAX_RECURSIONS = 5 class ToolUseDemo: """ Demonstrates the tool use feature with the Amazon Bedrock Converse API. """ def __init__(self): # Prepare the system prompt self.system_prompt = [{"text": SYSTEM_PROMPT}] # Prepare the tool configuration with the weather tool's specification self.tool_config = {"tools": [weather_tool.get_tool_spec()]} # Create a Bedrock Runtime client in the specified AWS Region. self.bedrockRuntimeClient = boto3.client( "bedrock-runtime", region_name=AWS_REGION ) def run(self): """ Starts the conversation with the user and handles the interaction with Bedrock. """ # Print the greeting and a short user guide output.header() # Start with an emtpy conversation conversation = [] # Get the first user input user_input = self._get_user_input() while user_input is not None: # Create a new message with the user input and append it to the conversation message = {"role": "user", "content": [{"text": user_input}]} conversation.append(message) # Send the conversation to Amazon Bedrock bedrock_response = self._send_conversation_to_bedrock(conversation) # Recursively handle the model's response until the model has returned # its final response or the recursion counter has reached 0 self._process_model_response( bedrock_response, conversation, max_recursion=MAX_RECURSIONS ) # Repeat the loop until the user decides to exit the application user_input = self._get_user_input() output.footer() def _send_conversation_to_bedrock(self, conversation): """ Sends the conversation, the system prompt, and the tool spec to Amazon Bedrock, and returns the response. :param conversation: The conversation history including the next message to send. :return: The response from Amazon Bedrock. """ output.call_to_bedrock(conversation) # Send the conversation, system prompt, and tool configuration, and return the response return self.bedrockRuntimeClient.converse( modelId=MODEL_ID, messages=conversation, system=self.system_prompt, toolConfig=self.tool_config, ) def _process_model_response( self, model_response, conversation, max_recursion=MAX_RECURSIONS ): """ Processes the response received via Amazon Bedrock and performs the necessary actions based on the stop reason. :param model_response: The model's response returned via Amazon Bedrock. :param conversation: The conversation history. :param max_recursion: The maximum number of recursive calls allowed. """ if max_recursion <= 0: # Stop the process, the number of recursive calls could indicate an infinite loop logging.warning( "Warning: Maximum number of recursions reached. Please try again." ) exit(1) # Append the model's response to the ongoing conversation message = model_response["output"]["message"] conversation.append(message) if model_response["stopReason"] == "tool_use": # If the stop reason is "tool_use", forward everything to the tool use handler self._handle_tool_use(message, conversation, max_recursion) if model_response["stopReason"] == "end_turn": # If the stop reason is "end_turn", print the model's response text, and finish the process output.model_response(message["content"][0]["text"]) return def _handle_tool_use( self, model_response, conversation, max_recursion=MAX_RECURSIONS ): """ Handles the tool use case by invoking the specified tool and sending the tool's response back to Bedrock. The tool response is appended to the conversation, and the conversation is sent back to Amazon Bedrock for further processing. :param model_response: The model's response containing the tool use request. :param conversation: The conversation history. :param max_recursion: The maximum number of recursive calls allowed. """ # Initialize an empty list of tool results tool_results = [] # The model's response can consist of multiple content blocks for content_block in model_response["content"]: if "text" in content_block: # If the content block contains text, print it to the console output.model_response(content_block["text"]) if "toolUse" in content_block: # If the content block is a tool use request, forward it to the tool tool_response = self._invoke_tool(content_block["toolUse"]) # Add the tool use ID and the tool's response to the list of results tool_results.append( { "toolResult": { "toolUseId": (tool_response["toolUseId"]), "content": [{"json": tool_response["content"]}], } } ) # Embed the tool results in a new user message message = {"role": "user", "content": tool_results} # Append the new message to the ongoing conversation conversation.append(message) # Send the conversation to Amazon Bedrock response = self._send_conversation_to_bedrock(conversation) # Recursively handle the model's response until the model has returned # its final response or the recursion counter has reached 0 self._process_model_response(response, conversation, max_recursion - 1) def _invoke_tool(self, payload): """ Invokes the specified tool with the given payload and returns the tool's response. If the requested tool does not exist, an error message is returned. :param payload: The payload containing the tool name and input data. :return: The tool's response or an error message. """ tool_name = payload["name"] if tool_name == "Weather_Tool": input_data = payload["input"] output.tool_use(tool_name, input_data) # Invoke the weather tool with the input data provided by response = weather_tool.fetch_weather_data(input_data) else: error_message = ( f"The requested tool with name '{tool_name}' does not exist." ) response = {"error": "true", "message": error_message} return {"toolUseId": payload["toolUseId"], "content": response} @staticmethod def _get_user_input(prompt="Your weather info request"): """ Prompts the user for input and returns the user's response. Returns None if the user enters 'x' to exit. :param prompt: The prompt to display to the user. :return: The user's input or None if the user chooses to exit. """ output.separator() user_input = input(f"{prompt} (x to exit): ") if user_input == "": prompt = "Please enter your weather info request, e.g. the name of a city" return ToolUseDemo._get_user_input(prompt) elif user_input.lower() == "x": return None else: return user_input if __name__ == "__main__": tool_use_demo = ToolUseDemo() tool_use_demo.run()

デモで使用される気象ツール。このスクリプトは、ツールの仕様を定義し、Open-Meteo API を使用して気象データを取得するロジックを実装します。

import requests from requests.exceptions import RequestException def get_tool_spec(): """ Returns the JSON Schema specification for the Weather tool. The tool specification defines the input schema and describes the tool's functionality. For more information, see https://json-schema.org/understanding-json-schema/reference. :return: The tool specification for the Weather tool. """ return { "toolSpec": { "name": "Weather_Tool", "description": "Get the current weather for a given location, based on its WGS84 coordinates.", "inputSchema": { "json": { "type": "object", "properties": { "latitude": { "type": "string", "description": "Geographical WGS84 latitude of the location.", }, "longitude": { "type": "string", "description": "Geographical WGS84 longitude of the location.", }, }, "required": ["latitude", "longitude"], } }, } } def fetch_weather_data(input_data): """ Fetches weather data for the given latitude and longitude using the Open-Meteo API. Returns the weather data or an error message if the request fails. :param input_data: The input data containing the latitude and longitude. :return: The weather data or an error message. """ endpoint = "https://api.open-meteo.com/v1/forecast" latitude = input_data.get("latitude") longitude = input_data.get("longitude", "") params = {"latitude": latitude, "longitude": longitude, "current_weather": True} try: response = requests.get(endpoint, params=params) weather_data = {"weather_data": response.json()} response.raise_for_status() return weather_data except RequestException as e: return e.response.json() except Exception as e: return {"error": type(e), "message": str(e)}
  • API の詳細については、「AWS SDK for Python (Boto3) API リファレンス」の「Converse」を参照してください。

AI21 Labs Jurassic-2

次のコード例は、Bedrock の Converse API を使用して AI21 Labs Jurassic-2 にテキストメッセージを送信する方法を示しています。

SDK for Python (Boto3)
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Bedrock の Converse API を使用して AI21 Labs Jurassic-2 にテキストメッセージを送信します。

# Use the Conversation API to send a text message to AI21 Labs Jurassic-2. import boto3 from botocore.exceptions import ClientError # Create a Bedrock Runtime client in the AWS Region you want to use. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Jurassic-2 Mid. model_id = "ai21.j2-mid-v1" # Start a conversation with the user message. user_message = "Describe the purpose of a 'hello world' program in one line." conversation = [ { "role": "user", "content": [{"text": user_message}], } ] try: # Send the message to the model, using a basic inference configuration. response = client.converse( modelId=model_id, messages=conversation, inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9}, ) # Extract and print the response text. response_text = response["output"]["message"]["content"][0]["text"] print(response_text) except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") exit(1)
  • API の詳細については、「AWS SDK for Python (Boto3) API リファレンス」の「Converse」を参照してください。

次のコード例は、Invoke Model API を使用して AI21 Labs Jurassic-2 にテキストメッセージを送信する方法を示しています。

SDK for Python (Boto3)
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Invoke Model API を使用してテキストメッセージを送信します。

# Use the native inference API to send a text message to AI21 Labs Jurassic-2. 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., Jurassic-2 Mid. model_id = "ai21.j2-mid-v1" # 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 = { "prompt": prompt, "maxTokens": 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["completions"][0]["data"]["text"] print(response_text)
  • API の詳細については、AWS SDK for Python (Boto3) API リファレンスの「InvokeModel」を参照してください。

Amazon Nova

次のコード例は、Bedrock の Converse API を使用して Amazon Nova にテキストメッセージを送信する方法を示しています。

SDK for Python (Boto3)
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Bedrock の Converse API を使用して Amazon Nova にテキストメッセージを送信します。

# Use the Conversation API to send a text message to Amazon Nova. import boto3 from botocore.exceptions import ClientError # Create a Bedrock Runtime client in the AWS Region you want to use. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Amazon Nova Lite. model_id = "amazon.nova-lite-v1:0" # Start a conversation with the user message. user_message = "Describe the purpose of a 'hello world' program in one line." conversation = [ { "role": "user", "content": [{"text": user_message}], } ] try: # Send the message to the model, using a basic inference configuration. response = client.converse( modelId=model_id, messages=conversation, inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9}, ) # Extract and print the response text. response_text = response["output"]["message"]["content"][0]["text"] print(response_text) except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") exit(1)
  • API の詳細については、「AWS SDK for Python (Boto3) API リファレンス」の「Converse」を参照してください。

次のコード例は、Bedrock の Converse API を使用して Amazon Nova にテキストメッセージを送信し、レスポンスストリームをリアルタイムで処理する方法を示しています。

SDK for Python (Boto3)
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Bedrock の Converse API を使用して Amazon Nova にテキストメッセージを送信し、レスポンスストリームをリアルタイムで処理します。

# Use the Conversation API to send a text message to Amazon Nova Text # and print the response stream. import boto3 from botocore.exceptions import ClientError # Create a Bedrock Runtime client in the AWS Region you want to use. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Amazon Nova Lite. model_id = "amazon.nova-lite-v1:0" # Start a conversation with the user message. user_message = "Describe the purpose of a 'hello world' program in one line." conversation = [ { "role": "user", "content": [{"text": user_message}], } ] try: # Send the message to the model, using a basic inference configuration. streaming_response = client.converse_stream( modelId=model_id, messages=conversation, inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9}, ) # Extract and print the streamed response text in real-time. for chunk in streaming_response["stream"]: if "contentBlockDelta" in chunk: text = chunk["contentBlockDelta"]["delta"]["text"] print(text, end="") except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") exit(1)
  • API の詳細については、「AWS SDK for Python (Boto3) API リファレンス」の「ConverseStream」を参照してください。

Amazon Nova Canvas

次のコード例は、Amazon Bedrock で Amazon Nova Canvas を呼び出してイメージを生成する方法を示しています。

SDK for Python (Boto3)
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と