目录
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)
插槽是机器人的记忆。它们充当一个键值对存储,可用于存储用户提供的信息(例如他们的家乡城市)以及收集的有关外部世界的信息(例如数据库查询的结果)。
在您的domain
的Slots
部分中定义了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
存储true
或false
。 - 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
的值是low
、medium
还是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_value
和max_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模型拾取了一个实体,并且您的域包含同名插槽,则插槽将自动设置,前提是满足以下条件:
store_entities_as_slots
设置为true
- 插槽的
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)
交互式会话表示助理和用户之间的对话。会话可以从三种方式开始:
- 用户开始与助手对话
- 用户在可配置的不活动期间后发送第一条消息
- 使用
/session_start
意图消息手动触发会话启动。
您可以在domain
中session_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
参数为该插槽关闭此行为。