AWS Step Functions で Amazon Bedrock の API 統合タスクが追加され、 Generative-AI アプリケーションを簡単に実装できるようになりました #AWSreInvent

2023.11.27

こんにちは、CX 事業本部 Delivery 部の若槻です。

本日、AWS Step FunctionsAmazon Bedrock との API 統合タスクが利用可能になりました。

ドキュメントはこちらになります。

このアップデートにより、AWS Step Functions で Amazon Bedrock を使用した Generative-AI アプリケーションを簡単に実装できるようになりました。

試してみた

バージニア北部リージョンで試してみます。

モデルへのアクセスの有効化

Amazon Bedrock のモデルへのアクセスの有効化がまだの場合は、使用するリージョンで有効化しておきます。

ステートマシンの作成

Step Functions のステートマシン作成メニューで、Perform AI prompt-chaining with Amazon Bedrock というテンプレートが追加されています。せっかくなのでこちらを試してみます。

プロンプトに応じてモデルを 3 回実行するステートマシンが作成できるようです。テンプレートの作り方は[Run a demo]を選択して、必要なリソースが CloudFormation により作成されるようにします。

テンプレートエディターです。

作成されるワークフローのデザインを確認したら、[Deploy and run]で必要なリソースのデプロイを開始します。

ステートマシンが作成できました。

CloudFormation スタックもデプロイされました。ステートマシン本体と、モデルへのアクセスを許可するための IAM ロールが作成されています。

ステートマシンの定義は以下のようになっています。

{
  "Comment": "An example of using Bedrock to chain prompts and their responses together.",
  "StartAt": "Invoke model with first prompt",
  "States": {
    "Invoke model with first prompt": {
      "Type": "Task",
      "Resource": "arn:aws:states:::bedrock:invokeModel",
      "Parameters": {
        "ModelId": "cohere.command-text-v14",
        "Body": {
          "prompt.$": "$.prompt_one",
          "max_tokens": 20
        },
        "ContentType": "application/json",
        "Accept": "*/*"
      },
      "Next": "Add first result to conversation history",
      "ResultPath": "$.result_one",
      "ResultSelector": {
        "result_one.$": "$.Body.generations[0].text"
      }
    },
    "Add first result to conversation history": {
      "Type": "Pass",
      "Next": "Invoke model with second prompt",
      "Parameters": {
        "convo_one.$": "States.Format('{}\n{}', $.prompt_one, $.result_one.result_one)"
      },
      "ResultPath": "$.convo_one"
    },
    "Invoke model with second prompt": {
      "Type": "Task",
      "Resource": "arn:aws:states:::bedrock:invokeModel",
      "Parameters": {
        "ModelId": "cohere.command-text-v14",
        "Body": {
          "prompt.$": "States.Format('{}\n{}', $.convo_one.convo_one, $.prompt_two)",
          "max_tokens": 200
        },
        "ContentType": "application/json",
        "Accept": "*/*"
      },
      "Next": "Add second result to conversation history",
      "ResultSelector": {
        "result_two.$": "$.Body.generations[0].text"
      },
      "ResultPath": "$.result_two"
    },
    "Add second result to conversation history": {
      "Type": "Pass",
      "Next": "Invoke model with third prompt",
      "Parameters": {
        "convo_two.$": "States.Format('{}\n{}\n{}', $.convo_one.convo_one, $.prompt_two, $.result_two.result_two)"
      },
      "ResultPath": "$.convo_two"
    },
    "Invoke model with third prompt": {
      "Type": "Task",
      "Resource": "arn:aws:states:::bedrock:invokeModel",
      "Parameters": {
        "ModelId": "cohere.command-text-v14",
        "Body": {
          "prompt.$": "States.Format('{}\n{}', $.convo_two.convo_two, $.prompt_three)",
          "max_tokens": 200
        },
        "ContentType": "application/json",
        "Accept": "*/*"
      },
      "End": true,
      "ResultSelector": {
        "result_three.$": "$.Body.generations[0].text"
      }
    }
  }
}

arn:aws:states:::bedrock:invokeModel という新しく追加された API 統合タスクを使用して、Amazon Bedrock のモデルを呼び出しています。

また使われるモデルはすべて cohere.command-text-v14 で、処理の流れとしては以下のようになっています。

  1. 最初のプロンプト ($.prompt_one) を最大トークン数 20 で処理します。
  2. 結果を $.result_one に会話履歴として格納します。
  3. 2 つ目のプロンプト($.prompt_two)を$.result_one に追加して、先ほど作成された会話履歴 ($.convo_one.convo_one) を組み合わせたプロンプトを最大トークン数 200 で処理します。
  4. 会話履歴、2 番目のプロンプト、2 番目の結果を組み合わせ、 $.result_two に会話履歴として格納します。
  5. 3 つ目のプロンプト($.prompt_three)を$.result_two に追加して、先ほど作成された会話履歴 ($.convo_two.convo_two) を組み合わせたプロンプトを最大トークン数 200 で処理します。

