Amazon Connectフロー上でAWS Lambdaを呼び出し、Q in Connectの別のAIエージェントに切り替える方法

Amazon Connectフロー上でAWS Lambdaを呼び出し、Q in Connectの別のAIエージェントに切り替える方法

Clock Icon2025.04.10

はじめに

Amazon Q in Connect を利用する場合、通常はConnectフロー上でデフォルトに設定したAIエージェントが使用されます。

cm-hirai-screenshot 2025-04-10 8.32.40

しかし、状況に応じて異なるAIエージェントを使い分けたいケースもあります。

本記事では、Amazon Connectフロー内でAWS Lambdaを呼び出し、Q in Connectの別のAIエージェントに切り替える方法をご紹介します。

具体的には、セルフサービスで利用するAIエージェントをデフォルトから別のものに変更する実装方法について解説します。

これにより、コンタクトフローの途中でAIエージェントを切り替え、状況に応じた最適な応対が可能になります。

ユースケース

AIエージェントを動的に切り替える機能は、以下のような場合に有効です。

  • 段階的な対応プロセス:最初のAIエージェントでお問い合わせ内容を分析・振り分けし、その結果に基づいて別のAIエージェント(より専門的なプロンプトを持つもの)に切り替えて詳細な回答を提供する場合

  • チャネル別の最適化:電話番号やメールアドレス、Connectフローごとに異なるAIエージェントを割り当て、各チャネルに最適化された応対を実現したい場合

これにより、一つのConnectインスタンスで複数のAIエージェントを効率的に活用できます。

事前準備

  • Q in Connectのセルフサービスで2つのAIエージェントが作成済みであること

IAMポリシー作成

LambdaがAIエージェントを切り替えるために必要な権限を持つIAMポリシーを作成します。

ConnectインスタンスのARNは各自の環境に合わせて変更してください。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "connect:DescribeContact"
            ],
            "Resource": [
                "arn:aws:connect:ap-northeast-1:111111111111:instance/3ff2093d-af96-43fd-b038-3c07cdd7609c/contact/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "wisdom:UpdateSession"
            ],
            "Resource": [
                "arn:aws:wisdom:ap-northeast-1:111111111111:assistant/*",
                "arn:aws:wisdom:ap-northeast-1:111111111111:session/*/*"
            ]
        }
    ]
}

Lambda作成

AIエージェントを切り替えるためのLambda関数を以下の設定で作成します。

  • Lambda名:cm-hirai-qconnect-ai-agent-switcher
  • ランタイム:Python 3.13
  • IAMポリシー:AWSLambdaBasicExecutionRoleと先ほど作成したIAMポリシー

以下のコードをLambdaに実装します。

import json
import boto3
from botocore.exceptions import ClientError

connect_client = boto3.client('connect')
qconnect_client = boto3.client('qconnect')

def lambda_handler(event, context):
    print('Event Received:' + json.dumps(event, ensure_ascii=False))

    # Connectコンタクトデータの取得
    contact_data = event.get('Details').get('ContactData')

    # 変更したいAIエージェントIDとそのバージョンをパラメータから取得
    ai_agent_id = event.get('Details').get('Parameters').get('aiAgentId')
    ai_agent_version = event.get('Details').get('Parameters').get('aiAgentVersion', 'LATEST')
    ai_agent_with_version = f"{ai_agent_id}:{ai_agent_version}"

    print(f"AI Agent ID: {ai_agent_id}")
    print(f"AI Agent Version: {ai_agent_version}")
    print(f"Combined AI Agent ID with Version: {ai_agent_with_version}")

    try:
        # Amazon Connect Contactの情報を取得
        contact_response = connect_client.describe_contact(
            InstanceId=contact_data.get('InstanceARN'),
            ContactId=contact_data.get('ContactId')
        ).get("Contact")

        # QConnect Session ARNからAssistant IdとSession Idを抽出
        session_arn = contact_response.get("WisdomInfo").get("SessionArn")
        assistant_id, session_id = session_arn.split('/')[1], session_arn.split('/')[2]

        # 更新するAIエージェントを指定
        ai_agent_configuration = {
            "SELF_SERVICE": {
                "aiAgentId": ai_agent_with_version
            }
        }

        # UpdateSession APIを呼び出してAIエージェントを変更
        update_session_response = qconnect_client.update_session(
            assistantId=assistant_id,
            sessionId=session_id,
            aiAgentConfiguration=ai_agent_configuration
        )

        print('Update Session Response:' + json.dumps(update_session_response, default=str))
        return {"success": True}

    except Exception as e:
        print(f"Error: {str(e)}")
        return {"success": False, "error": str(e)}

