Rasa NLU 0.15.1版本下rasa_core在线训练代码导入报错的适配方法咨询
报错核心原因
- 参考书籍的代码基于2018年Rasa 0.11及更早的分立版本编写,Rasa从0.14版本(即配套Rasa NLU 0.15.x的core版本)开始对模块结构做了大规模重构:
- 触发
ImportError: cannot import name ‘online’ from ‘rasa_core.training’的直接原因是原rasa_core.training包下的online子模块被完全移除,原有在线服务训练的逻辑被迁移整合到其他模块 - 原有的
train.train_dialog_model训练接口被重命名、调整入参结构,不再兼容旧版调用方式
- 触发
适配Rasa NLU 0.15.1版本的可运行代码
from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import logging from rasa_core import utils from rasa_core.interpreter import RasaNLUInterpreter from rasa_core.train import train_dialogue_model from rasa_core.training import interactive logger = logging.getLogger(__name__) def train_agent(interpreter): return train_dialogue_model( domain_file="horoscope_domain.yml", stories_file="data/stories.md", output_path="models/dialog", nlu_data=interpreter, endpoints_file="endpoints.yml", max_history=2, batch_size=50, epochs=200, max_training_samples=300 ) if __name__ == '__main__': utils.configure_colored_logging(loglevel="DEBUG") nlu_model_path = "./models/nlu/default/horoscopebot" interpreter = RasaNLUInterpreter(nlu_model_path) agent = train_agent(interpreter) # 启动交互式在线训练服务 interactive.run_interactive_learning(agent, interpreter=interpreter)
关键调整对照说明
- 导入逻辑替换:
- 原抽象类
NaturalLanguageInterpreter的直接创建逻辑,替换为具体实现类RasaNLUInterpreter加载本地NLU模型,兼容0.15.x版本的解释器接口 - 原废弃的
rasa_core.training.online导入删除,替换为rasa_core.training.interactive模块下的交互式训练能力
- 原抽象类
- 训练接口参数对齐:
- 原
train_dialog_model重命名为train_dialogue_model,原嵌套在kwargs字典里的训练超参(batch_size/epochs/max_training_samples)调整为顶层入参,不再需要嵌套传递 - 原
endpoints入参更名为endpoints_file,为端点配置文件的路径 - 原
nlu_model_path入参调整为nlu_data,接收已初始化完成的NLU解释器对象
- 原
- 在线服务逻辑替换:
- 原
online.serve_agent(agent)替换为interactive.run_interactive_learning(agent, interpreter=interpreter),保持原有的边对话边收集训练数据的在线训练能力
- 原
- 版本配套要求:Rasa NLU 0.15.1需搭配同时间线发布的Rasa Core 0.14.x版本使用,版本不匹配会进一步出现接口缺失、参数不兼容的问题
内容的提问来源于stack exchange,提问作者user1290932
相关产品推荐
相关产品推荐

