rasa core 中的逻辑

rasa core

rasa 逻辑

在这里插入图片描述

rasa core为核心的逻辑

在这里插入图片描述
Agent将Rasa Core的功能通过API开放出来,像模型训练,对话处理等都可以通过Agent完成,一个模型训练的例子:

import sys
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.agent import Agent

if len(sys.argv) < 3:
    print("请指定数据路径和模型的存储名称")
    exit()

domain = "{}/domain.yml".format(sys.argv[1])
stories = "{}/data/stories.md".format(sys.argv[1])
dialogue = "{}/models/{}".format(sys.argv[1], sys.argv[2])

agent = Agent(domain, policies=[KerasPolicy(validation_split=0.0,epochs=400)])
training_data = agent.load_data(stories)
agent.train(training_data)
agent.persist(dialogue)

rasa core的主要功能是实现:对话追踪 & 对话管理的任务

对话追踪

简单理解为把 对话的过程记录下来
主要涉及 tracker 、event类
在Rasa Core中Tracker负责记录整个对话的流程,而Tracker中数据的新增、编辑和删除是通过Event进行管理的。

Events

Events用于描述一个对话过程中任何可能发生的事情。简单来说,Event就是对bot一切行为的抽象。

{'event': 'user', 'timestamp': 1576028292.7156482, 'text': ' 文本审核', 
       'parse_data': {
                  'intent': {'name': 'request_search_TextsReviews', 'confidence': 0.9437154531478882}, 
                  'entities': [],
                  'intent_ranking': [{'name': 'request_search_TextsReviews', 'confidence': 0.9437154531478882}, {'name': 'query_attendance_rules', 'confidence': 0.13368795812129974}, {'name': 'request_search_EA', 'confidence': 0.12188508361577988}, {'name': 'inform_people_conference', 'confidence': 0.11569312959909439}, {'name': 'inform_time_conference', 'confidence': 0.0498206689953804}, {'name': 'confirm', 'confidence': 0.0410604402422905}, {'name': 'greet', 'confidence': 0.01581529527902603}, {'name': 'request_search_conference', 'confidence': 0.0}, {'name': 'inform_floor_conference', 'confidence': 0.0}, {'name': 'confirm + goodbye', 'confidence': 0.0}], 'text': '文本审核'}, 
 'input_channel': 'cmdline'}, 

rasa-core内部实现了以下Event
在这里插入图片描述
Event对象是Rasa Core描述会话中发生的所有事件和明确rasa.core.trackers.DialogueStateTracker该如何更新其状态的基类,因此不能被直接使用,而是通过它包含的具体事件(event)实现。
(1)通用事件

  • rasa_core.events.SlotSet(key, value=None, timestamp=None) #设置插槽值
  • rasa_core.events.Restarted(timestamp=None) # 重置tracker
  • rasa_core.events.AllSlotsReset(timestamp=None) #重置插槽
  • rasa_core.events.ReminderScheduled(action_name, trigger_date_time,
    name=None, kill_on_user_message=True, timestamp=None) #定时执行Action
  • rasa_core.events.ConversationPaused(timestamp=None)#暂停会话
  • rasa_core.events.ConversationResumed #恢复会话
  • rasa_core.events.FollowupAction(name,
    timestamp=None)#强制设置nex_action指定的action

(2)自动跟踪事件

  • rasa_core.events.UserUttered(text, intent=None, entities=None, parse_data=None, timestamp=None, input_channel=None, message_id=None)[source]#用户向机器人发一条message
  • rasa_core.events.BotUttered(text=None, data=None, timestamp=None)#机器人向用户发送一条message
  • rasa_core.events.UserUtteranceReverted(timestamp=None)
Tracker

tracker类的init

    def __init__(self, sender_id, slots,
                 max_event_history=None):
        """Initialize the tracker.
        A set of events can be stored externally, and we will run through all
        of them to get the current state. The tracker will represent all the
        information we captured while processing messages of the dialogue."""
        # 可以跟踪的最长历史,tracker记录状态是以event为单位的
        self._max_event_history = max_event_history
        self.events = self._create_events([]) # 历史事件列表
        self.sender_id = sender_id # 这个id和rasa的chenel特性有关系
        self.slots = {slot.name: copy.deepcopy(slot) for slot in slots} # slot列表
        ###
        # current state of the tracker - MUST be re-creatable by processing
        # all the events. This only defines the attributes, values are set in
        # `reset()`
        ###
        self._paused = None   # 暂停标志
        # 一些action记录
        self.followup_action = ACTION_LISTEN_NAME
        self.latest_action_name = None
        self.latest_message = None
        self.latest_bot_utterance = None   # bot的上一个返回内容
        self._reset()

对话管理

Policy

Policy是负责决策Action的调用在Tracker的状态发生变更之后,Policy来决定下一步的Action。

CollectionDispatcher

这个对象的主要作用是设计了各种魔法函数,处理不同类型的bot输出,并将其输出到OutputChannel中。看一下它的成员方法就能知道它的作用:
在这里插入图片描述

  • dispatcher.utter_elements#Sends a message with custom elements to the output channel
  • dispatcher.utter_message #Send a text to the output
    channel
  • dispatcher.utter_button_message#Sends a message with buttons
    to the output channel
  • dispatcher.utter_button_template#Sends a
    message template with buttons to the output channel

Processor

这个对象是对话系统的核心处理模块。它通过execute_action完成bot处理对话的流程。

这里需要注意一点,在processor执行action之前,agent将会调用processor的log_message方法,使用nlu_interpreter来对用户发送的文本做实体识别和意图识别,然后将信息保存在tracker中,这个逻辑比较简单,nlu模块也在前一篇文章中详细解析过了,因此这里就不详细展开了。

execute_action方法核心内容如下:
在这里插入图片描述

DialogueStateTracker

在多轮对话过程中全程记录对话状态信息。这个对象在开发自己的对话系统时,作用可是非常大的。很多对话状态信息,都可以从它这里得到。当然, 我们并不能直接去读写其定义的成员变量信息,需要通过其成员方法来操作成员变量,例如current_sate(),其核心内容如下:

在这里插入图片描述

Action

Action是对用户输入的一种回应:
Actions are the things your bot runs in response to user input. There are three kinds of actions in Rasa Core:
(1)default actions (action_listen, action_restart, action_default_fallback)
rasa系统内置的粒度较小的action。与rasa_sdk中的action不同,这个是直接在rasa_core/actions下面的。相对于上面的form action来说,这里的action功能更单一,与events比较像,但是还是略有不同,下面举个实例ActionRestart:
在这里插入图片描述
所有default action的列表如下,它们的命名都非常简单直接:

  • action_listen
  • action_restart
  • action_default_fallback
  • action_deactive_form
  • action_rever_fallback_events
  • action_default_ask_affirmation
  • action_default_ask_rephrase
  • action_back
    (2)utter actions, starting with utter_, which just sends a message to the user.
    (3)custom actions - any other action, these actions can run arbitrary code
Form Actions
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值