0
点赞
收藏
分享

微信扫一扫

<Rasa实战> 内容摘要(四)

dsysama 2022-04-25 阅读 17

基于规则的对话管理

fallback

NLU fallback

pipeline:
  - name: FallbackClassifier
    threshold: 0.6
    ambiguity_threshold: 0.1

如果在所有意图分类组件预测出的结果中,最高 的置信度不大于或等于0.6(threshold选项),或者最高的前2个意图的 得分之差不超过0.1分(ambiguity_threshold选项),那么NLU的意图就 会被替换成nlu_fallback。

- rule: 要求用户重新说一次
  steps:
    - intent: nlu_fallback
    - action: utter_please_rephrase

上面的例子将nlu_fallback映射成了utter_please_rephrase,意味着一旦nlu_fallback意图出现,就一定会执行utter_please_rephrase动作.

Handling Low Action Confidence

You can configure the action that is run in case low of action confidence as well as the corresponding confidence threshold using the following steps:

1. Updating the configuration

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:

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 Open Source 中的默认操作,它将 utter_default 响应发送给用户。 您可以创建自己的自定义操作以用作后备(有关自定义操作的更多信息,请参阅自定义操作)。 以下代码片段是一个自定义操作的实现,它与 action_default_fallback 执行相同但调度不同的模板 utter_fallback_template

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

人工切换

意图触发动作

自定义意图触发动作

在特定情况下,开发人员想要保证当某个意图出现时,无论什么样的上下文都能百分百触发某个或多个特定的动作,就得用RulePolicy的功能,该功能在stories.yml或data/rules.yml中定义:

- rule: 从some_intent 到 some_action 映射
  steps:
    - intent: some_intent
    - action: some_action

表单

To use forms with Rasa Open Source you need to make sure that the RulePolicy is added to your policy configuration

  • config.yml
policies:
- name: RulePolicy

一个表单DEMO格式

entities:
- cuisine
- number
slots:
  cuisine:
    type: text
    mappings:
    - type: from_entity
      entity: cuisine
  num_people:
    type: any
    mappings:
    - type: from_entity
      entity: number
forms:
  restaurant_form:
    ignored_intents: 
    - chitchat
    required_slots:
        - cuisine
        - num_people

一旦表单动作第一次被调用,表单就会被激活并提示用户输入下一个所需的槽值。 它通过查找名为 utter_ask_<form_name>_<slot_name>utter_ask_<slot_name> 的响应来执行此操作(如果未找到前者)。 确保在您的域文件中为每个必需的插槽定义这些响应。

Activating the Form

rules:
- rule: Activate form
  steps:
  - intent: request_restaurant
  - action: restaurant_form
  - active_loop: restaurant_form

Deactivating a Form

rules:
- rule: Submit form
  condition:
  # Condition that form is active.
  - active_loop: restaurant_form
  steps:
  # Form is deactivated
  - action: restaurant_form
  - active_loop: null
  - slot_was_set:
    - requested_slot: null
  # The actions we want to run when the form is submitted.
  - action: utter_submit
  - action: utter_slots_values

我正在「AI爱好者社区」服务器中聊天,来和我一起畅聊吧 ~ 点击加入:https://fanbook.mobi/3H6D5FVN
网页版,请入:
https://fanbook.mobi/web/home
网页版如要加入社区,请复制社区ID(3H6D5FVN)加入.

举报

相关推荐

0 条评论