Cookie の設定を選択する

当社は、当社のサイトおよびサービスを提供するために必要な必須 Cookie および類似のツールを使用しています。当社は、パフォーマンス Cookie を使用して匿名の統計情報を収集することで、お客様が当社のサイトをどのように利用しているかを把握し、改善に役立てています。必須 Cookie は無効化できませんが、[カスタマイズ] または [拒否] をクリックしてパフォーマンス Cookie を拒否することはできます。

お客様が同意した場合、AWS および承認された第三者は、Cookie を使用して便利なサイト機能を提供したり、お客様の選択を記憶したり、関連する広告を含む関連コンテンツを表示したりします。すべての必須ではない Cookie を受け入れるか拒否するには、[受け入れる] または [拒否] をクリックしてください。より詳細な選択を行うには、[カスタマイズ] をクリックしてください。

Amazon Bedrock Flows コードサンプルを実行する

フォーカスモード
Amazon Bedrock Flows コードサンプルを実行する - Amazon Bedrock

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

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

次のコードサンプルは、次の前提条件を満たしていることを前提としています。

  1. Amazon Bedrock アクションへのアクセス許可を持つロールを設定する。設定していない場合は、「Amazon Bedrock の開始方法」を参照します。

  2. AWS API を使用するように認証情報を設定します。設定していない場合は、「API の開始方法」を参照します。

  3. ユーザーに代わってフロー関連のアクションを実行するサービスロールを作成します。設定していない場合は、「Amazon Bedrock で Amazon Bedrock フローのサービスロールを作成する」を参照します。

フローを作成するには、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して CreateFlow リクエストを送信します。コード例については、「」を参照してください。 Amazon Bedrock Flows コードサンプルを実行する

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

フィールド 基本的な説明
名前 フローの名前。
executionRoleArn フローを作成して管理するためのアクセス許可を持つサービスロールの ARN

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

フィールド ユースケース
定義 フローを構成する nodesconnections が含まれます。
description フローについて記述する場合に指定します。
tags タグをエイリアスに関連付ける場合に指定します。詳細については、「Amazon Bedrock リソースにタグ付け」を参照してください。
customerEncryptionKeyArn KMS キーを使用してリソースを暗号化するには。詳細については、「Amazon Bedrock Flows リソースの暗号化」を参照してください。
clientToken API リクエストが 1 回だけ完了するようにします。詳細については、「べき等性の確保」を参照してください。

definition フィールドはオプションですが、フローが機能するために必要です。定義なしでフローを作成し、後でフローを更新することもできます。

nodes リスト内のノードごとに、type フィールドでノードのタイプを指定し、config フィールドでノードの対応する設定を指定します。さまざまなタイプのノードの API 構造の詳細については、「フローのノードタイプ」を参照してください。

Amazon Bedrock Flows のコードサンプルを試すには、任意の方法のタブを選択し、ステップに従います。

