RaSa2.5.x相关概念之二:Domain


Domain定义助手运行体系。它指定了 bot应该知道的意图( intents)、实体( entities)、插槽( slots)、响应( responses)、表单( forms)和行为( actions)。它还定义了对话的配置。
以下是一个完整的 domain示例,摘自 concertbot示例:

version: "2.0"

intents:
  - affirm
  - deny
  - greet
  - thankyou
  - goodbye
  - search_concerts
  - search_venues
  - compare_reviews
  - bot_challenge
  - nlu_fallback
  - how_to_get_started

entities:
  - name

slots:
  concerts:
    type: list
    influence_conversation: false
  venues:
    type: list
    influence_conversation: false
  likes_music:
    type: bool
    influence_conversation: true

responses:
  utter_greet:
    - text: "Hey there!"
  utter_goodbye:
    - text: "Goodbye :("
  utter_default:
    - text: "Sorry, I didn't get that, can you rephrase?"
  utter_youarewelcome:
    - text: "You're very welcome."
  utter_iamabot:
    - text: "I am a bot, powered by Rasa."
  utter_get_started:
    - text: "I can help you find concerts and venues. Do you like music?"
  utter_awesome:
    - text: "Awesome! You can ask me things like \"Find me some concerts\" or \"What's a good venue\""

actions:
  - action_search_concerts
  - action_search_venues
  - action_show_concert_reviews
  - action_show_venue_reviews
  - action_set_music_preference

session_config:
  session_expiration_time: 60  # value in minutes
  carry_over_slots_to_new_session: true

多个Domain文件(Multiple Domain Files)

Domain可以定义为单个YAML文件,也可以在一个目录中拆分为多个文件。当跨多个文件拆分时,Domain内容将被读取并自动合并在一起。
使用命令行界面,可以通过运行以下命令来训练具有拆分Domain文件的模型:

rasa train --domain path_to_domain_directory

意图(Intents)

Domain文件中的意图键列出了NLU数据和会话训练数据中使用的所有意图。

为某些目的忽略实体(Ignoring Entities for Certain Intents)

忽略某些意图的所有实体,可以将use_entities: []参数添加到域文件中的意图中,如下所示:

intents:
  - greet:
      use_entities: []

要忽略某些实体或仅明确考虑某些实体,可以使用以下语法:

intents:
- greet:
    use_entities:
      - name
      - first_name
    ignore_entities:
      - location
      - age

这些意图排除(忽略)的实体将是非特征化的,因此不会影响下一个动作预测。当您有一个不关心被选中的实体的意图时,这很有用。
如果列出不带此参数的意图,则实体将按正常方式进行特征化。

如果希望这些实体不影响行为预测,请为同名插槽设置influence_conversation: false参数。

实体(Entities)

entities部分列出了可以由NLU管道中的任何实体提取器提取的所有实体。如果要使用功能Entity Roles and Groups,则还需要在此部分列出实体的角色和组。
例如:

entities:
   - PERSON           # 由SpacyEntityExtractor提取的实体
   - time             # DucklingEntityExtractor提取的实体
   - membership_type  # 由DIETClassifier提取的自定义实体
   - priority         # 由DIETClassifier提取的自定义实体
   - city:            # 由DIETClassifier提取的自定义实体
       roles:
       - from
       - to
   - topping:         # 由DIETClassifier提取的自定义实体
       groups:
       - 1
       - 2
   - size:            # 由DIETClassifier提取的自定义实体
       groups:
       - 1
       - 2

插槽(Slots)

插槽是机器人的记忆。它们充当一个键值对存储,可用于存储用户提供的信息(例如他们的家乡城市)以及收集的有关外部世界的信息(例如数据库查询的结果)。
在您的domainSlots部分中定义了Slots,包括它们的名称、类型以及它们是否以及如何影响助手的行为。下面的示例定义了一个名为slot_name的插槽且类型为text

slots:
  slot_name:
    type: text

插槽与会话行为(Slots and Conversation Behavior)

您可以使用influence_conversation属性指定插槽(slot)是否影响会话。
如果要在不影响会话的情况下将信息存储在插槽中,请在定义插槽时设置influence_conversation: false
下面的示例定义了一个age插槽(slot),它将存储有关用户年龄的信息,但不会影响会话流。这意味着助手每次预测下一个操作时都会忽略插槽的值。

slots:
  age:
    type: text
    # this slot will not influence the predictions
    # of the dialogue policies
    influence_conversation: false

在定义插槽(slot)时,如果您省略influence_conversation或将其设置为true,则该插槽(slot)将影响下一个动作预测,除非它的插槽(slot)类型为any。插槽(slot)影响会话的方式将取决于它的插槽(slot)类型。
下面的示例定义了一个影响对话的home_city插槽。
text类型槽将影响助手的行为,具体取决于槽是否有值text类型槽的具体值(例如班加罗尔、纽约或香港)没有任何差别。

