Rasa 聊天机器人框架使用流程(详细!!)

简述:

Rasa是一个能用于构建机器人对话系统的框架,基于Rasa框架搭建机器人对话系统,可以使用于工业各类语音智能服务场景,如:远程医疗问诊、智能客户服务、保险产品销售、金融催收服务、手机智能助手等领域。支持基于规则、填槽和机器学习来构建对话系统。主要模块包括:NLU(意图识别和实体提取)和Core(基于模型及规则进行回复)。提供了搭建对话系统的脚手架。

常用命令有:

    rasa init   # 创建项目rasa train   # 训练NLU和Core模型rasa run actions  # 启动动作服务(主要是自己写的动作,系统默认的动作,不需要单独启动)rasa shell  # 启动对话机器人 (会自动启动默认动作服务)rasa x   # 启动rasa x可视化服务

1.新建工程

第一步,执行下面命令创建一个新的工程:

rasa init

rasa init --no-prompt

rasa init命令会创建Rasa工程需要的全部文件,并在初始化样本数据上训练简单的聊天机器人。如果命令没有携带–no-prompt标志,你会遇到一些关于工程设置的提问。

使用rasa init 创建mybot项目,目录结果如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dgtIKq2l-1645008096429)(/Users/liuzepei/Library/Application Support/typora-user-images/image-20220216181439694.png)]

以下文件将会被创建:

init.py 帮助python找到你的功能的空文件
actions.py 该文件中你可以自定义执行动作的代码
config.yml ‘*’ Rasa NLU和Rasa Core模型配置文件
credentials.yml 连接到其他服务的详细配置
data/nlu.yml ‘*’ Rasa NLU训练数据
data/stories.yml ‘*’ 故事编写文件
domain.yml ‘*’ 智能助手功能定义领域文件
endpoints.yml 连接到像fb messenger通道的详细配置
models/.tar.g 初始化模型

‘*’标记的是非常重要的文件,在该教程里有它们详细的解释。

2.查看和构建NLU训练数据

Rasa智能助手第一部分就是NLU模型。NLU是Natural Language Understanding的简称,它的意思就是把用户各种信息转换为结构化数据。要使用Rasa完成该功能,你需要提供训练语料,Rasa能够通过这些语料学习如何理解用户信息。并且Rasa能使用这些语料训练模型。

在rasa init 命令新建工程目录里,运行下面命令查看训练配置文件:

cat data/nlu.yml 

数据样例:

nlu.yml 用于训练nlu模型的训练数据

version: "2.0"
nlu:
- intent: greetexamples: |- 你好- 上午好- 中午好- 嗨
- intent: goodbyeexamples: |- 再见- 回头见- 晚安
- intent: affirmexamples: |- 是的- 有- 当然- 听上去不错
- intent: denyexamples: |- 不- 不要- 没有- 没有- 我不喜欢
- intent: mood_greatexamples: |- 太好了- 感觉不错- 非常好
- intent: mood_unhappyexamples: |- 不开心- 感到沮丧- 不高兴- 不太好
- intent: bot_challengeexamples: |- 你是个机器人吗?- 你是人类吗?- 我是在和机器人讲话吗?- 我是在和人类讲话吗?
- intent: request_namesexamples: |- 我想告诉你姓名- 你知道我的姓名吗?
- lookup: namesexamples: |- 猪八戒- 孙悟空- 沙悟净- 唐三藏

rules.yml 规则,根据用户意图,进行具体的动作(包括查询、填槽、回复等)

version: "2.0"rules:- rule: Say goodbye anytime the user says goodbyesteps:- intent: goodbye- action: utter_goodbye- rule: Say 'I am a bot' anytime the user challengessteps:- intent: bot_challenge- action: utter_iamabot- rule: Activate formsteps:- intent: request_names- action: name_form- active_loop: name_form- rule: Submit formcondition:- active_loop: name_formsteps:- action: name_form- active_loop: null- slot_was_set:- requested_slot: null- action: utter_submit- action: utter_slots_values

3.定义模型配置

通过配置文件定义你的模型需要用到的Rasa NLU和Rasa Core的组件。在该例子中,NLU模型将使用supervised_embeddings 管道。你可以通过页面here了解不同的NLU 管道。

通过下面命令查看模型配置文件:

