高校保卫处网站建设工作,海南有线微信公众号,上海论坛社区,网站的建设方法有哪些Rasa是一个主流的构建对话机器人的开源框架#xff0c;它的优点是几乎覆盖了对话系统的所有功能#xff0c;并且每个模块都有很好的可扩展性。参考文献收集了一些Rasa相关的开源项目和优质文章。
一.Rasa介绍
1.Rasa本地安装
直接Rasa本地安装一个不好的地方就是容易把本地… Rasa是一个主流的构建对话机器人的开源框架它的优点是几乎覆盖了对话系统的所有功能并且每个模块都有很好的可扩展性。参考文献收集了一些Rasa相关的开源项目和优质文章。
一.Rasa介绍
1.Rasa本地安装
直接Rasa本地安装一个不好的地方就是容易把本地计算机的Python包版本弄乱建议使用Python虚拟环境进行安装
pip3 install -U --user pip pip3 install rasa2.Rasa Docker Compose安装
查看本机Docker和Docker Compose版本 docker-compose.yml文件如下所示
version: 3.0
services:rasa:image: rasa/rasaports:- 5005:5005volumes:- ./:/appcommand: [run, --enable-api, --debug, --cors, *]3.Rasa命令介绍
用到的相关的Rasa命令如下所示
rasa init创建一个新的项目包含示例训练数据actions和配置文件。
rasa run使用训练模型开启一个Rasa服务。
rasa shell通过命令行的方式加载训练模型然后同聊天机器人进行对话。
rasa train使用NLU数据和stories训练模型模型保存在./models中。
rasa interactive开启一个交互式的学习会话通过会话的方式为Rasa模型创建一个新的训练数据。
telemetryConfiguration of Rasa Open Source telemetry reporting.
rasa test使用测试NLU数据和stories来测试Rasa模型。
rasa visualize可视化stories。
rasa data训练数据的工具。
rasa export通过一个event broker导出会话。
rasa evaluate评估模型的工具。
-h, --help帮助命令。
--version查看Rasa版本信息。
rasa run actions使用Rasa SDK开启action服务器。
rasa x在本地启动Rasa X。4.Rasa GitHub源码结构
Rasa的源码基本上都是用Python实现的
二.Rasa项目基本流程
1.使用rasa init初始化一个项目
使用rasa init初始化聊天机器人项目
.
├── actions
│ ├── __init__.py
│ └── actions.py
├── config.yml
├── credentials.yml
├── data
│ ├── nlu.yml
│ └── stories.yml
├── domain.yml
├── endpoints.yml
├── models
│ └── timestamp.tar.gz
└── tests└── test_stories.yml2.准备自定义的NLU训练数据
nlu.yml部分数据如下
version: 3.1nlu:
- intent: greetexamples: |- hey- hello- hi- hello there- good morning- good evening- moin- hey there- lets go- hey dude- goodmorning- goodevening- good afternoon上面的intent: greet表示意图为great下面的是具体的简单例子。稍微复杂点的例子格式是[实体值]实体类型名比如[明天]日期[上海]城市的天气如何其中的日期和城市就是NLP中实体识别中的实体了。除了intent必须外该文件还可以包含同义词synonym、正则表达式regex和查找表lookup等。
3.配置NLU模型
最主要就是pipeline的配置了。相关的config.yml文件如下
pipeline:
# # No configuration for the NLU pipeline was provided. The following default pipeline was used to train your model.
# # If youd like to customize it, uncomment and adjust the pipeline.
# # See https://rasa.com/docs/rasa/tuning-your-model for more information.
# - name: WhitespaceTokenizer
# - name: RegexFeaturizer
# - name: LexicalSyntacticFeaturizer
# - name: CountVectorsFeaturizer
# - name: CountVectorsFeaturizer
# analyzer: char_wb
# min_ngram: 1
# max_ngram: 4
# - name: DIETClassifier
# epochs: 100
# constrain_similarities: true
# - name: EntitySynonymMapper
# - name: ResponseSelector
# epochs: 100
# constrain_similarities: true
# - name: FallbackClassifier
# threshold: 0.3
# ambiguity_threshold: 0.1pipeline主要是分词组件、特征提取组件、NER组件和意图分类组件等通过NLP模型进行实现并且组件都是可插拔可替换的。
4.准备story数据
stories.yml文件如下
version: 3.1stories:- 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这里面可看做是用户和机器人一个完整的真实的对话流程对话策略可通过机器学习或者深度学习的方式从其中进行学习。
5.定义domain
domain.yml文件如下
version: 3.1intents:- greet- goodbye- affirm- deny- mood_great- mood_unhappy- bot_challengeresponses:utter_greet:- text: Hey! How are you?utter_cheer_up:- text: Here is something to cheer you up:image: https://i.imgur.com/nGF1K8f.jpgutter_did_that_help:- text: Did that help you?utter_happy:- text: Great, carry on!utter_goodbye:- text: Byeutter_iamabot:- text: I am a bot, powered by Rasa.session_config:session_expiration_time: 60 #单位是min设置为0表示无失效期carry_over_slots_to_new_session: true #设置为false表示不继承历史词槽领域(domain)中包含了聊天机器人的所有信息包括意图(intent)、实体(entity)、词槽(slot)、动作(action)、表单(form)和回复(response)等。
6.配置Rasa Core模型
最主要就是policies的配置了。相关的config.yml文件如下
# 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 youd like to customize them, uncomment and adjust the policies.
# # See https://rasa.com/docs/rasa/policies for more information.
# - name: MemoizationPolicy
# - name: RulePolicy
# - name: UnexpecTEDIntentPolicy
# max_history: 5
# epochs: 100
# - name: TEDPolicy
# max_history: 5
# epochs: 100
# constrain_similarities: truepolicies主要就是对话策略的配置常用的包括TEDPolicy、UnexpecTEDIntentPolicy、MemoizationPolicy、AugmentedMemoizationPolicy、RulePolicy和Custom Policies等并且策略之间也是有优先级顺序的。
7.使用rasa train训练模型
rasa train
或者
rasa train nlu
rasa train core使用data目录中的数据作为训练数据使用config.yml作为配置文件并将训练后的模型保存到models目录中。
8.使用rasa test测试模型
通常把数据分为训练集和测试集在训练集上训练模型在测试集上测试模型
rasa data split nlu
rasa test nlu -u test_set.md --model models/nlu-xxx.tar.gz说明当然也是可以通过交叉验证的方式来评估模型的。
9.让用户使用聊天机器人
可以通过shell用指定的模型进行交互
rasa shell -m models/nlu-xxx.tar.gz还可以通过rasa run --enable-api这种rest方式进行交互。如下
三.Rasa系统架构
1.Rasa处理消息流程 下图展示了从用户的Message输入到用户收到Message的基本流程 步骤1用户输入的Message传递到Interpreter(NLP模块)然后识别Message中的意图(intent)和提取实体(entity)。 步骤2Rasa Core将Interpreter提取的intent和entity传递给Tracker然后跟踪记录对话状态。 步骤3Tracker把当前状态和历史状态传递给Policy。 步骤4Policy根据当前状态和历史状态进行预测下一个Action。 步骤5Action完成预测结果并将结果传递到Tracker成为历史状态。 步骤6Action将预测结果返回给用户。
2.Rasa系统结构 Rasa主要包括Rasa NLU(自然语言理解即图中的NLU Pipeline)和Rasa Core(对话状态管理即图中的Dialogue Policies)两个部分。Rasa NUL将用户的输入转换为意图和实体信息。Rasa Core基于当前和历史的对话记录决策下一个Action。 除了核心的自然语言理解(NLU)和对话状态管理(DSM)外还有Agent代理系统Action Server自定义后端服务系统通过HTTP和Rasa Core通信辅助系统Tracker Store、Lock Store和Event Broker等。还有上图没有显示的channel它连接用户和对话机器人支持多种主流的即时通信软件对接Rasa。 (1)Agent组件从用户角度来看主要是接收用户输入消息返回Rasa系统的回答。从Rasa角度来看它连接自然语言理解(NLU)和对话状态管理(DSM)根据Action得到回答并且保存对话数据到数据库。 (2)Tracker Store将用户和Rasa机器人的对话存储到Tracker Store中Rasa提供的开箱即用的系统包括括PostgreSQL、SQLite、Oracle、Redis、MongoDB、DynamoDB当然也可以自定义存储。 (3)Lock Store一个ID产生器当Rasa集群部署的时候会用到当消息处于活动状态时锁定会话以此保证消息的顺序处理。 (4)Event Broker简单理解就是一个消息队列把Rasa消息转发给其它服务来处理包括RabbitMQ、Kafka等。 (5)FileSystem保存训练好的模型可以放在本地磁盘、云服务器等位置。 (6)Action Server通过rasa-sdk可以实现Rasa的一个热插拔功能比如查询天气预报等。
参考文献 [1]Rasa 3.x官方文档https://rasa.com/docs/rasa/ [2]Rasa Action Serverhttps://rasa.com/docs/action-server/ [3]Rasa Enterprisehttps://rasa.com/docs/rasa-enterprise/ [4]Rasa Bloghttps://rasa.com/blog/ [5]Rasa GitHubhttps://github.com/rasahq/rasa [6]Awesome-Chinese-NLPhttps://github.com/crownpku/Awesome-Chinese-NLP [7]BotSharp文档https://botsharp.readthedocs.io/en/latest/ [8]BotSharp GitHubhttps://github.com/SciSharp/BotSharp [9]rasa-ui GitHubhttps://github.com/paschmann/rasa-ui [10]rasa-ui Giteehttps://gitee.com/jindao666/rasa-ui [11]rasa_chatbot_cnhttps://github.com/GaoQ1/rasa_chatbot_cn [12]Rasa_NLU_Chihttps://github.com/crownpku/Rasa_NLU_Chi [13]nlp-architecthttps://github.com/IntelLabs/nlp-architect [14]rasa-nlp-architecthttps://github.com/GaoQ1/rasa-nlp-architect [15]rasa_shopping_bothttps://github.com/whitespur/rasa_shopping_bot [16]facebook/ducklinghttps://github.com/facebook/duckling [17]rasa-voice-interfacehttps://github.com/RasaHQ/rasa-voice-interface [18]Rasahttps://github.com/RasaHQ [19]ymcui/Chinese-BERT-wwmhttps://github.com/ymcui/Chinese-BERT-wwm [20]Hybrid Chathttps://gitlab.expertflow.com/expertflow/hybrid-chat [21]rasa-nlu-trainerhttps://rasahq.github.io/rasa-nlu-trainer [22]crownpku/Rasa_NLU_Chihttps://github.com/crownpku/rasa_nlu_chi [23]jiangdongguo/ChitChatAssistanthttps://github.com/jiangdongguo/ChitChatAssistant [24]Rasa框架应用https://www.zhihu.com/column/c_1318281710002663424 [25]Rasa开源引擎介绍https://zhuanlan.zhihu.com/p/331806270 [26]Rasa聊天机器人专栏开篇https://cloud.tencent.com/developer/article/1550247 [27]rasa-nlu的究极形态及rasa的一些难点https://www.jianshu.com/p/553e37ffbac0 [28]Rasa官方文档手册https://juejin.cn/post/6844903922042142734 [29]Rasa官方视频教程https://www.bilibili.com/video/BV1xC4y1H7HG?p1 [30]用Rasa NLU构建自己的中文NLU系统http://www.crownpku.com/2017/07/27/用Rasa_NLU构建自己的中文NLU系统.html [31]Rasa Core开发指南https://blog.csdn.net/AndrExpert/article/details/92805022