一、Rasa简介
Rasa是一套用来构建基于上下文的AI小助手和聊天机器人框架。
分为两个主要的模块:
- NLU:自然语言理解模块,实现意图识别以及槽值的提取,将用户的输入转化为结构性数据,在训练过程中,为了提高从用户信息的实体识别能力,采用了预先训练的实体提取器Pre-trained Entity Extractors,正则表达式Regexes,同义词Synonyms等
- Rasa Core:对话管理模块,也是一个平台,用来预测决定下一步做什么,其中对话数据包括了stories以及rules
- 其中设计对话数据时分为happy and unhappy paths,前者表示用户按照预期来进行会话,但用户通常会通过提问,闲聊或其他要求来进入后者。
- RasaX:一个交互工具,帮助用户构建,改进和部署由Rasa框架搭成的聊天机器人
其中,rasa版本已经发展到了3.0,与之前的1.0,2.0都有较大的差距,在这里会直接介绍最新版本,感兴趣的可以了解其他版本。
二、Rasa的数据构成
在此处,构建小助手是使用rasa官网的交互式网页来构建的,可以直接将训练数据输入并训练,再进行测试,十分方便。也可以将你的模型下载下来,方便之后的使用与改进。以下是Rasa的数据构成
2.1NLUdata
在这里,意向(intent)及其示例(examples)用作聊天小助手的自然语言理解 (NLU) 模型的训练数据
nlu:
- intent: greet
examples: |
- Hi
- Hey!
- Hallo
- Good day
- Good morning
- intent: subscribe
examples: |
- I want to get the newsletter
- Can you send me the newsletter?
- Can you sign me up for the newsletter?
2.2Responses
现在,聊天小助手已经理解了用户可能会说的几条消息,它需要可以发回给用户的响应Responses
responses:
utter_greet:
- text: |
Hello! How can I help you?
- text: |
Hi!
utter_ask_email:
- text: |
What is your email address?
utter_subscribed:
- text: |
Check your inbox at {email} in order to finish subscribing to newsletter!
- text: |
You're all set! Check your inbox at {email} to confirm your subscription.
2.3Stories
Stories是示例对话,可根据用户之前在对话中所说的内容正确响应训练助手。格式显示用户消息的意图,后跟助手的操作或响应。
stories:
- story: greet and subscribe
steps:
- intent: greet
- action: utter_greet
- intent: subscribe
- action: newsletter_form
- active_loop: newsletter_form
2.4Forms
在许多情况下,聊天助手需要从用户那里收集信息。例如,当用户想要订阅新闻稿时,助理必须询问其电子邮件地址,您可以使用表单在Rasa中执行此操作
slots:
email:
type: text
mappings:
- type: from_text
conditions:
- active_loop: newsletter_form
requested_slot: email
forms:
newsletter_form:
required_slots:
- email
2.5Rules
规则描述了对话的某些部分,无论之前在对话中说了什么,这些部分都应始终遵循相同的规则。我们希望我们的助手始终使用特定操作响应某个意图,因此我们使用Rules将该操作映射到意图
rules:
- rule: activate subscribe form
steps:
- intent: subscribe
- action: newsletter_form
- active_loop: newsletter_form
- rule: submit form
condition:
- active_loop: newsletter_form
steps:
- action: newsletter_form
- active_loop: null
- action: utter_subscribed
Rules的好处:
- 单轮交互:某些消息不需要任何上下文来回答它们。规则Rules是将意向映射到响应的简单方法,可指定这些消息的固定答案。
- 回退行为:与回退分类器结合使用时,可以编写规则来响应具有特定回退行为的低可信度用户消息。
- 表单:激活和提交表单通常遵循固定路径。您还可以编写规则来处理表单中的意外输入
举个例子,比如询问使用信用卡A支付还是信用卡B来支付,但是用户通常会询问信用卡的账户余额,而不是直接回答选择哪一个来支付。
以上数据准备完成之后就可以在官网上进行训练并测试结果,也可以下载下来。
三、Rasa的简单安装
python版本为3.7或者3.8,推荐使用Ubuntu系统,并且使用虚拟环境。
Linux环境下python虚拟环境virtualenv安装和使用——包含虚拟环境集中管理库
python3 -m venv ./venv #创建虚拟环境,
source venv/bin/activate #激活虚拟环境
pip3 install -U pip && pip3 install rasa # 安装rasa,pip的版本在21.3
rasa init #创建默认的初始项目,项目文件
此项目还需要其他的依赖包,需要什么就下载什么就可以了。