6 Star 35 Fork 22

Friende / Doctor-Friende

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

目录

English ReadMe

Rasa版本

  • 此处为Rasa==2.0.x(其它Rasa版本请切换branch)

演示视频

  • 演示视频

  • 视频中的demo演示网址用的上海服务器,低配,加载速度慢,各位轻虐~~ >.<

GIF动图展示

  • 2020/05/20 聊天窗的颜色略有不同

image

image

说明

  • 本程序实现的是基于中文的医疗知识图谱的问答机器人MedicalKBQA, 是基于 Rasa-2.0.x 版本及其支持的外部组件实现的, 并使用了图数据库 Neo4j 构建知识图谱。

  • Rasa的Pipeline配置如下:

    pipeline:
      - name: "MitieNLP"
        model: "data/total_word_feature_extractor_zh.dat"
      - name: "JiebaTokenizer"
        dictionary_path: "jieba_userdict"
      - name: "MitieEntityExtractor"
      - name: "EntitySynonymMapper"
      - name: "RegexFeaturizer"
      - name: "MitieFeaturizer"
      - name: "SklearnIntentClassifier"
  • 注意: rasa-nlu和rasa-core已经合并成rasa

配置环境

  1. Python ≈ 3.8.5

  2. 下载zip包或者git clone

  3. 进入Doctor-Friende目录,conda记得activate环境

  4. 安装Mitie其实很简单,参考安装Rasa所需的MITIE组件

  5. 然后在命令行使用命令安装依赖

    pip install -r requirements.txt
  6. 提示

    • 国内推荐使用镜像加速(此命令是临时使用镜像,并非全局都用),比如:

      pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
    • 如果你有代理,可以在pip install命令后加上 --proxy=地址:端口

数据导入neo4j

  • 前提是已经有了可以连接的neo4j graph

  • 解压MedicalSpider/data/data.tar.gz,直接解压到MedicalSpider/data下,不要新建文件夹,则medical.json是所有数据的汇总, 将会被导入到你的知识图谱中

  • 修改MedicalSpider.process_data下的create_graph.py,把neo4j数据库的链接信息改成你自己的,然后运行该文件 (为了防止路径问题,建议使用Pycharm打开本项目后运行)

  • 关于爬虫:爬虫实现是使用了 scrapy 库,若想运行,可以在Doctor-Friende目录下运行SpiderMain.py

  • 整体规模:

    • 13,635 nodes (5 labels)
    • 114,163 relationships (6 types)

image

  • 数据结构
实体类型 含义 数量 举例
Disease 疾病 6,143 百日咳\n头痛
Department 科室 54 儿科\n小儿内科
Drug 药品 1,124 硫辛酸片\n曲克芦丁片
Food 食物 378 蟹肉\n鱿鱼(干)
Symptom 疾病症状 5,936 角弓反张\n视网膜Roth斑
Total 总计 13,635 约1.3万实体量级

训练Rasa模型

  1. Rasa训练数据集的构造:使用到了 Chatito工具

  2. 下载用于mitie的模型文件放到chat/data文件夹下, 百度网盘 ,密码:p4vx, 或者 Mega云盘

  3. 训练命令举例: 开启terminal/cmd进入chat目录,然后输入命令,命令含义参照 Rasa文档

    rasa train -c config/config_pretrained_embeddings_mitie_zh.yml --data data/medical/M3-training_dataset_1564317234.json data/medical/stories.md --out models/medicalRasa2 --domain config/domains.yml --num-threads 5 --augmentation 100 -vv

Shell方式测试模型

  1. 修改endpoints.yml中的tracker_store字段,将数据库连接信息换成你自己的(现成的db或新建db皆可, 我新建了一个db,Rasa会生成一个名为events的表),dialect字段是用了 SQLAlchemy 里的,这个链接是Rasa官方文档在 Tracker Store 给出的,详情参考官方文档

  2. 若要自己定制消息记录方式,请修改MyChannel/MyUtils.py中数据库连接信息,并确保你的MySQL数据库中 有message_received表,当然你可以取别的名字,记得在myio.pyhandle_message函数里把对应代码改掉

  3. chat/MyActions下的actions.py中同样需要先把neo4j数据库的链接信息改成你自己的

  4. 打开2个终端,都cd到chat目录下,conda记得activate环境

  5. 一个终端(启动Action Server)

    rasa run actions --actions MyActions.actions --cors "*" -vv  
  6. 另一个终端(Rasa Shell)

    rasa shell -m models/medicalRasa2/20201108-200002.tar.gz --endpoints config/endpoints.yml -vv

服务形式运行bot

  1. 参照上方除了最后一步其他都一样

  2. 另一个终端(启动NLU & Core Server)

    rasa run --enable-api -m models/medicalRasa2/20201108-200002.tar.gz --port 5000 --endpoints config/endpoints.yml --credentials config/credentials.yml -vv
  3. 前端页面位于: ChatHTML 如果用了我写的自定义socketio接口,请把前端中的socketPath做对应修改,默认就改成/mysocket.io/

  4. 提示

    • 部署在服务器推荐使用nohup等类似的方式在后台运行 ,并将控制台输出指向指定的文件。

参考

更新记录

  • 2020/11/08 修复bug

    • 配置文件的Pipeline恢复成和以前相同的内容了

    • MyUtils.pyget_record_db_cursor()里加了一句charset="utf8"

    • 修改了myio.py,请使用新版myio.py

  • 2020/10/26

    • 更新 Rasa 到 2.0.x

    • Python版本使用了3.8.5

    • 配置文件的Pipeline有很大改变,添加了名为HFTransformersNLP的组件

    • 必须用新的model,旧的无法使用了

    • domains.yml中,surepre_diseasetype改为any

  • 2020/10/24

    • 更新 Rasa 到 1.7.4

    • Python版本使用了3.7.9

    • 必须用新的model,旧的无法使用了

    • domains.yml中添加了session_config,这是Rasa要求的

    • chat/data/medical/stories.md中90行first对应的action改为action_first, 原先写的是utter_greet,这会导致action_first不执行,直接执行utter_greet,这是Rasa1.3.0 开始会发生的问题

  • 2020/05/20

    • 更新 Rasa 到 1.2.9

    • endpoints.yml中使用了Rasa的新特性 Tracker Store , 这个配置将会自动把Tracker存入MySQL中叫rasa的数据库(见endpoints.yml), 虽然有官方的这个存储Tracker的方式,但是我也加入了一个自定义存储消息记录的功能,见下方

    • chat/MyChannel里更新了myio.pyMyUtils.pymyio.py中自定义了一个socket接口以便定制存储消息记录于MySQL, 存储的字段在myio.py中可以看到,在handle_message函数中,这个自定义的socket接口是 以rasa.core.channels.socketio.SocketIOInput这个class为模版修改的

    • MyUtils.py中主要提供了数据库连接的信息,用到的mysqlclient依赖库可能安装会有困难,大家注意

    • 修改了credentials.yml,加入了使用上述自定义socketio的配置

    • 演示服务器在4月1日出了点问题,已修复,顺便更新了聊天窗依赖的js,聊天窗颜色略有不同

如有问题可以issue

MIT License Copyright (c) 2019 Friende Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Rasa-Doctor-Friende.A chinese medical chatbot based on Neo4j knowledge graph and Rasa. 展开 收起
Python 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/Friende/Doctor-Friende.git
git@gitee.com:Friende/Doctor-Friende.git
Friende
Doctor-Friende
Doctor-Friende
master

搜索帮助