slots:
  # this slot will influence the conversation depending on
  # whether the slot is set or not
  home_city:
    type: text
    influence_conversation: true

例如,考虑两个输入“天气怎么样?”和“班加罗尔的天气怎么样?”会话应根据NLU是否自动设置home_city插槽而不同。
如果插槽已经设置,bot可以预测action_forecast操作。如果没有设置插槽,它需要获得home_city信息,然后才能预测天气。

插槽类型(Slot Types)

Text Slot

  • Type
    text
  • Use For
    存储文本值。
  • Example
slots:
   cuisine:
      type: text
  • Description
    如果influence_conversation设置为true,助手的行为将根据是否设置了槽而改变。不同的文本不会进一步影响谈话。这意味着以下两层是相等的:
stories:
- story: French cuisine
  steps:
  - intent: inform
  - slot_was_set:
    - cuisine: french

- story: Vietnamese cuisine
  steps:
  - intent: inform
  - slot_was_set:
    - cuisine: vietnamese

Boolean Slot

  • Type
    bool
  • Use For
    存储truefalse
  • Example
slots:
   is_authenticated:
      type: bool
  • Description
    如果influence_conversation设置为true,助手的行为将根据槽是否为空、设置为true还是设置为false而改变。注意,空的bool插槽对会话的影响与插槽设置为false时不同。

Categorical Slot

  • Type
    categorical
  • Use For
    存储可以取N个值之一的插槽。
  • Example
slots:
  risk_level:
    type: categorical
    values:
      - low
      - medium
      - high
  • Description
    如果influence_conversation设置为true,助手的行为将根据槽的具体值而改变。这意味着助手的行为是不同的,这取决于上面示例中的slot的值是lowmedium还是high
    默认值__other__将自动添加到用户定义的值中。遇到的所有未在插槽的values中显式定义的值都映射到__other____other__不应用作用户定义的值;如果定义了other,它仍然将作为映射所有未显示值的默认值。

Float Slot

  • Type
    float
  • Use For
    存储实数。
  • Example
slots:
  temperature:
    type: float
    min_value: -100.0
    max_value:  100.0
  • Defaults
    max_value=1.0, min_value=0.0
  • Description
    如果influence_conversation设置为true,助手的行为将根据槽的值而改变。如果值介于min_valuemax_value之间,则使用数字的特定值。min_value以下的所有值都将被视为min_value,而max_value以上的所有值都将被视为max_value。因此,如果将max_value设置为1,则插槽值2和3.5之间没有差异。

List Slot

  • Type
    list
  • Use For
    存储值列表
  • Example
slots:
  shopping_items:
    type: list
  • Description
    如果influence_conversation设置为true,助手的行为将根据列表是否为空而改变。存储在插槽中的列表长度不影响对话。列表长度是零还是非零才重要。

Any Slot

  • Type
    any
  • Use For
    存储任意值(可以是任何类型,如字典或列表)。
  • Example
slots:
  shopping_items:
    type: any
  • Description
    any类型的槽在会话期间总是被忽略。对于此类型插槽,属性influence_conversation不能设置为true。如果要存储会影响会话的自定义数据结构,请使用自定义插槽类型。

Custom Slot Types

也许你的餐厅预订系统最多只能处理6个人的预订。在这种情况下,您希望槽的值影响下一个选定的操作(而不仅仅是它是否被指定)。您可以通过定义一个定制的slot类来实现这一点。

下面的代码定义了一个名为NumberOfPeopleSlot的自定义槽类。特征化定义了如何将这个slot的值转换为向量,以便Rasa开源机器学习模型能够处理它。NumberOfPeopleSlot有三个可能的“值”,可以用长度为2的向量表示。
(0,0) 尚未设置
(1,0) 1到6之间
(0,1) 大于6

my_custom_slots.py文件中:

from rasa.shared.core.slots import Slot

class NumberOfPeopleSlot(Slot):

    def feature_dimensionality(self):
        return 2

    def as_feature(self):
        r = [0.0] * self.feature_dimensionality()
        if self.value:
            if self.value <= 6:
                r[0] = 1.0
            else:
                r[1] = 1.0
        return r

您可以将自定义插槽类实现为独立的python模块,与自定义操作代码分离。将自定义插槽的代码保存在一个名为"init.py"的空文件旁边的目录中,以便将其识别为python模块。然后,您可以通过其模块路径引用自定义插槽类。
例如,假设您已将上述代码保存在"addons/my_custom_slots.py"中,该目录与您的bot项目相关:

