You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在AWS Lex中从welcomeMsg(Intent A)调用Intent B?求Python实现

解决AWS Lex从Intent A跳转到Intent B的问题

我来帮你搞定这个AWS Lex的跳转问题!要实现从welcomeMsg(Intent A)跳转到Intent B,核心是在Lambda函数里判断用户的输入,然后引导Lex切换到目标Intent。下面是具体的代码和配置步骤:

前提准备

  • 确保你已经在Lex控制台创建好welcomeMsg(Intent A)和目标Intent B,并且Intent B的样本 utterances包含与"B"相关的表述(比如"B"、"我选B"等)。
  • 给welcomeMsg Intent配置Dialog Code Hook和Fulfillment Code Hook,关联到下面的Lambda函数。

Python Lambda代码实现

import json

def lambda_handler(event, context):
    # 获取当前触发的Intent名称和用户的输入内容
    current_intent = event['currentIntent']['name']
    user_input = event['inputTranscript'].strip().lower()
    
    # 处理welcomeMsg Intent的逻辑
    if current_intent == 'welcomeMsg':
        # 第一次进入Intent A时,返回预设的欢迎语
        if event['invocationSource'] == 'FulfillmentCodeHook':
            return {
                "dialogAction": {
                    "type": "ElicitIntent",
                    "message": {
                        "contentType": "PlainText",
                        "content": "Hi, I am a xxx-BOT. I can help you with following: A B C"
                    }
                }
            }
        # 处理用户回复后的逻辑(比如用户输入了B)
        elif event['invocationSource'] == 'DialogCodeHook':
            # 可以扩展更多匹配规则,比如匹配"选B"、"我要B"等表述
            if user_input in ['b', 'option b', '选b', '我要b']:
                # 引导Lex切换到Intent B,替换成你实际的Intent B名称
                return {
                    "dialogAction": {
                        "type": "ElicitIntent",
                        "intentName": "IntentB",
                        "message": {
                            "contentType": "PlainText",
                            "content": "Got it, let's handle your request for B right away."
                        }
                    }
                }
            else:
                # 用户输入非目标内容时,提示重新选择
                return {
                    "dialogAction": {
                        "type": "ElicitIntent",
                        "message": {
                            "contentType": "PlainText",
                            "content": "Sorry, I didn't catch that. Please choose from A, B, or C."
                        }
                    }
                }
    # 处理其他Intent的默认逻辑(可选)
    else:
        return {
            "dialogAction": {
                "type": "Close",
                "message": {
                    "contentType": "PlainText",
                    "content": "Oops, something went wrong. Let's try again later."
                }
            }
        }

代码说明

  • FulfillmentCodeHook:第一次触发welcomeMsg时,返回预设的欢迎语,等待用户回复。
  • DialogCodeHook:监听用户的回复,判断是否匹配"B"相关的输入,如果匹配则通过ElicitIntent指令引导Lex切换到Intent B。
  • 你可以根据需求扩展user_input的匹配规则,覆盖更多用户可能的表述。

内容的提问来源于stack exchange,提问作者ajaysheoran2323

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:44:19