1. 训练数据格式
使用YAML作为统一且可扩展的方式来管理所有训练数据,包括NLU数据、stories、rules。训练数据可以使用多个YAML文件,每个文件包含NLU数据、stories、rules的任意组合。
version: "3.1"
nlu:
- intent: greet
examples: |
- Hey
- Hi
- hey there [Sara](name)
- intent: faq/language
examples: |
- What language do you speak?
- Do you only handle english?
stories:
- story: greet and faq
steps:
- intent: greet
- action: utter_greet
- intent: faq
- action: utter_faq
rules:
- rule: Greet user
steps:
- intent: greet
- action: utter_greet
| 是管道符号
nlu、stories、rules是对应的
data/nul.yml下包含训练数据
2. NLU 训练数据
保存为data/nlu.yml。
由用户话语的例子组成。examples包括实体,还可以添加synonym(同义词)、regex(正则表达式)、lookup(查找)以帮助正确识别意图和实体。
结构:nlu是一个列表,列表中每个元素都是一个字典,根据特殊含义的键区分不同字典的功能。
普通字符直接表示即可。注: 需要用小写字母。
nlu:
- intent: greet
examples: |
- 你好
- 您好
- 怎么了
可以自定义NLU组件和需要示例的metadata,metadata包含任意键值对,会在nlu pipeline中被组件接收。
nlu:
- intent: greet
examples:
- text: |
你好
metadata:
情绪:中性
- text: |
嘿!
代表该意图下所有的例子都包含该metadata。
nlu:
- intent: greet
metadata:
情绪: 中性
examples:
- text: |
你好
- text: |
嘿!
在检索意图后添加后缀,用于标识机器人特定响应键。
nlu:
-intent: chitchat/ask_name
examples: |
- 你叫什么名字?
- 我可以知道你的名字吗?
- 大家叫你什么?
- 你有自己的名字吗?
-intent: chitchat/ask_weather
examples: |
- 今天天气如何?
- 今天外面阳光明媚吗?
- 噢,你介意帮我查一下天气吗?
- 我喜欢柏林阳光明媚的日子。
用实体值表示。
完整语法:(role、group、value字段可选)
[<entity-text>]{"entity": "<entity name>", "role": "<role name>", "group": "<group name>", "value": "<entity synonym>"}
</entity></group></role></entity></entity-text>
如:”明天上海的天气如何”表示为”明天上海的天气如何?”明天是日期,上海是城市。
另一种表示方式:[实体值]{“key”: “value”,…}
[明天]{“entity”: “日期”}[上海]{“entity”: “城市”}的天气如何?
nlu:
- intent: check_weather
examples: |
- [明天](日期)[上海](城市)的天气如何?
- [明天]{"entity": "日期"}[上海]{"entity": "城市"}的天气如何?
❃ 获取方法:机器学习模型训练、正则表达式RegexEntityExtractor。
❃ 要从具有特定role/group的实体填充插槽,您需要为插槽定义 from_entity
,插槽映射并指定所需的角色/组。
entities:
- city:
roles:
- departure
- destination
slots:
departure:
type: any
mappings:
- type: from_entity
entity: city
role: departure
destination:
type: any
mappings:
- type: from_entity
entity: city
role: destination
存储同义词信息。在启动EntitySynonymMapper组件时,推理时会将得到的实体值的同义词替换成它的”标准词”。
只修改实体的值,不影响实体的类型。
nlu:
- synonym: 番茄
examples: |
- 蕃茄
- 西红柿
- 洋柿子
- 火柿子
将正则表达式匹配的内容是否出现作为特征传给NER、意图识别。如提取身份证号码、电话号码、IP地址。
- RegexFeaturizer组件:正则表达式的名称无关紧要。
- RegexEntityExtractor组件:正则表达式的名称与要提取的实体名称匹配。
nlu:
- regex: account_number
examples: |
- \d{10,12}
- intent: inform
examples: |
- 我的帐号是 [1234567891](account_number)
- 这是我的帐号 [1234567891](account_number)
存储查找表。实体识别和意图识别时,若能提供额外的特征,可以提高准确度。如提供一个特征词列表(查找表)。
nlu:
- lookup: 城市
examples: |
- 北京
- 上海
- ...
- 广州
- 深圳
3. 对话训练数据
用于训练对话管理模型。
故事是用户和AI助手之间对话,转换成特定的格式, 用户输入表示为 意图(必要时表示为实体),而 AI的响应和动作表示为 动作名称。
stories:
- story: 收集餐厅预订信息
steps:
- intent: greet
- action: utter_ask_howcanhelp
- intent: inform
entities:
- location: "罗马"
- price: "便宜"
- action: utter_on_it
- action: utter_ask_cuisine
- intent: inform
entities:
- cuisine: "西班牙"
- action: utter_ask_num_people
故事用于训练机器人对话管理模型的训练数据,记录对话过程。记录用户的语义表达和系统内部正确的状态变化。
stories
由 story
、 metadata
、一系列 steps
组成。
stories:
- story: 和用户打招呼
metadata:
author: 某人
key: value
steps:
- intent: greet
- action: utter_greet
- story:取值任意,不参与训练。值代表这个故事的备注,用于给开发者提供该故事的信息。
- metadata:取值任意,不参与训练,可选值。存储有关该故事的相关信息,比如作者author。
- steps:通过列表线性表示用户和机器人之间的交互:每个step可以包含以下信息:
由 意图和 实体表示的用户消息。
steps:
- intent: inform
entities:
- location: "上海"
- price: "实惠"
故事仅仅在某个对话节点上存在不同,可以使用or来精简故事。
stories:
- story: 流程开始
steps:
- action:utter_ask_confirm
- or:
- intent: affirm
- intent: thankyou
- action: action_handle_affirmation
机器人执行的所有操作。在训练和测试对话管理系统时,rasa不会真正地执行相关的动作,无法获得动作运行的结果(事件)是什么,因此需要开发者在故事中明确地给出。
- 回复(Responses): 以
utter_
开头,发送一个特定的消息给用户 - 自定义动作(custom actions): 以
action_
开头,运行自定义代码,并且可以发送或不发送消息。
stories:
- story: story with a response
steps:
- intent: greet
- action: utter_greet
stories:
- story: story with a custom action
steps:
- intent: feedback
- action: action_store_feedback
对于复杂的故事,可能存在用户请求一次后rasa连续执行多次动作的情况。
- action: action_on_it
- action: aticon_ask_howcanhelp
内置的动作,rasa按照动作的类型自动给出返回的事件。 自定义事件的故事,需要手动给出动作改变的状态。这种改变叫做 事件。常用的事件包括词槽事件和active_loop事件。
- 词槽事件:能对词槽状态进行更改的事件 ❃ 伴随
slot name
和可选的slot value
。slot value是通过entities
或者custom actions(自定义动作)
设定的。
- slot_was_set:
- asked_for_help: true
stories:
- story: story with a slot
steps:
- intent: celebrate_bot
- slot_was_set:
- feedback_value: positive
- action: utter_yay
若slot value无关紧要,那只需要列出slot name:
stories:
- story: story with a slot
steps:
- intent: greet
- slot_was_set:
- name
- action: utter_greet_user_by_name
- active_loop:负责激活和取消激活表单form。
-active_loop: restaurant_form
特殊的自定义动作,包含了一个要求的槽位集合。 在 domain.yml
的 forms
section定义。一旦定义,需要为form指定一个 happy path
作为一个 rule
。在form中,也需要定义 unhappy paths
让模型能够识别未曾见过的对话序列。示例格式如下:
stories:
- story: 有表单的故事
steps:
- intent: search_restaurant
- action: restaurant_form
- active_loop: restaurant_form
- active_loop: null
- action: utter_restaurant_found
action
激活了form并且开启了槽位填充的循环。active_loop: restaurant_form
表示现在有一个激活的form。active_loop:null
表示在当前form完成前,别的form不被激活。
form可以在被中途打断了后仍处于激活状态;在这种情况下,中断应该出现在 action: <form to activate></form>
步骤之后,然后是 active_loop: <active form></active>
步骤。表单的中断可能如下所示:
stories:
- story: 中断食物
steps:
- intent: request_restaurant
- action: restaurant_form
- intent: chitchat
- action: utter_chitchat
- active_loop: restaurant_form
- active_loop: null
- action: utter_slots_values
减少故事中重复部分,名字相同的检查点之间可以互相跳转,将故事与另一个故事联系起来。
stories:
- story: 流程开始
steps:
- intent: greet
- action: action_ask_user_question
- checkpoint: check_asked_question
- story: 处理用户确认
steps:
- checkpoint: check_asked_question
- intent: affirm
- action: action_handle_affirmation
故事开头的检查点也可以以设置的插槽为条件:
stories:
- story: story_with_a_conditional_checkpoint
steps:
- checkpoint: greet_checkpoint
slot_was_set:
- context_scenario: holiday
- holiday_name: thanksgiving
- intent: greet
- action: utter_greet_thanksgiving
规则描述了应该始终 遵循相同路径的简短对话。格式类似于story, conversation_started
和 conditions
键用于指定规则应适用的条件。
rules:
- rule: 当用户以 greet
意图开始对话时说 hello
conversation_start: true
steps:
- intent: greet
- action: utter_greet
rules:
- rule: 只有在用户提供姓名时才说"嘿"
condition:
- slot_was_set:
- user_provided_name: true
steps:
- intent: greet
- action: utter_greet
❃ 在规则结束时跳过等待用户输入:完成最后一步后等待下一条用户消息
rules:
- rule: 应用时等待用户消息的规则
steps:
- intent: greet
- action: utter_greet
❃ 若将下一个动作预测交给另一个故事或规则, 添加 wait_for_user_input: false
到规则中。
rules:
- rule: 应用就不会等待用户消息的规则
steps:
- intent: greet
- action: utter_greet
wait_for_user_input: false
不要过度使用规则
测试一个mesage是否被成功分类,用user指定实际的消息文本和文本中包含的实体。
stories:
- story: A basic end-to-end test
steps:
- user: |
hey
intent: greet
- action: utter_ask_howcanhelp
- user: |
show me [chinese]{"entity": "cuisine"} restaurants
intent: inform
- action: utter_ask_location
- user: |
in [Paris]{"entity": "location"}
intent: inform
- action: utter_ask_price
不必处理 NLU 管道提取的消息的特定意图,使用 user
将用户消息直接放入stories。
stories:
- story: user message structure
steps:
- user: the actual text of the user message
- action: action_name
可以添加实体标签,也可以将机器人话语直接放入stories中。
stories:
- story: 完整端到端的故事
steps:
- intent: greet
entities:
- name: 李明
- bot: 你好!
- intent: search_restaurant
- action: utter_suggest_cuisine
- user: 我总是去吃 [寿司](菜)
- bot: 就我个人而言,我更喜欢披萨,不过我们还是去找找寿司店吧。
- action: utter_suggest_cuisine
- user: 祝你有美好的一天!
- action: utter_goodbye
参考文献:
[1] 孔小泉,王冠.Rasa实战:构建开源对话机器人[M].电子工业出版社.2022:201.
[2] RASA官方文档 https://rasa.com/docs/rasa/rules
Original: https://blog.csdn.net/qnstar_/article/details/125046335
Author: 就要辣谢谢。
Title: 对话机器人(三)——RASA:训练数据
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/544619/
转载文章受原作者版权保护。转载请注明原作者出处!