Rasa处理业务逻辑的分步指南


表单的工作方式是提示用户输入信息,直到它收集了所有必需的信息。信息存储在 插槽中。一旦所有必需的插槽都被填满,bot就会满足用户的原始请求。

1.定义表单(form)

要定义表单,您需要定义:

  • 插槽映射:要收集的必需信息
  • 响应:你的机器人应该如何回答每一条信息

插槽映射(Slot Mappings)

对于餐馆搜索示例,我们希望从用户那里收集以下信息:

  • 美食(cuisine)
  • 人数(number of people)
  • 是否坐在外面(whether they want to sit outside or not)

可以在domain中通过为每个必需信息指定槽映射定义表单(form)。插槽映射定义了需要哪些插槽以及如何填充每个插槽:

forms:
  restaurant_form:
    cuisine:
      - type: from_entity
        entity: cuisine
    num_people:
      - type: from_entity
        entity: number
    outdoor_seating:
      - type: from_intent
        intent: affirm
        value: true
      - type: from_intent
        intent: deny
        value: false

对于任何填充的from_entity的插槽,需要将该实体添加到域中:

entities:
  - cuisine
  - number

数字等实体可以由DucklingEntityExtractor提取。要使用它,请将DucklingEntityExtractor添加到NLU管道:

language: en
pipeline:
# other components
- DucklingEntityExtractor:
  dimensions: ["number"]

根据用户的意图(intent)填充outdoor_seating插槽:如果是affirm,则为true,如果是deny,则为false
由于表单依赖于某些可用的slots,因此需要将这些slots添加到domain中。通过form填充slots通常不影响谈话。将influence_conversation设置为false可忽略它在对话期间的取值:

slots:
  cuisine:
    type: text
    auto_fill: false
    influence_conversation: false
  num_people:
    type: float
    auto_fill: false
    influence_conversation: false
  outdoor_seating:
    type: text
    auto_fill: false
    influence_conversation: false

验证插槽(Validating Slots)

通常,您会希望在接收用户的输入之前验证用户的输入,例如通过检查给定的菜肴(cuisine)是否在助手的可用菜肴数据库中。有关验证操作的详细信息,请参阅validating form input文档。

请求插槽(Requesting Slots)

要指定bot如何请求所需信息,请在domain中定义名为utter_ask_{slotname}的响应:

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.更新配置

表单的happy path应定义为rule,这意味着您需要将RulePolicy添加到policies中:

policies:
  - name: RulePolicy

3.创建规则

表单本身负责询问用户所有必需信息的逻辑,因此您只需要两个规则就可以获得一个表单的快乐路径:一个定义何时开始,另一个定义何时填充。以餐馆搜索为例,在现实生活中,助手会根据用户的喜好查找餐馆。在本例中,bot将发出一个响应,其中包含用于搜索的详细信息。

rules:
  - rule: activate restaurant form   # 规则名称
    steps:                           # 步骤
      - intent: request_restaurant   # 触发表单激活的意图
      - action: restaurant_form      # 运行表单
      - active_loop: restaurant_form # 此表单处于活动状态

  - rule: submit form
    condition:                       # 条件
    - active_loop: restaurant_form   # 此表单必须处于活动状态
    steps:
      - action: restaurant_form      # 运行表单
      - active_loop: null            # 表单已被填充,因此不再处于活动状态
      - action: utter_submit         # 表单完成后要采取的操作
      - action: utter_slots_values   # 表单完成后要采取的操作

通过拆分表单的激活和提交,如果用户提供意外输入或通过闲聊中断表单,规则仍然适用。

4.更新NLU训练数据

您需要为激活表单的意图添加示例,以及用户如何提供所需信息的示例。

表单激活意图

您需要为激活表单的意图提供训练示例。添加意图request_restaurant的示例:

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实体作为第一条信息的一部分,则该插槽将在表格开头填写,bot将不再要求他们再要求cuisine

表格填充意图

当表单填充插槽时,它不会注意预测了哪个意图,除非插槽映射明确要求或排除了意图。
对于餐馆搜索示例,outdoor_seating槽映射到两个意图,因此需要为这些意图添加训练数据。
对于cuisinenumber槽,没有指定意图,因此可以向通用的inform意图添加示例。您需要对cuisine实体进行注释,以便DIETClassifier可以学习提取它。您不需要注释number实体,因为DucklingEntityExtractor是一个基于规则的提取器,它没有根据您的训练数据进行训练。对于每一个意图,只给出了几个例子;要使bot正常工作,您应该添加更多的训练数据,如下所示:

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以包含以下意图:

intents:
  - request_restaurant
  - affirm
  - deny
  - inform

5.定义响应

添加提交表单后发送的响应:

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}"

总结

表单可以简化收集用户信息的逻辑。要定义像上面餐馆搜索示例那样的最小表单,以下是您需要执行的操作:

  • RulePolicy添加到config.yml
  • domain中定义带有插槽映的表单
  • 将所有必需的插槽(required slots)添加到domain
  • 添加激活和提交表单的规则
  • 为激活表单的意图添加示例
  • 为填充required slots的意图添加示例
  • 定义表单完成时bot要执行的操作或响应
  • 用您定义的新意图和actions更新您的domain

要尝试新定义的表单,请运行rasa train并启动rasa shell来重新训练bot的模型。因为DucklingEntityExtractor用于提取实体,所以您还需要在后台启动Duckling(请参阅运行Duckling的说明)。

参考

1.官方文档
2.formbot example bot

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值