動作確認

次のプロンプトを指定してステートマシンを実行してみます。

{
  "prompt_one": "AWS Step Functions について日本語で教えてください。",
  "prompt_two": "AWS Step Functions はどんなサービスですか?",
  "prompt_three": "ユースケースについても知りたいです!"
}

実行が成功しました。

それぞれのタスクでの実行の入出力は次のようになりました。

{
  "resourceType": "bedrock",
  "resource": "invokeModel",
  "output": {
    "Body": {
      "generations": [
        {
          "finish_reason": "MAX_TOKENS",
          "id": "f85efe12-b401-4c29-9231-e6f66ad1ee44",
          "text": " \nとにかく、 **English** でお願いします"
        }
      ],
      "id": "0efd539a-ca2c-4fac-826c-8363ac153bb6",
      "prompt": "AWS Step Functions について日本語で教えてください。"
    },
    "ContentType": "application/json"
  },
  "outputDetails": {
    "truncated": false
  }
}

\nとにかく、 English でお願いします

{
  "resourceType": "bedrock",
  "resource": "invokeModel",
  "output": {
    "Body": {
      "generations": [
        {
          "finish_reason": "MAX_TOKENS",
          "id": "e7580d19-cf4a-4a2f-9d2c-0c31f3867743",
          "text": " I will answer your question in English. \n\nAWS Step Functions is a service that helps you build and maintain cloud workflows using visual workflows and functions. It enables you to stitch together various AWS services into a series of steps, automating them into a visual workflow. For example, you can use Step Functions to automate your business processes, data processing, machine learning, and more. It is particularly useful for complex workflows that need to orchestrate across multiple services and require robust error handling and state management.\n\nWith Step Functions, you define workflows as state machines, where each step in the process is a state. This state machine approach makes it easier to manage complex processes, allowing you to focus on the individual steps rather than the entire process. You can also integrate Step Functions with other AWS services, such as Amazon S3, Amazon DynamoDB, and AWS Lambda, to extend the functionality of your workflows.\n\nThe service is designed to be scalable, reliable, and easy to use, with a"
        }
      ],
      "id": "70572ead-4e5d-49b0-834b-31518909a02f",
      "prompt": "AWS Step Functions について日本語で教えてください。\n \nとにかく、 **English** でお願いします\nAWS Step Functions はどんなサービスですか?"
    },
    "ContentType": "application/json"
  },
  "outputDetails": {
    "truncated": false
  }
}

I will answer your question in English. \n\nAWS Step Functions is a service that helps you build and maintain cloud workflows using visual workflows and functions. It enables you to stitch together various AWS services into a series of steps, automating them into a visual workflow. For example, you can use Step Functions to automate your business processes, data processing, machine learning, and more. It is particularly useful for complex workflows that need to orchestrate across multiple services and require robust error handling and state management.\n\nWith Step Functions, you define workflows as state machines, where each step in the process is a state. This state machine approach makes it easier to manage complex processes, allowing you to focus on the individual steps rather than the entire process. You can also integrate Step Functions with other AWS services, such as Amazon S3, Amazon DynamoDB, and AWS Lambda, to extend the functionality of your workflows.\n\nThe service is designed to be scalable, reliable, and easy to use, with a

{
  "resourceType": "bedrock",
  "resource": "invokeModel",
  "output": "{\"Body\":{\"generations\":[{\"finish_reason\":\"MAX_TOKENS\",\"id\":\"20b03a45-9134-48de-a130-96cfd4e4e831\",\"text\":\" はい、ユースケースについてもご説明しています。\\n\\nAWS Step Functions のユースケースは、 below:\\n\\n- プロセス管理、プログラミング、パスワード管理、パスワードの変更など、これらのBP を自動化されたワークフローに関する時もあります。 \\n\\nしかし、 Step Functions はワークフローのズームズームや進捗を監視、ワークフローの状態をよりよく管理するため\"}],\"id\":\"3828c70e-f318-4bb8-b222-82092e81a3dd\",\"prompt\":\"AWS Step Functions について日本語で教えてください。\\n \\nとにかく、 **English** でお願いします\\nAWS Step Functions はどんなサービスですか?\\n I will answer your question in English. \\n\\nAWS Step Functions is a service that helps you build and maintain cloud workflows using visual workflows and functions. It enables you to stitch together various AWS services into a series of steps, automating them into a visual workflow. For example, you can use Step Functions to automate your business processes, data processing, machine learning, and more. It is particularly useful for complex workflows that need to orchestrate across multiple services and require robust error handling and state management.\\n\\nWith Step Functions, you define workflows as state machines, where each step in the process is a state. This state machine approach makes it easier to manage complex processes, allowing you to focus on the individual steps rather than the entire process. You can also integrate Step Functions with other AWS services, such as Amazon S3, Amazon DynamoDB, and AWS Lambda, to extend the functionality of your workflows.\\n\\nThe service is designed to be scalable, reliable, and easy to use, with a\\nユースケースについても知りたいです!\"},\"ContentType\":\"application/json\"}",
  "outputDetails": {
    "truncated": false
  }
}

