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

如何使用AMAZON.NoIntent处理多轮对话中的多个否定表述

如何在多轮对话中正确使用AMAZON.NoIntent处理多次否定?

先看你给出的对话场景:

Alexa:你想去看电影吗?
用户:不
Alexa:我还能帮你做些别的吗?
用户:不
Alexa:好的,下次再见。

要实现这种逻辑,核心是用会话属性跟踪用户的否定次数,区分第一次和后续的否定操作,下面是具体的实现思路和代码示例:

核心逻辑思路

  • 第一次触发AMAZON.NoIntent:不结束会话,询问用户是否需要其他帮助
  • 第二次及以后触发AMAZON.NoIntent:判断会话状态,直接结束会话并给出告别语

代码实现示例

def handle_intent(intent_name, session_attributes):
    if intent_name == "AMAZON.NoIntent":
        return no_response(session_attributes)

def no_response(session_attributes):
    # 初始化会话属性,首次否定时记录次数为1
    if session_attributes is None:
        session_attributes = {"negation_count": 1}
        card_title = "NO RESPONSE"
        speech_output = "Okay. Do you want me to help with anything else?"
        reprompt_text = "Sorry. Can you repeat that again, please?"
        should_end_session = False
    else:
        # 检查是否已经有过一次否定,这次直接结束会话
        if session_attributes.get("negation_count") == 1:
            speech_output = "好的,下次再见。"
            should_end_session = True
            card_title = "Goodbye"
            reprompt_text = None
        else:
            # 兜底处理:如果出现意外的多次否定,也友好结束会话
            speech_output = "好的,有需要再找我哦。"
            should_end_session = True
            card_title = "Goodbye"
            reprompt_text = None
    
    # 构建符合Alexa规范的响应
    return build_response(session_attributes, {
        "card": {
            "type": "Simple",
            "title": card_title,
            "content": speech_output
        },
        "outputSpeech": {
            "type": "PlainText",
            "text": speech_output
        },
        "reprompt": {
            "outputSpeech": {
                "type": "PlainText",
                "text": reprompt_text
            }
        } if reprompt_text else None,
        "shouldEndSession": should_end_session
    })

# 辅助函数:生成Alexa要求的标准响应格式
def build_response(session_attributes, response):
    return {
        "version": "1.0",
        "sessionAttributes": session_attributes,
        "response": response
    }

关键注意事项

  • 会话属性的作用:必须借助session_attributes来记录用户的否定次数,这是区分不同轮次否定的核心——因为Alexa的会话是无状态的,需要我们自己存储会话过程中的关键信息
  • 会话结束的控制:第一次否定时should_end_session要设为False,保持会话活跃;第二次否定时设为True,让会话正常结束
  • 兜底逻辑:加入意外情况的处理,避免因为异常的会话状态导致无限循环或者不友好的回复

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:24:02