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

关于/restart指令失效及日志清除的Rasa Slot问题咨询

解决Rasa机器人/restart失效及重启时清除日志的问题

看起来你遇到了两个关联的问题:自定义slot收集action运行时/restart指令没反应,以及需要在重启时清除日志。我来一步步帮你解决:

一、修复/restart指令失效的问题

1. 让自定义action支持中断

如果你的自定义action是表单(FormAction):

  • 在表单类里设置interruptible = True,这样当用户发送/restart时,表单会立即停止,转而执行重启逻辑:
class MySlotCollectionForm(FormAction):
    interruptible = True  # 关键设置,允许表单被中断
    def name(self) -> str:
        return "my_slot_collection_form"
    # 其他表单逻辑...

如果是非表单的自定义action:

  • 在action的run方法开头,先检查用户最新的意图是否是restart,如果是,直接返回Restarted()事件,触发重启流程:
from rasa_sdk.events import Restarted

class MyCustomSlotAction(Action):
    def name(self) -> str:
        return "my_custom_slot_action"

    def run(self, dispatcher, tracker, domain):
        # 优先处理restart意图
        if tracker.latest_intent_name == "restart":
            dispatcher.utter_message(text="正在重启对话...")
            return [Restarted()]
        
        # 原来的slot收集逻辑
        # ...

2. 确保重启流程被正确配置

  • 在domain.yml中确认包含重启相关action:
actions:
  - action_restart
  - my_custom_slot_action
  # 其他action...
  • 在stories.yml中添加处理重启的故事路径,覆盖slot收集过程中的场景:
stories:
  - story: restart during slot collection
    steps:
      - intent: start_slot_collection
      - action: my_custom_slot_action
      - intent: restart
      - action: action_restart
      - action: utter_restarted

二、实现重启时清除日志的功能

默认的action_restart只会重置slot和对话状态,要清除日志,你需要自定义一个重启action来替代默认的:

1. 编写自定义重启action

from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.events import Restarted, SlotSet
import os

class ActionCustomRestart(Action):
    def name(self) -> str:
        return "action_custom_restart"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: dict) -> list:
        # 1. 重置所有slot
        reset_slots = [SlotSet(slot, None) for slot in tracker.slots.keys()]
        
        # 2. 清除日志(根据你的日志存储方式调整)
        log_file_path = "bot.log"  # 替换成你的日志文件路径
        if os.path.exists(log_file_path):
            with open(log_file_path, "w") as f:
                f.truncate(0)  # 清空文件内容
        
        # 3. 发送重启提示
        dispatcher.utter_message(text="对话已重启,日志已清除。")
        
        # 4. 返回重启事件和slot重置事件
        return reset_slots + [Restarted()]

2. 更新配置

  • 在domain.yml中替换默认的action_restart为你的自定义action:
actions:
  - action_custom_restart
  - my_custom_slot_action
  # 其他action...
  • 在nlu.yml中确保restart意图能触发这个自定义action:
nlu:
  - intent: restart
    examples: |
      - /restart
      - 重启对话
      - 重新开始

这样调整后,当用户在slot收集过程中发送/restart,你的自定义slot action会优先处理重启请求,触发自定义的重启action,既重置了对话状态和slot,又清除了日志。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 15:04:09