Transformer课程 业务对话机器人Rasa 3.x Handling Business Logic
Transformer课程 业务对话机器人Rasa 3.x Handling Business Logic
Rasa官网
Handling Business Logic
处理业务逻辑:会话助理通常需要向用户询问信息以帮助他们。您可以使用表单收集所需的用户信息并完成请求。
会话助理通常支持用户目标,包括在为用户做某事之前从用户那里收集所需信息。例如,餐馆搜索机器人需要收集一些关于用户偏好的信息,以便为他们找到合适的餐馆:
本文是关于处理收集用户信息以满足请求的业务逻辑的指南。在上面的示例中,业务逻辑包括需要知道用户喜欢的菜肴、聚会规模和座位偏好。本页上的示例来自formbot示例bot。
1.通过运行以下程序,训练包含Rasa NLU和Rasa核心模型的Rasa模型:
rasa train
模型将作为压缩文件存储在/models
目录中。
2.运行[duckling]的实例(https://rasa.com/docs/rasa/nlu/components/#ducklingentityextractor)
在端口8000上运行docker命令
docker run -p 8000:8000 rasa/duckling
或直接在您的计算机上启动服务器。
3.通过运行以下命令来测试助理:
rasa run actions&
rasa shell -m models --endpoints endpoints.yml
这将在命令行中加载助理,供您聊天。
运行结果如下
Step-by-step Guide on Using Forms to Handle Business Logic
表单的工作方式是提示用户输入信息,直到用户收集了所有必需的信息。信息存储在插槽中。一旦所有需要的插槽都被填满,bot就会满足用户的原始请求。
1. Defining the form
要定义表单,您需要定义:
- 插槽映射:收集所需的信息
- 回答:你的机器人应该如何询问每一条信息
Slot Mappings
对于餐馆查询示例,我们希望从用户处收集以下信息:
- 烹饪类型
- 人数
- 是否愿意在餐馆室外就餐
通过在表单名称下指定所需插槽的列表,可以在域中定义表单:
domain.yml
forms:
restaurant_form:
required_slots:
- cuisine
- num_people
- outdoor_seating
这些插槽需要添加到域的插槽部分,以及定义如何填充插槽的插槽映射。对于从from_entity填充的任何插槽,该实体也需要添加到域中。由表单填充的时段通常不会影响对话,因此请将“influence_conversation”设置为“false:
domain.yml
entities:
- cuisine
- number
slots:
cuisine:
type: text
mappings:
- type: from_entity
entity: cuisine
num_people:
type: float
mappings:
- type: from_entity
entity: number
outdoor_seating:
type: bool
mappings:
- type: from_intent
intent: affirm
value: true
conditions:
- active_loop: restaurant_form
requested_slot: outdoor_seating
- type: from_intent
intent: deny
value: false
conditions:
- active_loop: restaurant_form
requested_slot: outdoor_seating
数字number槽由实体填充。数字number等实体可以由DucklingEntityExtractor提取。要使用它,请将DucklingEntityExtractor添加到NLU管道:
config.yml
language: en
pipeline:
# other components
- name: DucklingEntityExtractor
dimensions: ["number"]
Slot Mappings with Conditions
outdoor_seating根据用户的意图填充:如果确认,则为true;如果拒绝,则为false。
但是,只有当用户回答问题“您想坐在外面吗?”时,插槽才应设置为true或false。为强制执行此条件,outdoor_seating槽的条件要求餐厅座位restaurant_form处于活动状态,且请求的座位槽为室外座位outdoor_seating。如果没有条件,并且用户在对话之前发送了一条带有确认或拒绝意图的消息,则在激活表单时,户外座位槽将已经填满。因此,该表格不会提示用户选择户外座位。有关详细信息,请参见映射条件。
Validating Slots
通常,您会希望在接受用户输入之前验证用户的输入,例如,检查给定的菜肴是否在助理的可用菜肴数据库中。有关验证操作的更多信息,请参阅有关验证表单输入的文档。
Requesting Slots
要指定bot应如何请求所需信息,请在域中定义名为utter_ask_u{slotname}的响应
domain.yml
responses:
utter_ask_cuisine:
- text: "What cuisine?"
utter_ask_num_people:
- text: "How many people?"
utter_ask_outdoor_seating:
- text: "Do you want to sit outside?"
2. Updating the configuration
表单的快乐路径应定义为规则,这意味着您需要将RulePolicy添加到策略中:
config.yml
policies:
- name: RulePolicy
3. Creating rules
表单本身负责询问用户所有所需信息的逻辑,因此表单的快乐路径只需要两条规则:一条定义何时开始,另一条定义填充后发生的事情。对于餐馆搜索示例,在现实生活中,助手会根据用户的偏好查找餐馆。在这种情况下,bot将发出一个响应,其中包含用于搜索的详细信息。
rules.yml
rules:
- rule: activate restaurant form
steps:
- intent: request_restaurant # intent that triggers form activation
- action: restaurant_form # run the form
- active_loop: restaurant_form # this form is active
- rule: submit form
condition:
- active_loop: restaurant_form # this form must be active
steps:
- action: restaurant_form # run the form
- active_loop: null # the form is no longer active because it has been filled
- action: utter_submit # action to take after the form is complete
- action: utter_slots_values # action to take after the form is complete
通过拆分表单的激活和提交,如果用户提供 unexpected input 或通过聊天中断表单,则规则仍然适用。
4. Updating the NLU training data
您需要为激活表单的意图添加示例,以及用户如何提供所需信息的示例。
Form Activation Intent(s)
您需要为激活表单的意图提供训练示例,例如request_restaurant:
nlu.yml
nlu:
- intent: request_restaurant
examples: |
- im looking for a restaurant
- can i get [swedish](cuisine) food in any area
- a restaurant that serves [caribbean](cuisine) food
- id like a restaurant
- im looking for a restaurant that serves [mediterranean](cuisine) food
- can i find a restaurant that serves [chinese](cuisine)
默认情况下,只要提取了正确的实体,使用from_entity填充的槽可以由任何用户话语填充,而不管意图如何。这意味着,如果用户将烹饪实体cuisine作为其第一条消息的一部分提供,则将在表单的开头填充该槽,并且机器人不会再次向他们索要烹饪。
Form Filling Intent(s)
当表单填充槽时,它不会注意预测了哪个意图,除非槽映射明确要求或排除意图。
对于餐馆搜索示例,户外座位槽outdoor_seating映射到两个意图,因此需要为这些意图添加训练数据。
对于烹饪cuisine 和number数字槽,没有指定意图,因此您可以向通用意图inform中添加示例,您需要对烹饪实体cuisine进行注释,以便DIETClassifier可以学习提取它。您不需要对数字实体number 进行注释,因为DucklingEntityExtractor是一个基于规则的提取器,它没有根据您的训练数据进行训练。对于每个意图,仅显示了几个示例;要使您的机器人正常工作,您应该添加比此处显示的更多的训练数据:
nlu.yml
nlu:
- intent: affirm
examples: |
- Yes
- yes, please
- yup
- intent: deny
examples: |
- no don't
- no
- no I don't want that
- intent: inform
examples: |
- [afghan](cuisine) food
- how bout [asian oriental](cuisine)
- what about [indian](cuisine) food
- uh how about [turkish](cuisine) type of food
- um [english](cuisine)
- im looking for [tuscan](cuisine) food
- id like [moroccan](cuisine) food
- for ten people
- 2 people
- for three people
- just one person
- book for seven people
- 2 please
- nine people
更新您的域以包含这些意图
domain.yml
intents:
- request_restaurant
- affirm
- deny
- inform
5. Defining the responses
添加提交表单后发送的响应:
domain.yml
responses:
utter_submit:
- text: "All done!"
utter_slots_values:
- text: "I am going to run a restaurant search using the following parameters:\n
- cuisine: {cuisine}\n
- num_people: {num_people}\n
- outdoor_seating: {outdoor_seating}"
Summary
表单可以简化收集用户信息的逻辑。要定义上述餐馆搜索示例中的最小表单,以下是您需要执行的操作的摘要:
将规则策略添加到配置config.yml
在域中定义具有所需插槽的表单
为域中所有必需的插槽添加插槽映射
添加激活和提交表单的规则
为激活表单的意图添加示例
为填充所需插槽的意图添加示例
定义表单完成时bot要执行的操作或响应
使用您定义的新意图和操作更新您的域
要尝试新定义的表单,请通过运行rasa train并启动rasa shell来重新训练bot的模型。由于DucklingEntityExtractor用于提取实体,因此还需要在后台启动Duckling(请参阅运行Duckling的说明)。