このLambdaは、以下のステップでAIエージェントを切り替えます。

  1. Connectのコンタクトフローから受け取ったコンタクト情報とパラメータ(AIエージェントIDとバージョン)を取得
  2. describe_contact APIを使用してコンタクト詳細情報を取得
  3. セッションARNからアシスタントIDとセッションIDを抽出
  4. update_session APIを呼び出してセッションのAIエージェントを更新

今回は、UpdateSession API でSELF_SERVICEを更新していますが、ANSWER_RECOMMENDATIONMANUAL_SEARCH の指定も可能です。

https://docs.aws.amazon.com/connect/latest/APIReference/API_amazon-q-connect_UpdateSession.html#connect-amazon-q-connect_UpdateSession-request-aiAgentConfiguration

Connectフロー

AIエージェントを切り替えるためのConnectフローを作成します。
今回のフローの構成は以下のとおりです。

作成するAIエージェントによってフローは異なりますので参考程度にしてください。

cm-hirai-screenshot 2025-03-24 14.45.42

(クリックで展開)
{
  "Version": "2019-10-30",
  "StartAction": "eefc95bb-80a9-49e9-b199-4b392dae906d",
  "Metadata": {
    "entryPointPosition": {
      "x": 265.6,
      "y": -4
    },
    "ActionMetadata": {
      "d87f1832-7b0d-4094-8871-a7203ad8dbf7": {
        "position": {
          "x": 327.2,
          "y": 183.2
        },
        "children": [
          "3c05280a-47ce-4107-a67f-c8330fcf5b7d"
        ],
        "parameters": {
          "WisdomAssistantArn": {
            "displayName": "arn:aws:wisdom:ap-northeast-1:111111111111:assistant/a1793008-f4de-481b-a9ed-3697ef373ff2"
          }
        },
        "fragments": {
          "SetContactData": "3c05280a-47ce-4107-a67f-c8330fcf5b7d"
        }
      },
      "3c05280a-47ce-4107-a67f-c8330fcf5b7d": {
        "position": {
          "x": 327.2,
          "y": 183.2
        },
        "dynamicParams": []
      },
      "b4b584b7-9c7a-49c1-ab17-7806a2e8f0d4": {
        "position": {
          "x": 576,
          "y": 11.2
        },
        "children": [
          "e8df2e07-6987-4a06-baff-93bb07d1203c"
        ],
        "overrideConsoleVoice": true,
        "fragments": {
          "SetContactData": "e8df2e07-6987-4a06-baff-93bb07d1203c"
        },
        "overrideLanguageAttribute": true
      },
      "e8df2e07-6987-4a06-baff-93bb07d1203c": {
        "position": {
          "x": 576,
          "y": 11.2
        },
        "dynamicParams": []
      },
      "757fcc83-5237-4e08-9d8e-fce40a04aaee": {
        "position": {
          "x": 1449.6,
          "y": 197.6
        },
        "parameters": {
          "LambdaFunctionARN": {
            "displayName": "cm-hirai-qconnect-ai-agent-switcher"
          }
        },
        "dynamicMetadata": {
          "aiAgentId": false,
          "aiAgentVersion": false
        }
      },
      "e2c42030-f2a8-405d-9f05-5e68deccc6ae": {
        "position": {
          "x": 1231.2,
          "y": 192
        }
      },
      "1e79e345-7b31-403f-80ea-9fc535d637ac": {
        "position": {
          "x": 558.4,
          "y": 192.8
        },
        "parameters": {
          "LexV2Bot": {
            "AliasArn": {
              "displayName": "TestBotAlias",
              "useLexBotDropdown": true,
              "lexV2BotName": "cm-hirai-q-in-connect"
            }
          }
        },
        "useLexBotDropdown": true,
        "lexV2BotName": "cm-hirai-q-in-connect",
        "lexV2BotAliasName": "TestBotAlias",
        "conditionMetadata": []
      },
      "Route 1": {
        "position": {
          "x": 786.4,
          "y": 193.6
        },
        "isFriendlyName": true,
        "conditions": [],
        "conditionMetadata": [
          {
            "id": "81d8df8a-464e-4daf-9f7c-8d07880d84ad",
            "operator": {
              "name": "Equals",
              "value": "Equals",
              "shortDisplay": "="
            },
            "value": "ROUTING"
          }
        ]
      },
      "Route 2": {
        "position": {
          "x": 1004,
          "y": 191.2
        },
        "isFriendlyName": true,
        "conditions": [],
        "conditionMetadata": [
          {
            "id": "a8abfb5d-fb99-4c28-a7f8-793ad1cf3522",
            "operator": {
              "name": "Equals",
              "value": "Equals",
              "shortDisplay": "="
            },
            "value": "support"
          }
        ]
      },
      "2805f917-7885-4f2d-bac5-828fbbb7f2d3": {
        "position": {
          "x": 1672.8,
          "y": 198.4
        },
        "parameters": {
          "LexV2Bot": {
            "AliasArn": {
              "displayName": "TestBotAlias",
              "useLexBotDropdown": true,
              "lexV2BotName": "cm-hirai-q-in-connect"
            }
          }
        },
        "useLexBotDropdown": true,
        "lexV2BotName": "cm-hirai-q-in-connect",
        "lexV2BotAliasName": "TestBotAlias",
        "conditionMetadata": []
      },
      "88156212-ee5a-4fa2-8391-7caaabcc4be7": {
        "position": {
          "x": 1242.4,
          "y": 649.6
        }
      },
      "b0da1dee-bcf5-41cb-ad07-a73297d7a57f": {
        "position": {
          "x": 1235.2,
          "y": 431.2
        }
      },
      "d99b6026-7bdc-40b9-88cb-69fe76f18e7f": {
        "position": {
          "x": 1894.4,
          "y": 733.6
        }
      },
      "eefc95bb-80a9-49e9-b199-4b392dae906d": {
        "position": {
          "x": 356,
          "y": -6.4
        }
      }
    },
    "Annotations": [],
    "name": "cm-hirai-q-in-connect-ai-agent-switcher",
    "description": "",
    "type": "contactFlow",
    "status": "PUBLISHED",
    "hash": {}
  },
  "Actions": [
    {
      "Parameters": {
        "WisdomAssistantArn": "arn:aws:wisdom:ap-northeast-1:111111111111:assistant/a1793008-f4de-481b-a9ed-3697ef373ff2"
      },
      "Identifier": "d87f1832-7b0d-4094-8871-a7203ad8dbf7",
      "Type": "CreateWisdomSession",
      "Transitions": {
        "NextAction": "3c05280a-47ce-4107-a67f-c8330fcf5b7d",
        "Errors": [
          {
            "NextAction": "88156212-ee5a-4fa2-8391-7caaabcc4be7",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Parameters": {
        "WisdomSessionArn": "$.Wisdom.SessionArn"
      },
      "Identifier": "3c05280a-47ce-4107-a67f-c8330fcf5b7d",
      "Type": "UpdateContactData",
      "Transitions": {
        "NextAction": "1e79e345-7b31-403f-80ea-9fc535d637ac",
        "Errors": [
          {
            "NextAction": "88156212-ee5a-4fa2-8391-7caaabcc4be7",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Parameters": {
        "TextToSpeechEngine": "Neural",
        "TextToSpeechStyle": "None",
        "TextToSpeechVoice": "Kazuha"
      },
      "Identifier": "b4b584b7-9c7a-49c1-ab17-7806a2e8f0d4",
      "Type": "UpdateContactTextToSpeechVoice",
      "Transitions": {
        "NextAction": "e8df2e07-6987-4a06-baff-93bb07d1203c",
        "Errors": [
          {
            "NextAction": "d87f1832-7b0d-4094-8871-a7203ad8dbf7",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Parameters": {
        "LanguageCode": "ja-JP"
      },
      "Identifier": "e8df2e07-6987-4a06-baff-93bb07d1203c",
      "Type": "UpdateContactData",
      "Transitions": {
        "NextAction": "d87f1832-7b0d-4094-8871-a7203ad8dbf7",
        "Errors": [
          {
            "NextAction": "d87f1832-7b0d-4094-8871-a7203ad8dbf7",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Parameters": {
        "LambdaFunctionARN": "arn:aws:lambda:ap-northeast-1:111111111111:function:cm-hirai-qconnect-ai-agent-switcher",
        "InvocationTimeLimitSeconds": "8",
        "LambdaInvocationAttributes": {
          "aiAgentId": "9f6c6cce-bbe7-42e5-b432-3b32b59eb52a",
          "aiAgentVersion": "1"
        },
        "ResponseValidation": {
          "ResponseType": "STRING_MAP"
        }
      },
      "Identifier": "757fcc83-5237-4e08-9d8e-fce40a04aaee",
      "Type": "InvokeLambdaFunction",
      "Transitions": {
        "NextAction": "2805f917-7885-4f2d-bac5-828fbbb7f2d3",
        "Errors": [
          {
            "NextAction": "2805f917-7885-4f2d-bac5-828fbbb7f2d3",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Parameters": {
        "Text": "技術サポート関連"
      },
      "Identifier": "e2c42030-f2a8-405d-9f05-5e68deccc6ae",
      "Type": "MessageParticipant",
      "Transitions": {
        "NextAction": "757fcc83-5237-4e08-9d8e-fce40a04aaee",
        "Errors": [
          {
            "NextAction": "757fcc83-5237-4e08-9d8e-fce40a04aaee",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Parameters": {
        "Text": "問い合わせ内容をお伝え下さい。",
        "LexV2Bot": {
          "AliasArn": "arn:aws:lex:ap-northeast-1:111111111111:bot-alias/A3QN58KMQ7/TSTALIASID"
        }
      },
      "Identifier": "1e79e345-7b31-403f-80ea-9fc535d637ac",
      "Type": "ConnectParticipantWithLexBot",
      "Transitions": {
        "NextAction": "88156212-ee5a-4fa2-8391-7caaabcc4be7",
        "Errors": [
          {
            "NextAction": "Route 1",
            "ErrorType": "NoMatchingCondition"
          },
          {
            "NextAction": "88156212-ee5a-4fa2-8391-7caaabcc4be7",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Parameters": {
        "ComparisonValue": "$.Lex.SessionAttributes.Tool"
      },
      "Identifier": "Route 1",
      "Type": "Compare",
      "Transitions": {
        "NextAction": "88156212-ee5a-4fa2-8391-7caaabcc4be7",
        "Conditions": [
          {
            "NextAction": "Route 2",
            "Condition": {
              "Operator": "Equals",
              "Operands": [
                "ROUTING"
              ]
            }
          }
        ],
        "Errors": [
          {
            "NextAction": "88156212-ee5a-4fa2-8391-7caaabcc4be7",
            "ErrorType": "NoMatchingCondition"
          }
        ]
      }
    },
    {
      "Parameters": {
        "ComparisonValue": "$.Lex.SessionAttributes.category"
      },
      "Identifier": "Route 2",
      "Type": "Compare",
      "Transitions": {
        "NextAction": "b0da1dee-bcf5-41cb-ad07-a73297d7a57f",
        "Conditions": [
          {
            "NextAction": "e2c42030-f2a8-405d-9f05-5e68deccc6ae",
            "Condition": {
              "Operator": "Equals",
              "Operands": [
                "support"
              ]
            }
          }
        ],
        "Errors": [
          {
            "NextAction": "b0da1dee-bcf5-41cb-ad07-a73297d7a57f",
            "ErrorType": "NoMatchingCondition"
          }
        ]
      }
    },
    {
      "Parameters": {
        "Text": "問い合わせ内容をお伝え下さい。",
        "LexV2Bot": {
          "AliasArn": "arn:aws:lex:ap-northeast-1:111111111111:bot-alias/A3QN58KMQ7/TSTALIASID"
        }
      },
      "Identifier": "2805f917-7885-4f2d-bac5-828fbbb7f2d3",
      "Type": "ConnectParticipantWithLexBot",
      "Transitions": {
        "NextAction": "d99b6026-7bdc-40b9-88cb-69fe76f18e7f",
        "Errors": [
          {
            "NextAction": "d99b6026-7bdc-40b9-88cb-69fe76f18e7f",
            "ErrorType": "NoMatchingCondition"
          },
          {
            "NextAction": "d99b6026-7bdc-40b9-88cb-69fe76f18e7f",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Parameters": {
        "Text": "エラー"
      },
      "Identifier": "88156212-ee5a-4fa2-8391-7caaabcc4be7",
      "Type": "MessageParticipant",
      "Transitions": {
        "NextAction": "d99b6026-7bdc-40b9-88cb-69fe76f18e7f",
        "Errors": [
          {
            "NextAction": "d99b6026-7bdc-40b9-88cb-69fe76f18e7f",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Parameters": {
        "Text": "一致なし その他"
      },
      "Identifier": "b0da1dee-bcf5-41cb-ad07-a73297d7a57f",
      "Type": "MessageParticipant",
      "Transitions": {
        "NextAction": "d99b6026-7bdc-40b9-88cb-69fe76f18e7f",
        "Errors": [
          {
            "NextAction": "d99b6026-7bdc-40b9-88cb-69fe76f18e7f",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Parameters": {},
      "Identifier": "d99b6026-7bdc-40b9-88cb-69fe76f18e7f",
      "Type": "DisconnectParticipant",
      "Transitions": {}
    },
    {
      "Parameters": {
        "FlowLoggingBehavior": "Enabled"
      },
      "Identifier": "eefc95bb-80a9-49e9-b199-4b392dae906d",
      "Type": "UpdateFlowLoggingBehavior",
      "Transitions": {
        "NextAction": "b4b584b7-9c7a-49c1-ab17-7806a2e8f0d4"
      }
    }
  ]
}

AIエージェントの切り替えフローの流れは以下の通りです。

  1. デフォルトAIエージェントの利用

    • Amazon Q Connectブロック(紫)の後に、Lexボット呼び出しブロック(青)を配置すると、デフォルトのAIエージェントが使用されます
  2. AIエージェント切り替え

    • Lambda関数呼び出しブロック(赤)を経由した後のLexボット呼び出しブロック(青)では、Lambda関数で指定した別のAIエージェントが使用されます

Lambda関数ブロックでは、切り替え先のAIエージェント情報をパラメータとして渡します。

  • aiAgentId
  • aiAgentVersion
    cm-hirai-screenshot 2025-04-10 8.03.31

それぞれ値はConnectの管理コンソールから確認できます。
cm-hirai-screenshot 2025-03-24 15.01.37

上記のフローで実際に電話すると、呼び出される2つのLexボットがそれぞれ異なるAIエージェントであると確認できました。

まとめ

本記事では、Amazon Connectフロー上でAWS Lambdaを活用し、Q in ConnectのAIエージェントを切り替える方法を紹介しました。

UpdateSession APIを使用することでAIエージェントの切り替えが可能です。

この実装により、以下のようなユースケースが可能になります。

  • 段階的な対応プロセスの実現(振り分け用AIエージェント→専門回答用AIエージェント)
  • 電話番号やフローごとに異なるAIエージェントの使用

現在、Connectフロー上でデフォルトで設定したAIエージェント以外のAIエージェントを利用する場合、Lambdaを利用する必要があります。

AIエージェントを切り替えられる専用のフローブロックがリリースされるとよいですね。

参考

https://catalog.workshops.aws/amazon-q-in-connect/en-US/content-segmentation/applying-tags#step-2:-create-an-aws-lambda-function-for-amazon-q-in-connect-content-segmentation

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/qconnect/update-session.html

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.