└── rasa_bot
    ├── addons
    │   ├── __init__.py
    │   └── my_custom_slots.py
    ├── config.yml
    ├── credentials.yml
    ├── data
    ├── domain.yml
    ├── endpoints.yml

自定义插槽类型的模块路径是addons.my_custom_slots.NumberOfPeopleSlot。使用模块路径引用domain文件中的自定义插槽类型:

slots:
  people:
    type: addons.my_custom_slots.NumberOfPeopleSlot
    influence_conversation: true

既然您的定制slot类可以被Rasa开源使用,那么可以添加一些基于people槽的值的训练故事。你可以写一个故事来描述一个people的槽值在1到6之间,一个故事来描述一个大于6的值。您可以选择这些范围内的任何值放入您的故事中,因为它们都以相同的方式进行特征化(请参阅上面的特征化表)。

stories:
- story: collecting table info
  steps:
  # ... other story steps
  - intent: inform
    entities:
    - people: 3
  - slot_was_set:
    - people: 3
  - action: action_book_table

- story: too many people at the table
  steps:
  # ... other story steps
  - intent: inform
    entities:
    - people: 9
  - slot_was_set:
    - people: 9
  - action: action_explain_table_limit

Unfeaturized Slot

已弃用
槽类型unfeaturized已弃用。请使用此处所述的influence_conversation属性或槽类型any

  • Type
    unfeaturized

  • Use For
    要存储的数据,而且不应影响对话流。

  • Example

slots:
  internal_user_id:
    type: unfeaturized
  • Description
    这种类型的插槽永远不会影响对话。

Slot Auto-fill

如果您的NLU模型拾取了一个实体,并且您的域包含同名插槽,则插槽将自动设置,前提是满足以下条件:

  1. store_entities_as_slots设置为true
  2. 插槽的auto_fill属性设置为true

例如:

stories:
- story: entity slot-filling
  steps:
  - intent: greet
    entities:
    - name: Ali
  - slot_was_set:
    - name: Ali
  - action: utter_greet_by_name

在本例中,您不必在故事中包含slot_was_set部分,因为它会自动拾取:

stories:
- story: entity slot-filling
  steps:
  - intent: greet
    entities:
    - name: Ali
  - action: utter_greet_by_name

要禁用特定插槽的自动填充,可以在domain文件中将auto_fill属性设置为false

slots:
  name:
    type: text
    auto_fill: false

初始化槽值(Initial slot values)

您可以为domain文件中的插槽提供初始值:

slots:
  num_fallbacks:
    type: float
    initial_value: 0

响应(Responses)

响应是向用户发送消息而不运行任何自定义代码或返回事件的操作。这些响应可以直接在domain文件中的responses键下定义,并且可以包含丰富的内容,例如按钮和附件。有关响应以及如何定义响应的更多信息,请参阅响应

表单(Forms)

表单是一种特殊类型的操作,旨在帮助助手从用户那里收集信息。在domain文件的forms键下定义表单。有关窗体以及如何定义表单的详细信息,请参见表单

行为(Actions)

行为(Actions)是你的机器人能做的事情。例如,行为(Actions)可以是:

  • 响应用户
  • 进行外部API调用
  • 查询数据库
  • 什么都可以

所有自定义行为(Actions)都应列在domain中,但响应不必列在actions:下,因为它们已列在responses:下。

会话配置(Session configuration)

交互式会话表示助理和用户之间的对话。会话可以从三种方式开始:

  1. 用户开始与助手对话
  2. 用户在可配置的不活动期间后发送第一条消息
  3. 使用/session_start意图消息手动触发会话启动。
    您可以在domainsession_config键下定义触发新会话的非活动期。
    可用参数包括:
  • session_expiration_time定义新会话开始后的非活动时间(以分钟为单位)。
  • carry_over_slots_to_new_sessio确定是否应将现有的集合槽转移到新会话。

默认会话配置如下所示:

session_config:
  session_expiration_time: 60  # 值(以分钟为单位),0表示无限长
  carry_over_slots_to_new_session: true  # 设置为false可忽略会话之间的插槽

这意味着,如果用户在60分钟不活动后发送第一条消息,则会触发一个新的会话会话,并且任何现有的槽位都会转入新会话。将session_expiration_time的值设置为0表示会话不会结束(请注意,action_session_start操作仍将在会话的最开始触发)。

配置(Config)

domain文件中的config键维护(maintains)store_entities_as_slots参数。当一个实体被NLU模型识别并且实体名称与插槽名称匹配时,store_entities_as_slots定义实体值是否应该放在该插槽中。默认情况下,实体将自动填充同名的插槽。
通过将store_entities_as_slots参数设置为false,可以关闭所有插槽自动填充:

config:
  store_entities_as_slots: false

也可以使用特定插槽定义中的auto_fill参数为该插槽关闭此行为。

参考

  1. 官方文档
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值