Rasa学习笔记1-rasa响应消息的步骤和名词解释

1、响应消息的步骤

Rasa是一个对话机器人框架
在这里插入图片描述

2、Rasa名词解释和作用

(1)意图intents:要做什么(意图:订车票)

(2)实体entities:谁做,做啥(实体:人物:张三;目的地:北京)

(3)词槽slot:定位对话信息(词槽:人物、目的地)

(4)故事stories:故事告诉我们的机器人在对话的哪一点做什么;是对话系统训练数据样本

1)创建故事的方式

  • 手动创建故事,直接将它们写入文件:
    • 故事以“##”一个字符串作为标识符开始。
    • 用户操作以“*”开头。
    • 机器人操作由以“-”开头的行指定。
    • 故事的结尾用换行符表示。
## happy path              # name of the story - just for debugging 
* greet             
  - utter_greet
* mood_great               # user utterance, in format_intent[entities] 
  - utter_happy
 
## sad path 1              # this is already the start of the next story
* greet
  - utter_greet            # action of the bot to execute 
* mood_unhappy
  - utter_cheer_up
  - utter_did_that_help
* mood_affirm
  - utter_happy
 
## sad path 2
* greet
  - utter_greet
* mood_unhappy
  - utter_cheer_up
  - utter_did_that_help
* mood_deny
  - utter_goodbye
 
## say goodbye
* goodbye
  - utter_goodbye
  • 使用交互式学习创建故事。

(5)动作action:机器人在对话过程中采取的动作

Rasa响应消息的步骤:Rasa机器人在识别到用户意图和实体后会执行对应的action,Rasa总共具备四种action:Response、Custom Actions、Forms、Default Actions。

1)Responses

  • Response一般定义在domain中或者单独建立response.yml文件存放定义
    /domain.yml

    response:
      utter_greet:
      - text:"Hey,{name},How are you?"
    
  • Response可以放置多条回复模版- text,回复的时候会随机发送

  • Response可以使用变量,上面是其中一种方式,response被调用后会在name的slot找到对应的值进行填充,如果识别不到即填充为None;另外,可以通过自定义Action进行传参

    dispatcher.utter_message(
    	template = "utter_greet",
    	name = "Amen"
    )
    
  • Response可以发送纯文本、富文本或者自定义输出形式。

  • Response的调用:
    ① 以utter_开头,在domain.yml文件的response关键字下列出,该response可以直接被当作action进行调用,并且不用再domain文件的action关键字下列出。
    ② 在stories的action中进行调用
    /stories.yml

    stories:
    -story:greet user
     step:
     - intent:greet
     - action:utter_greet
    

    ③在自定义action中调用

    from rasa_sdk.interfaces import Action
    
    class ActionGreet(Action):
    	def name(self):
    		return 'acction_greet'
    	
    	def run(self, dispatcher, tracker, domain):
    		dispatcher.utter_message(template="utter_greet")
    		return []
    

2) Custom Action:自定义action,基类是action,运行自定义的action server

class MyCustomAction(Action):
	
    def name(self) -> Text:
		# 返回的名字要添加到domain的actions部分
        return "action_name"

    async def run(
        self, dispatcher, tracker: Tracker, domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
		"""
		dispatcher – 将消息发送给用户
		tracker – 当前用户的状态跟踪器
		domain – 机器人的 domain文件
		"""
        return []

官网以restaurant bot为例,如果用户说 “show me a Mexican restaurant”,那么机器人就会执行ActionCheckRestaurants这个action,如下所示:
这个action会查询数据库以查找与请求的菜肴相匹配的餐馆,并使用找到的餐馆列表来设置(SlotSet)匹配槽位-slot的值

from rasa_sdk import Action
from rasa_sdk.events import SlotSet

class ActionCheckRestaurants(Action):
   def name(self) -> Text:
      return "action_check_restaurants" # 这个名字要添加到domain的actions部分

   def run(self,
           dispatcher: CollectingDispatcher,
           tracker: Tracker,
           domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

      cuisine = tracker.get_slot('cuisine') # 访问名为cuisine的slot
      q = "select * from restaurants where cuisine='{0}' limit 1".format(cuisine)
      result = db.query(q) # 查询数据库

      return [SlotSet("matches", result if result is not None else [])]

3)Forms:作用就是完成slot填充
最常见的对话方式:填表式对话,根据意图所需实体槽位进行补充填入。因此,当用户提供的实体信息不足时,请求用户提供更多的实体信息以做出对应的动作。所以Forms的作用就是完成slot的填充。

注:要将Forms与Rasa Open Source一起使用,需要确保将Rule Policy添加到config文件中

Form的使用
①定义Forms:在domain文件中进行,bot会根据domain定义forms时,slot的先后顺序,依次向用户反问slot;
②Activating Forms:story或者rule中进行;
③Dectivating Forms:story或者rule中进行;

Rasa附带四个自定义Slot Mapping方法:可以根据用户输入消息填充不Froms的空slot:from_entity、from_text、from_inten、from_trigger_intent

Forms的自定义用法: Validating Form Input、Custom Slot Mappings、Dynamic Form Behavior、Using a Custom Action to Ask For the Next Slot

4)Default Action
①Default Actions:默认情况下内置在对话管理器(dialogue manager)中的actions,根据某些对话情况自动预测。
②重写Default Action需要自定义action:自定义action的名字,返回的名字(str类型)要添加到domain的action部分

(6)事件event:谈话中发生的事情。

① 通用目的的Events: 设置槽位、重启对话、重置槽位、安排提醒暂停对话、继续对话、强制采取后续行动
②自动追踪events: 用户发送消息、机器回复消息、撤销用户消息、撤销action、记录一个执行的action

(7)同义词synonym:同一事物多种表达方式,提高识别成功率。

(8)追踪器tracker:维护对话状态的组件,跟踪会话状态的对象。负责接收是否有了新的对话消息。

(9)策略policy :选择执行哪个action。

(10)域domain :存放所有信息,指定了bot可操控的范围。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值