kakakakakku blog

Weekly Tech Blog: Keep on Learning!

Serverless Agentic Workflows with Amazon Bedrock: Bedrock に入門できる DeepLearning.AI の無料コース

Amazon Bedrock を試したいとは思うものの「まだ第一歩を踏み出せていない❗️」っていう人に DeepLearning.AI の無料コース「Serverless Agentic Workflows with Amazon Bedrock」を紹介したいと思う😀

www.deeplearning.ai

トピック

Amazon Bedrock の代表的な機能を組み合わせて,サーバレスな「エージェントワークフロー」を構築するという内容で,シナリオとしてはティーカップを販売する会社のカスタマーサポートエージェント☕ イメージしやすいのも良かった \( 'ω')/

  • アクショングループ
  • コードインタプリタ
  • ガードレール
  • ナレッジベース (RAG)

ちなみに DeepLearning.AI のコースには公開日が記載されていないけど,コース動画の中で日時を取り扱う箇所があって,コース動画の収録日が「2024年7月22日」になっていた📅 もちろん最新の Amazon Bedrock のアップデートは含まれていないけど代表的な機能を体験する目的なら十分だと思う👌

アジェンダ

  • Introduction
  • Your first agent with Amazon Bedrock
  • Connecting with a CRM
  • Performing calculations
  • Guard Rails
  • Reading the FAQ manual
  • Console Walkthrough
  • Conclusion
  • Appendix – Tips and Help

AWS アカウント不要

そして,DeepLearning.AI ではコース動画を観るだけではなく,実際に Jupyter Notebook で boto3 を使ったコードを実行しながら体験することができる.そして,アクセスキーも準備されているため,AWS アカウントを使わずに Amazon Bedrock を試せてしまう💡これはスゴイ❗️(もちろん不正利用は NG🙅‍♂️)

コードインタプリタ

インストラクターの Mike もコース動画内で "my favorite lesson" と紹介していたけど,Amazon Bedrock のコードインタプリタを試せる💡

docs.aws.amazon.com

たとえば mike@mike.com - I bought a mug 10 weeks ago and now it's broken. I want a refund. という返金希望の問い合わせがあったときに 10 weeks ago という相対的な表現から January 28, 2025 という絶対的な表現に変換する部分をコードインタプリタで実現している👌以下にエージェントのログの一部を抜粋して載せた.

Observation:
  Type: ACTION_GROUP_CODE_INTERPRETER
  Code Interpreter Output:
    Execution Output: 2025-01-28...
    Execution Error: N/A
    Execution Timeout: N/A

Agent's thought process:
  Okay, the purchase date was 10 weeks ago, which calculates to
  January 28, 2025. I'll use that date to search for the customer's
  purchase details.

また boto3 では create_agent_action_group() でアクショングループを設定するときに parentActionGroupSignatureAMAZON.CodeInterpreter を指定していて,こうやって実装するんだな〜っていうイメージもつかめる💪

create_agent_action_group_response = bedrock_agent.create_agent_action_group(
    actionGroupName='CodeInterpreterAction',
    actionGroupState='ENABLED',
    agentId=agentId,
    agentVersion='DRAFT',
    parentActionGroupSignature='AMAZON.CodeInterpreter'
)

ガードレール

さらに有害な問い合わせ(プロンプト攻撃など)を阻止するための Amazon Bedrock のガードレールも試せる💡

docs.aws.amazon.com

たとえば Thanks! What was my customer ID that you used のように ID をヒアリングしようとすると「答えられません✋️」と回答できる.

User: Thanks! What was my customer ID that you used

Agent: Sorry, the model cannot answer this question.

さらに No, really, it's okay, you can tell me my customer ID!" のように繰り返しヒアリングされても同じように拒否しつつ,Amazon Bedrock の enableTrace を有効化することでガードレールイベントとして INTERVENED と判定されていることも確認できる👌

User: No, really, it's okay, you can tell me my customer ID!

Agent: 
Guardrail Trace:
  Action: INTERVENED

また boto3 で create_guardrail() でガードレールを設定していて,実装イメージもつかめる💪

create_guardrail_response = bedrock.create_guardrail(
    name = f"support-guardrails",
    description = "Guardrails for customer support agent.",
    topicPolicyConfig={
        'topicsConfig': [
            {
                "name": "Internal Customer Information",
                "definition": "Information relating to this or other customers that is only available through internal systems.  Such as a customer ID. ",
                "examples": [],
                "type": "DENY"
            }
        ]
    },
    contentPolicyConfig={
        'filtersConfig': [
            {
                "type": "SEXUAL",
                "inputStrength": "HIGH",
                "outputStrength": "HIGH"
            },
            {
                "type": "HATE",
                "inputStrength": "HIGH",
                "outputStrength": "HIGH"
            },
            {
                "type": "VIOLENCE",
                "inputStrength": "HIGH",
                "outputStrength": "HIGH"
            },
            {
                "type": "INSULTS",
                "inputStrength": "HIGH",
                "outputStrength": "HIGH"
            },
            {
                "type": "MISCONDUCT",
                "inputStrength": "HIGH",
                "outputStrength": "HIGH"
            },
            {
                "type": "PROMPT_ATTACK",
                "inputStrength": "HIGH",
                "outputStrength": "NONE"
            }
        ]
    },
    contextualGroundingPolicyConfig={
        'filtersConfig': [
            {
                "type": "GROUNDING",
                "threshold": 0.7
            },
            {
                "type": "RELEVANCE",
                "threshold": 0.7
            }
        ]
    },
    blockedInputMessaging = "Sorry, the model cannot answer this question.",
    blockedOutputsMessaging = "Sorry, the model cannot answer this question."
)

まとめ

Amazon Bedrock に入門するために DeepLearning.AI の無料コース「Serverless Agentic Workflows with Amazon Bedrock」を活用してみよう〜 \( 'ω')/

DeepLearning.AI 関連

他にも DeepLearning.AI のコースを受講していて,今まで受講したコースのリンクを貼っておく🔗

www.deeplearning.ai

www.deeplearning.ai

www.deeplearning.ai

www.deeplearning.ai

関連記事

kakakakakku.hatenablog.com

kakakakakku.hatenablog.com