Skip to content

rasa安装手册V2

km1994 edited this page Jan 19, 2021 · 1 revision

【关于 rasa 安装 】那些你不知道的事

作者:杨夕

项目地址:https://github.com/km1994/nlp_paper_study

个人介绍:大佬们好,我叫杨夕,该项目主要是本人在研读顶会论文和复现经典论文过程中,所见、所思、所想、所闻,可能存在一些理解错误,希望大佬们多多指正。

目录

安装 Rasa

温馨提示:由于 安装 Rasa 过程中,会安装各种 乱七八糟的 依赖库(eg:tensorflow 2.0,...),导致 安装失败,所以建议 用 conda ,新建 一个 conda 环境,然后在 该环境上面开发。

  • 创建环境
  $ conda create -n rasa python=3.6
  • 激活环境
  $conda activate rasa

Rasa 推荐 安装方式

    pip install rasa-x --extra-index-url https://pypi.rasa.com/simple

注:该命令将同时安装 Rasa 和 Rasa X,如果你不想 安装 Rasa X,你可以用以下 命令:

    pip install Rasa

sklearn 和 MITIE 库 安装

  pip install -U scikit-learn sklearn-crfsuite
  pip install git+https://github.com/mit-nlp/MITIE.git

注:MITIE 库比较大,所以这种 安装方式容易出现问题,所以我用另一种安装方式

  $ git clone https://github.com/mit-nlp/MITIE.git
  $ cd MITIE/
  $ python setup.py install

