{"id":17990291,"url":"https://github.com/doragd/chinese-chatbot-pytorch-implementation","last_synced_at":"2025-05-16T12:04:40.750Z","repository":{"id":50573060,"uuid":"186056630","full_name":"Doragd/Chinese-Chatbot-PyTorch-Implementation","owner":"Doragd","description":":four_leaf_clover: Another Chinese chatbot implemented in PyTorch,  which is the sub-module of intelligent work order processing robot. 👩‍🔧","archived":false,"fork":false,"pushed_at":"2024-07-25T10:15:31.000Z","size":85548,"stargazers_count":889,"open_issues_count":7,"forks_count":194,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-02T04:57:09.091Z","etag":null,"topics":["chatbot","chinese-nlp","deep-learning","pytorch","pytorch-nlp"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Doragd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-10T21:29:45.000Z","updated_at":"2025-03-25T03:14:39.000Z","dependencies_parsed_at":"2024-12-06T08:13:05.432Z","dependency_job_id":null,"html_url":"https://github.com/Doragd/Chinese-Chatbot-PyTorch-Implementation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Doragd%2FChinese-Chatbot-PyTorch-Implementation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Doragd%2FChinese-Chatbot-PyTorch-Implementation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Doragd%2FChinese-Chatbot-PyTorch-Implementation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Doragd%2FChinese-Chatbot-PyTorch-Implementation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Doragd","download_url":"https://codeload.github.com/Doragd/Chinese-Chatbot-PyTorch-Implementation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247987285,"owners_count":21028895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatbot","chinese-nlp","deep-learning","pytorch","pytorch-nlp"],"created_at":"2024-10-29T19:17:21.830Z","updated_at":"2025-04-09T06:09:24.683Z","avatar_url":"https://github.com/Doragd.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🍀 小智，又一个中文聊天机器人:yum:\n\n💖 利用有趣的中文语料库qingyun，由@Doragd 同学编写的中文聊天机器人:snowman:\n\n* 尽管她不是那么完善:muscle:，不是那么出色:paw_prints:\n* 但她是由我自己coding出来的:sparkling_heart: ，所以![](https://img.shields.io/badge/-It%20means%20everything-ff69b4.svg)\n\n* **希望大家能够多多star支持**:star: 这个NLP初学者:runner:和他的朋友🍀 小智 \n\n\n\n## :rainbow:背景\n\n这个项目实际是软件工程课程设计的子模块。我们的目标是开发一个智能客服工单处理系统。\n\n智能客服工单系统实际的工作流程是：当人向系统发出提问时，系统首先去知识库中查找是否存在相关问题，如果有，则返回问题的答案，此时如何人不满意，则可以直接提交工单。如果知识库中不存在，则调用这个聊天机器人进行自动回复。\n\n该系统服务的场景类似腾讯云的客服系统，客户多是来咨询相关问题的(云服务器，域名等)，所以知识库也是有关云服务器，域名等的咨询，故障处理的 (问题,答案) 集合。\n\n系统的前端界面和前后端消息交互由另一个同学@adjlyadv 完成，主要采用React+Django方式。\n\n@Doragd 同学负责的是知识库的获取和聊天机器人的编写，训练，测试。这个repo的内容也是关于这个的。\n\n\n\n## :star2: 测试效果\n\n* 不使用知识库进行回答\n\u003cimg src=\"https://i.loli.net/2019/05/11/5cd69c8de54c1.png\" width=50%  title=\"测试效果|不使用知识库\" /\u003e\n\n* 使用知识库进行回答\n\u003cimg src=\"https://i.loli.net/2019/05/11/5cd69dce836a6.png\" width=50%  title=\"测试效果|使用知识库\" /\u003e\n\n\n* 整个系统效果：\n  \u003cimg src=\"https://i.loli.net/2019/05/11/5cd69aa2eb38d.png\" width=100%  title=\"聊天界面\" /\u003e\n\n\n\n## :floppy_disk:项目结构\n\n```\n│  .gitignore\n│  config.py               #模型配置参数\n│  corpus.pth              #已经过处理的数据集\n│  dataload.py             #dataloader\n│  datapreprocess.py       #数据预处理\n│  LICENSE\n│  main.py               \n│  model.py       \n│  README.md\n│  requirements.txt\n│  train_eval.py            #训练和验证,测试\n│  \n├─checkpoints              \n│      chatbot_0509_1437   #已经训练好的模型\n│      \n├─clean_chat_corpus\n│      qingyun.tsv         #语料库\n│      \n├─QA_data\n│      QA.db               #知识库\n│      QA_test.py          #使用知识库时调用\n│      stop_words.txt      #停用词\n│      __init__.py\n│      \n└─utils\n        beamsearch.py      #to do 未完工\n        greedysearch.py    #贪婪搜索，用于测试\n        __init__.py\n```\n\n\n\n## :couple:依赖库\n\n![torch](https://img.shields.io/badge/torch-1.0.1-orange.svg)\n![torchnet](https://img.shields.io/badge/torchnet-0.0.4-brightgreen.svg)\n![fire](https://img.shields.io/badge/fire-0.1.3-red.svg)\n![jieba](https://img.shields.io/badge/jieba-0.39-blue.svg)\n\n安装依赖\n\n```shell\n$ pip install -r requirements.txt\n```\n\n\n\n## :sparkling_heart:开始使用\n\n### 数据预处理(可省略)\n\n```shell\n$ python datapreprocess.py\n```\n\n对语料库进行预处理，产生corpus.pth （**这里已经上传好corpus.pth, 故此步可以省略**）\n\n可修改参数:\n\n```\n# datapreprocess.py\ncorpus_file = 'clean_chat_corpus/qingyun.tsv' #未处理的对话数据集\nmax_voc_length = 10000 #字典最大长度\nmin_word_appear = 10 #加入字典的词的词频最小值\nmax_sentence_length = 50 #最大句子长度\nsave_path = 'corpus.pth' #已处理的对话数据集保存路径\n```\n\n### 使用\n\n* 使用知识库\n\n使用知识库时, 需要传入参数`use_QA_first=True` 此时，对于输入的字符串，首先在知识库中匹配最佳的问题和答案，并返回。找不到时，才调用聊天机器人自动生成回复。\n\n这里的知识库是爬取整理的腾讯云官方文档中的常见问题和答案，100条，仅用于测试！\n\n```shell\n$ python main.py chat --use_QA_first=True\n```\n\n* 不使用知识库\n\n由于课程设计需要，加入了腾讯云的问题答案对，但对于聊天机器人这个项目来说是无关紧要的，所以一般使用时，`use_QA_first=False`  ，该参数默认为`True`\n\n```shell\n$ python main.py chat --use_QA_first=False\n```\n\n* 使用默认参数\n\n```shell\n$ python main.py chat\n```\n\n* 退出聊天：输入`exit`, `quit`, `q`  均可\n\n### 其他可配置参数\n\n在`config.py` 文件中说明\n\n需要传入新的参数时，只需要命令行传入即可，形如\n\n```shell\n$ python main.py chat --model_ckpt='checkpoints/chatbot_0509_1437' --use_QA_first=False\n```\n\n上面的命令指出了加载已训练模型的路径和是否使用知识库\n\n\n\n## :cherry_blossom:技术实现\n\n### 语料库\n\n| 语料名称            | 语料数量 | 语料来源说明       | 语料特点         | 语料样例                                  | 是否已分词 |\n| ------------------- | -------- | ------------------ | ---------------- | ----------------------------------------- | ---------- |\n| qingyun（青云语料） | 10W      | 某聊天机器人交流群 | 相对不错，生活化 | Q:看来你很爱钱 A:噢是吗？那么你也差不多了 | 否         |\n\n* 来源：\u003chttps://github.com/codemayq/chinese_chatbot_corpus\u003e\n\n### Seq2Seq\n\n* Encoder：两层双向GRU\n* Decoder：双层单向GRU\n\n### Attention\n\n* Global attention，采用dot计算分数\n* Ref. https://arxiv.org/abs/1508.04025\n\n\n\n## :construction_worker:模型训练与评估\n\n```shell\n$ python train_eval.py train [--options]\n```\n\n定量评估部分暂时还没写好，应该采用困惑度来衡量，目前只能生成句子，人为评估质量\n\n```shell\n$ python train_eval.py eval [--options]\n```\n\n\n\n## :sob:跳坑记录与总结\n\n* 最深刻的体会就是“深度学习知识的了解和理解之间差了N个编程实现”。虽然理论大家都很清楚，但是真正到编程实现时，总会出这样，那样的问题：从数据集的处理，到许多公式的编程实现，到参数的调节，GPU配置等等各种问题\n* 这次实践的过程实际是跟着PyTorch Tutorial先过了一遍Chatbot部分，跑通以后，再更换语料库，处理语料库，再按照类的风格去重构了代码，然后就是无尽的Debug过程，遇到了很多坑，尤其是把张量移到GPU上遇到各种问题，主要是不清楚to(device)时究竟移动了哪些。\n  * 通过测试发现，model.to(device)只会把参数移到GPU，不会把类中定义的成员tensor移过去，所以如果在forward方法中定义了新的张量，要记得移动。\n  * 还有就是移动的顺序问题：先把模型移动到GPU，再去定义优化器。以及移动的方法：model=model.to(device)，不要忘记赋值。 \n  * 很容易出现GPU显存不足的情况，注意写代码时要考虑内存利用率问题，尽量减少重复tensor。\n  * 在一开始更换中文语料库后，训练总是不收敛，最后才发现原来是batch_size设置小了，实际上我感觉batch_size在显存足够时要尽量大，其实之前看到过，只是写代码的时候完全忘记这回事了。说明自己当时看mini-batch时还不够理解，还是要真的写代码才能够深入人心，至少bug深入人心\n  * 还有一个问题就是误解了torch.long，以为是高精度浮点，结果是int64型，造成了一个bug，找了好久才发现怎么回事。这告诉我们要认真看文档。\n  * 最后的收获就是熟悉了如何实际实现一个模型，这很重要。\n* 实际上这个模型的效果不是很好，除开模型本身的问题不谈，我发现分词的质量会严重影响句子的质量，但是分词时我连停用词还没设置，会出现一些奇特的结果\n* 还有一个问题是处理变长序列时，损失函数如果用自己定义的，很容易出现不稳定情况，现在还在研究官方API\n* 本次实践还发现自己对一些参数理解还不够深，不知道怎么调，还要补理论。\n* 对模型的评估这部分还要继续做。\n\n## :pray:致谢\n\n* 官方的Chatbot Tutorial\n  * \u003chttps://pytorch.org/tutorials/beginner/chatbot_tutorial.html\u003e\n* 提供中文语料库\n  * \u003chttps://github.com/codemayq/chinese_chatbot_corpus\u003e \n* 与官方的Chatbot Tutorial内容一致，但是有详尽的代码注释\n  * \u003chttp://fancyerii.github.io/2019/02/14/chatbot/\u003e\n* 模型的写法和习惯均参考\n  * \u003chttps://github.com/chenyuntc/pytorch-book\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoragd%2Fchinese-chatbot-pytorch-implementation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdoragd%2Fchinese-chatbot-pytorch-implementation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoragd%2Fchinese-chatbot-pytorch-implementation/lists"}