Transformer课程 业务对话机器人Rasa 3.x Rules
规则是一种训练数据,用于训练助手的对话管理模式。规则描述应始终遵循相同路径的简短对话。
不要过度使用规则。规则对于处理小的特定对话模式非常有用,但与故事不同,规则不能概括为看不见的对话路径。将规则和故事结合起来,让你的助手更加健壮,能够处理实际的用户行为。
如果你不能决定是写一个故事还是一个规则来实现某个行为,请参阅编写对话数据的最佳实践。
有关在Rasa助手中实现规则的其他示例,请参阅我们的规则示例机器人。
https://github.com/RasaHQ/rasa/tree/main/examples/rules
Rules example bot
规则示例机器人
https://github.com/RasaHQ/rasa/blob/main/examples/rules/actions/actions.py
from typing import Dict, Text, List
from rasa_sdk import Tracker
from rasa_sdk.events import EventType
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk import Action
from rasa_sdk.events import SlotSet
class ActionSwitchFAQ(Action):
def name(self) -> Text:
return "action_switch_faq"
def run(
self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
) -> List[EventType]:
return [SlotSet("detailed_faq", not tracker.get_slot("detailed_faq"))]
class ValidateSlots(Action):
def name(self) -> Text:
"""Unique identifier of the form"""
return "action_validate_loop_q_form"
def run(
self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
) -> List[EventType]:
dispatcher.utter_message("validate_some_slot")
return [SlotSet("some_slot", "sdk")]
https://github.com/RasaHQ/rasa/blob/main/examples/rules/data/nlu.yml
version: "3.0"
nlu:
- intent: greet
examples: |
- Hi
- Hey
- Hi bot
- Hey bot
- Hello
- Good morning
- hi again
- hi folks
- hi Mister
- hi pal!
- hi there
- greetings
- hello everybody
- hello is anybody there
- hello robot
- hallo
- heeey
- hi hi
- hey
- hey hey
- hello there
- hi
- hello
- yo
- hola
- hi?
- hey bot!
- hello friend
- 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)
- i am looking for any place that serves [indonesian](cuisine) food for three
- i need to find a restaurant
- uh im looking for a restaurant that serves [kosher](cuisine) food
- uh can i find a restaurant and it should serve [brazilian](cuisine) food
- im looking for a restaurant serving [italian](cuisine) food
- restaurant please
- i'd like to book a table for two with [spanish](cuisine) cuisine
- i need a table for 4
- book me a table for three at the [italian](cuisine) restaurant
- can you please book a table for 5?
- I would like to book a table for 2
- looking for a table at the [mexican](cuisine) restaurant for five
- find me a table for 7 people
- Can I get a table for four at the place which server [greek](cuisine) food?
- intent: affirm
examples: |
- yeah a cheap restaurant serving international food
- correct
- ye
- uh yes
- let's do it
- yeah
- uh yes
- um yes
- yes knocking
- that's correct
- yes yes
- right
- yea
- yes
- yes right
- yes and i dont care
- right on
- i love that
- intent: deny
examples: |
- no
- no new selection
- no thanks
- no thank you
- uh no
- breath no
- do you have something else
- no this does not work for me
- 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
- [seafood](cuisine)
- [french](cuisine) food
- serves [british](cuisine) food
- id like [canapes](cuisine)
- serving [jamaican](cuisine) food
- um what about [italian](cuisine) food
- im looking for [corsica](cuisine) food
- im looking for [world](cuisine) food
- serves [french](cuisine) food
- how about [indian](cuisine) food
- can i get [chinese](cuisine) food
- [irish](cuisine) food
- [english](cuisine) food
- [spanish](cuisine) food
- how bout one that serves [portuguese](cuisine) food and is cheap
- [german](cuisine)
- [korean](cuisine) food
- im looking for [romanian](cuisine) food
- serves [canapes](cuisine) food
- [gastropub](cuisine)
- i want [french](cuisine) food
- how about [modern european](cuisine) type of food
- it should serve [scandinavian](cuisine) food
- how [european](cuisine)
- how about [european](cuisine) food
- serves [traditional](cuisine) food
- [indonesian](cuisine) food
- [modern european](cuisine)
- serves [brazilian](cuisine)
- i would like [modern european](cuisine) food
- looking for [lebanese](cuisine) food
- [portuguese](cuisine)
- [european](cuisine)
- i want [polish](cuisine) food
- id like [thai](cuisine)
- i want to find [moroccan](cuisine) food
- [afghan](cuisine)
- [scottish](cuisine) food
- how about [vietnamese](cuisine)
- hi im looking for [mexican](cuisine) food
- how about [indian](cuisine) type of food
- [polynesian](cuisine) food
- [mexican](cuisine)
- instead could it be for four people
- any [japanese](cuisine) food
- what about [thai](cuisine) food
- how about [asian oriental](cuisine) food
- im looking for [japanese](cuisine) food
- im looking for [belgian](cuisine) food
- im looking for [turkish](cuisine) food
- serving [corsica](cuisine) food
- serving [gastro pub](cuisine:gastropub)
- is there [british](cuisine) food
- [world](cuisine) food
- im looking for something serves [japanese](cuisine) food
- id like a [greek](cuisine)
- im looking for [malaysian](cuisine) food
- i want to find [world](cuisine) food
- serves [pan asian](cuisine:asian) food
- looking for [afghan](cuisine) food
- that serves [portuguese](cuisine) food
- [asian oriental](cuisine:asian) food
- [russian](cuisine) food
- [corsica](cuisine)
- [asian oriental](cuisine:asian)
- serving [basque](cuisine) food
- how about [italian](cuisine)
- looking for [spanish](cuisine) food in the center of town
- it should serve [gastropub](cuisine) food
- [welsh](cuisine) food
- i want [vegetarian](cuisine) food
- im looking for [swedish](cuisine) food
- um how about [chinese](cuisine) food
- [world](cuisine) food
- can i have a [seafood](cuisine) please
- how about [italian](cuisine) food
- how about [korean](cuisine)
- [corsica](cuisine) food
- [scandinavian](cuisine)
- [vegetarian](cuisine) food
- what about [italian](cuisine)
- how about [portuguese](cuisine) food
- serving [french](cuisine) food
- [tuscan](cuisine) food
- how about uh [gastropub](cuisine)
- im looking for [creative](cuisine) food
- im looking for [malaysian](cuisine) food
- im looking for [unusual](cuisine) food
- [danish](cuisine) food
- how about [spanish](cuisine) food
- im looking for [vietnamese](cuisine) food
- [spanish](cuisine)
- a restaurant serving [romanian](cuisine) food
- im looking for [lebanese](cuisine) food
- [italian](cuisine) food
- a restaurant with [afghan](cuisine) food
- im looking for [traditional](cuisine) food
- uh i want [cantonese](cuisine) food
- im looking for [thai](cuisine)
- i want to seat [outside](seating)
- i want to seat [inside](seating)
- i want to seat [outdoor](seating)
- i want to seat [indoor](seating)
- let's go [inside](seating)
- [inside](seating)
- [outdoor](seating)
- prefer sitting [indoors](seating)
- I would like to seat [inside](seating) please
- I prefer sitting [outside](seating)
- my feedback is [good](feedback)
- my feedback is [great](feedback)
- it was [terrible](feedback)
- i consider it [success](feedback)
- you are [awful](feedback)
- for ten people
- 2 people
- for three people
- just one person
- book for seven people
- 2 please
- nine people
- intent: thankyou
examples: |
- um thank you good bye
- okay cool uh good bye thank you
- okay thank you good bye
- you rock
- and thats all thank you and good bye
- thank you and good bye
- sorry about my mistakes thank you good bye
- noise thank you good bye
- thank you goodbye noise
- okay thank you goodbye
- uh thank you good bye
- thank you goodbye
- thank you goodbye noise thank you goodbye
- breath thank you goodbye
- thank you
- okay thank you
- thanks goodbye
- ah thank you goodbye
- thank you noise
- thank you good bye
- breath thank you very much goodbye
- thanks
- noise thank you goodbye
- unintelligible thank you goodbye
- uh okay thank you good bye
- thank you bye
- um okay thank you good bye
- intent: chitchat
examples: |
- can you share your boss with me?
- i want to get to know your owner
- i want to know the company which designed you
- i want to know the company which generated you
- i want to know the company which invented you
- i want to know who invented you
- May I ask who invented you?
- please tell me the company who created you
- please tell me who created you
- tell me more about your creators
- tell me more about your founders
- Ahoy matey how are you?
- are you alright
- are you having a good day
- Are you ok?
- are you okay
- Do you feel good?
- how are things going
- how are things with you?
- How are things?
- how are you
- how are you doing
- how are you doing this morning
- how are you feeling
- how are you today
- How are you?
- How is the weather today?
- What's the weather like?
- How is the weather?
- What is the weather at your place?
- Do you have good weather?
- Is it raining?
- What's it like out there?
- Is it hot or cold?
- Beautiful day, isn't it?
- What's the weather forecast?
- Is it quite breezy outside?
- intent: stop
examples: |
- ok then you cant help me
- that was shit, you're not helping
- you can't help me
- you can't help me with what i need
- i guess you can't help me then
- ok i guess you can't help me
- that's not what i want
- ok, but that doesnt help me
- this is leading to nothing
- this conversation is not really helpful
- you cannot help me with what I want
- I think you cant help me
- hm i don't think you can do what i want
- stop
- stop go back
- do you get anything?
- and you call yourself bot company? pff
- and that's it?
- nothing else?
- intent: bot_challenge
examples: |
- are you a bot?
- are you a human?
- am I talking to a bot?
- am I talking to a human?
https://github.com/RasaHQ/rasa/blob/main/examples/rules/data/rules.yml
version: "3.0"
rules:
- rule: Greet
# This rule only applies to the start of a session.
conversation_start: True
steps:
- intent: greet
- action: utter_greet
- rule: Activate form 'q_form'
steps:
- intent: activate_q_form
- action: loop_q_form
- active_loop: loop_q_form
- rule: Example of an unhappy path for the 'loop_q_form'
condition:
# Condition that form is active.
- active_loop: loop_q_form
- slot_was_set:
- requested_slot: some_slot
steps:
# This unhappy path handles the case of an intent `explain`.
- intent: explain
- action: utter_explain_some_slot
# Return to form after handling the `explain` intent
- action: loop_q_form
- active_loop: loop_q_form
- rule: Submit form
condition:
- active_loop: loop_q_form
steps:
- action: loop_q_form
- active_loop: null
- slot_was_set:
- requested_slot: null
# The action we want to run when the form is submitted.
- action: utter_stop
- rule: FAQ question
steps:
- intent: ask_possibilities
- action: utter_list_possibilities
- rule: FAQ simple
condition:
- slot_was_set:
- detailed_faq: false
steps:
- intent: faq
- action: utter_faq
- rule: FAQ detailed
condition:
- slot_was_set:
- detailed_faq: true
steps:
- intent: faq
- action: utter_faq
# Don't predict `action_listen` after running `utter_faq`
wait_for_user_input: False
- rule: FAQ helped - continue
condition:
- slot_was_set:
- detailed_faq: true
steps:
- action: utter_faq
- action: utter_ask_did_help
- intent: affirm
- action: utter_continue
- rule: FAQ did not help
condition:
- slot_was_set:
- detailed_faq: true
steps:
- action: utter_faq
- action: utter_ask_did_help
- intent: deny
- action: utter_detailed_faq
# Don't predict `action_listen` after running `utter_faq`
wait_for_user_input: False
- rule: Detailed FAQ did not help
condition:
- slot_was_set:
- detailed_faq: true
steps:
- action: utter_detailed_faq
- action: utter_ask_did_help
- intent: deny
- action: utter_ask_stop
- rule: User decides to stop
steps:
- action: utter_ask_stop
- intent: affirm
- action: utter_stop
- rule: User decides to continue
steps:
- action: utter_ask_stop
- intent: deny
- action: utter_continue
- rule: Implementation of the TwoStageFallbackPolicy
steps:
# This intent is automatically triggered by the `FallbackClassifier` in the NLU
# pipeline in case the intent confidence was below the specified threshold.
- intent: nlu_fallback
# The Fallback is implemented as now implemented as form.
- action: action_two_stage_fallback
- active_loop: action_two_stage_fallback
https://github.com/RasaHQ/rasa/blob/main/examples/rules/config.yml
recipe: default.v1
language: en
pipeline:
- name: WhitespaceTokenizer
- name: LexicalSyntacticFeaturizer
- name: CountVectorsFeaturizer
token_pattern: (?u)\b\w+\b
- name: DucklingEntityExtractor
url: http://localhost:8000
dimensions:
- number
- name: DIETClassifier
epochs: 100
- name: EntitySynonymMapper
- name: FallbackClassifier
# If the highest ranked intent has a confidence lower than the threshold than
# the NLU pipeline predicts an intent `nlu_fallback` which you can then use in
# stories / rules to implement an appropriate fallback.
threshold: 0.5
policies:
- name: RulePolicy
# Confidence of the prediction if no rule matched and de-facto threshold for a
# core fallback.
core_fallback_threshold: 0.3
# Name of the action which should be predicted if no rule matched.
core_fallback_action_name: "action_default_fallback"
# If `True` `core_fallback_action_name` is predicted in case no rule matched.
enable_fallback_prediction: True
https://github.com/RasaHQ/rasa/blob/main/examples/rules/domain.yml
version: "3.0"
intents:
- activate_q_form
- inform
- explain
- stopp
- ask_possibilities
- faq
- affirm
- deny
- greet
- switch_faq
- nlu_fallback
entities:
- some_slot
slots:
some_slot:
type: text
influence_conversation: false
mappings:
# Slot mappings can be defined in the domain.
# You can also implement custom slot mappings in your
# `action_validate_slot_mappings` function by returning the desired slot events.
# The slot mappings follow the same syntax as currently in the SDK implementation.
- type: from_entity
entity: some_slot
# Example of a slot mapping which extracts the slot value from the message text if
# the intent is `greet`, the active loop is `loop_q_form` and `requested_slot` is
# the same slot:
# - type: from_text
# intent: greet
# conditions:
# - active_loop: loop_q_form
# requested_slot: some_slot
# Example of a slot mapping which sets the slot to `my value` in case the message
# has an intent `greet`
# - type: from_intent
# intent: greet
# value: "my value"
detailed_faq:
type: bool
mappings:
- type: custom
actions:
- utter_explain_some_slot
- action_stop_q_form
- utter_list_possibilities
- utter_faq
- utter_ask_did_help
- utter_continue
- utter_detailed_faq
- utter_ask_stop
- utter_stop
- utter_greet
- action_switch_faq
- utter_did_you_mean
# You can implement a custom action to validate extracted slots of your form.
# Return a `slot` event which sets the value to `None` in order to make the form request
# the slot again. You can also return `slot` events for other slots which you can
# extract as part of your custom action.
#- validate_loop_q_form
forms:
loop_q_form:
required_slots:
- some_slot
session_config:
session_expiration_time: 60 # value in minutes
carry_over_slots_to_new_session: true
responses:
utter_ask_some_slot:
- text: "utter_ask_some_slot"
utter_explain_some_slot:
- text: "utter_explain_some_slot"
utter_list_possibilities:
- text: "utter_list_possibilities"
utter_faq:
- text: "utter_faq"
utter_ask_did_help:
- text: "utter_ask_did_help"
utter_continue:
- text: "utter_continue"
utter_detailed_faq:
- text: "utter_detailed_faq"
utter_ask_stop:
- text: "utter_ask_stop"
utter_stop:
- text: "utter_stop"
utter_greet:
- text: "utter_greet"
utter_did_you_mean:
- text: "utter_did_you_mean"
utter_revert_fallback_and_reapply_last_intent:
- text: "utter_revert_fallback_and_reapply_last_intent"
utter_default:
- text: "I give up."
https://github.com/RasaHQ/rasa/blob/main/examples/rules/endpoints.yml
action_endpoint:
url: http://localhost:5055/webhook
Writing a Rule
在开始编写规则之前,必须确保将规则策略添加到模型配置中:
policies:
- ... # Other policies
- name: RulePolicy