该文是Rasa官网文档的一个简单翻译,用于平常查看。html
官网文档地址:Rasa文档python
推荐经过pip
命令安装Rasagit
pip install rasa-x --extra-index-url https://pypi.rasa.com/simple
复制代码
该命令会安装Rasa
和Rasa X
。若是不想使用Rasa X
,使用pip install rasa
替换便可。 除非你已经安装了numpy和scipy,不然咱们强烈建议你安装和使用Anaconda
。github
若是您想使用Rasa的开发版本,能够从GitHub获取:web
git clone https://github.com/RasaHQ/rasa.git
cd rasa
pip install -r requirements.txt
pip install -e .
复制代码
Windows先决条件算法
确保安装了Microsoft VC ++编译器,所以python能够编译任何依赖项。您能够从Visual Studio获取编译器。下载安装程序,而后在列表中选择VC ++ Build工具。docker
NLU Pipeline Dependenciesshell
Rasa NLU具备用于识别意图和实体的不一样组件,其中大多数具备一些额外的依赖性。json
当您训练NLU模型时,Rasa将检查是否安装了全部必需的依赖项,并告诉您是否缺乏任何依赖项。选择管道的页面将帮助您选择要使用的管道。后端
若是你想确保为你可能须要的任何组件安装了依赖项,而且你不介意附加的依赖项,你可使用以下命令安装全部依赖项。
pip install -r alt_requirements/requirements_full.txt 复制代码
入门:使用spaCy
预训练向量
pretrained_embeddings_spacy
管道结合了一些不一样的库,是一种流行的选择。有关更多信息,请查看spaCy文档。 能够经过下面的命令安装:
pip install rasa[spacy]
python -m spacy download en_core_web_md
python -m spacy link en_core_web_md en
复制代码
该命令会安装Rasa NLU以及spacy和它的英语语言模型。咱们建议使用最新的“medium”大小的模型(_md),代替spacy默认的最小的en_core_web_sm
模型。小的模型须要更少的内存来运行,但有时会下降意图分类的性能。
第一种选择:Tensorflow
为了使用supervised_embeddings
管道,须要安装tensorflow
,为了实现实体识别须要安装sklearn-crfsuite
库。运行下面的命令:
pip install rasa
复制代码
第二种选择:MITIE
后端使用MITIE
对于小型数据集更适用,若是你有数百个例子,训练时间可能会比较长。将来咱们可能会弃用MITIE。 首先,运行:
pip install git+https://github.com/mit-nlp/MITIE.git
pip install rasa[mitie]
复制代码
下载MITIE模型文件total_word_feature_extractor.dat
。
MITIE的完整pipeline:
language: "en"
pipeline:
- name: "MitieNLP"
model: "data/total_word_feature_extractor.dat"
- name: "MitieTokenizer"
- name: "MitieEntityExtractor"
- name: "EntitySynonymMapper"
- name: "RegexFeaturizer"
- name: "MitieFeaturizer"
- name: "SklearnIntentClassifier"
复制代码
单独使用MITIE训练可能会很慢,但你可使用以下配置:
language: "en"
pipeline:
- name: "MitieNLP"
model: "data/total_word_feature_extractor.dat"
- name: "MitieTokenizer"
- name: "MitieEntityExtractor"
- name: "EntitySynonymMapper"
- name: "RegexFeaturizer"
- name: "MitieIntentClassifier"
复制代码
本页介绍了使用Rasa构建助手的基础知识,并显示了Rasa项目的结构。你能够在这里测试它而不须要安装任何东西。您还能够安装Rasa并在命令行中进行操做。
在这个教程中,你将创建一个简单、友好的助手,它会问你在作什么并发送给你一个有趣的图片让你振做起来。
1. 建立一个新项目
第一步是建立一个新的Rasa项目,运行:
rasa init --no-prompt
复制代码
rasa init
命令建立Rasa项目所需的全部文件,并在一些示例数据上训练一个简单的bot。若是省略--no-prompt
标志,将会询问有关您但愿如何设置项目的一些问题。 将建立如下文件:
文件 | 含义 |
---|---|
__init__py |
一个空文件,帮助python找到actions |
actions.py |
自定义actions的代码 |
config.yml ‘*’ |
NLU和Core模型的配置 |
credentials.yml |
链接其余服务的细节 |
data/nlu.md ‘*’ |
NLU训练数据 |
data/stories.md ‘*’ |
stories |
domain.yml ‘*’ |
助手的domain |
endpoints.yml |
链接通道的细节 |
model/<timestamp>.tar.gz |
初始化模型 |
标‘*’
的是重要的文件。
2. 查看训练数据
Rasa助手的第一部分是NLU模型。NLU表明天然语言理解,这意味着将用户消息转换为结构化数据。要使用Rasa执行此操做,须要提供训练示例,以说明Rasa应如何理解用户消息,而后经过向其展现这些示例来训练模型。
## intent:greet
- hey
- hello
- hi
- good morning
- good evening
- hey there
## intent:goodbye
- bye
- goodbye
- see you around
- see you later
复制代码
以##
开头的行定义了意图的名称,这些意图是具备相同含义的消息组。当您的用户向助理发送新的,看不见的消息时,Rasa的工做将是预测正确的意图。您能够在“训练数据格式”中找到数据格式的全部详细信息。
3. 定义你的模型配置
配置文件定义模型将使用的NLU和Core组件。在此示例中,您的NLU模型将使用supervised_embeddings管道。您能够在此处了解不一样的NLU管道。 咱们来看看你的模型配置文件:
cat config.yml
配置以下:
# Configuration for Rasa NLU.
# https://rasa.com/docs/rasa/nlu/components/
language: en
pipeline: supervised_embeddings
# Configuration for Rasa Core.
# https://rasa.com/docs/rasa/core/policies/
policies:
- name: MemoizationPolicy
- name: KerasPolicy
- name: MappingPolicy
复制代码
关键字language
和pipeline
指定了NLU模型如何创建。policy
定义了Core模型使用的决策信息。
4. 写你的第一个stories
在此阶段,您将教您的助手如何回复您的消息,这称为对话管理,由您的Core模型处理。 Core模型以训练“故事”的形式从真实的会话数据中学习。故事是用户和助手之间的真实对话。具备意图和实体的行反映了用户的输入,操做名称显示了助手应该作出的响应。
如下是简单对话的示例。用户打招呼,助理回答问好。这就是它看起来像一个故事:
## story1
* greet
- utter_greet
复制代码
以 -
开头的行是助手采起的行动。在本教程中,咱们的全部操做都是发送回用户的消息,例如utter_greet
,但一般,操做能够执行任何操做,包括调用API和与外部世界交互。
运行下面的命令行查看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
复制代码
5. 定义domain
domain定义了您的助手所处的领域:它应该得到的用户输入,应该可以预测的操做,如何响应以及要存储的信息。咱们助手的domain保存在名为domain.yml
的文件中:
intents:
- greet
- goodbye
- affirm
- deny
- mood_great
- mood_unhappy
actions:
- utter_greet
- utter_cheer_up
- utter_did_that_help
- utter_happy
- utter_goodbye
templates:
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?"
复制代码
名称 | 含义 |
---|---|
intents | things you expect users to say |
actions | things your assistant can do and say |
templates | template strings for the things your assistant can say |
这如何组合在一块儿?Rasa Core的工做是选择在对话的每一个步骤执行的正确操做。 在这种状况下,咱们的操做只是向用户发送消息,这些简单的话语动做是域中以utter_
开头的动做。助理将根据模板部分的模板回复消息。请参阅自定义操做以构建不只仅是发送消息的操做。
6. 训练模型
不管什么时候咱们添加新的NLU或Core数据,或更新domain或配置,咱们都须要在咱们的示例stories和NLU数据上从新训练神经网络。为此,请运行如下命令。此命令将调用Rasa Core和NLU训练功能,并将训练好的模型存储到模型/目录中。若是数据或配置发生变化,该命令将自动仅从新训练不一样的模型部件。
rasa train
复制代码
rasa train
命令将查找NLU和Core数据,并将训练组合模型。
7. 和你的助手交谈
恭喜!🚀你刚刚创建了一个彻底由机器学习驱动的助手。下一步就是尝试一下!若是您在本地计算机上关注本教程,请经过运行如下方式与助理通话:
rasa shell
复制代码
您还可使用Rasa X收集更多对话并改进助手。
命令行接口(CLI)为您提供易于记忆的常见任务命令。
命令 | 做用 |
---|---|
rasa_init | 建立一个新的项目,包含示例训练数据,动做和配置文件 |
rasa_train | 使用NLU数据和stories训练模型,保存模型在./models 中 |
rasa interactive | 经过交谈开启一个新的交互学习会话来建立新的训练数据 |
rasa shell | 加载训练模型,与助手经过命令行交谈 |
rasa run | 使用训练的模型开启一个Rasa服务 |
rasa run actions | 使用Rasa SDK开启action服务器 |
rasa visualize | 可视化stories |
rasa test | 使用测试NLU数据和故事来测试训练好的Rasa模型 |
rasa data split nlu | 根据指定的百分比执行NLU数据的拆分 |
rasa data convert nlu | 在不一样格式之间转换NLU训练数据 |
rasa x | 在本地启动Rasa X |
rasa -h | 显示全部可用命令 |
建立新项目
单个命令为您设置一个完整的项目,其中包含一些示例训练数据。
rasa init
复制代码
包含如下文件:
.
├── __init__.py
├── actions.py
├── config.yml
├── credentials.yml
├── data
│ ├── nlu.md
│ └── stories.md
├── domain.yml
├── endpoints.yml
└── models
└── <timestamp>.tar.gz
复制代码
rasa init
命令会询问你是否想要使用数据训练一个初始模型,若是回答no,model目录将为空。
训练模型
命令:
rasa train
复制代码
该命令训练Rasa模型,该模型结合了Rasa NLU和Rasa Core模型。若是您只想训练NLU或Core模型,您能够运行rasa train nlu
或rasa train core
。可是,若是训练数据和配置没有改变,Rasa将自动跳过训练Core或NLU。
rasa trian
将存储训练好的模型到--out
指定的目标下。模型的名字默认是<timestamp>.tar.gz
。若是想要本身命名,能够经过--fixed-model-name
来指定。
如下参数可用于配置训练过程:
usage: rasa train [-h] [-v] [-vv] [--quiet] [--data DATA [DATA ...]]
[-c CONFIG] [-d DOMAIN] [--out OUT]
[--augmentation AUGMENTATION] [--debug-plots]
[--dump-stories] [--fixed-model-name FIXED_MODEL_NAME]
[--force]
{core,nlu} ...
positional arguments:
{core,nlu}
core Trains a Rasa Core model using your stories.
nlu Trains a Rasa NLU model using your NLU data.
optional arguments:
-h, --help show this help message and exit
--data DATA [DATA ...]
Paths to the Core and NLU data files. (default:
['data'])
-c CONFIG, --config CONFIG
The policy and NLU pipeline configuration of your bot.
(default: config.yml)
-d DOMAIN, --domain DOMAIN
Domain specification (yml file). (default: domain.yml)
--out OUT Directory where your models should be stored.
(default: models)
--augmentation AUGMENTATION
How much data augmentation to use during training.
(default: 50)
--debug-plots If enabled, will create plots showing checkpoints and
their connections between story blocks in a file
called `story_blocks_connections.html`. (default:
False)
--dump-stories If enabled, save flattened stories to a file.
(default: False)
--fixed-model-name FIXED_MODEL_NAME
If set, the name of the model file/directory will be
set to the given name. (default: None)
--force Force a model training even if the data has not
changed. (default: False)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
复制代码
使用
rasa train
命令训练模型时,确保Core和NLU的训练数据存在。若是仅存在一种模型类型的训练数据,则该命令将根据提供的训练文件自动回退到rasa train nlu
或rasa train core
。
交互式学习
要与助理开始交互式学习会话,请运行:
rasa interactive
复制代码
若是使用--model
参数指定训练模型,则使用提供的模型启动交互式学习过程。若是没有指定模型,rasa interactive
将训练一个新的Rasa模型,若是没有其余目录传递给--data
标志,则其数据位于data /
中。在训练初始模型以后,交互式学习会话开始。若是训练数据和配置没有改变,将跳过训练。
rasa interactive
全部参数列表以下:
usage: rasa interactive [-h] [-v] [-vv] [--quiet] [-m MODEL]
[--data DATA [DATA ...]] [--skip-visualization]
[--endpoints ENDPOINTS] [-c CONFIG] [-d DOMAIN]
[--out OUT] [--augmentation AUGMENTATION]
[--debug-plots] [--dump-stories] [--force]
{core} ... [model-as-positional-argument]
positional arguments:
{core}
core Starts an interactive learning session model to create
new training data for a Rasa Core model by chatting.
Uses the 'RegexInterpreter', i.e. `/<intent>` input
format.
model-as-positional-argument
Path to a trained Rasa model. If a directory is
specified, it will use the latest model in this
directory. (default: None)
optional arguments:
-h, --help show this help message and exit
-m MODEL, --model MODEL
Path to a trained Rasa model. If a directory is
specified, it will use the latest model in this
directory. (default: None)
--data DATA [DATA ...]
Paths to the Core and NLU data files. (default:
['data'])
--skip-visualization Disable plotting the visualization during interactive
learning. (default: False)
--endpoints ENDPOINTS
Configuration file for the model server and the
connectors as a yml file. (default: None)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
Train Arguments:
-c CONFIG, --config CONFIG
The policy and NLU pipeline configuration of your bot.
(default: config.yml)
-d DOMAIN, --domain DOMAIN
Domain specification (yml file). (default: domain.yml)
--out OUT Directory where your models should be stored.
(default: models)
--augmentation AUGMENTATION
How much data augmentation to use during training.
(default: 50)
--debug-plots If enabled, will create plots showing checkpoints and
their connections between story blocks in a file
called `story_blocks_connections.html`. (default:
False)
--dump-stories If enabled, save flattened stories to a file.
(default: False)
--force Force a model training even if the data has not
changed. (default: False)
复制代码
与你的助手交谈
使用以下命令:
rasa shell
复制代码
应该用于与机器人交互的模型能够由--model
指定。若是使用仅NLU模型启动shell,则rasa shell
容许您获取在命令行上键入的任何文本的intent和实体。若是您的模型包含通过训练的Core模型,您能够与机器人聊天,并查看机器人预测的下一步操做。若是您已经训练了一个组合的Rasa模型,可是想要查看模型从文本中提取的意图和实体,您可使用命令rasa shell nlu
。
要增长调试的日志记录级别,请运行:
rasa shell --debug
复制代码
rasa shell
的全部选项列表以下:
usage: rasa shell [-h] [-v] [-vv] [--quiet] [-m MODEL] [--log-file LOG_FILE]
[--endpoints ENDPOINTS] [-p PORT] [-t AUTH_TOKEN]
[--cors [CORS [CORS ...]]] [--enable-api]
[--remote-storage REMOTE_STORAGE]
[--credentials CREDENTIALS] [--connector CONNECTOR]
[--jwt-secret JWT_SECRET] [--jwt-method JWT_METHOD]
{nlu} ... [model-as-positional-argument]
positional arguments:
{nlu}
nlu Interprets messages on the command line using your NLU
model.
model-as-positional-argument
Path to a trained Rasa model. If a directory is
specified, it will use the latest model in this
directory. (default: None)
optional arguments:
-h, --help show this help message and exit
-m MODEL, --model MODEL
Path to a trained Rasa model. If a directory is
specified, it will use the latest model in this
directory. (default: models)
--log-file LOG_FILE Store logs in specified file. (default: None)
--endpoints ENDPOINTS
Configuration file for the model server and the
connectors as a yml file. (default: None)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
Server Settings:
-p PORT, --port PORT Port to run the server at. (default: 5005)
-t AUTH_TOKEN, --auth-token AUTH_TOKEN
Enable token based authentication. Requests need to
provide the token to be accepted. (default: None)
--cors [CORS [CORS ...]]
Enable CORS for the passed origin. Use * to whitelist
all origins. (default: None)
--enable-api Start the web server API in addition to the input
channel. (default: False)
--remote-storage REMOTE_STORAGE
Set the remote location where your Rasa model is
stored, e.g. on AWS. (default: None)
Channels:
--credentials CREDENTIALS
Authentication credentials for the connector as a yml
file. (default: None)
--connector CONNECTOR
Service to connect to. (default: None)
JWT Authentication:
--jwt-secret JWT_SECRET
Public key for asymmetric JWT methods or shared
secretfor symmetric methods. Please also make sure to
use --jwt-method to select the method of the
signature, otherwise this argument will be ignored.
(default: None)
--jwt-method JWT_METHOD
Method used for the signature of the JWT
authentication payload. (default: HS256)
复制代码
启动服务器
要启动运行Rasa模型的服务器,请运行:
rasa run
复制代码
下面的参数能够用来配置你的Rasa服务器
usage: rasa run [-h] [-v] [-vv] [--quiet] [-m MODEL] [--log-file LOG_FILE]
[--endpoints ENDPOINTS] [-p PORT] [-t AUTH_TOKEN]
[--cors [CORS [CORS ...]]] [--enable-api]
[--remote-storage REMOTE_STORAGE] [--credentials CREDENTIALS]
[--connector CONNECTOR] [--jwt-secret JWT_SECRET]
[--jwt-method JWT_METHOD]
{actions} ... [model-as-positional-argument]
positional arguments:
{actions}
actions Runs the action server.
model-as-positional-argument
Path to a trained Rasa model. If a directory is
specified, it will use the latest model in this
directory. (default: None)
optional arguments:
-h, --help show this help message and exit
-m MODEL, --model MODEL
Path to a trained Rasa model. If a directory is
specified, it will use the latest model in this
directory. (default: models)
--log-file LOG_FILE Store logs in specified file. (default: None)
--endpoints ENDPOINTS
Configuration file for the model server and the
connectors as a yml file. (default: None)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
Server Settings:
-p PORT, --port PORT Port to run the server at. (default: 5005)
-t AUTH_TOKEN, --auth-token AUTH_TOKEN
Enable token based authentication. Requests need to
provide the token to be accepted. (default: None)
--cors [CORS [CORS ...]]
Enable CORS for the passed origin. Use * to whitelist
all origins. (default: None)
--enable-api Start the web server API in addition to the input
channel. (default: False)
--remote-storage REMOTE_STORAGE
Set the remote location where your Rasa model is
stored, e.g. on AWS. (default: None)
Channels:
--credentials CREDENTIALS
Authentication credentials for the connector as a yml
file. (default: None)
--connector CONNECTOR
Service to connect to. (default: None)
JWT Authentication:
--jwt-secret JWT_SECRET
Public key for asymmetric JWT methods or shared
secretfor symmetric methods. Please also make sure to
use --jwt-method to select the method of the
signature, otherwise this argument will be ignored.
(default: None)
--jwt-method JWT_METHOD
Method used for the signature of the JWT
authentication payload. (default: HS256)
复制代码
开启Action服务器
使用以下命令运行:
rasa run actions
复制代码
下面的参数可用于调整服务器设置:
usage: rasa run actions [-h] [-v] [-vv] [--quiet] [-p PORT]
[--cors [CORS [CORS ...]]] [--actions ACTIONS]
optional arguments:
-h, --help show this help message and exit
-p PORT, --port PORT port to run the server at (default: 5055)
--cors [CORS [CORS ...]]
enable CORS for the passed origin. Use * to whitelist
all origins (default: None)
--actions ACTIONS name of action package to be loaded (default: None)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
复制代码
可视化你的stories
rasa visualize
复制代码
一般,训练的stories位于data
目录下是可视的。如何你的stories存储在其余地方,使用--stories
指定他们的位置。
参数以下:
usage: rasa visualize [-h] [-v] [-vv] [--quiet] [-d DOMAIN] [-s STORIES]
[-c CONFIG] [--out OUT] [--max-history MAX_HISTORY]
[-u NLU]
optional arguments:
-h, --help show this help message and exit
-d DOMAIN, --domain DOMAIN
Domain specification (yml file). (default: domain.yml)
-s STORIES, --stories STORIES
File or folder containing your training stories.
(default: data)
-c CONFIG, --config CONFIG
The policy and NLU pipeline configuration of your bot.
(default: config.yml)
--out OUT Filename of the output path, e.g. 'graph.html'.
(default: graph.html)
--max-history MAX_HISTORY
Max history to consider when merging paths in the
output graph. (default: 2)
-u NLU, --nlu NLU File or folder containing your NLU data, used to
insert example messages into the graph. (default:
None)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
复制代码
会生成一个graph.html
文件在当前路径下。
在测试数据上验证模型
运行:
rasa test
复制代码
使用--model
指定模型。 参数以下:
usage: rasa test [-h] [-v] [-vv] [--quiet] [-m MODEL] [-s STORIES]
[--max-stories MAX_STORIES] [--e2e] [--endpoints ENDPOINTS]
[--fail-on-prediction-errors] [--url URL]
[--evaluate-model-directory] [-u NLU] [--out OUT]
[--report [REPORT]] [--successes [SUCCESSES]]
[--errors ERRORS] [--histogram HISTOGRAM] [--confmat CONFMAT]
[-c CONFIG [CONFIG ...]] [--cross-validation] [-f FOLDS]
[-r RUNS] [-p PERCENTAGES [PERCENTAGES ...]]
{core,nlu} ...
positional arguments:
{core,nlu}
core Tests Rasa Core models using your test stories.
nlu Tests Rasa NLU models using your test NLU data.
optional arguments:
-h, --help show this help message and exit
-m MODEL, --model MODEL
Path to a trained Rasa model. If a directory is
specified, it will use the latest model in this
directory. (default: models)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
Core Test Arguments:
-s STORIES, --stories STORIES
File or folder containing your test stories. (default:
data)
--max-stories MAX_STORIES
Maximum number of stories to test on. (default: None)
--e2e, --end-to-end Run an end-to-end evaluation for combined action and
intent prediction. Requires a story file in end-to-end
format. (default: False)
--endpoints ENDPOINTS
Configuration file for the connectors as a yml file.
(default: None)
--fail-on-prediction-errors
If a prediction error is encountered, an exception is
thrown. This can be used to validate stories during
tests, e.g. on travis. (default: False)
--url URL If supplied, downloads a story file from a URL and
trains on it. Fetches the data by sending a GET
request to the supplied URL. (default: None)
--evaluate-model-directory
Should be set to evaluate models trained via 'rasa train core --config <config-1> <config-2>'. All models
in the provided directory are evaluated and compared
against each other. (default: False)
NLU Test Arguments:
-u NLU, --nlu NLU File or folder containing your NLU data. (default:
data)
--out OUT Output path for any files created during the
evaluation. (default: results)
--report [REPORT] Output path to save the intent/entity metrics report.
(default: None)
--successes [SUCCESSES]
Output path to save successful predictions. (default:
None)
--errors ERRORS Output path to save model errors. (default:
errors.json)
--histogram HISTOGRAM
Output path for the confidence histogram. (default:
hist.png)
--confmat CONFMAT Output path for the confusion matrix plot. (default:
confmat.png)
-c CONFIG [CONFIG ...], --config CONFIG [CONFIG ...]
Model configuration file. If a single file is passed
and cross validation mode is chosen, cross-validation
is performed, if multiple configs or a folder of
configs are passed, models will be trained and
compared directly. (default: None)
复制代码
拆分Train Test训练数据 要进行NLU数据的拆分,请运行:
rasa data split nlu
复制代码
你能够指定训练数据,输出目录等,参数以下:
usage: rasa data split nlu [-h] [-v] [-vv] [--quiet] [-u NLU]
[--training-fraction TRAINING_FRACTION] [--out OUT]
optional arguments:
-h, --help show this help message and exit
-u NLU, --nlu NLU File or folder containing your NLU data. (default:
data)
--training-fraction TRAINING_FRACTION
Percentage of the data which should be in the training
data. (default: 0.8)
--out OUT Directory where the split files should be stored.
(default: train_test_split)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
复制代码
此命令将尝试在训练和测试中保持意图的比例相同。
在Markdown和JSON之间转换数据
要将NLU数据从LUIS数据格式,WIT数据格式,Dialogflow数据格式,JSON或Markdown转换为JSON或Markdown,请运行:
rasa data convert nlu
复制代码
能够指定输入文件,输出文件,输出格式等,参数以下:
usage: rasa data convert nlu [-h] [-v] [-vv] [--quiet] --data DATA --out OUT
[-l LANGUAGE] -f {json,md}
optional arguments:
-h, --help show this help message and exit
--data DATA Path to the file or directory containing Rasa NLU
data. (default: None)
--out OUT File where to save training data in Rasa format.
(default: None)
-l LANGUAGE, --language LANGUAGE
Language of data. (default: en)
-f {json,md}, --format {json,md}
Output format the training data should be converted
into. (default: None)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
复制代码
启动Rasa X
Rasa X是一个工具,可帮助您构建\改进和部署由Rasa框架提供支持的AI Assistants。您能够在此处找到有关它的更多信息。
您能够经过执行来本地启动Rasa X:
rasa x
复制代码
为了可以启动Rasa X,您须要安装Rasa X,您须要进入Rasa项目。
默认状况下,
Rasa X
在端口5002
上运行。使用参数--rasa-x-port
能够将其更改成任何其余端口。
如下参数可用于rasa x:
usage: rasa x [-h] [-v] [-vv] [--quiet] [-m MODEL] [--data DATA] [--no-prompt]
[--production] [--rasa-x-port RASA_X_PORT] [--log-file LOG_FILE]
[--endpoints ENDPOINTS] [-p PORT] [-t AUTH_TOKEN]
[--cors [CORS [CORS ...]]] [--enable-api]
[--remote-storage REMOTE_STORAGE] [--credentials CREDENTIALS]
[--connector CONNECTOR] [--jwt-secret JWT_SECRET]
[--jwt-method JWT_METHOD]
optional arguments:
-h, --help show this help message and exit
-m MODEL, --model MODEL
Path to a trained Rasa model. If a directory is
specified, it will use the latest model in this
directory. (default: models)
--data DATA Path to the file or directory containing stories and
Rasa NLU data. (default: data)
--no-prompt Automatic yes or default options to prompts and
oppressed warnings. (default: False)
--production Run Rasa X in a production environment. (default:
False)
--rasa-x-port RASA_X_PORT
Port to run the Rasa X server at. (default: 5002)
--log-file LOG_FILE Store logs in specified file. (default: None)
--endpoints ENDPOINTS
Configuration file for the model server and the
connectors as a yml file. (default: None)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
Server Settings:
-p PORT, --port PORT Port to run the server at. (default: 5005)
-t AUTH_TOKEN, --auth-token AUTH_TOKEN
Enable token based authentication. Requests need to
provide the token to be accepted. (default: None)
--cors [CORS [CORS ...]]
Enable CORS for the passed origin. Use * to whitelist
all origins. (default: None)
--enable-api Start the web server API in addition to the input
channel. (default: False)
--remote-storage REMOTE_STORAGE
Set the remote location where your Rasa model is
stored, e.g. on AWS. (default: None)
Channels:
--credentials CREDENTIALS
Authentication credentials for the connector as a yml
file. (default: None)
--connector CONNECTOR
Service to connect to. (default: None)
JWT Authentication:
--jwt-secret JWT_SECRET
Public key for asymmetric JWT methods or shared
secretfor symmetric methods. Please also make sure to
use --jwt-method to select the method of the
signature, otherwise this argument will be ignored.
(default: None)
--jwt-method JWT_METHOD
Method used for the signature of the JWT
authentication payload. (default: HS256)
复制代码
消息处理
此图显示了使用Rasa构建的助手如何响应消息的基本步骤:
步骤以下:
Tracker
是跟踪对话状态的对象。它接收新消息进入的信息。Tracker
的当前状态。Tracker
记录。消息能够是人类输入的文本,也能够是按钮按下等结构化输入。
若是你是在本地计算机(不是服务器)上进行测试,你须要使用ngrok。这会给你的机器一个domain名字让Facebook, Slack等
知道消息发送到哪里来到达你的本地机器。
为了让你的助手在消息传递平台上可用,您须要在credentials.yml
文件中提供凭据。运行rasa init
时会建立一个示例文件,所以最简单的方法是编辑该文件并在其中添加凭据 。如下是Facebook凭据的示例:
facebook:
verify: "rasa-bot"
secret: "3e34709d01ea89032asdebfe5a74518"
page-access-token: "EAAbHPa7H9rEBAAuFk4Q3gPKbDedQnx4djJJ1JmQ7CAqO4iJKrQcNT0wtD"
复制代码
评估NLU模型
使用以下命令分离NLU的训练和测试数据:
rasa data split nlu
复制代码
使用NLU模型在测试用例上预测:
rasa test nlu -u test_set.md --model models/nlu-xxx.tar.gz
复制代码
若是您不想建立单独的测试集,仍可使用交叉验证来估计模型的优化程度。为此,请添加标志--cross-validation
:
rasa test nlu -u data/nlu.md --config config.yml --cross-validation
复制代码
该脚本的完整选项列表以下:
usage: rasa test nlu [-h] [-v] [-vv] [--quiet] [-m MODEL] [-u NLU] [--out OUT]
[--report [REPORT]] [--successes [SUCCESSES]]
[--errors ERRORS] [--histogram HISTOGRAM]
[--confmat CONFMAT] [-c CONFIG [CONFIG ...]]
[--cross-validation] [-f FOLDS] [-r RUNS]
[-p PERCENTAGES [PERCENTAGES ...]]
optional arguments:
-h, --help show this help message and exit
-m MODEL, --model MODEL
Path to a trained Rasa model. If a directory is
specified, it will use the latest model in this
directory. (default: models)
-u NLU, --nlu NLU File or folder containing your NLU data. (default:
data)
--out OUT Output path for any files created during the
evaluation. (default: results)
--report [REPORT] Output path to save the intent/entity metrics report.
(default: None)
--successes [SUCCESSES]
Output path to save successful predictions. (default:
None)
--errors ERRORS Output path to save model errors. (default:
errors.json)
--histogram HISTOGRAM
Output path for the confidence histogram. (default:
hist.png)
--confmat CONFMAT Output path for the confusion matrix plot. (default:
confmat.png)
-c CONFIG [CONFIG ...], --config CONFIG [CONFIG ...]
Model configuration file. If a single file is passed
and cross validation mode is chosen, cross-validation
is performed, if multiple configs or a folder of
configs are passed, models will be trained and
compared directly. (default: None)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
Cross Validation:
--cross-validation Switch on cross validation mode. Any provided model
will be ignored. (default: False)
-f FOLDS, --folds FOLDS
Number of cross validation folds (cross validation
only). (default: 10)
Comparison Mode:
-r RUNS, --runs RUNS Number of comparison runs to make. (default: 3)
-p PERCENTAGES [PERCENTAGES ...], --percentages PERCENTAGES [PERCENTAGES ...]
Percentages of training data to exclude during
comparison. (default: [0, 25, 50, 75])
复制代码
比较NLU管道 经过将多个管道配置(或包含它们的文件夹)传递给CLI,Rasa将在管道之间进行比较检查。
rasa test nlu --config pretrained_embeddings_spacy.yml supervised_embeddings.yml
--nlu data/nlu.md --runs 3 --percentages 0 25 50 70 90
复制代码
上例中的命令将根据数据建立训练/测试集,而后屡次训练每一个管道,其中0,25,50,70和90%的意图数据从训练集中排除。而后在测试集上评估模型,并记录每一个排除百分比的f1-scores。该过程运行三次(即总共有3个测试集),而后使用f1-scores的平均值和标准误差绘制图表。 f1-score图表 - 以及全部训练/测试集,训练模型,分类和错误报告将保存到名为nlu_comparison_results
的文件夹中。
意图分类
评估脚本将为您的模型生成报告,混淆矩阵和置信度直方图。
该报告记录每一个意图和实体的精确度,召回率和f1度量,并提供整体平均值。您可使用--report
参数将这些报告另存为JSON文件。
混淆矩阵向您显示哪些意图被误认为是其余意图;任何错误预测的样本都会被记录并保存到名为errors.json
的文件中,以便于调试。
脚本生成的直方图容许您可视化全部预测的置信度分布,其中正确和错误预测的量分别由蓝色和红色条显示。提升训练数据的质量会使蓝色直方图条向右移动,红色直方图条移动到图的左侧。
只有在评估测试集上的模型时,才会建立混淆矩阵。在交叉验证模式下,将不会生成混淆矩阵。
实体识别
CRFEntityExtractor
是您使用本身的数据训练的惟一实体提取器,所以是惟一将被评估的实体提取器。若是您使用spaCy
或duckling
预训练实体提取器,Rasa NLU将不会在评估中包含这些。
Rasa NLU将报告CRFEntityExtractor
通过培训识别的每种实体类型的召回,精确度和f1度量。
实体得分
为了评估实体提取,咱们应用一种简单的基于标签的方法。咱们不考虑BILOU
标记,而只考虑每一个标记的实体类型标记。对于像“亚历山大广场附近”这样的位置实体,咱们指望标签LOC LOC而不是基于BILOU的B-LOC L-LOC。咱们的方法在评估时更宽松,由于它奖励部分提取而且不惩罚实体的分裂。
评估Core模型
rasa test core --stories test_stories.md --out results
复制代码
将失败的stories输出在results/failed_stories.md
中。若是至少有一个动做被错误预测,咱们会将任何故事视为失败。
此外,这会将混淆矩阵保存到名为results / story_confmat.pdf
的文件中。对于您domian的每一个操做,混淆矩阵会显示操做的正确预测频率以及预测错误操做的频率。
脚本的全部选项列表以下:
usage: rasa test core [-h] [-v] [-vv] [--quiet] [-m MODEL [MODEL ...]]
[-s STORIES] [--max-stories MAX_STORIES] [--out OUT]
[--e2e] [--endpoints ENDPOINTS]
[--fail-on-prediction-errors] [--url URL]
[--evaluate-model-directory]
optional arguments:
-h, --help show this help message and exit
-m MODEL [MODEL ...], --model MODEL [MODEL ...]
Path to a pre-trained model. If it is a 'tar.gz' file
that model file will be used. If it is a directory,
the latest model in that directory will be used
(exception: '--evaluate-model-directory' flag is set).
If multiple 'tar.gz' files are provided, all those
models will be compared. (default: [None])
-s STORIES, --stories STORIES
File or folder containing your test stories. (default:
data)
--max-stories MAX_STORIES
Maximum number of stories to test on. (default: None)
--out OUT Output path for any files created during the
evaluation. (default: results)
--e2e, --end-to-end Run an end-to-end evaluation for combined action and
intent prediction. Requires a story file in end-to-end
format. (default: False)
--endpoints ENDPOINTS
Configuration file for the connectors as a yml file.
(default: None)
--fail-on-prediction-errors
If a prediction error is encountered, an exception is
thrown. This can be used to validate stories during
tests, e.g. on travis. (default: False)
--url URL If supplied, downloads a story file from a URL and
trains on it. Fetches the data by sending a GET
request to the supplied URL. (default: None)
--evaluate-model-directory
Should be set to evaluate models trained via 'rasa train core --config <config-1> <config-2>'. All models
in the provided directory are evaluated and compared
against each other. (default: False)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
复制代码
比较策略Policy
选择特定的策略配置,或选择特定策略的超参数,您须要衡量Rasa Core将会归纳为之前从未见过的对话的程度。特别是在项目的开始阶段,你没有不少真正的对话来用来训练你的机器人,因此你不仅是想扔掉一些用做测试集。
Rasa Core有一些脚本可帮助您选择和微调策略配置。一旦您满意,您就能够在完整数据集上训练最终配置。要作到这一点,首先必须为不一样的策略训练模型。建立两个(或更多)配置文件,包括要比较的策略(每一个只包含一个策略),而后使用训练脚本的比较模式训练模型:
rasa train core -c config_1.yml config_2.yml \
-d domain.yml -s stories_folder --out comparison_models --runs 3 \
--percentages 0 5 25 50 70 95
复制代码
低于提供的每一个policy配置,Rasa Core将进行屡次培训,将0,5,25,50,70和95%的培训故事排除在培训数据以外。这是为屡次运行完成的,以确保一致的结果。
训练完成后,进行评估:
rasa test core -m comparison_models --stories stories_folder
--out comparison_results --evaluate-model-directory
复制代码
这将评估训练集上的每一个模型,并绘制一些图表以显示哪一个策略表现最佳。经过评估整套故事,您能够衡量Rasa Core对预测故事的预测效果。
若是您不肯定要比较哪些策略,咱们建议您尝试使用EmbeddingPolicy``和KerasPolicy
来查看哪一种策略更适合您。
这个训练过程可能须要很长时间,因此咱们建议让它在后台运行,不能中断。
端到端评估
Rasa容许您端到端地评估对话,运行测试对话并确保NLU和Core都能作出正确的预测。 为此,您须要一些端到端格式的故事,其中包括NLU输出和原始文本。这是一个例子:
## end-to-end story 1
* greet: hello
- utter_ask_howcanhelp
* inform: show me [chinese](cuisine) restaurants
- utter_ask_location
* inform: in [Paris](location)
- utter_ask_price
复制代码
若是您将端到端故事保存为名为e2e_stories.md
的文件,则能够经过运行如下命令来评估您的模型:
rasa test --stories e2e_stories.md --e2e
复制代码
确保模型中的模型文件是组合
Core
和nlu
模型。若是它不包含NLU
模型,Core
将使用默认的RegexInterpreter
。
要验证域文件,NLU数据或故事数据中是否存在任何错误,请运行验证脚本。您可使用如下命令运行它:
rasa data validate
复制代码
上面的脚本运行文件的全部验证。如下是脚本的选项列表:
usage: rasa data validate [-h] [-v] [-vv] [--quiet] [-d DOMAIN] [--data DATA]
optional arguments:
-h, --help show this help message and exit
-d DOMAIN, --domain DOMAIN
Domain specification (yml file). (default: domain.yml)
--data DATA Path to the file or directory containing Rasa data.
(default: data)
Python Logging Options:
-v, --verbose Be verbose. Sets logging level to INFO. (default:
None)
-vv, --debug Print lots of debugging statements. Sets logging level
to DEBUG. (default: None)
--quiet Be quiet! Sets logging level to WARNING. (default:
None)
复制代码
还能够经过导入Validator
类来运行这些验证,该类具备如下方法:
from_files()
:从字符串路径中的文件建立实例。verify_intents()
:检查domain文件中的intents列表包含NLU数据。verify_intents_in_stories()
:验证stories中的意图,检查它们是否有效。verify_utterances()
:检查domain文件,以肯定话语模板与操做下列出的话语之间的一致性。verify_utterances_in_stories()
:验证stories中的话语,检查它们是否有效。verify_all()
:运行上面的全部验证。要使用这些函数,必须建立Validator对象并初始化记录器。请参阅如下代码:
import logging
from rasa import utils
from rasa.core.validator import Validator
logger = logging.getLogger(__name__)
utils.configure_colored_logging('DEBUG')
validator = Validator.from_files(domain_file='domain.yml',
nlu_data='data/nlu_data.md',
stories='data/stories.md')
validator.verify_all()
复制代码
可使用通过训练的Rasa模型运行一个简单的HTTP服务器来处理请求:
rasa run -m models --enable-api --log-file out.log
复制代码
此API公开的全部端点都记录在HTTP API中。
-m
: 包含Rasa 模型的文件夹路径--enable-api
: enable this additional API--log-file
: log文件路径Rasa能够经过三种不一样的方式加载您的模型:
-m
加载指定的模型Rasa尝试按上述顺序加载模型,即若是没有配置模型服务器和远程存储,它只会尝试从本地存储系统加载模型。
提醒: 确保经过限制对服务器的访问(例如,使用防火墙)或启用身份验证方法来保护您的服务器:安全注意事项。
Note:
- 若是使用自定义操做,请确保操做服务器正在运行(请参阅启动操做服务器)。若是您的操做在另外一台计算机上运行,或者您没有使用
Rasa SDK
,请确保更新您的endpoints.yml
文件。- 若是使用仅NLU模型启动服务器,则不能调用全部可用端点。请注意,某些端点将返回409状态代码,由于须要通过培训的Core模型来处理请求。
能够配置HTTP服务器以从其余URL获取模型:
rasa run --enable-api --log-file out.log --endpoints my_endpoints.yml
复制代码
模型服务器在端点配置(my_endpoints.yml
)中指定,您能够在其中指定服务器URL Rasa按期查询压缩的Rasa模型:
models:
url: http://my-server.com/models/default@latest
wait_time_between_pulls: 10 # [optional](default: 100)
复制代码
Note 若是要从服务器中仅拉取模型一次,请将
wait_time_between_pulls
设置为None
。
Note 您的模型服务器必须提供压缩的Rasa模型,并将
{“ETag”:<model_hash_string>}
做为其标头之一。若是此模型哈希发生更改,Rasa将仅下载新模型。
Rasa可能对您的模型服务器提出的示例请求以下所示:
curl --header "If-None-Match: d41d8cd98f00b204e9800998ecf8427e" http://my-server.com/models/default@latest
复制代码
能够配置Rasa服务器以从远程存储中获取模型:
rasa run -m 20190506-100418.tar.gz --enable-api --log-file out.log --remote-storage aws
复制代码
该模型将下载并存储在本地存储系统的临时目录中。
咱们建议不要将Rasa服务器暴露给外部世界,而是经过专用链接(例如,在docker容器之间)从后端链接到它。 然而,内置了两种身份验证方法:
--auth-token thisismysecret
传递令牌:rasa run \
-m models \
--enable-api \
--log-file out.log \
--auth-token thisismysecret
复制代码
请求应该传递令牌,在咱们的案例中是thisismysecret
,做为参数:curl -XGET localhost:5005/conversations/default/tracker?token=thisismysecret
复制代码
--jwt-secret thisismysecret
启用基于JWT的身份验证。对服务器的请求须要在使用此密钥和HS25
6算法签名的Authorization
标头中包含有效的JWT令牌。 用户必须具备username
和role
属性。若是role是admin
,则能够访问全部端点。若是role
是user
,则只有sender_id
与用户的username匹配时才能访问具备sender_id
参数的端点。rasa run \
-m models \
--enable-api \
--log-file out.log \
--jwt-secret thisismysecret
复制代码
请求应该设置正确的JWT标头:"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ"
"zdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIi"
"wiaWF0IjoxNTE2MjM5MDIyfQ.qdrr2_a7Sd80gmCWjnDomO"
"Gl8eZFVfKXA6jhncgRn-I"
复制代码
要将Rasa链接到其余端点,您能够在YAML
文件中指定端点配置。而后使用标志--endpoints <path to endpoint configuration.yml>
运行Rasa。 例如:
rasa run \
--m <Rasa model> \
--endpoints <path to endpoint configuration>.yml
复制代码
可使用$ {name of environment variable}
指定配置文件中的环境变量。而后,这些占位符将替换为环境变量的值。