美文网首页
Rasa3.X中使用Form支持多轮对话

Rasa3.X中使用Form支持多轮对话

作者: 掉了西红柿皮_Kee | 来源:发表于2023-02-19 14:49 被阅读0次

安装Rasa运行环境

# first step:
conda create -n py38_rasa python=3.8
# second step:
conda activate py38_rasa
# third step:
python -m pip install pip==20.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
# forth step:
pip install rasa==3.4 -i https://pypi.tuna.tsinghua.edu.cn/simple/ --extra-index-url https://pypi.rasa.com/simple

Form的使用场景在于:要完成的需求需要两个及两个以上的填充值。

  1. config.yml中polices中添加RulePolicy
  2. 在domain.yml中配置intents-actions-response-entities-slots
    主要是slots的配置,这里以station为实体,配置两个slot:start_station & end_station
intents:
  - ask_price

entities:
  - station

slots:
  start_station:
    type: text
    mappings:
      - type: from_entity
        entity: station
        role: departure
        intent: ask_price
  end_station:
    type: text
    mappings:
      - type: from_entity
        entity: station
        role: destination
        intent: ask_price
# 配置对应于slot的询问actions
responses:
  # ask price
  utter_ask_start_station:
    - text: 请告诉我你的出发地?
    - text: 你的出发地是?
  utter_ask_end_station:
    - text: 请告诉我你的目的地?
    - text: 你的目的地是?
# 声明action_ask_price为action,并后续在actions.py中实现
actions:
  - action_ask_price
# 定义触发时候的Forms
forms:
  price_form: # for intent:ask price
    required_slots:
      - start_station
      - end_station

# carry_over_slots_to_new_session是一个布尔值,控制是否在新的会话中继续使用当前会话的slot值。
# 如果为True,则会在新的会话中保留slot的值;如果为False,则在新的会话中不会保留slot的值。
# 用于指定会话的有效时间。如果会话在一段时间内没有接收到任何用户输入,那么这个会话就会被视为过期,Rasa 就会创建一个新的会话。
# 这个时间是以秒为单位,默认值是 300 秒,也就是 5 分钟。
session_config:
  session_expiration_time: 60
  carry_over_slots_to_new_session: true
  1. actions.py中定义在获得所需信息之后的动作
from typing import Any, Dict, List, Text
from datetime import datetime, timedelta
from rasa_sdk import Action, Tracker, FormValidationAction
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.events import UserUtteranceReverted

# for actions - action_ask_price
class ActionAskPrice(Action):
    def name(self):
        return "action_ask_price"

    def run(self, dispatcher, tracker, domain):
        start_station = tracker.get_slot("start_station")
        end_station = tracker.get_slot("end_station")
        response = '您要查询的票价 - 出发站:{},终点站:{}'.format(start_station, end_station)
        dispatcher.utter_message(response)
        return []
  1. 配置price_form的Rules
rules:
  # for actions test
  - rule: query station info
    steps:
      - intent: ask_price
      - action: price_form
      - active_loop: price_form
    # active_loop 是会话中的活动循环,是一个机器人询问用户输入、进行预测、获得用户回应,并再次询问的循环过程。

    # 满足条件后进行deactivete,并且执行对应的action
  - rule: submit station info for calling api
    condition:
      # Condition that form is active.
      - active_loop: price_form
    steps:
      # Form is deactivated
      - action: price_form
      - active_loop: null
      - slot_was_set:
          - requested_slot: null
      # The actions we want to run when the form is submitted.
      - action: action_ask_price
  1. nlu.yml中配置所需的训练数据

以上就是简单的多伦对话收集用户的意图及所需的信息。至于在过程中需要的信息校验FormValidationAction,将会在后续的学习中更新。
对话展示:


相关文章

网友评论

      本文标题:Rasa3.X中使用Form支持多轮对话

      本文链接:https://www.haomeiwen.com/subject/vovvkdtx.html