如何在Rasa中实现用户输入的错误提示输出功能?
Rasa实现餐厅查找机器人两类错误提示的方法
两类场景的实现逻辑完全独立,分别对应不同的触发链路:
- NLU识别层面:未匹配任何intent的兜底提示
- 业务逻辑层面:地点查询无结果的餐厅提示
未匹配任何Intent的兜底错误提示
这类场景当用户输入和所有训练的意图都不匹配、或识别置信度低于阈值时触发,通过Rasa内置的兜底组件实现即可,步骤如下:
- 在
config.yml的pipeline末尾添加FallbackClassifier配置,设置合理的置信度阈值,当所有意图的识别置信度低于阈值时,会自动触发内置的nlu_fallback意图:
# config.yml 示例配置 pipeline: # ... 你已配置的其他NLU组件 - name: FallbackClassifier threshold: 0.7 ambiguity_threshold: 0.1
- 在
rules.yml中添加对应规则,将nlu_fallback意图绑定到你的自定义回复:
# rules.yml 示例规则 rules: - rule: 未识别意图时的兜底回复 steps: - intent: nlu_fallback - action: utter_intent_unrecognized
- 在
domain.yml的responses块中定义对应的回复文案:
# domain.yml 示例回复 responses: utter_intent_unrecognized: - text: "抱歉我没理解你说的内容,你可以告诉我你想找哪个位置的餐厅"
识别到有效地点但无相关餐厅的提示
这类场景即机器人已经成功识别到用户输入的地点实体、填充了位置slot,但查询后没有匹配的餐厅,需要在自定义查询action中处理:
- 首先在
domain.yml中配置好位置slot的抽取规则,确保能从用户输入中正确识别地点:
# domain.yml 示例slot配置 slots: location: type: text influence_conversation: false mappings: - type: from_entity entity: location
- 在自定义的餐厅查询action中,加入空结果判断逻辑,查询无返回时直接输出指定提示:
# actions.py 示例自定义action from typing import Any, Text, Dict, List from rasa_sdk import Action, Tracker from rasa_sdk.executor import CollectingDispatcher class ActionSearchRestaurant(Action): def name(self) -> Text: return "action_search_restaurant" def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: target_location = tracker.get_slot("location") # 替换为你实际的餐厅查询逻辑 matched_restaurants = get_restaurant_list(target_location) if not matched_restaurants: dispatcher.utter_message(text="抱歉该位置没有餐厅") return [] # 有匹配结果时的正常展示逻辑 dispatcher.utter_message(text=f"为你找到{target_location}附近的{len(matched_restaurants)}家餐厅...") return []
内容的提问来源于stack exchange,提问作者Aayush Dangol
相关产品推荐
相关产品推荐

