Rasa框架之使用rasa-nlu库开发星座运势聊天机器人

Horoscope星座机器人

项目开发记录

Rasa NLU部分

  1. 安装rasa-nlu库、tensorflow库以及其他依赖库
# 使用清华大学镜像在这里插入代码片
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple rasa-nlu==0.15.1 tensorflow==1.13.2 sklearn_crfsuite
  1. 在项目目录中创建名为data的文件夹,在data文件夹中创建data.json文件。文件的框架和内容分别如下:
# data.json文件的基本框架
{
    "rasa_nlu_data":{
        "common_examples":[],
        "regex_features":[],
        "entity_synonyms":[]
    }
}
# data.json 本例中的文件内容
{
  "rasa_nlu_data": {
    "common_examples": [
      {
        "text": "Hello",
        "intent": "greeting",
        "entities": []
      },
      {
        "text": "I wand to know my Horoscope",
        "intent": "get_horoscope",
        "entities": []
      },
      {
        "text": "Can you please tell me my horoscope?",
        "intent": "get_horoscope",
        "entities": []
      },
      {
        "text": "Please subscribe me",
        "intent": "subscription"
      }
    ],
    "regex_features": [],
    "entity_synonyms": []
  }
}
  1. 创建好data.json文件之后,创建配置文件,在项目目录下创建一个名为config.json的json文件。添加如下配置:
# pipline: 管道将指定使用哪种特征器或特征提取器来解析文本和提取必要信息,在例子中,我们将使用tensorflow_embedding.
# path: 路径本质上是训练后保存模型的目录,将在/models/nlu目录下保存我们的模型。
# data: 数据是需要置顶的路径;基本上就是训练数据所在的位置。
{
  "pipeline": "tensorflow_embedding",
  "path": "./models/nlu",
  "data": "./data/data.json"
}
  1. 编写python代码训练模型并进行预测:首先在项目目录中创建一个名为rasa-nlu.py的文件,文件内容如下:
from rasa_nlu.training_data import load_data
from rasa_nlu.model import Trainer
from rasa_nlu import config
from rasa_nlu.model import Interpreter

def train_horoscopebot(data_json,config_file,model_dir):
    training_data = load_data(data_json)
    trainer = Trainer(config.load(config_file))
    trainer.train(training_data)
    model_directory = trainer.persist(model_dir,fixed_model_name='horoscopebot')

def predict_intent(text):
    interpreter = Interpreter.load('./models/nlu/default/horoscopebot')
    print(interpreter.parse(text))
  1. 使用data.jsonconfig.json文件并通过tensorflow_embedding管道训练模型。
# 在步骤4中的代码后面添加如下代码,运行该文件,训练模型
train_horoscopebot('./data/data.json','config.json','./models/nlu')
# 代码将会自动创建models的文件夹
# 输出结果:Epochs: 100%|██████████| 300/300 [00:00<00:00, 316.48it/s, loss=0.085, acc=1.000]
  1. 从模型进行预测。
# 在执行完步骤5后,模型已经训练好并保存在了models文件夹中,步骤5的代码注释掉,重新添加如下代码,调用predice_intent方法来查看训练好的模型的效果
predict_intent("I am looking for my horoscope for today. I am wondering if you can tell me that.")
# 只有当训练数据发生变化时,才需要重新训练模型,如果模型没有发生变化,不需要每次都重新运行步骤5所示的代码,可以直接加载现有的训练模型对新示例进行预测。
  1. 运行结果如下,表示模型预测该文本的置信度约为96%。
{'intent': {'name': 'get_horoscope', 'confidence': 0.963240921497345}, 'entities': [], 'intent_ranking': [{'name': 'get_horoscope', 'confidence': 0.963240921497345}, {'name': 'greeting', 'confidence': 0.0}, {'name': 'subscription', 'confidence': 0.0}], 'text': 'I am looking for my horoscope for today. I am wondering if you can tell me that.'}

执行完以上步骤后,项目目录结构如下:

Horoscope
|   config.json
|   rasa-nlu.py
+---data
|       data.json
|       __init__.py
|       
+---models
|   \---nlu
|       \---default
|           \---horoscopebot
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值