目录
Handling Out-of-scope Messages#
1. Creating an Out-of-scope Intent#
2. Defining the response message
3. Creating an Out-of-Scope Rule#
Handling Specific Out-of-scope Messages#
Handling Low Action Confidence
Fallback and Human Handoff
这是一个指导如何处理你的助手的各种失败。
即使你的机器人设计得很完美,用户也不可避免地会对你的助手说一些你没有预料到的事情。在这些情况下,你的助理会失败,你要确保优雅地失败,这很重要。
Handling Out-of-scope Messages#
为了避免用户受挫,您可以处理您知道用户可能会问的问题,但您还没有实现用户目标。
1. Creating an Out-of-scope Intent#
例如,您需要在NLU训练数据中定义一个out_of_scope意图,并添加任何已知的超出作用域的请求作为训练示例
nlu.yml
nlu:
- intent: out_of_scope
examples: |
- I want to order food
- What is 2 + 2?
- Who's the US President?
As with every intent, you should source the majority of your examples from real conversations.
2. Defining the response message
You'll need to define an out-of-scope response in the domain file. Using the utterance utter_out_of_scope
as the default response, that would look like:
domain.yml
responses:
utter_out_of_scope:
- text: Sorry, I can't handle that request.
3. Creating an Out-of-Scope Rule#
Finally, you will need to write a rule for what should happen for in out-of-scope request:
rules.yml
rules:
- rule: out-of-scope
steps:
- intent: out_of_scope
- action: utter_out_of_scope
Handling Specific Out-of-scope Messages#
如果你观察你的用户所要求的某些东西,你希望这些东西在将来成为用户的目标,你可以将它们作为单独的意图来处理,让用户知道你已经理解了他们的信息,但还没有一个解决方案。例如,如果用户问“我想申请在Rasa工作”,我们就可以回答“我知道你在找工作,但我恐怕还不能掌握这项技能。”
与out_of_scope意图示例类似,您需要使用训练示例创建一个新的意图,定义响应消息,并创建一个规则。
NLU Fallback#
要处理NLU可信度较低的传入消息,请使用FallbackClassifier。使用此配置,当所有其他意图预测低于配置的置信度阈值时,意图nlu_fallback将被预测。然后,您可以编写一个规则,说明在预测到nlu_fallback时机器人应该做什么。
1. Updating the configuration#
To use the FallbackClassifier, add it to your NLU pipeline:
config.yml
pipeline:
# other components
- name: FallbackClassifier
threshold: 0.7
2. Defining the response message#
Define the message the bot should send when a message is classified with low confidence by adding a response:
domain.yml
responses:
utter_please_rephrase:
- text: I'm sorry, I didn't quite understand that. Could you rephrase?
3. Creating an NLU fallback rule#
The following Rule will ask the user to rephrase when they send a message that is classified with low confidence:
rules.yml
rules:
- rule: Ask the user to rephrase whenever they send a message with low NLU confidence
steps:
- intent: nlu_fallback
- action: utter_please_rephrase
Handling Low Action Confidence
由于用户可能会发送意料之外的消息,他们的行为可能会导致他们走上未知的对话路径。Rasa的机器学习策略(如TED策略)被优化以处理这些未知路径。
为了处理机器学习策略无法预测下一个动作的高度置信度的情况,如果没有策略的下一个动作的置信度超过可配置的阈值,可以配置规则策略预测默认动作。
您可以使用以下步骤配置在动作置信度低的情况下运行的动作以及相应的置信度阈值:
1. 更新配置#
您需要在config.yml中将规则策略添加到您的策略中。默认情况下,规则策略的设置如下:
config.yml
policies:
- name: RulePolicy
# Confidence threshold for the `core_fallback_action_name` to apply.
# The action will apply if no other action was predicted with
# a confidence >= core_fallback_threshold
core_fallback_threshold: 0.4
core_fallback_action_name: "action_default_fallback"
enable_fallback_prediction: True
2. Defining the default response message#
To define what your bot will say when action confidence is below the threshold, define a response utter_default
:
domain.yml
responses:
utter_default:
- text: Sorry I didn't get that. Can you rephrase?
当动作的信心值低于阈值时,Rasa将运行动作action_default_fallback。这将发送响应utter_default,并恢复到导致回退的用户消息之前的会话状态,因此它不会影响未来操作的预测。
3. Customizing the default action (optional)#
action_default_fallback是Rasa开源中的一个默认操作,它将utter_default响应发送给用户。您可以创建自己的自定义操作作为备用(有关自定义操作的更多信息,请参阅自定义操作)。下面的代码片段是一个自定义动作的实现,它与action_default_fallback做的一样,但分派了一个不同的模板utter_fallback_template:
actions.py
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.events import UserUtteranceReverted
from rasa_sdk.executor import CollectingDispatcher
class ActionDefaultFallback(Action):
"""Executes the fallback action and goes back to the previous state
of the dialogue"""
def name(self) -> Text:
return ACTION_DEFAULT_FALLBACK_NAME
async def run(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[Dict[Text, Any]]:
dispatcher.utter_message(template="my_custom_fallback_template")
# Revert user message which led to fallback.
return [UserUtteranceReverted()]
Two-Stage Fallback#
为了让机器人有机会了解用户的需求,您通常希望它通过询问明确的问题来试图澄清用户的信息。采用两阶段回退,在多个阶段使用以下顺序处理低NLU置信度:
用户消息以低可信度分类
用户被要求确认意图
用户确认或拒绝这个意图
如果他们证实了,对话将继续进行,就好像意图从一开始就被高度机密地分类了一样。没有采取进一步的退让步骤。
如果他们拒绝,用户就会被要求重新表述他们的信息。
用户重新表达他们的意图
如果消息是高度机密的,对话就会继续,就好像用户从一开始就有这个意图。
如果重新措辞的用户消息仍然缺乏可信度,则会要求用户确认意图。
用户确认或拒绝重新表述的意图
如果他们确认了,对话就会继续,就好像用户从一开始就有这个意图一样。
如果他们拒绝,一个最终的撤退行动就会被触发(例如,移交给人类)。
可以通过以下步骤启用两阶段回退:
可以通过以下步骤启用两阶段回退:
1. 更新配置#
将FallbackClassifier添加到管道中,将RulePolicy添加到策略配置中:
config.yml
pipeline:
# other components
- name: FallbackClassifier
threshold: 0.7
policies:
# other policies
- RulePolicy
2. Defining the fallback responses#
To define how your bot asks the user to rephrase their message, define the response utter_ask_rephrase
:
domain.yml
responses:
utter_ask_rephrase:
- text: I'm sorry, I didn't quite understand that. Could you rephrase?
Rasa提供了询问用户意图的默认实现,并要求用户重新措辞。要定制这些操作的行为,请参阅关于默认操作的文档。
3. Defining a Two-Stage Fallback rule#
在训练数据中添加以下规则。此规则将确保在收到低分类可信度的消息时激活两阶段回退:
rules.yml
rules:
- rule: Implementation of the Two-Stage-Fallback
steps:
- intent: nlu_fallback
- action: action_two_stage_fallback
- active_loop: action_two_stage_fallback
Human Handoff#
作为你的撤退行动的一部分,你可能希望机器人移交给人类代理,例如作为两阶段撤退的最后行动,或者当用户明确要求一个人。实现人工切换的一种简单方法是配置消息或语音通道,根据特定的机器人或用户消息切换它所监听的主机。
例如,作为两阶段后退的最后一个动作,机器人可以问用户,“你想被转移到人类助理吗?”如果他们说愿意,机器人就会发送一条特定的信息,比如。“handoff_to_human”到频道。当通道看到此消息时,它将停止侦听Rasa服务器,并向人类通道发送一条消息,其中包含当时的聊天对话文本。
从前端传递给人类的实现将取决于您使用的是哪个通道。您可以在金融演示和Helpdesk-Assistant starterpacks中看到一个使用聊天室频道改编的示例实现。
Summary
要让您的助手优雅地处理故障,您应该处理已知的范围外消息并添加一种形式的回退行为。如果您想要添加人工切换,您可以添加人工切换,或者将其作为后备设置中的最后一步。下面是你需要对每个方法做的修改的总结:
For out-of-scope intents:
- Add training examples for each out-of-scope intent to your NLU data
- Define the out-of-scope response or action
- Define rules for each out-of-scope intent
- Add the RulePolicy to config.yml
For single stage NLU fallback:
- Add FallbackClassifier to your pipeline in config.yml
- Define the fallback response or action
- Define a rule for the
nlu_fallback
intent - Add the RulePolicy to config.yml
For handling low core confidence:
- Configure the RulePolicy for core fallback in config.yml
- Optionally customize the fallback action you configure
- Define an
utter_default
response
For Two-Stage Fallback:
- Add FallbackClassifier to your pipeline in
config.yml
- Define a rule for the
nlu_fallback
intent that triggers theaction_two_stage_fallback
action - Define an out-of-scope intent in your domain
- Add RulePolicy to config.yml
For handing off to a human:
- Configure your front end to switch hosts
- Write a custom action (which could be your fallback action) to send the handoff payload
- Add a rule for triggering handoff (if not part of fallback)
- Add RulePolicy to config.yml