cat config.yml  # 即对NLU和Core模型的配置
cat domain.yml  # 领域配置
credentials.yml # 证书配置,用于调用语音通道的接口
endpoints.yml # 端点配置,如:机器人要使用的模型、动作、存储服务等

language 和 pipeline主要确定应该如何构建Rasa NLU模型。policies主要定义Rasa Core模型使用的策略policies。

涉及配置文件有:

config.yml 即对NLU和Core模型的配置

# Configuration for Rasa NLU.
# https://rasa.com/docs/rasa/nlu/components/
language: zhpipeline:
# # No configuration for the NLU pipeline was provided. The following default pipeline was used to train your model.
# # If you'd like to customize it, uncomment and adjust the pipeline.
# # See https://rasa.com/docs/rasa/tuning-your-model for more information.
#   - name: WhitespaceTokenizer- name: JiebaTokenizer #支持中文- name: RegexFeaturizer- name: LexicalSyntacticFeaturizer- name: CountVectorsFeaturizer- name: CountVectorsFeaturizeranalyzer: char_wbmin_ngram: 1max_ngram: 4- name: DIETClassifierepochs: 100constrain_similarities: true- name: EntitySynonymMapper- name: ResponseSelectorepochs: 100constrain_similarities: true- name: FallbackClassifierthreshold: 0.3ambiguity_threshold: 0.1# Configuration for Rasa Core.
# https://rasa.com/docs/rasa/core/policies/
policies:
# # No configuration for policies was provided. The following default policies were used to train your model.
# # If you'd like to customize them, uncomment and adjust the policies.
# # See https://rasa.com/docs/rasa/policies for more information.- name: MemoizationPolicy- name: TEDPolicymax_history: 5epochs: 100constrain_similarities: true- name: RulePolicy

domain.yml 领域配置

version: '2.0'
config:store_entities_as_slots: true
session_config:session_expiration_time: 60carry_over_slots_to_new_session: true
intents:
- greet:use_entities: true
- deny:use_entities: true
- request_names:use_entities: true
- goodbye:use_entities: true
- affirm:use_entities: true
- mood_great:use_entities: true
- mood_unhappy:use_entities: true
- bot_challenge:use_entities: true
entities: []
slots:first_name:type: rasa.shared.core.slots.TextSlotinitial_value: nullauto_fill: trueinfluence_conversation: truelast_name:type: rasa.shared.core.slots.TextSlotinitial_value: nullauto_fill: trueinfluence_conversation: truename_spelled_correctly:type: rasa.shared.core.slots.BooleanSlotinitial_value: nullauto_fill: trueinfluence_conversation: truerequested_slot:type: rasa.shared.core.slots.UnfeaturizedSlotinitial_value: nullauto_fill: trueinfluence_conversation: false
responses:utter_greet:- text: 嗨,你好吗?utter_cheer_up:- image: https://i.imgur.com/nGF1K8f.jpgtext: '这是某些使你嗨起来的东西:'utter_did_that_help:- text: 这对你有帮助吗?utter_happy:- text: 好极了,继续!utter_goodbye:- text: 再见utter_iamabot:- text: 我是个机器人,由Rasa框架提供支持.utter_ask_first_name:- text: 你贵姓?utter_ask_last_name:- text: 你的名字?utter_ask_name_spelled_correctly:- buttons:- payload: /affirmtitle:- payload: /denytitle:text:{first_name} 拼写对了吗?utter_submit:- text: 好的,谢谢!utter_slots_values:- text: 我记住你了, {first_name} {last_name}!
actions:
- utter_greet
- utter_slots_values
- utter_submit
- validate_name_form
forms:name_form:first_name:- type: from_textlast_name:- type: from_text
e2e_actions: []

credentials.yml 证书配置,用于调用语音通道的接口

# This file contains the credentials for the voice & chat platforms
# which your bot is using.
# https://rasa.com/docs/rasa/messaging-and-voice-channelsrest:
#  # you don't need to provide anything here - this channel doesn't
#  # require any credentials#facebook:
#  verify: "<verify>"
#  secret: "<your secret>"
#  page-access-token: "<your page access token>"#slack:
#  slack_token: "<your slack token>"
#  slack_channel: "<the slack channel>"
#  slack_signing_secret: "<your slack signing secret>"#socketio:
#  user_message_evt: <event name for user message>
#  bot_message_evt: <event name for bot messages>
#  session_persistence: <true/false>#mattermost:
#  url: "https://<mattermost instance>/api/v4"
#  token: "<bot token>"
#  webhook_url: "<callback URL>"# This entry is needed if you are using Rasa X. The entry represents credentials
# for the Rasa X "channel", i.e. Talk to your bot and Share with guest testers.
rasa:url: "http://localhost:5002/api"

