让自家的智能语音助手实现todo任务的添加

我家的树莓派在成为了“智能语音助手”后,经过rasa学习训练,已经可以帮忙查日期/时间,查天气预报,进行一些简单的闲聊。但是,我希望它的功能还可以再强大些,比如说,可以帮我记录todo任务。为了实现这一目标,又花了一周时间,终于在今天实现了这个功能。

要实现这个功能,说白了,就是定义一个todo class,然后通过rasa 的自定义actions来调用这个class,从而实现todo task的创建、查询、删除这几个基本功能。

插一句话:接下来分享的代码,都是基于我的1.4.0版rasa来说的,要在其他版本上使用,需要根据相应版本的规则自行适配。

1 增加nlu.md中的intents

2 domain.yml作相应调整(只列新增部分)

3 stories.md新增故事

4 在actions.py中定义domain中出现的actions和forms

4.1 显示任务列表

class ActionShowTasks(Action):
    def name(self) -> Text:
        return "action_show_tasks"
    
    def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:

        todos = todo.Todo()
        todolist = todos.all()
        result = ""
        for todo_dict in todolist:
            result += " 编号 " + str(todo_dict.id) + " 任务 " + todo_dict.content
        dispatcher.utter_message(text=result)

        return []

4.2 新增todo task

class AddTaskForm(FormAction):
    def name(self) -> Text:
        return "add_task_form"

    @staticmethod
    def required_slots(tracker: Tracker) -> List[Text]:
        return ["content"]

    def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
        return {
            "content": [self.from_entity(entity="content"), self.from_text()]
        }

    def submit(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> Dict[Text, Any]:
        
        todos = todo.Todo()
        content = tracker.get_slot("content")
        if content is not None and content != "":
            todos.content = content
            todos.save()
            todolist = todos.all()
            msg = "新增任务 编号 " + str(todolist[-1].id) + " 任务 " + todolist[-1].content
        else:
            msg = "任务创建失败"
        dispatcher.utter_message(text=msg)
        return [SlotSet("content", None)]

4.3 查询todo task

class ActionQueryTaskForm(FormAction):
    def name(self) -> Text:
        return "query_task_form"

    @staticmethod
    def required_slots(tracker: Tracker) -> List[Text]:
        return ["id"]
    
    def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
        return {
            "id": [self.from_entity(entity="id"), self.from_text()]
        }

    def submit(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> Dict[Text, Any]:
        
        todos = todo.Todo()
        tid = tracker.get_slot("id")
        if tid is not None and tid != "": 
           if is_int(tid) and int(tid) >0:
              task = todos.getById(int(tid))
              if task is not None:
                  msg = "定位任务 编号 " + str(task.id) + " 任务 " + task.content
                  dispatcher.utter_message(text=msg)
              else:
                  dispatcher.utter_message(text='任务不存在')
           else:
              dispatcher.utter_message(text='编号格式错误')
        else:
           dispatcher.utter_message(text='未收到正确信息')
        return [SlotSet("id", None)]

4.4 删除指定todo task

class ActionDeleteTaskForm(FormAction):
    def name(self) -> Text:
        return "delete_task_form"
    
    @staticmethod
    def required_slots(tracker: Tracker) -> List[Text]:
        return ["id"]

    def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
        return {
            "id": [self.from_entity(entity="id"), self.from_text()]
        }

    def submit(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> Dict[Text, Any]:
        
        tid = tracker.get_slot("id")
        if tid is not None and tid != "":
           if is_int(tid) and int(tid) > 0:
              todos = todo.Todo()
              taskid = int(tid)
              todos.id = taskid
              todos.delete()
              msg = "任务已删除"
              dispatcher.utter_message(text=msg)
           else:
              dispatcher.utter_message(text='编号格式错误')
        else:
           dispatcher.utter_message(text='未收到有效信息')
        return [SlotSet("id", None)]

如上就是四个功能的实现代码。其中,定位和删除需要判断输入的slot值是否为数字,就定义了一个检测函数来实现数字判断。

def is_int(string: Text) -> bool:
    try:
       int(string)
       return True
    except ValueError:
       return False

actions.py文件头部需要引用的模块罗列如下:

from typing import Any, Text, Dict, List, Union, Optional
from datetime import datetime, timedelta
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormAction
from rasa_sdk.events import SlotSet
import ssl
from urllib import request, parse
import json
import todo

完成上述四个文件的编写后,在模型训练前请确认一下config.yml,我的模型用的是MitieNLP和jieba,网上大多数人用的是SpacyNLP,所以你要根据自己的实际情况来修改。如下是我的config.yml。

# Configuration for Rasa NLU.
# https://rasa.com/docs/rasa/nlu/components/
language: zh
# pipeline: supervised_embeddings
pipeline:
  - name: MitieNLP
    model: data/total_word_feature_extractor_zh.dat
  - name: JiebaTokenizer
  - name: MitieEntityExtractor
  - name: EntitySynonymMapper
  - name: RegexFeaturizer
  - name: MitieFeaturizer
  - name: SklearnIntentClassifier

# Configuration for Rasa Core.
# https://rasa.com/docs/rasa/core/policies/
policies:
  - name: MemoizationPolicy
  - name: KerasPolicy
  - name: MappingPolicy
  - name: FormPolicy

执行rasa train完成模型训练,同时记得执行rasa run actions –actions actions来注册todo相关的四个actions。模型生成后,运行rasa shell测试通过,就可以让智能语音助手来执行对应的todo指令了。

写在后面:

1.最后的语音对话图请忽略我的识别耗时,直接用sherpa-ncnn的测试代码跑wav识别耗时很少,可能还是我的代码需要进一步优化。我所使用的语音助手整合代码请看这篇博文《树莓派智能语音助手之功能整合

2.todo的新增,查询和删除使用了form,但我暂时没有找到rasa1.4.0对应form的全部完整定义规则,导致实际运行时会出现“This can throw of the prediction. Make sure to include training examples in your stories for the different types of slots this action can return.

”的warning,不影响运行结果,但应该是属于stories定义没写完整。有知道怎么解决的朋友也可以教教我。

3. 由于我在查询和删除form中使用了同一个slot——“id”,结果当我执行完查询马上执行删除时,tracker.get_slot(‘id’)会直接读取前一个form中的slot值。因此需要reset slot,即把class结束的return[]改成“return [SlotSet("id", None)]”。

4. actions中import todo所引用的todo class代码请看这篇博文《用python实现todo功能》。

5. 若不清楚怎么进行rasa的模型训练,可以看这篇博文《树莓派智能语音助手之首次RASA模型训练》。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天飓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值