小皮博客 | Xiaopi's Blog

106-【RASA系列教程-1】-简单开始

RASA可以用于构建基于上下文的对话平台。RASA主要有两个模块,RASA-NLU用于做语义理解。RASA-Core用于对话管理。RASA的官方文档在:https://rasa.com/docs/rasa/

安装RASA及RASA X

假设已经安装好了python3(因为本机已经有python2.7版本,所以使用了别名),virtualenv.

virtualenv -p python3 venv
source venv/bin/activate
pip3 install rasa-x --extra-index-url https://pypi.rasa.com/simple

RASA的Helloworld

官方示例:https://rasa.com/docs/rasa/prototype-an-assistant#train-and-talk-to-your-assistant,这里可以直接通过浏览器测试。

创建默认的初始项目

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 '/Users/shengl/0000-hugerfuture/0-code/20-xiaoling/205-brain/2052-rasa-framework'.
...
Your Rasa model is trained and saved at '/Users/shengl/0000-hugerfuture/0-code/20-xiaoling/205-brain/2052-rasa-framework/models/20200926-023658.tar.gz'.
If you want to speak to the assistant, run **'rasa shell'** at any time inside the project directory.
  • 如描述所说,可以使用“rasa shell”来运行对话管理。

这些命令将创建以下文件

-rw-r--r--  1 shengl  staff     0  9  3 22:33 __init__.py
drwxr-xr-x  4 shengl  staff   128  9 26 02:36 __pycache__
-rw-r--r--  1 shengl  staff   757  9  3 22:33 actions.py
-rw-r--r--  1 shengl  staff   622  9  3 22:33 config.yml
-rw-r--r--  1 shengl  staff   938  9  3 22:33 credentials.yml
drwxr-xr-x  4 shengl  staff   128  9 26 02:36 data
-rw-r--r--  1 shengl  staff   549  9  3 22:33 domain.yml
-rw-r--r--  1 shengl  staff  1456  9  3 22:33 endpoints.yml
drwxr-xr-x  3 shengl  staff    96  9 26 02:38 models
drwxr-xr-x  3 shengl  staff    96  9 26 02:36 tests

解读

nlu

  • data/nlu.md 用于定义我们的对话管理想要处理的意图。系统默认的有这些:
## intent:greet
- hey
- hello
- hi
- good morning
- good evening
- hey there

## intent:goodbye
- bye
- goodbye
- see you around
- see you later

## intent:affirm
- yes
- indeed
- of course
- that sounds good
- correct
  • RASA NLU会识别其中的intents(意图)

系统配置

vim config.yml # 主要定义了pipline和策略

pipeline:
  - name: WhitespaceTokenizer
  - name: RegexFeaturizer
  - name: LexicalSyntacticFeaturizer
  - name: CountVectorsFeaturizer
  - name: CountVectorsFeaturizer
    analyzer: "char_wb"
    min_ngram: 1
    max_ngram: 4
  - name: DIETClassifier
    epochs: 100
  - name: EntitySynonymMapper
  - name: ResponseSelector
    epochs: 100

对话的流程

  • 对话管理(DM,Dialogue Management)是对话平台的核心调度模块。在RASA中由RASA Core负责。我们需要定义好stories(对话场景的流程,一个story处理一种对话场景)。基本要素包括用户的意图和它需要处理的行动。

vim data/stories.md

## happy path
* greet
  - utter_greet
* mood_great
  - utter_happy

## sad path 1
* greet
  - utter_greet
* mood_unhappy
  - utter_cheer_up
  - utter_did_that_help
* affirm
  - utter_happy

Domain-领域

Domain定义了意图(intent)和动作(action),以及对应的内容模板(templates).

vim domain.yml

ntents:
  - greet
  - goodbye
  - affirm
  - deny
  - mood_great
  - mood_unhappy
  - bot_challenge

responses:
  utter_greet:
  - text: "Hey! How are you?"

  utter_cheer_up:
  - text: "Here is something to cheer you up:"
    image: "https://i.imgur.com/nGF1K8f.jpg"

  utter_did_that_help:
  - text: "Did that help you?"

  utter_happy:
  - text: "Great, carry on!"
  • RASA Core将管理这些流程,并且在对应的场景选择对应的动作。

训练模型

rasa train

Nothing changed. You can use the old model stored at '/Users/shengl/0000-hugerfuture/0-code/20-xiaoling/205-brain/2052-rasa-framework/01-rasa-startup/models/20200926-023658.tar.gz'.

对话

rasa shell

2020-09-26 03:26:31 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-26 03:26:31 INFO     root  - Starting Rasa server on http://localhost:5005
2020-09-26 03:26:41 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 ->  whats your name                                                                                                                                                                              
I am a bot, powered by Rasa.
Your input ->  who am i                                                                                                                                                                                     
I am a bot, powered by Rasa.
Your input ->         

在浏览器中访问服务

中文样例

配置修改

  • 在data/nlu.md, data/stories.md和domain.yml里面增加对中文的支持。

vim data/nlu.md

## intent:greet
- hey
- hello
- hi
- good morning
- good evening
- hey there
- 你好
- 早上好
- 中午好
- 晚上好
- 你好呀

## intent:mood_happy
- great
- very good
- nice
- 很好
- 不错
- 我很好

## intent:mood_unhappy
- sad

vim data/stories.md # 由于这里只涉及流程,我们不准备新增流程,所以中文和英文的流程是一样的,暂时不做修改。

vim domain.yml

...
responses:
  utter_greet:
  - text: 你好,你今天过的如何?
  utter_happy:
  - text: 那很不错
  utter_unhappy:
  - text: 发生什么事情啦,可以和我说说嘛?
actions:
...

训练及运行

rasa train
rasa shell nlu
rasa shell

2020-09-26 03:37:03 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-26 03:37:03 INFO     root  - Starting Rasa server on http://localhost:5005
2020-09-26 03:37:13 INFO     root  - Rasa server is up and running.
Bot loaded. Type a message and press enter (use '/stop' to exit): 
Your input ->  hello                                                                                                                                                                                        
你好,你今天过的如何?
Your input ->  你好                                                                                                                                                                                         
你好,你今天过的如何?
Your input ->  还可以                                                                                                                                                                                       
你好,你今天过的如何?
Your input ->  不错                                                                                                                                                                                         
那很不错
Your input ->  晚上好                                                                                                                                                                                       
你好,你今天过的如何?
Your input ->  不好                                                                                                                                                                                         
发生什么事情啦,可以和我说说嘛?
Your input ->  你懂个啥                                                                                                                                                                                     
你好,你今天过的如何?
Your input ->    
  • 最终效果如下:

最终效果

版权声明

本文标题:106-【RASA系列教程-1】-简单开始

文章作者:盛领

发布时间:2020年09月25日 - 16:35:10

原始链接:http://blog.xiaoyuyu.net/post/51ca0d3.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

如您有任何商业合作或者授权方面的协商,请给我留言:sunsetxiao@126.com

盛领 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!