对话机器人 Rasa(二十):Custom Action 中无法读取 Slot 值

文章目录

    现象

    rasa custom action 中的代码,无法获取到 slot 值:

    items = tracker.get_slot("items")
    

    而我确实在之前的流程中已经设置了 slot 的值

    return [
    	SlotSet("items", items),
    	FollowupAction("action_a"),
    ]
    

    疑惑

    通过打印日志,可以确定的是,赋给 slot 的值不是空。

    可是为何获取不到呢?

    我怀疑是 story 中没有加入对应的流程,即 slot_was_set 逻辑。

    解决方法

    测试了一下,确实如此,加入 slot_was_set 逻辑之后,slot 值就能正常获取到了。

    - story: some story in Sunday
      steps:
      - intent: test_slot_set
      - action: action_test_slot_set
      - slot_was_set:
        - items: true
    

    Rasa 的开发真是魔幻啊!解决问题全靠猜。

    slot_was_set 的作用

    During training, Rasa does not call the action server. This means that your assistant’s dialogue management model doesn’t know which events a custom action will return. Because of this, events such as setting a slot or activating/deactivating a form have to be explicitly written out as part of the stories.

    即,在 rasa train 过程中,无法直接解析 custom action 中的 python 代码逻辑。
    所以,需要在 story 中注明 custom action 中返回的事件,例如 slot_was_set。

    如果设置为 None,对应的 story 规则为:

    - slot_was_set:
      - my_slot: null
    

    但,这并没有解答我的疑惑,为何会导致 slot 值无法正确设置。算了,先按照规范写吧。

    查看合集

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

    关于作者 🌱

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