相关概念

Rasa Stack 是一组开放源码机器学习工具,供开发人员创建支持上下文的人工智能助理和聊天机器人:

• Core = 聊天机器人框架包含基于机器学习的对话管理

• NLU = 用于自然语言理解的库包含意图识别和实体提取

NLU 和 Core 是独立的。您可以使用没有 Core 的 NLU,反之亦然。我们建议两者都使用。

让我们从一个例子开始。想象一下你已经建立了一个人工智能助理来预约医生。在谈话开始时,你问你的用户你在找什么?他们回答我需要94301的家庭医生。现在是 Rasa Stack 开始工作的时候了:

rasa-ecosystem.png

1. NLU根据您之前的训练数据了解用户的信息:

• 意图分类:根据预先定义的意图解释含义(例如:我需要94301中的一个GP是一个寻找医生意图的置信度是93%)

• 实体提取:识别结构化数据(例如:gp 是医生类型和 94301 是一个邮政编码)

2. Core 决定本次对话接下来会发生什么。它是基于机器学习的对话管理,根据 NLU 的输入、对话历史和您的训练数据预测下一个最佳行动。(例如:Core 有87%的信心,预约是下一个最佳操作,与用户确认是否希望更改主要联系信息)。

尝试一下

原文链接可以直接交互,译文只能展示流程,交互效果请查看最后的原文链接体验。

本教程将向您展示构建机器人所需的不同部分。您可以在文档中直接运行代码,而无需安装任何东西,也可以安装 Rasa Core 并在本地计算机上的 Jupyter notebook 中运行示例!如果您想在本地运行这个,请转到步骤3:首先开始构建来安装 Rasa Stack 。

目标
你将建立一个友好的聊天机器人,它会问你做得怎么样,并发送一张有趣的图片给你,让你在悲伤时振作起来。

mood_bot.png

使用 RASA NLU 教 bot 了解用户输入

1. 创建 NLU 案例

你首先要教你的助手理解你的信息。为此,您将训练 NLU 模型,该模型将以简单的文本格式接收输入并提取结构化数据。这种称为意图的结构化数据将帮助bot理解您的消息。

您要做的第一件事是定义bot应该理解的用户消息。您将通过定义意图并提供一些用户表达意图的方法来实现这一点。

运行下面的代码单元将 RASA NLU 训练示例保存到文件nlu.md:

