メインコンテンツまでスキップ

Step FunctionsがBedrockに対応したのでやってみた

· 約10分
moritalous
お知らせ

過去にQiitaに投稿した内容のアーカイブです。

re:Inventの直前にStep FunctionsがBedrockに対応したとのアナウンスがありました。

https://aws.amazon.com/jp/about-aws/whats-new/2023/11/aws-step-functions-optimized-integration-bedrock/

個人的にとても期待していたアップデートなので、re:Inventの帰りの飛行機を待ちながら検証しました。

テンプレートからデプロイしてみる

  1. マネジメントコンソールにアクセスし今すぐ始めるをクリックします。 image.png

  2. 真ん中のテンプレートを選択をクリックします。 image.png

  3. 1つ目にBedrockでプロンプトチェイニングを実行するを選択して次へをクリックします。 image.png

  4. テンプレートの内容が表示されます。テンプレートの使用をクリックします。

    image.png

  5. CloudFormationでデプロイするようです。右上のデプロイと実行をクリックします。 image.png

これでStep Functionsのステートマシンが作成されました。

使ってみる

ステートマシンの作成後に、実行前の状態になりますので、このまま実行します。

image.png

インプットのJSONを整形するとこのような形です。3回Bedrockを呼び出すのでそれぞれのプロンプトを指定します。

{
"prompt_one": "Name a random city. You must provide a non-fictitious city. You must only provide the city's name, followed by a comma and the city's country of origin.",
"prompt_two": "Write two to three sentences describing the city mentioned above",
"prompt_three": "Write three to four sentences describing the city mentioned above"
}
1つ目のプロンプト(日本語訳)
任意の都市名を挙げてください。架空の都市ではありません。都市名の後にカンマ、そしてその都市の出身国のみを記入すること。
2つ目のプロンプト(日本語訳)
上記の都市について、2~3つの文章で説明してください。
3つ目のプロンプト(日本語訳)
上記の都市について、3~4つの文章で説明してください。

実際に呼び出してみましょう。

  • Invoke model with first promptステップ

    1回目のBedrock呼び出しです。

    入力
    {
    "prompt_one": "Name a random city. You must provide a non-fictitious city. You must only provide the city's name, followed by a comma and the city's country of origin.",
    "prompt_two": "Write two to three sentences describing the city mentioned above",
    "prompt_three": "Write three to four sentences describing the city mentioned above"
    }
    出力
    {
    "prompt_one": "Name a random city. You must provide a non-fictitious city. You must only provide the city's name, followed by a comma and the city's country of origin.",
    "prompt_two": "Write two to three sentences describing the city mentioned above",
    "prompt_three": "Write three to four sentences describing the city mentioned above",
    "result_one": {
    "result_one": " Well, the following are some random cities from around the world:\n\n1. Kyoto, Japan"
    }
    }

    Bedrockで生成した文書がresult_oneに出力されました。

  • Add first result to conversation historyステップ

    チャット履歴を保持します。

    入力
    {
    "prompt_one": "Name a random city. You must provide a non-fictitious city. You must only provide the city's name, followed by a comma and the city's country of origin.",
    "prompt_two": "Write two to three sentences describing the city mentioned above",
    "prompt_three": "Write three to four sentences describing the city mentioned above",
    "result_one": {
    "result_one": " Well, the following are some random cities from around the world:\n\n1. Kyoto, Japan"
    }
    }
    出力
    {
    "prompt_one": "Name a random city. You must provide a non-fictitious city. You must only provide the city's name, followed by a comma and the city's country of origin.",
    "prompt_two": "Write two to three sentences describing the city mentioned above",
    "prompt_three": "Write three to four sentences describing the city mentioned above",
    "result_one": {
    "result_one": " Well, the following are some random cities from around the world:\n\n1. Kyoto, Japan"
    },
    "convo_one": {
    "convo_one": "Name a random city. You must provide a non-fictitious city. You must only provide the city's name, followed by a comma and the city's country of origin.\n Well, the following are some random cities from around the world:\n\n1. Kyoto, Japan"
    }
    }

    チャット履歴がconvo_oneに追加されています。内容は1度目のBedrock呼び出しの入力プロンプトと出力を連結して保持しています。