endpoints.yml 端点配置,如:机器人要使用的模型、动作、存储服务等

# This file contains the different endpoints your bot can use.# Server where the models are pulled from.
# https://rasa.com/docs/rasa/model-storage#fetching-models-from-a-server#models:
#  url: http://my-server.com/models/default_core@latest
#  wait_time_between_pulls:  10   # [optional](default: 100)# Server which runs your custom actions.
# https://rasa.com/docs/rasa/custom-actions#action_endpoint:
#  url: "http://localhost:5055/webhook"
action_endpoint:url: "http://localhost:5055/webhook"# Tracker store which is used to store the conversations.
# By default the conversations are stored in memory.
# https://rasa.com/docs/rasa/tracker-stores#tracker_store:
#    type: redis
#    url: <host of the redis instance, e.g. localhost>
#    port: <port of your redis instance, usually 6379>
#    db: <number of your database within redis, e.g. 0>
#    password: <password used for authentication>
#    use_ssl: <whether or not the communication is encrypted, default false>#tracker_store:
#    type: mongod
#    url: <url to your mongo instance, e.g. mongodb://localhost:27017>
#    db: <name of the db within your mongo instance, e.g. rasa>
#    username: <username used for authentication>
#    password: <password used for authentication>
tracker_store:type: SQLdialect: sqlitedb: trackers.db# Event broker which all conversation events should be streamed to.
# https://rasa.com/docs/rasa/event-brokers#event_broker:
#  url: localhost
#  username: username
#  password: password
#  queue: queue
event_broker:type: SQLdialect: sqlitedb: events.db

names.txt 本案例使用的姓名列表

孙悟空
猪八戒
唐三藏
沙悟净
诸葛青云

4.编写你的第一个故事

在这一节,你要教你的助手如何响应你的消息。这被称作对话管理,由你的Rasa Core模型来处理。Rasa Core模型以训练”stories”方式从真实的会话数据中学习。一个故事代表用户和助手之间真实的会话过程。

intents和entities行反应了用户的输入,action names 展示助手如何响应用户输入。

一个对话的例子。用户说hello,助手也回复hello。这就是为什么它看起来像一个故事。你可以在 Stories中查看详细信息。

以 - 开始的行是助手的action(执行动作)。在这个会话中,所有的action是把用户的信息原样返回,像utter_greet。但是,通常action可以做任何事情,包括调用api,与外部交互等。

运行下面的命令可以查看data/stories.md文件下的故事例子:

cat data/stories.md

stories.yml 故事情节,描述对话的流程

version: "2.0"stories:- story: happy pathsteps:- intent: greet- action: utter_greet- intent: mood_great- action: utter_happy- story: sad path 1steps:- intent: greet- action: utter_greet- intent: mood_unhappy- action: utter_cheer_up- action: utter_did_that_help- intent: affirm- action: utter_happy- story: sad path 2steps:- intent: greet- action: utter_greet- intent: mood_unhappy- action: utter_cheer_up- action: utter_did_that_help- intent: deny- action: utter_goodbye- story: interactive_story_1steps:- intent: greet- action: utter_greet- intent: request_names- action: name_form- active_loop: name_form- slot_was_set:- requested_slot: first_name- slot_was_set:- name_spelled_correctly: None- slot_was_set:- first_name: None- slot_was_set:- requested_slot: last_name- slot_was_set:- name_spelled_correctly: None- slot_was_set:- last_name: None- slot_was_set:- requested_slot: null- active_loop: null- action: utter_submit- action: utter_slots_values

5.定义领域

接下来我们要定义一个领域。这个领域定义了你的助手所能处理的边界:希望用户输入什么样的信息,能够预测哪些执行动作,如何响应用户,存储什么样的信息。助手的领域存储在文件domain.yml中:

cat domain.yml

那么不同部分的意思是什么?

intents 你希望用户说的
actions 助手能做的和说的
templates 助手说的话术模板

这些是如何组合的呢?Rasa Core的工作就是在会话的每一步选择一个正确的action去执行。在该例子中,我们的actions只是简单的发送一个消息给用户。这些简单的话语执行的就是domain中以utter_开头的actions。助手将从templates中选择一个模板响应消息。参考 Custom Actions创建自定义action。