nlu_md = """## intent:greet- hey- hello- hi- good morning- good evening- hey there## intent:goodbye- bye- goodbye- see you around- see you later## intent:mood_affirm- yes- indeed- of course- that sounds good- correct## intent:mood_deny- no- never- I don't think so- don't like that- no way- not really## intent:mood_great- perfect- very good- great- amazing- wonderful- I am feeling very good- I am great- I'm good## intent:mood_unhappy- sad- very sad- unhappy- bad- very bad- awful- terrible- not very good- extremely sad- so sad"""%store nlu_md > nlu.mdprint("The data has been successfully saved inside the nlu.md file! You can move on to the next step!")## intent:greet
-hey
-hello
-hi
-good morning
-good evening
-hey there## intent:goodbye
-bye
-goodbye
-see you around
-see you later## intent:mood_affirm
-yes
-indeed
-of course
-that sounds good
-correct## intent:mood_deny
-no
-never
-I don't think so
-don't like that
-no way
-not really## intent:mood_great
-perfect
-very good
-great
-amazing
-wonderful
-I am feeling very good
-I am great
-I'm good## intent:mood_unhappy
-sad
-very sad
-unhappy
-bad
-very bad
-awful
-terrible
-not very good
-extremely sad
-so sad
"""
%store nlu_md > nlu.mdprint("The data has been successfully saved inside the nlu.md file! You can move on to the next step!")

2. 定义NLU模型配置

NLU模型配置定义如何训练NLU模型以及如何从文本输入中提取特征。在本例中,您将使用一个预定义的 TensorFlow_Embedding Pipeline,您可以在这里了解更多信息。

下面的代码块将把NLU模型配置保存到名为 nlu_config.yml 的文件中。

nlu_config = """language: enpipeline: tensorflow_embedding"""%store nlu_config > nlu_config.ymlprint("The configuration has been successfully stored inside the nlu_config.yml file. You can now move on to the next step!")3.训练 NLU 模型现在您拥有训练 NLU 模型所需的所有组件。运行下面的单元,该单元将调用 rasa.nlu 模型,传递先前定义的 nlu.md 和 nlu_config.yml 文件,并将模型保存在 models/current/nlu 目录中。from rasa_nlu.model import Metadata, Interpreterimport jsondef pprint(o): # small helper to make dict dumps a bit prettier print(json.dumps(o, indent=2))interpreter = Interpreter.load('./models/current/nlu')pprint(interpreter.parse(u"Hello"))
%store nlu_config > nlu_config.ymlprint("The configuration has been successfully stored inside the nlu_config.yml file. You can now move on to the next step!")3.训练 NLU 模型现在您拥有训练 NLU 模型所需的所有组件。运行下面的单元,该单元将调用 rasa.nlu 模型,传递先前定义的 nlu.md 和 nlu_config.yml 文件,并将模型保存在 models/current/nlu 目录中。from rasa_nlu.model import Metadata, Interpreter
import jsondef pprint(o):
# small helper to make dict dumps a bit prettierprint(json.dumps(o, indent=2))interpreter = Interpreter.load('./models/current/nlu')
pprint(interpreter.parse(u"Hello"))

4. 测试模型

现在,您可以测试模型,看看机器人是否能理解您。下面的代码块将加载您刚刚培训的模型,并返回消息hello的意向分类结果。您也可以通过编辑hello字符串对不同的消息进行测试:

from rasa_nlu.model import Metadata, Interpreterimport jsondef pprint(o): # small helper to make dict dumps a bit prettier print(json.dumps(o, indent=2))interpreter = Interpreter.load('./models/current/nlu')pprint(interpreter.parse(u"Hello"))import Metadata, Interpreter
import jsondef pprint(o):
# small helper to make dict dumps a bit prettierprint(json.dumps(o, indent=2))interpreter = Interpreter.load('./models/current/nlu')
pprint(interpreter.parse(u"Hello"))

使用 Rasa Core 指导机器人做出响应

5. 写故事

在这个阶段,您将教您的聊天机器人使用 Rasa Core 响应您的消息。 Rasa Core 将训练对话管理模型,并预测机器人应如何在对话的特定状态下做出响应。

Rasa Core 模型以训练“故事”的形式从真实的会话数据中学习。故事是用户和机器人之间的真实对话,其中用户输入表示为意图和机器人的响应被表示为动作名称。下面是一个简单对话的例子:用户向我们的机器人打招呼,机器人向我们打招呼。这就是它看起来像一个故事:

## story1* greet - utter_greet# story1
* greet- utter_greet

故事以 ## 开头 跟随着的是名字(可选)。以 * 开头的行是用户发送的消息。虽然您不写实际的消息,但它代表了用户的意图。以 - 开头的行是您的bot所采取的操作。在这种情况下,我们的所有操作都只是发送回用户的消息,比如说问候语,但是一般来说,一个操作可以做任何事情,包括调用API和与外部世界交互。

运行下面的单元格将示例故事保存在名为'stories.md'的文件中:

stories_md = """## happy path* greet - utter_greet* mood_great - utter_happy## sad path 1* greet - utter_greet* 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"""%store stories_md > stories.mdprint("The training stories have been successfully saved inside the stories.md file. You can move on to the next step!")## happy path
* greet- utter_greet
* mood_great- utter_happy## sad path 1
* greet- utter_greet
* 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
"""
%store stories_md > stories.mdprint("The training stories have been successfully saved inside the stories.md file. You can move on to the next step!")

6. 定义域

接下来我们需要做的就是定义一个域。这个域定义了你的机器人所处的世界——它应该得到什么样的用户输入,它应该能够预测什么样的动作,如何响应以及存储什么样的信息。下面是我们的bot的一个示例域,您将写入名为domain.yml的文件:

domain_yml = """intents: - greet - goodbye - mood_affirm - mood_deny - mood_great - mood_unhappyactions:- utter_greet- utter_cheer_up- utter_did_that_help- utter_happy- utter_goodbyetemplates: utter_greet: - text: "Hey! How are you?" utter_cheer_up: - text: "Here is something to cheer you up:" image: "https://i.imgur.com/nGF1K8f.jpg" utter_did_that_help: - text: "Did that help you?" utter_happy: - text: "Great carry on!" utter_goodbye: - text: "Bye""""%store domain_yml > domain.ymlprint("The domain has been successfully saved inside the domain.yml file. You can move on to the next step!")"
intents:- greet- goodbye- mood_affirm- mood_deny- mood_great- mood_unhappyactions:
- utter_greet
- utter_cheer_up
- utter_did_that_help
- utter_happy
- utter_goodbyetemplates:utter_greet:- text: "Hey! How are you?"utter_cheer_up:- text: "Here is something to cheer you up:"image: "https://i.imgur.com/nGF1K8f.jpg"utter_did_that_help:- text: "Did that help you?"utter_happy:- text: "Great carry on!"utter_goodbye:- text: "Bye"
"""
%store domain_yml > domain.ymlprint("The domain has been successfully saved inside the domain.yml file. You can move on to the next step!")