同様に2回目、3回目とBedrockを呼び出します。

3回目の呼び出し結果はこのようになります。

{
"result_three": " Kyoto is a city on the most populous island of Japan, Honshu. It is a place of cultural importance in Japan, having been the country's capital for more than one thousand years. Kyoto is home to hundreds of temples, gardens, and Shinto shrines, most of which are preserved thanks to the city's late 19th-century survival of American bombing campaigns during World War II. This city is a favorite destination for those seeking historical and cultural experiences, especially in the spring when the cherry blossoms bloom. The cherry blossom season, beginning in March, is the most popular time to visit Kyoto and attracts visitors worldwide. The city also has a vibrant cultural scene and offers traditional Japanese arts, performances, and festivals. In addition, Kyoto is known for its savory cuisine, including its fresh, local dishes and its vegetarian temple food. With so much to offer, it's no wonder this beautiful city is a popular tourist destination. \n\nWould you like to know more about"
}

京都は日本で最も人口の多い島、本州にある都市である。千年以上もの間、日本の首都であった京都は、日本文化にとって重要な場所である。京都には何百もの寺院、庭園、神社があり、そのほとんどは19世紀後半に第二次世界大戦中のアメリカの爆撃を免れたおかげで保存されている。 この街は、歴史的、文化的な体験を求める人々にとって、特に桜が咲く春の旅行先として人気がある。 3月から始まる桜の季節は、京都を訪れるのに最も人気のある時期で、世界中から観光客が訪れる。 また、京都には活気ある文化シーンがあり、日本の伝統芸能やパフォーマンス、お祭りを楽しむことができる。 さらに、京都は新鮮な郷土料理や精進料理の寺院料理など、味わい深い料理でも知られている。 これだけの魅力があれば、この美しい街が人気の観光地であるのもうなずける。についてもっと知りたいですか?

仕組みにDive Deep

一体どのようにBedrockを呼び出しているのでしょうか?

1つ目のBedrock呼び出しの設定タブはこの様になっていました。

image.png

設定タブでは主に以下の設定を行います。

  • API パラメータ

    通常かプロビジョンドかを選択します

  • Bedrock model identifier

    使用する基盤モデルのModel IDを指定します。直接入力だけでなく選択肢も出してくれます。実行時に前ステップの変数を指定することもできます。

    image.png

    ちなみに、画像生成のStable Diffusionも使えそうです。

  • Bedrock モデルパラメータ

    モデル呼び出し時のパラメーターを指定します。サンプルテンプレートでは以下のないようです。

    {
    "prompt.$": "$.prompt_one",
    "max_tokens": 20
    }

    また、プリセットから選ぶこともできます。

    image.png

    Claudeを選んだ場合はこのようにひな形がセットされるので、あとはプロンプト部分を修正するだけです。

    {
    "prompt": "\n\nHuman:<prompt>\n\nAssistant:",
    "max_tokens_to_sample": 200
    }

出力タブも確認しましょう。

image.png

  • ResultSelector を使用して結果を変換

    Bedrockのinvoke model APIのレスポンスから、生成した文言だけを抽出しています。Bedrockを使ったことがあれば違和感はないと思います。(PythonのBoto3の場合はStreamingBodyからreadしたりjsonに変換したり面倒なコードが必要でしたが、Step Functionsでは不要でいいですね!)

    {
    "result_one.$": "$.Body.generations[0].text"
    }
  • ResultPath を使用して元の入力を出力に追加

今回のデモは3つのプロんプロトを入力パラメーターで指定しているので、後続のステップに入力パラメーターを引き継ぐ必要があります。そのため、ここで設定し、Bedrockで生成した文書はresult_oneにセットしています。

まとめ

Bedrock用に最適化されてインテグレーションされていることが確認できました。生成系AIの呼び出しは複数ステップになることが多いので、Bedrockを使う際はStep Functionsの利用も検討してみてはいかがでしょうか?