对话机器人 Rasa(十五):slot type 与 influence conversation

文章目录

    domain.yml 中的 slot 配置

    以下是一段有问题的 slot 配置

    slots:
      email:
        type: any
        influence_conversation: true
        mappings:
        - type: from_text
          conditions:
             - active_loop: email_form
    

    训练时的错误信息

    > rasa train --force
    
    InvalidSlotConfigError: An AnySlot cannot be featurized. Please use a different slot type for slot 'email' instead. If you need to featurize a data type which is not supported out of the box, implement a custom slot type by subclassing 'Slot'. See the documentation for more information: https://rasa.com/docs/rasa/domain#slots
    

    修复方法

    influence_conversation: false
    

    influence_conversation 为何物

    Slots act as your bot’s memory. When you define a slot, you can define whether a slot should influence the conversation or not. Slots with the property influence_conversation set to false can only store information. Slots with the property influence_conversation set to true can affect the dialogue flow based on the information stored in it.

    例如,Rasa 官方给出的这个例子:

    stories:
    - story: Welcome message, premium user
      steps:
       - intent: greet
       - action: action_check_profile
       - slot_was_set:
         - premium_account: true
       - action: utter_welcome_premium
    
    - story: Welcome message, basic user
      steps:
       - intent: greet
       - action: action_check_profile
       - slot_was_set:
         - premium_account: false
       - action: utter_welcome_basic
       - action: utter_ask_upgrade
    

    这个 story 的大致逻辑是:

    1. 用户打招呼
    2. custom action action_check_profile 中,使用 python 代码查询当前用户是否为付费用户。然后设置 slot 信息,来影响对话的逻辑
    3. 如果是付费用户,就给出针对付费会员的欢迎信息
    4. 如果是非付费用户,先给出欢迎信息,然后询问是否需要升级账号为付费账号

    即,这种在 story 中使用了 slot 影响对话逻辑的情况才应该设置:

    influence_conversation: true
    

    slot type any

    当然,我也觉得用 any 不合适,email 本质上还是个 text,所以这里同时改成 text 比较合适。

    最终的 slot 配置

    slots:
      email:
        type: text
        influence_conversation: false
        mappings:
        - type: from_text
          conditions:
             - active_loop: email_form
    

    上面示例对应的设置

    premium_account:
        type: bool
        influence_conversation: true
        mappings:
        - type: custom
    

    actions.py :一定要注意,python 的 True/False 首字母是大写,yaml 中是小写。。。被坑。而且 python 默认没有编译检测,很容易出问题

    if some_condition:
        # ...
        return [
            SlotSet("premium_account", True),
            FollowupAction("action_a"),
        ]
    else:
        # ...
        return [
            SlotSet("premium_account", None),
            FollowupAction("action_b"),
        ]
    

    注意不是 False

    rasa slot_was_set 值 false 和 null 的区别

    在 Rasa 中,slot_was_set 是一个用于检查槽位是否已设置的方法。当槽位被设置为不同的值时,slot_was_set 的行为与其值为 FalseNone 时有所不同。

    1. 当槽位被设置为 False 时,slot_was_set 返回 True。这表示槽位已经被明确设置为布尔值 False
    2. 当槽位被设置为 None(即空值)时,slot_was_set 返回 False。这表示槽位的值是空的,尚未被设置为任何有效的值。

    注意哇。。。如果槽位被设置为 False,则返回 True;如果槽位被设置为 None,则返回 False

    查看合集

    📖 对话机器人 Rasa 中文系列教程

    参考

    • https://forum.rasa.com/t/why-i-cant-use-type-any-for-the-slot-phone-number/57728
    • https://rasa.com/docs/rasa/writing-stories/

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式