那么不同的部分意味着什么呢?

intents:你希望用户说的话。见Rasa NLU

actions:你的机器人能做和说的事情

templates:模板字符串用于bot可以说的内容

这是怎么结合起来的?Rasa Core的工作是在对话的每个步骤中选择要执行的正确操作。简单的操作只是向用户发送一条消息。这些简单的操作是域中的操作,从 utter_ 开始。他们只会根据模板部分中的模板回复一条消息。有关如何构建更有趣的操作,请参见自定义操作。

7. 训练对话模型

下一步是在我们的例子中训练一个神经网络。要执行此操作,请运行下面的命令。此命令将调用Rasa Core 训练功能,将域和故事文件传递给它,并将训练后的模型存储到models/dialogue目录中。此命令的输出将包括每个训练阶段的训练结果。

!python -m rasa_core.train -d domain.yml -s stories.md -o models/dialogueprint("Finished training! You can move on to the next step!")o models/dialogueprint("Finished training! You can move on to the next step!")

8. 和你的机器人聊天

就这样!现在你已经拥有了开始与机器人交互所需的一切!让我们使用下面的命令启动您的完整bot,包括rasa core和rasa nlu模型!

如果您没有运行上面的单元,这将不起作用!

import IPythonfrom IPython.display import clear_output, HTML, displayfrom rasa_core.agent import Agentfrom rasa_core.interpreter import RasaNLUInterpreterimport timeinterpreter = RasaNLUInterpreter('models/current/nlu')messages = ["Hi! you can chat in this window. Type 'stop' to end the conversation."]agent = Agent.load('models/dialogue', interpreter=interpreter)def chatlogs_html(messages): messages_html = "" for m in messages: if m.endswith('.jpg'): messages_html += "<img src={}, alt='Tiger pub'></img>".format(m) else: messages_html += "<p>{}</p>".format(m) chatbot_html = """<div class="chat-window" {}</div>""".format(messages_html) return chatbot_htmlwhile True: clear_output() display(HTML(chatlogs_html(messages))) time.sleep(0.3) a = input() messages.append(a) if a == 'stop': break responses = agent.handle_message(a) for r in responses: key = 'image' if 'image' in r.keys() else 'text' messages.append(r.get(key))
from IPython.display import clear_output, HTML, display
from rasa_core.agent import Agent
from rasa_core.interpreter import RasaNLUInterpreter
import timeinterpreter = RasaNLUInterpreter('models/current/nlu')
messages = ["Hi! you can chat in this window. Type 'stop' to end the conversation."]
agent = Agent.load('models/dialogue', interpreter=interpreter)def chatlogs_html(messages):messages_html = ""
for m in messages:
if m.endswith('.jpg'):messages_html += "<img src={}, alt='Tiger pub'></img>".format(m)
else:messages_html += "<p>{}</p>".format(m)chatbot_html = """<div class="chat-window" {}</div>""".format(messages_html)
return chatbot_htmlwhile True:clear_output()display(HTML(chatlogs_html(messages)))time.sleep(0.3)a = input()messages.append(a)
if a == 'stop':
breakresponses = agent.handle_message(a)
for r in responses:key = 'image' if 'image' in r.keys() else 'text'messages.append(r.get(key))

祝贺你!你刚刚从头开始构建了一个机器人,完全由机器学习提供动力。为什么不玩耍上面的代码呢?

教你的机器人更好地理解你。添加更多的NLU数据,重新导入NLU模型并重新启动bot。

添加更多的故事以提供更多关于您的bot应该如何工作的示例。然后重新训练 Rasa Core 模型来尝试它!

编辑域中的响应模板,重新导入模型并查看结果!