はい、ユースケースについてもご説明しています。\n\nAWS Step Functions のユースケースは、 below:\n\n- プロセス管理、プログラミング、パスワード管理、パスワードの変更など、これらのBP を自動化されたワークフローに関する時もあります。 \n\nしかし、 Step Functions はワークフローのズームズームや進捗を監視、ワークフローの状態をよりよく管理するため\"}],\"id\":\"3828c70e-f318-4bb8-b222-82092e81a3dd\",\"prompt\":\"AWS Step Functions について日本語で教えてください。\n \nとにかく、 English でお願いします\nAWS Step Functions はどんなサービスですか?\n I will answer your question in English. \n\nAWS Step Functions is a service that helps you build and maintain cloud workflows using visual workflows and functions. It enables you to stitch together various AWS services into a series of steps, automating them into a visual workflow. For example, you can use Step Functions to automate your business processes, data processing, machine learning, and more. It is particularly useful for complex workflows that need to orchestrate across multiple services and require robust error handling and state management.\n\nWith Step Functions, you define workflows as state machines, where each step in the process is a state. This state machine approach makes it easier to manage complex processes, allowing you to focus on the individual steps rather than the entire process. You can also integrate Step Functions with other AWS services, such as Amazon S3, Amazon DynamoDB, and AWS Lambda, to extend the functionality of your workflows.\n\nThe service is designed to be scalable, reliable, and easy to use, with a\nユースケースについても知りたいです!\はい、ユースケースについてもご説明しています。\n\nAWS Step Functions のユースケースは、 below:\n\n- プロセス管理、プログラミング、パスワード管理、パスワードの変更など、これらのBP を自動化されたワークフローに関する時もあります。 \n\nしかし、 Step Functions はワークフローのズームズームや進捗を監視、ワークフローの状態をよりよく管理するため\"}],\"id\":\"3828c70e-f318-4bb8-b222-82092e81a3dd\",\"prompt\":\"AWS Step Functions について日本語で教えてください。\n \nとにかく、 English でお願いします\nAWS Step Functions はどんなサービスですか?\n I will answer your question in English. \n\nAWS Step Functions is a service that helps you build and maintain cloud workflows using visual workflows and functions. It enables you to stitch together various AWS services into a series of steps, automating them into a visual workflow. For example, you can use Step Functions to automate your business processes, data processing, machine learning, and more. It is particularly useful for complex workflows that need to orchestrate across multiple services and require robust error handling and state management.\n\nWith Step Functions, you define workflows as state machines, where each step in the process is a state. This state machine approach makes it easier to manage complex processes, allowing you to focus on the individual steps rather than the entire process. You can also integrate Step Functions with other AWS services, such as Amazon S3, Amazon DynamoDB, and AWS Lambda, to extend the functionality of your workflows.\n\nThe service is designed to be scalable, reliable, and easy to use, with a\nユースケースについても知りたいです!\

日本語での返信を求めているのに対して敢えて英語で返信が行われているため、これはプロンプトの入力側の指定の工夫が試される部分となりそうです。一方でトークン数の上限に達して回答が途中で途切れているので、途切れた場合は Map ステートにより繰り返し処理を行うようにするなど実行側の工夫もできそうです。

上記に目をつぶれば回答はおおよそ正しく行われているように見えます。

おわりに

AWS Step Functions で Amazon Bedrock の API 統合タスクが追加され、 Generative-AI アプリケーションを簡単に実装できるようになったのでご紹介しました。

今回のアップデートにより、Amazon Bedrock のモデルを利用する上での API の呼び出しが Step Functions から可能になったため、より少ないコードの実装のみでチャット形式の AI アプリケーションを実装できるようになりました。これにより Amazon Bedrock の活用がより進むようになりそうですね。

以上