安装结果

  Compiling src/text_feature_extraction.cpp
  Compiling ../dlib/dlib/threads/multithreaded_object_extension.cpp
  Compiling ../dlib/dlib/threads/threaded_object_extension.cpp
  Compiling ../dlib/dlib/threads/threads_kernel_1.cpp
  Compiling ../dlib/dlib/threads/threads_kernel_2.cpp
  Compiling ../dlib/dlib/threads/threads_kernel_shared.cpp
  Compiling ../dlib/dlib/threads/thread_pool_extension.cpp
  Compiling ../dlib/dlib/misc_api/misc_api_kernel_1.cpp
  Compiling ../dlib/dlib/misc_api/misc_api_kernel_2.cpp
  Linking libmitie.so
  Making libmitie.a
  Build Complete
  make[1]: Leaving directory `/web/workspace/yangkm/python_wp/nlu/DSWp/MITIE/mitielib'
  running build_py
  creating build
  creating build/lib
  creating build/lib/mitie
  copying mitielib/__init__.py -> build/lib/mitie
  copying mitielib/mitie.py -> build/lib/mitie
  copying mitielib/libmitie.so -> build/lib/mitie
  running install_lib
  copying build/lib/mitie/__init__.py -> /home/amy/.conda/envs/yangkm/lib/python3.6/site-packages/mitie
  copying build/lib/mitie/mitie.py -> /home/amy/.conda/envs/yangkm/lib/python3.6/site-packages/mitie
  copying build/lib/mitie/libmitie.so -> /home/amy/.conda/envs/yangkm/lib/python3.6/site-packages/mitie
  byte-compiling /home/amy/.conda/envs/yangkm/lib/python3.6/site-packages/mitie/__init__.py to __init__.cpython-36.pyc
  byte-compiling /home/amy/.conda/envs/yangkm/lib/python3.6/site-packages/mitie/mitie.py to mitie.cpython-36.pyc
  running install_egg_info
  Writing /home/amy/.conda/envs/yangkm/lib/python3.6/site-packages/mitie-0.7.0-py3.6.egg-info

注:会存在 一些 warning 警告,对结果 影响不大

项目初尝试

创建新项目

  1. 第一步是创建一个新的Rasa项目。要做到这一点,运行下面的代码:
  • 启动 Rasa
    rasa init --no-prompt

注:rasa init命令创建rasa项目所需的所有文件,并根据一些示例数据训练一个简单的机器人。如果你省略了——no-prompt参数,将会询问你一些关于项目设置的问题。

  • 运行过程
$ rasa init --no-prompt
Welcome to Rasa! 🤖

To get started quickly, an initial project will be created.
If you need some help, check out the documentation at https://rasa.com/docs/rasa.

Created project directory at '/web/workspace/yangkm/python_wp/nlu/DSWp'.
Finished creating project structure.
Training an initial model...
Training Core model...
Processed Story Blocks: 100%|█████████████████████████████████████████████| 5/5 [00:00<00:00, 3562.34it/s, # trackers=1]
Processed Story Blocks: 100%|█████████████████████████████████████████████| 5/5 [00:00<00:00, 1523.54it/s, # trackers=5]
Processed Story Blocks: 100%|█████████████████████████████████████████████| 5/5 [00:00<00:00, 380.28it/s, # trackers=20]
Processed Story Blocks: 100%|█████████████████████████████████████████████| 5/5 [00:00<00:00, 301.26it/s, # trackers=24]
Processed trackers: 100%|█████████████████████████████████████████████████| 5/5 [00:00<00:00, 2233.39it/s, # actions=16]
Processed actions: 16it [00:00, 14986.35it/s, # examples=16]
Processed trackers: 100%|█████████████████████████████████████████████| 231/231 [00:00<00:00, 899.80it/s, # actions=126]
Epochs:   0%|                                                                                   | 0/100 [00:00<?, ?it/s]/home/amy/.conda/envs/yangkm/lib/python3.6/site-packages/rasa/utils/tensorflow/model_data.py:386: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  final_data[k].append(np.concatenate(np.array(v)))
Epochs: 100%|████████████████████████████████████| 100/100 [00:06<00:00, 14.77it/s, t_loss=0.083, loss=0.009, acc=1.000]
2020-09-17 16:46:48 INFO     rasa.utils.tensorflow.models  - Finished training.
2020-09-17 16:46:48 INFO     rasa.core.agent  - Persisted model to '/tmp/tmpjkpkgun2/core'
Core model training completed.
Training NLU model...
2020-09-17 16:46:48 INFO     rasa.nlu.training_data.training_data  - Training data stats:
2020-09-17 16:46:48 INFO     rasa.nlu.training_data.training_data  - Number of intent examples: 43 (7 distinct intents)
2020-09-17 16:46:48 INFO     rasa.nlu.training_data.training_data  -   Found intents: 'mood_unhappy', 'bot_challenge', 'deny', 'affirm', 'greet', 'mood_great', 'goodbye'
2020-09-17 16:46:48 INFO     rasa.nlu.training_data.training_data  - Number of response examples: 0 (0 distinct responses)
2020-09-17 16:46:48 INFO     rasa.nlu.training_data.training_data  - Number of entity examples: 0 (0 distinct entities)
2020-09-17 16:46:48 INFO     rasa.nlu.model  - Starting to train component WhitespaceTokenizer
2020-09-17 16:46:48 INFO     rasa.nlu.model  - Finished training component.
2020-09-17 16:46:48 INFO     rasa.nlu.model  - Starting to train component RegexFeaturizer
2020-09-17 16:46:48 INFO     rasa.nlu.model  - Finished training component.
2020-09-17 16:46:48 INFO     rasa.nlu.model  - Starting to train component LexicalSyntacticFeaturizer
2020-09-17 16:46:48 INFO     rasa.nlu.model  - Finished training component.
2020-09-17 16:46:48 INFO     rasa.nlu.model  - Starting to train component CountVectorsFeaturizer
2020-09-17 16:46:48 INFO     rasa.nlu.model  - Finished training component.
2020-09-17 16:46:48 INFO     rasa.nlu.model  - Starting to train component CountVectorsFeaturizer
2020-09-17 16:46:48 INFO     rasa.nlu.model  - Finished training component.
2020-09-17 16:46:48 INFO     rasa.nlu.model  - Starting to train component DIETClassifier
/home/amy/.conda/envs/yangkm/lib/python3.6/site-packages/rasa/utils/common.py:363: UserWarning: You specified 'DIET' to train entities, but no entities are present in the training data. Skip training of entities.
Epochs:   0%|                                                                                   | 0/100 [00:00<?, ?it/s]/home/amy/.conda/envs/yangkm/lib/python3.6/site-packages/rasa/utils/tensorflow/model_data.py:386: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  final_data[k].append(np.concatenate(np.array(v)))
Epochs: 100%|████████████████████████████████| 100/100 [00:05<00:00, 18.36it/s, t_loss=1.475, i_loss=0.095, i_acc=1.000]
2020-09-17 16:46:58 INFO     rasa.utils.tensorflow.models  - Finished training.
2020-09-17 16:46:59 INFO     rasa.nlu.model  - Finished training component.
2020-09-17 16:46:59 INFO     rasa.nlu.model  - Starting to train component EntitySynonymMapper
2020-09-17 16:46:59 INFO     rasa.nlu.model  - Finished training component.
2020-09-17 16:46:59 INFO     rasa.nlu.model  - Starting to train component ResponseSelector
2020-09-17 16:46:59 INFO     rasa.nlu.selectors.response_selector  - Retrieval intent parameter was left to its default value. This response selector will be trained on training examples combining all retrieval intents.
2020-09-17 16:46:59 INFO     rasa.nlu.model  - Finished training component.
2020-09-17 16:46:59 INFO     rasa.nlu.model  - Successfully saved model into '/tmp/tmpjkpkgun2/nlu'
NLU model training completed.
Your Rasa model is trained and saved at '/web/workspace/yangkm/python_wp/nlu/DSWp/models/20200917-164632.tar.gz'.
If you want to speak to the assistant, run 'rasa shell' at any time inside the project directory.
  • 运行结果

将在该目录下参加以下文件:

文件名称 作用说明
init.py 帮助python查找操作的空文件
actions.py 为你的自定义操作编写代码
config.yml ‘*’ 配置NLU和Core模型
credentials.yml 连接到其他服务的详细信息
data/nlu.md ‘*’ 你的NLU训练数据
data/stories.md ‘*’ 你的故事
config.yml ‘*’ 配置NLU和Core模型
domain.yml ‘*’ 你的助手的域
endpoints.yml 接到fb messenger等通道的详细信息
models/.tar.gz 你的初始模型

注:最重要的文件用“*”标记。你将在本教程中了解所有这些文件。

模型训练

每当我们添加新的NLU或Core数据,或更新域或配置时,我们都需要根据示例故事和NLU数据重新训练一个神经网络。为此,运行下面的命令。该命令将调用Rasa Core和NLU训练函数,并将训练后的模型存储到models/目录中。该命令只会在数据或配置发生更改时自动对不同的模型部件进行重新训练。

  rasa train
  echo "Finished training."

rasa train命令将同时查找NLU和Core数据,并训练一个组合模型。

测试

恭喜你! 🚀 你刚刚建立了一个完全由机器学习驱动的助手。 下一步就是尝试一下!如果你正在本地机器上学习本教程,请运行以下命令与助手对话:

  
  $ rasa shell
  2020-09-17 19:44:19 INFO     root  - Connecting to channel 'cmdline' which was specified by the '--connector' argument. Any other channels will be ignored. To connect to all given channels, omit the '--connector' argument.
  2020-09-17 19:44:19 INFO     root  - Starting Rasa server on http://localhost:5005
  2020-09-17 19:44:26 INFO     root  - Rasa server is up and running.
  Bot loaded. Type a message and press enter (use '/stop' to exit): 
  Your input ->  hello                                                                                                    
  Hey! How are you?
  Your input ->  i'm fine                                                                                                 
  Great, carry on!
  Your input ->  Did that help you                                                                                        
  I am a bot, powered by Rasa.
  Your input ->  are you a human?                                                                                         
  I am a bot, powered by Rasa.
  Your input ->  am I talking to a bot?                                                                                   
  I am a bot, powered by Rasa.
  Your input ->  see you around                                                                                           
  Bye
  Your input ->  of course                                                                                                
  Great, carry on!
  Your input ->  I don't think so                                                                                         
  Bye
  Your input ->  no way                                                                                                   
  Bye
  Your input ->  I am feeling very good                                                                                   
  Great, carry on!
  Your input ->  very bad                                                                                                 
  Here is something to cheer you up:
  Image: https://i.imgur.com/nGF1K8f.jpg
  Did that help you?
  Your input ->  are you a bot?                                                                                           
  I am a bot, powered by Rasa.

Rasa 命令行 备忘录

命令 作用说明
rasa init 使用示例训练数据,操作和配置文件创建新项目
rasa train 使用你的NLU数据和故事训练模型,在./model中保存训练的模型
rasa interactive 启动交互式学习会话,通过聊天创建新的训练数据
rasa shell 加载已训练的模型,并让你在命令行上与助手交谈
rasa run 使用已训练的的模型启动Rasa服务。有关详细信息,请参阅运行服务文档
rasa run actions 使用Rasa SDK启动操作服务
rasa visualize 可视化故事
rasa test 使用你的测试NLU数据和故事测试已训练的Rasa模型
rasa data split nlu 根据指定的百分比执行NLU数据的拆分
rasa data convert nlu 在不同格式之间转换NLU训练数据
rasa x 在本地启动Rasa X
rasa -h 显示所有可用命令

具体介绍,可以查看 Rasa 命令行界面

Rasa 架构

  • Rasa构建的助手如何响应消息的基本步骤:
      1. 收到消息并将其传递给解释器(Interpreter),解释器将其转换为包含原始文本,意图和找到的任何实体的字典。这部分由NLU处理;
      1. 跟踪器(Tracker)是跟踪对话状态的对象。它接收新消息进入的信息;
      1. 策略(Policy)接收跟踪器的当前状态。 该策略选择接下来采取的操作(action)。
      1. 选择的操作由跟踪器记录。
      1. 响应被发送给用户。

参考资料

  1. Rasa 安装
  2. Rasa 聊天机器人中文官方文档|磐创AI
  3. Rasa 学习
  4. rasa_chatbot_cn
  5. 用Rasa NLU构建自己的中文NLU系统
  6. Rasa_NLU_Chi
  7. _rasa_chatbot
  8. rasa 源码分析
Clone this wiki locally