现在,您已经准备好构建自己的机器人了!立即安装并立即运行。

英文原文:https://rasa.com/docs/get_started_step1/

欢迎关注磐创博客资源汇总站:
http://docs.panchuang.net/

欢迎关注PyTorch官方中文教程站:
http://pytorch.panchuang.net/

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

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

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

  2. 建立人工智能助理应用程序的过程

    我们每天都会听到越来越多关于征服IT行业的虚拟助手的消息.几乎每家大公司都在尝试做一些与众不同的事情.Apple的Siri,三星的Bixby,亚马逊的Alexa,微软的Cortana等.您可以通过说& ...

  3. 为什么现在的人工智能助理都像人工智障?

    本文由 S先生(公众号:TheMisterS)授权转载,作者:S先生左脑 "我不是针对谁,只是在座现在所有做 C端智能助理的都是坑." - S先生 对群嘲做一个限定 现在:在&qu ...

  4. Alian解读SpringBoot 2.6.0 源码(六):启动流程分析之创建应用上下文

    目录 一.背景 1.1.run方法整体流程 1.2.本文解读范围 二.创建应用上下文 2.1.初始化入口 2.2.初始化AbstractApplicationContext 2.3.初始化Generi ...

  5. 工具箱支持汽车质量人工智能

    工具箱支持汽车质量人工智能 Toolkit supports automotive-quality AI NXP半导体公司推出了一个新的深度学习工具包,叫做eIQ Auto.NXP正在寻求通过使其工具 ...

  6. 创建支持ssh的docker镜像

    docker容器运行,一般不能ssh,这容器的管理带来麻烦,下面将介绍如何创建支持ssh的docker镜像 首先从dock hub  下载 ubuntu的镜像 命令: docker pull ubun ...

  7. 【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )

    文章目录 一.创建支持 Kotlin 的 NDK 项目 二.Kotlin 语言中使用 NDK 要点 1.加载动态库 2.声明 ndk 方法 3.Project 下的 build.gradle 配置 4 ...

  8. [Swift通天遁地]九、拔剑吧-(9)创建支持缩放、移动、裁切的相机视图控制器

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  9. WCF技术剖析之五:利用ASP.NET兼容模式创建支持会话(Session)的WCF服务

    原文:WCF技术剖析之五:利用ASP.NET兼容模式创建支持会话(Session)的WCF服务 在<基于IIS的WCF服务寄宿(Hosting)实现揭秘>中,我们谈到在采用基于IIS(或者 ...

最新文章

  1. Android渲染机制和丢帧分析
  2. html禁止文本选择,[译]用CSS来禁止文本选择
  3. (iPhone)怎样从photo album中获取所有图片 “****TWO*****” ---》 获取所有图片从Photo Album?...
  4. VS2010安装帮助文档出现错误
  5. 迅捷路由器 服务器无响应,如果路由器重启还是上不了网 几招搞定
  6. CSS只是进化的一部分
  7. 锐捷官方提供122套实验题.
  8. windows遍历目录下所有文件
  9. 是vans_Vans 的旧海报上原来有这么多学问…
  10. python线程的学习
  11. nagios监控安装及设置案例
  12. [C语言] 单向链表的构建以及翻转算法_图文详解(附双向链表构建代码)
  13. 能够做到这10点,成功将离你不远
  14. 2022华为软件精英挑战赛——梯度方法
  15. Python中文分词神器---jieba
  16. cv2.cvtColor() 的使用
  17. matlab 太阳系仿真,三维仿真太阳系
  18. 柳江南:校园绝品狂徒
  19. PS案例提升课视频教程
  20. android appwidget 空间动画,Android学习之AppWidget笔记分享

热门文章

  1. Redis基于内存非关系型数据库
  2. 终端定制行业分销初步设计
  3. python绘制蟒蛇,绘制五彩蟒蛇
  4. 举个栗子!Tableau 技巧(116):做一个有趣的锥状柱形图
  5. java 时区 edt_JAVA TimeZone发行EDT对EST
  6. 固态硬盘颗粒:SLC/MLC/TLC有什么区别?
  7. Zotero——一款文献管理工具
  8. 计算机教育专业的专业任选课,什么叫自由选修课 又什么叫全校任选课
  9. 我奋斗了18年才和你坐在一起喝咖啡与我奋斗了18年不是为了和你一起喝咖啡
  10. 英语天天读】Cultivating a Hobby