Python
  1. 次のノードを持つ Agents for Amazon Bedrock ビルドタイムエンドポイントCreateFlow リクエストを使用してフローを作成します。

    • 入力ノード。

    • 2 つの変数 (genrenumber) を使用して音楽プレイリストを作成するプロンプトがインラインで定義されているプロンプトノード。

    • モデルの完了を返す出力ノード。

    次のコードスニペットを実行して をロードし AWS SDK for Python (Boto3)、Amazon Bedrock エージェントクライアントを作成し、ノードを使用してフローを作成します ( executionRoleArnフィールドをフロー用に作成したサービスロールの ARN に置き換えます)。

    # Import Python SDK and create client import boto3 client = boto3.client(service_name='bedrock-agent') # Replace with the service role that you created. For more information, see https://docs.aws.amazon.com/bedrock/latest/userguide/flows-permissions.html FLOWS_SERVICE_ROLE = "arn:aws:iam::123456789012:role/MyFlowsRole" # Define each node # The input node validates that the content of the InvokeFlow request is a JSON object. input_node = { "type": "Input", "name": "FlowInput", "outputs": [ { "name": "document", "type": "Object" } ] } # This prompt node defines an inline prompt that creates a music playlist using two variables. # 1. {{genre}} - The genre of music to create a playlist for # 2. {{number}} - The number of songs to include in the playlist # It validates that the input is a JSON object that minimally contains the fields "genre" and "number", which it will map to the prompt variables. # The output must be named "modelCompletion" and be of the type "String". prompt_node = { "type": "Prompt", "name": "MakePlaylist", "configuration": { "prompt": { "sourceConfiguration": { "inline": { "modelId": "amazon.nova-lite-v1:0", "templateType": "TEXT", "inferenceConfiguration": { "text": { "temperature": 0.8 } }, "templateConfiguration": { "text": { "text": "Make me a {{genre}} playlist consisting of the following number of songs: {{number}}." } } } } } }, "inputs": [ { "name": "genre", "type": "String", "expression": "$.data.genre" }, { "name": "number", "type": "Number", "expression": "$.data.number" } ], "outputs": [ { "name": "modelCompletion", "type": "String" } ] } # The output node validates that the output from the last node is a string and returns it as is. The name must be "document". output_node = { "type": "Output", "name": "FlowOutput", "inputs": [ { "name": "document", "type": "String", "expression": "$.data" } ] } # Create connections between the nodes connections = [] # First, create connections between the output of the flow input node and each input of the prompt node for input in prompt_node["inputs"]: connections.append( { "name": "_".join([input_node["name"], prompt_node["name"], input["name"]]), "source": input_node["name"], "target": prompt_node["name"], "type": "Data", "configuration": { "data": { "sourceOutput": input_node["outputs"][0]["name"], "targetInput": input["name"] } } } ) # Then, create a connection between the output of the prompt node and the input of the flow output node connections.append( { "name": "_".join([prompt_node["name"], output_node["name"]]), "source": prompt_node["name"], "target": output_node["name"], "type": "Data", "configuration": { "data": { "sourceOutput": prompt_node["outputs"][0]["name"], "targetInput": output_node["inputs"][0]["name"] } } } ) # Create the flow from the nodes and connections response = client.create_flow( name="FlowCreatePlaylist", description="A flow that creates a playlist given a genre and number of songs to include in the playlist.", executionRoleArn=FLOWS_SERVICE_ROLE, definition={ "nodes": [input_node, prompt_node, output_node], "connections": connections } ) flow_id = response.get("id")
  2. Agents for Amazon Bedrock ビルドタイムエンドポイントListFlows リクエストを行うには、次のコードスニペットを実行して、作成したフローを含め、アカウントのフローを一覧表示します。

    client.list_flows()
  3. 次のコードスニペットを実行して作成したフローに関する情報を取得し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して GetFlow リクエストを行います。

    client.get_flow(flowIdentifier=flow_id)
  4. 作業中のドラフトからの最新の変更が適用され、バージョン作成の準備が整うようにフローを準備します。次のコードスニペットを実行して、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して PrepareFlow リクエストを行います。

    client.prepare_flow(flowIdentifier=flow_id)
  5. フローの作業中のドラフトをバージョン化し、フローの静的スナップショットを作成し、次のアクションでそのスナップショットに関する情報を取得します。

    1. 次のコードスニペットを実行してバージョンを作成し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して CreateFlowVersion リクエストを行います。

      response = client.create_flow_version(flowIdentifier=flow_id) flow_version = response.get("version")
    2. Agents for Amazon Bedrock ビルドタイムエンドポイントを使用して ListFlowVersions リクエストを行うには、次のコードスニペットを実行してフローのすべてのバージョンを一覧表示します。

      client.list_flow_versions(flowIdentifier=flow_id)
    3. Agents for Amazon Bedrock ビルドタイムエンドポイントを使用して GetFlowVersion リクエストを行うには、次のコードスニペットを実行してバージョンに関する情報を取得します。

      client.get_flow_version(flowIdentifier=flow_id, flowVersion=flow_version)
  6. 作成したフローのバージョンを指すエイリアスを作成し、次のアクションでそのエイリアスに関する情報を取得します。

    1. エイリアスを作成し、次のコードスニペットを実行して作成したバージョンを指定し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して CreateFlowAlias リクエストを行います。

      response = client.create_flow_alias( flowIdentifier=flow_id, name="latest", description="Alias pointing to the latest version of the flow.", routingConfiguration=[ { "flowVersion": flow_version } ] ) flow_alias_id = response.get("id")
    2. Agents for Amazon Bedrock ビルドタイムエンドポイントを使用して ListFlowAliass リクエストを行うには、次のコードスニペットを実行してフローのすべてのエイリアスを一覧表示します。

      client.list_flow_aliases(flowIdentifier=flow_id)
    3. 次のコードスニペットを実行して作成したエイリアスに関する情報を取得し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して GetFlowAlias リクエストを行います。

      client.get_flow_alias(flowIdentifier=flow_id, aliasIdentifier=flow_alias_id)
  7. 次のコードスニペットを実行して、Amazon Bedrock Agents Runtime クライアントを作成し、フローを呼び出します。リクエストはフローのプロンプトの変数を埋め、モデルからのレスポンスを返し、Amazon Bedrock エージェントのランタイムエンドポイントを使用して InvokeFlow リクエストを行います。

    client_runtime = boto3.client('bedrock-agent-runtime') response = client_runtime.invoke_flow( flowIdentifier=flow_id, flowAliasIdentifier=flow_alias_id, inputs=[ { "content": { "document": { "genre": "pop", "number": 3 } }, "nodeName": "FlowInput", "nodeOutputName": "document" } ] ) result = {} for event in response.get("responseStream"): result.update(event) if result['flowCompletionEvent']['completionReason'] == 'SUCCESS': print("Flow invocation was successful! The output of the flow is as follows:\n") print(result['flowOutputEvent']['content']['document']) else: print("The flow invocation completed because of the following reason:", result['flowCompletionEvent']['completionReason'])

    応答は、3 曲で構成されるポップミュージックのプレイリストを返す必要があります。

  8. 次のアクションで作成したエイリアス、バージョン、フローを削除します。

    1. 次のコードスニペットを実行してエイリアスを削除し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して DeleteFlowAlias リクエストを行います。

      client.delete_flow_alias(flowIdentifier=flow_id, aliasIdentifier=flow_alias_id)
    2. 次のコードスニペットを実行してバージョンを削除し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して DeleteFlowVersion リクエストを行います。

      client.delete_flow_version(flowIdentifier=flow_id, flowVersion=flow_version)
    3. 次のコードスニペットを実行してフローを削除し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して DeleteFlow リクエストを行います。

      client.delete_flow(flowIdentifier=flow_id)
  1. 次のノードを持つ Agents for Amazon Bedrock ビルドタイムエンドポイントCreateFlow リクエストを使用してフローを作成します。

    • 入力ノード。

    • 2 つの変数 (genrenumber) を使用して音楽プレイリストを作成するプロンプトがインラインで定義されているプロンプトノード。

    • モデルの完了を返す出力ノード。

    次のコードスニペットを実行して をロードし AWS SDK for Python (Boto3)、Amazon Bedrock エージェントクライアントを作成し、ノードを使用してフローを作成します ( executionRoleArnフィールドをフロー用に作成したサービスロールの ARN に置き換えます)。

    # Import Python SDK and create client import boto3 client = boto3.client(service_name='bedrock-agent') # Replace with the service role that you created. For more information, see https://docs.aws.amazon.com/bedrock/latest/userguide/flows-permissions.html FLOWS_SERVICE_ROLE = "arn:aws:iam::123456789012:role/MyFlowsRole" # Define each node # The input node validates that the content of the InvokeFlow request is a JSON object. input_node = { "type": "Input", "name": "FlowInput", "outputs": [ { "name": "document", "type": "Object" } ] } # This prompt node defines an inline prompt that creates a music playlist using two variables. # 1. {{genre}} - The genre of music to create a playlist for # 2. {{number}} - The number of songs to include in the playlist # It validates that the input is a JSON object that minimally contains the fields "genre" and "number", which it will map to the prompt variables. # The output must be named "modelCompletion" and be of the type "String". prompt_node = { "type": "Prompt", "name": "MakePlaylist", "configuration": { "prompt": { "sourceConfiguration": { "inline": { "modelId": "amazon.nova-lite-v1:0", "templateType": "TEXT", "inferenceConfiguration": { "text": { "temperature": 0.8 } }, "templateConfiguration": { "text": { "text": "Make me a {{genre}} playlist consisting of the following number of songs: {{number}}." } } } } } }, "inputs": [ { "name": "genre", "type": "String", "expression": "$.data.genre" }, { "name": "number", "type": "Number", "expression": "$.data.number" } ], "outputs": [ { "name": "modelCompletion", "type": "String" } ] } # The output node validates that the output from the last node is a string and returns it as is. The name must be "document". output_node = { "type": "Output", "name": "FlowOutput", "inputs": [ { "name": "document", "type": "String", "expression": "$.data" } ] } # Create connections between the nodes connections = [] # First, create connections between the output of the flow input node and each input of the prompt node for input in prompt_node["inputs"]: connections.append( { "name": "_".join([input_node["name"], prompt_node["name"], input["name"]]), "source": input_node["name"], "target": prompt_node["name"], "type": "Data", "configuration": { "data": { "sourceOutput": input_node["outputs"][0]["name"], "targetInput": input["name"] } } } ) # Then, create a connection between the output of the prompt node and the input of the flow output node connections.append( { "name": "_".join([prompt_node["name"], output_node["name"]]), "source": prompt_node["name"], "target": output_node["name"], "type": "Data", "configuration": { "data": { "sourceOutput": prompt_node["outputs"][0]["name"], "targetInput": output_node["inputs"][0]["name"] } } } ) # Create the flow from the nodes and connections response = client.create_flow( name="FlowCreatePlaylist", description="A flow that creates a playlist given a genre and number of songs to include in the playlist.", executionRoleArn=FLOWS_SERVICE_ROLE, definition={ "nodes": [input_node, prompt_node, output_node], "connections": connections } ) flow_id = response.get("id")
  2. Agents for Amazon Bedrock ビルドタイムエンドポイントListFlows リクエストを行うには、次のコードスニペットを実行して、作成したフローを含め、アカウントのフローを一覧表示します。

    client.list_flows()
  3. 次のコードスニペットを実行して作成したフローに関する情報を取得し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して GetFlow リクエストを行います。

    client.get_flow(flowIdentifier=flow_id)
  4. 作業中のドラフトからの最新の変更が適用され、バージョン作成の準備が整うようにフローを準備します。次のコードスニペットを実行して、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して PrepareFlow リクエストを行います。

    client.prepare_flow(flowIdentifier=flow_id)
  5. フローの作業中のドラフトをバージョン化し、フローの静的スナップショットを作成し、次のアクションでそのスナップショットに関する情報を取得します。

    1. 次のコードスニペットを実行してバージョンを作成し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して CreateFlowVersion リクエストを行います。

      response = client.create_flow_version(flowIdentifier=flow_id) flow_version = response.get("version")
    2. Agents for Amazon Bedrock ビルドタイムエンドポイントを使用して ListFlowVersions リクエストを行うには、次のコードスニペットを実行してフローのすべてのバージョンを一覧表示します。

      client.list_flow_versions(flowIdentifier=flow_id)
    3. Agents for Amazon Bedrock ビルドタイムエンドポイントを使用して GetFlowVersion リクエストを行うには、次のコードスニペットを実行してバージョンに関する情報を取得します。

      client.get_flow_version(flowIdentifier=flow_id, flowVersion=flow_version)
  6. 作成したフローのバージョンを指すエイリアスを作成し、次のアクションでそのエイリアスに関する情報を取得します。

    1. エイリアスを作成し、次のコードスニペットを実行して作成したバージョンを指定し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して CreateFlowAlias リクエストを行います。

      response = client.create_flow_alias( flowIdentifier=flow_id, name="latest", description="Alias pointing to the latest version of the flow.", routingConfiguration=[ { "flowVersion": flow_version } ] ) flow_alias_id = response.get("id")
    2. Agents for Amazon Bedrock ビルドタイムエンドポイントを使用して ListFlowAliass リクエストを行うには、次のコードスニペットを実行してフローのすべてのエイリアスを一覧表示します。

      client.list_flow_aliases(flowIdentifier=flow_id)
    3. 次のコードスニペットを実行して作成したエイリアスに関する情報を取得し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して GetFlowAlias リクエストを行います。

      client.get_flow_alias(flowIdentifier=flow_id, aliasIdentifier=flow_alias_id)
  7. 次のコードスニペットを実行して、Amazon Bedrock Agents Runtime クライアントを作成し、フローを呼び出します。リクエストはフローのプロンプトの変数を埋め、モデルからのレスポンスを返し、Amazon Bedrock エージェントのランタイムエンドポイントを使用して InvokeFlow リクエストを行います。

    client_runtime = boto3.client('bedrock-agent-runtime') response = client_runtime.invoke_flow( flowIdentifier=flow_id, flowAliasIdentifier=flow_alias_id, inputs=[ { "content": { "document": { "genre": "pop", "number": 3 } }, "nodeName": "FlowInput", "nodeOutputName": "document" } ] ) result = {} for event in response.get("responseStream"): result.update(event) if result['flowCompletionEvent']['completionReason'] == 'SUCCESS': print("Flow invocation was successful! The output of the flow is as follows:\n") print(result['flowOutputEvent']['content']['document']) else: print("The flow invocation completed because of the following reason:", result['flowCompletionEvent']['completionReason'])

    応答は、3 曲で構成されるポップミュージックのプレイリストを返す必要があります。

  8. 次のアクションで作成したエイリアス、バージョン、フローを削除します。

    1. 次のコードスニペットを実行してエイリアスを削除し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して DeleteFlowAlias リクエストを行います。

      client.delete_flow_alias(flowIdentifier=flow_id, aliasIdentifier=flow_alias_id)
    2. 次のコードスニペットを実行してバージョンを削除し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して DeleteFlowVersion リクエストを行います。

      client.delete_flow_version(flowIdentifier=flow_id, flowVersion=flow_version)
    3. 次のコードスニペットを実行してフローを削除し、Amazon Bedrock エージェントのビルドタイムエンドポイントを使用して DeleteFlow リクエストを行います。

      client.delete_flow(flowIdentifier=flow_id)
プライバシーサイト規約Cookie の設定
© 2025, Amazon Web Services, Inc. or its affiliates.All rights reserved.