文章目录
- 1. fallback
- NLU fallback
- 2. 意图触发动作
- 3. 表单
- 定义表单
- 激活表单
- 执行表单任务
- 4. 天气预报机器人
- nlu.yml
- stories.yml
- rules.yml
- cities.yml
- domain.yml
- config.yml
- endpoints.yml
- credentials.yml
- actions.py
- 测试
learn from https://github.com/Chinese-NLP-book/rasa_chinese_book_code
1. fallback
机器人不明白用户意思的时候,兜底的操作,答出 “对不起,xxx” 的友好提示
NLU fallback
pipeline:
- name: FallbackClassifier
threshold: 0.6
ambiguity_threshold: 0.1
配置的意思:意图分类组件预测的结果中,最高的置信度 <= 0.6 or 最高的前2个意图得分之差 <= 0.1 ,那么 NLU 的意图 就会被替换为 nlu_fallback
比如,
rules:
- rule: 要求用户重说
steps:
- intent: nlu_fallback
- action: utter_please_rephrase
如果预测的下一个动作 置信度不高,或者有两个很相近的,也可以配置策略
policies:
- name: RulePolicy
core_fallback_threshold: 0.3
core_fallback_action_name: "action_dafault_fallback" # 很模糊时,配置执行该动作
enable_fallback_prediction: True
2. 意图触发动作
- 自定义意图触发动作
rules:
- rule: 意图 -> 动作映射
steps:
- intent: some_intent
- action: some_action
在 policy 字段值为 RulePolicy 时,用户表达 意图后,100% 触发 配置的动作
3. 表单
理解用户意图后,引导用户填表,完毕后执行动作
需要规则策略
policies:
- name: RulePolicy
定义表单
forms:
weather_form:
required_slots: # 表单必须指定该字段
- address
- date-time
激活表单
设定什么时候进入该表单, rule 指定
rules:
- rule: 激活 rule
steps:
- intent: weather # 出现该意图
- action: weather_form # 执行该动作
- active_loop: weather_form # 进入表单填词槽-询问过程
执行表单任务
满足表单条件后,词槽全部填好,执行任务
- rule: 提交 form
condition:
- active_loop: weather_form
steps:
- action: weather_form
- active_loop: null
- slot_was_set:
- requested_slot: null # 所有词槽填完
- action: action_weather_form_submit # 执行动作
4. 天气预报机器人
tree
.
├── actions.py
├── config.yml
├── credentials.yml
├── data
│ ├── cities.yml
│ ├── nlu.yml
│ ├── responses.yml
│ ├── rules.yml
│ └── stories.yml
├── domain.yml
├── endpoints.yml
├── index.html
├── index.js
├── __init__.py
├── media
│ └── demo.png
├── README.md
└── service
├── __init__.py
├── normalization.py
└── weather.py
nlu.yml
version: "3.0"
nlu:
- intent: goodbye
examples: |
- 拜拜
- 再见
- 拜
- 退出
- 结束
- intent: greet
examples: |
- 你好
- 您好
- hello
- hi
- 喂
- 在么
- intent: weather
examples: |
- 显示天气
- 天气
- 我需要不需要雨靴
- 我该穿外套吗
- 去外边要穿外衣吗
- 去外边要带夹克吗
- 外边需要雨伞吗
- 天气是不是很凉快
- 最近的天气是不是很冷
- 天气预报
- 天气很冷吗
- 天气会不会很热
- 天气温和吗
- [北京](address)会不会阴雨
- [上海](address)什么天气
- 不好意思可以帮我查[香港](address)的天气
- [厦门](address)啥天气
- [上海](address)多热
- [台北市](address)温度
- [台南市](address)几度
- [上海](address)啥温度
- [台南市南区](address)现在几度
- [上海](address)的天气
- [上海](address)的天气怎么样
- [首都](address)的天气
- [首都](address)的天气怎么样
- [魔都](address)的天气
- [魔都](address)的天气怎么样
- 我要[上海](address)[明天](date-time)的天气
- 我要[上海](address)[后天](date-time)的天气
- [上海](address)[明天](date-time)的天气
- [上海](address)[昨天](date-time)的天气
- [上海](address)[前天](date-time)的天气
- [上海](address)[后天](date-time)的天气
- [下个星期五](date-time)[南京](address)的天气
- [明天](date-time)[北京](address)什么天气
- [沈阳](address)[五天后](date-time)的天气怎么样
- [下星期一](date-time)[北京](address)的天气呢
- [今天](date-time)[天津](address)的预报
- [青岛](address)[明天](date-time)的
- [下星期日](date-time)的[苏州](address)
- [两天后](date-time)的[上海](address)
- [三天后](date-time)的[武汉](address)呢
- [三天后](date-time)[杭州](address)多云吗
- [十月三号](date-time)[沈阳](address)会下雨吗
- [明天](date-time)[台北](address)天气
- [今天](date-time)[台北](address)的天气如何
- [三天后](date-time)[台北](address)的天气
- [北京](address)[今天](date-time)的天气如何
- [杭州](address)[今天](date-time)的天气怎么样
- [下个星期五](date-time)[台北](address)天气好吗
- [今天](date-time)[台北](address)天气如何
- [今天](date-time)[上海](address)的天气
- [两天前](date-time)[上海](address)的天气如何
- [今天](date-time)[台北市](address)的天气如何
- [明天](date-time)[北京](address)我需要不需要雨衣
- [下星期日](date-time)[北京](address)外边需要毛线帽吗
- [今天](date-time)[北京](address)去外边要穿羊毛袜吗
- [三月五号](date-time)[北京](address)去外边要穿外衣吗
- [下个星期五](date-time)我在[厦门](address)需要带伞吗
- [上海](address)[三天后](date-time)多少度
- [明天](date-time)[上海](address)的温度如何
- [今天](date-time)[上海](address)的气温如何
- [明天](date-time)[马来西亚](address)最近的天气是不是很冷
- [下星期一](date-time)[马来西亚](address)天气凉快吗
- [下星期日](date-time)[马来西亚](address)的天气会很热吗
- [首都](address)[明天](date-time)的天气
- [魔都](address)[下午](date-time)的天气
- [首都](address)[明天](date-time)的天气怎么样
- [魔都](address)[下午](date-time)的天气怎么样
- [今天](date-time)天气如何
- 你知道[现在](date-time)外面冷不冷么
- 我还想知道[一月一号](date-time)的天气
- 稍后[晚上](date-time)会下雨吗
- [今天](date-time)会不会晴朗
- [昨天](date-time)几度
- [9月初四](date-time)的天气如何
- [明天](date-time)天气多少摄氏度
- [今天](date-time)天气
- [昨天](date-time)什么天气
- [明天](date-time)要不要戴手套
- [今天](date-time)去外边要穿毛衣吗
- [明天](date-time)去外边要带雨伞吗
- [两天后](date-time)我需要不需要雨靴
- [下星期一](date-time)在外边需要墨镜吗
- [明天](date-time)的天气会温和吗
- [今天](date-time)天气很热不
- [今天](date-time)天气几度
- [两天后](date-time)的天气会不会很冷
- [明天](date-time)的天气是不是很暖
- intent: info_date
examples: |
- [明天](date-time)
- [后天](date-time)
- [下个星期日](date-time)怎么样
- 还需要[昨天](date-time)的
- 我还要[昨天](date-time)的
- [明天](date-time)如何
- [后天](date-time)如何
- [星期六](date-time)呢
- [后天](date-time)的呢
- [明天](date-time)的怎么说
- [两天后](date-time)的大概什么样
- [前天](date-time)的
- 帮我查查[三天前](date-time)
- 帮我查查[下星期五](date-time)的
- 还要[明天](date-time)的
- intent: info_address
examples: |
- 告诉我[广州](address)怎么样
- [广州](address)
- 那么[辽宁](address)呢
- [北京](address)啥情况
- [厦门](address)怎么样
- [武汉](address)呢
- [香港](address)呢
- 我在[杭州](address)
- [上海](address)
- 在[宁波](address)呢
- [宁波](address)
- [首都](address)
- intent: affirm
examples: |
- 是
- 是的
- 没问题
- 好
- 好的
- 可以
- 挺好
- 没错
- 继续
- 没毛病
- intent: deny
examples: |
- 不
- 不行
- 没
- no
- 不可以
- 算了
- 不要
- 不要了
- 没有
- intent: stop
examples: |
- 停
- 停止
- intent: chitchat/whoyouare
examples: |
- 你是谁啊
- 你是谁啊?
- 你是谁啊?
- 你叫什么名字
- 你叫什么名字呢
- 你叫什么呀
- 你叫什么呀?
- 你叫什么呀?
- 介绍一下自己
- 介绍自己
- intent: chitchat/whatyoucando
examples: |
- 你能干啥
- 你能干什么
- 你能干什么呢
- 你能干啥呀
- 你能干啥呀?
- 你能干啥呀?
- 你能做什么
- 你能做什么?
- 你能做什么?
- 你能做什么呢
- 你能做什么呢?
- 你能做什么呢?
- 你有哪些技能
- 你有哪些技能?
- 你有哪些技能?
- 你的本领是什么
- 你的本领是什么呢
- synonym: 下个星期一
examples: |
- 下星期一
- synonym: 今天
examples: |
- 早上
- 中午
- 晚上
- 下午
- 傍晚
- 今日
- synonym: 明天
examples: |
- 明日
- synonym: 北京
examples: |
- 首都
- synonym: 上海
examples: |
- 魔都
stories.yml
version: "3.0"
stories:
- story: greet
steps:
- intent: greet
- action: utter_greet
- story: say goodbye
steps:
- intent: goodbye
- action: utter_goodbye
- story: chitchat
steps:
- intent: chitchat
- action: respond_chitchat
- story: form with stop then deny
steps:
- or:
- intent: weather
- intent: weather
entities:
- address: 上海
- intent: weather
entities:
- date-time: 明天
- intent: weather
entities:
- date-time: 明天
- address: 上海
- action: weather_form
- active_loop: weather_form
- intent: stop
- action: utter_ask_continue
- intent: deny
- action: action_deactivate_loop
- active_loop: null
rules.yml
version: "3.0"
rules:
- rule: activate weather form
steps:
- intent: weather
- action: weather_form
- active_loop: weather_form
- rule: Submit form
condition:
# Condition that form is active.
- active_loop: weather_form
steps:
- action: weather_form
- active_loop: null
- slot_was_set:
- requested_slot: null
# The action we want to run when the form is submitted.
- action: action_weather_form_submit
cities.yml
version: "3.0"
nlu:
- lookup: cities
examples: |
- 江宁区
- 常德市西洞庭管理区
- 常德市津市市
- 桂阳县
domain.yml
version: "3.0"
session_config:
session_expiration_time: 60
carry_over_slots_to_new_session: true
intents:
- goodbye
- greet
- weather
- chitchat
- deny
- stop
- affirm
- info_date
- info_address
entities:
- address
- date-time
slots:
address:
type: text
influence_conversation: false
mappings:
- entity: address
type: from_entity
date-time:
type: text
influence_conversation: false
mappings:
- entity: date-time
type: from_entity
responses:
utter_greet:
- text: 你好,请说出需要提供天气预测服务的地点和时间
utter_goodbye:
- text: 再见!
utter_ask_address:
- text: 想查询哪里的天气呢?
utter_ask_date-time:
- text: 想查询什么时候的天气呢?
utter_ask_continue:
- text: 是否要继续?
utter_default:
- text: 系统不明白您说的话,请换个说法。
actions:
- utter_ask_address
- utter_ask_date-time
- utter_goodbye
- utter_greet
- utter_ask_continue
- utter_default
- respond_chitchat
- action_weather_form_submit
forms:
weather_form:
ignored_intents: []
required_slots:
- address
- date-time
config.yml
recipe: default.v1
language: zh
pipeline:
- name: JiebaTokenizer
- name: LanguageModelFeaturizer
model_name: bert
model_weights: bert-base-chinese
- name: RegexFeaturizer
- name: DIETClassifier
epochs: 100
learning_rate: 0.001
tensorboard_log_directory: ./log
- name: ResponseSelector
epochs: 100
learning_rate: 0.001
- name: EntitySynonymMapper
policies:
- name: MemoizationPolicy
- name: TEDPolicy
- name: RulePolicy
endpoints.yml
action_endpoint:
url: "http://localhost:5055/webhook"
credentials.yml
socketio:
user_message_evt: user_uttered
bot_message_evt: bot_uttered
session_persistence: false
rasa:
url: "http://localhost:5002/api"
actions.py
from typing import Any, Dict, List, Text
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from service.normalization import text_to_date
from service.weather import get_text_weather_date
class WeatherFormAction(Action):
def name(self) -> Text:
return "action_weather_form_submit"
def run(
self, dispatch: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]
) -> List[Dict]:
city = tracker.get_slot("address")
date_text = tracker.get_slot("date-time")
date_object = text_to_date(date_text)
if not date_object: # parse date_time failed
msg = "暂不支持查询 {} 的天气".format([city, date_text])
dispatch.utter_message(msg)
else:
try:
weather_data = get_text_weather_date(city, date_object, date_text)
except Exception as e:
exec_msg = str(e)
dispatch.utter_message(exec_msg)
else:
dispatch.utter_message(weather_data)
return []
测试
rasa train
搜索 心知天气
,注册获取免费的 api key
运行动作服务器 SENIVERSE_KEY=XXX rasa run actions
windows set %SENIVERSE_KEY%=xxx
rasa run --cors "*"
python -m http.server
-
测试结果:
-
缺少词槽的情况:
-
上下文继承