涉及的配置文件在上文已经逐一列出,下面看看自定义的action是如何写的,主要完成了输入验证的过程,默认的action是以utter_打头,系统默认支

import yaml
import pathlib
from typing import Text, List, Any, Dict, Optionalfrom rasa_sdk import Tracker, FormValidationAction
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.types import DomainDictnames = pathlib.Path("names.txt").read_text().split("\n")class ValidateNameForm(FormValidationAction):def name(self) -> Text:return "validate_name_form"async def required_slots(self,slots_mapped_in_domain: List[Text],dispatcher: "CollectingDispatcher",tracker: "Tracker",domain: "DomainDict",) -> Optional[List[Text]]:first_name = tracker.slots.get("first_name")if first_name is not None:if first_name not in names:return ["name_spelled_correctly"] + slots_mapped_in_domainreturn slots_mapped_in_domainasync def extract_name_spelled_correctly(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict) -> Dict[Text, Any]:intent = tracker.get_intent_of_latest_message()return {"name_spelled_correctly": intent == "affirm"}def validate_name_spelled_correctly(self,slot_value: Any,dispatcher: CollectingDispatcher,tracker: Tracker,domain: DomainDict,) -> Dict[Text, Any]:"""Validate `first_name` value."""if tracker.get_slot("name_spelled_correctly"):return {"first_name": tracker.get_slot("first_name"), "name_spelled_correctly": True}return {"first_name": None, "name_spelled_correctly": None}def validate_first_name(self,slot_value: Any,dispatcher: CollectingDispatcher,tracker: Tracker,domain: DomainDict,) -> Dict[Text, Any]:"""Validate `first_name` value."""# If the name is super short, it might be wrong.print(f"姓 ={slot_value}长度 ={len(slot_value)}")if len(slot_value) <=1:dispatcher.utter_message(text=f"姓太短了,你确定拼写对了?")return {"first_name": None}else:return {"first_name": slot_value}def validate_last_name(self,slot_value: Any,dispatcher: CollectingDispatcher,tracker: Tracker,domain: DomainDict,) -> Dict[Text, Any]:"""Validate `last_name` value."""# If the name is super short, it might be wrong.print(f"名字 ={slot_value}长度 ={len(slot_value)}")if len(slot_value) <= 1:dispatcher.utter_message(text=f"名字太短了,你确定拼写对了?")return {"last_name": None}else:return {"last_name": slot_value}

6.训练模型

一旦我们添加了新的NLU或Core数据,或者更新领域或配置,就要在样本故事和NLU数据上重新训练神经网络。可以通过使用下面命令重新训练神经网络。该命令将调用Rasa 的Core和NLU训练功能并且把训练好的模型存储到models/文件夹下。该命令将自动重新训练Rasa的数据或配置改变部分的模型。

rasa train

rasa train命令将查找NLU和CORE的数据并训练组合模型。

图示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qVfxKaYp-1645008096430)(/Users/liuzepei/Library/Application Support/typora-user-images/image-20220216182032542.png)]

7.使用你的助手

恭喜!你刚刚建立了一个完全由机器学习驱动的助手。

下一步就是测试它!如果你在本机学习该教程,开始通过下面命令启动和使用你的助手吧:

rasa shell

启动机器人进行对话

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xHbgqHxW-1645008096430)(/Users/liuzepei/Library/Application Support/typora-user-images/image-20220216182605475.png)]

8.创建 rasa x

你可以使用Rasa X收集更多会话来改进你的助手:

在项目目录下启动rasa X 命令如下:

rasa x

启动示例:

如果是在服务器上开启服务,IE浏览器登录时填上服务器的ip地址,例如:192.168.10.192:5002

输入密码图示:

登录后图示:

功能展示界面:

Rasa 聊天机器人框架使用流程相关推荐

  1. 聊天机器人框架Rasa资源整理

      Rasa是一个主流的构建对话机器人的开源框架,它的优点是几乎覆盖了对话系统的所有功能,并且每个模块都有很好的可扩展性.参考文献收集了一些Rasa相关的开源项目和优质文章. 一.Rasa介绍 1.R ...

  2. 【聊天机器人】您必须了解的最佳聊天机器人框架

    在本博客中,我们将讨论 7 大聊天机器人开发框架. 聊天机器人现在已成为许多企业不可或缺的一部分.他们利用聊天机器人提供客户支持服务.聊天机器人增强了人工代理以提供客户服务支持.企业每天都会收到大量查 ...

  3. rasa聊天机器人_Rasa-X是持续改进聊天机器人的独特方法

    rasa聊天机器人 介绍 (Introduction) When it comes to chatbot improvement, three elements are paramount: 在改善聊 ...

  4. html 简单机器人对话页面,简单的js聊天机器人框架BotUI

    Botui是一款简单的js聊天机器人框架.Botui基于Vue.js,通过简单配置,就可以制作出一个功能简单的聊天机器人,非常神奇. 使用方法 在页面中引入botui.min.css和botui-th ...

  5. uibot在子程序执行js失败_JS聊天机器人框架BotUI 使用笔记

    前言 一款自动回复文字.图片.视频的JS聊天机器人框架 BotUI,可以自由设置多种选项.触发关键词.输入框等内容,聊天内容或范围也可以自由设置,回复内容可以是文字.图片(GIF亦可).视频,我在博客 ...

  6. rasa算法_Rasa 聊天机器人框架使用

    一.Rasa Rasa是一个开源机器学习框架,用于构建上下文AI助手和聊天机器人. Rasa有两个主要模块: Rasa NLU :用于理解用户消息,包括意图识别和实体识别,它会把用户的输入转换为结构化 ...

  7. python开源聊天机器人ChatterBot——聊天机器人搭建、流程分析、源码分析

    开源聊天机器人ChatterBot 3.1  ChatterBot简介 ChatterBot是一个Python库,可以轻松生成对用户输入的自动响应.ChatterBot使用一系列机器学习算法来产生不同 ...

  8. 开源对话机器人框架:Rasa概述【中小型公司使用Rasa框架,降低准入门槛。灵活性不够高】【可以本地部署】【保护数据隐私(其他框架需要将自己的数据上传到框架官方云服务器,不安全)】【可以重写一些类】

    一.Rasa安装与使用 1.安装 pip3 install rasa-x --extra-index-url https://pypi.rasa.com/simple 2.创建项目 rasa init ...

  9. Rasa Stack:创建支持上下文的人工智能助理和聊天机器人教程

    相关概念 Rasa Stack 是一组开放源码机器学习工具,供开发人员创建支持上下文的人工智能助理和聊天机器人: • Core = 聊天机器人框架包含基于机器学习的对话管理 • NLU = 用于自然语 ...

最新文章

  1. 【 Linux 】Vim学习指南
  2. 6410 gpio控制及接口
  3. android: a system image must be selected to continmue
  4. 百亿身家中年男子告别房地产转行学Python,我们推荐他读这6本书
  5. 【java】java 随机数 Random ThreadLocalRandom SecureRandom
  6. python语言入门t_Python基础学习
  7. 硬件开发板-嵌入式开发
  8. Linux硬链接与软链接的区别
  9. linux apache支持ipv6,如何在Nginx和Apache中启用IPv6?
  10. 【语音合成】基于matlab重叠相加法的信号分帧与还原【含Matlab源码 568期】
  11. 学习webpack系列之四 ---- (学习开发环境)
  12. C语言程序设计 循环结构程序设计
  13. python描述对象静态特性的数据为_要设置单选按钮,应使用的控件是:_学小易找答案...
  14. 牛客网经典120道Java面试常见题(试题+答案)
  15. 40 篇原创干货,带你进入 Spring Boot 殿堂!
  16. AI笔记: 数学基础之方向导数的计算和梯度
  17. 从0到1智能风控决策引擎构建
  18. BeyondCorp 打造得物零信任安全架构
  19. 惠普1005打印机自检页_hp1005打印机自检报告.docx
  20. 生命如歌-五年级每日一记

热门文章

  1. 一次奇怪的服务器响应延时分析
  2. css选择器及其优先级
  3. 【java初学】面向对象继承
  4. 英语四六级常用八种时态
  5. 章鱼体验思杰第二天:
  6. python社区发现对gml文件的分析,在Python中从GML文件中提取数据
  7. 日历类报表可以这样实现
  8. css:currentColor和inherit属性的区别
  9. 2022.05面试总结
  10. 阿里云 Windows Server 2012 R2 使用FileZilla Server 快速搭建FTP服务器