{"id":13672514,"url":"https://github.com/yangjianxin1/GPT2-chitchat","last_synced_at":"2025-04-27T22:32:20.380Z","repository":{"id":41273168,"uuid":"226807532","full_name":"yangjianxin1/GPT2-chitchat","owner":"yangjianxin1","description":"GPT2 for Chinese chitchat/用于中文闲聊的GPT2模型(实现了DialoGPT的MMI思想)","archived":false,"fork":false,"pushed_at":"2023-10-30T16:56:20.000Z","size":523,"stargazers_count":3013,"open_issues_count":71,"forks_count":678,"subscribers_count":41,"default_branch":"master","last_synced_at":"2025-04-14T08:10:01.840Z","etag":null,"topics":["chichat","dialogpt","dialogue-model","gpt-2","gpt2","nlp","text-generation","transformer"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yangjianxin1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-12-09T07:09:39.000Z","updated_at":"2025-04-08T14:11:02.000Z","dependencies_parsed_at":"2024-01-07T03:51:50.042Z","dependency_job_id":"b15e3838-6270-4581-b65f-7f301e7d5dd3","html_url":"https://github.com/yangjianxin1/GPT2-chitchat","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/yangjianxin1%2FGPT2-chitchat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangjianxin1%2FGPT2-chitchat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangjianxin1%2FGPT2-chitchat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangjianxin1%2FGPT2-chitchat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yangjianxin1","download_url":"https://codeload.github.com/yangjianxin1/GPT2-chitchat/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251219601,"owners_count":21554444,"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":["chichat","dialogpt","dialogue-model","gpt-2","gpt2","nlp","text-generation","transformer"],"created_at":"2024-08-02T09:01:37.897Z","updated_at":"2025-04-27T22:32:19.779Z","avatar_url":"https://github.com/yangjianxin1.png","language":"Python","funding_links":[],"categories":["A01_文本生成_文本对话","Python","Chinese NLP Toolkits 中文NLP工具"],"sub_categories":["其他_文本生成_文本对话","QA \u0026 Chatbot 问答和聊天机器人"],"readme":"# GPT2 for Chinese chitchat\n\n## News\n公众号【YeungNLP】\n- 2023.04.05：发布[Firefly(流萤): 中文对话式大语言模型](https://github.com/yangjianxin1/Firefly) ，开源1.1M中文多任务指令数据集，以及模型权重。详情见[文章](https://mp.weixin.qq.com/s/TX7wj8IzD_EaMTvk0bjRtA)\n- 2023.04.02：发布[LLMPruner: 大语言模型裁剪工具](https://github.com/yangjianxin1/LLMPruner) ,分享裁剪方法及其裁剪后的模型权重。详情见[文章](https://mp.weixin.qq.com/s/leVtrwZc1zLput51Nr99lw) 。\n- 2023.02.13：发布[OFA-Chinese](https://github.com/yangjianxin1/OFA-Chinese) ，中文多模态统一预训练模型OFA在Image Caption任务上的应用。详情见[文章](https://mp.weixin.qq.com/s/thRbR1i6cZk8zUz3y2mq6g) 。\n- 2022.12.04: 发布[CLIP-Chinese](https://github.com/yangjianxin1/CLIP-Chinese) ，中文CLIP预训练模型。使用140万中文图文对数据进行预训练，在图文相似度、文本相似度、图片相似度任务上有不错的表现。详情见[文章](https://mp.weixin.qq.com/s/6gQX91M-Lt7eiMimhYRJEw) 。\n- 2022.03.30：发布[ClipCap-Chinese](https://github.com/yangjianxin1/ClipCap-Chinese) ，一种基于CLIP模型的Image Caption模型。详情见[文章](https://mp.weixin.qq.com/s/Mofjqi0ErSdRqRyP9gq6Zw) 。\n- 2021.06.16：发布[CPM中文文本生成项目](https://github.com/yangjianxin1/CPM) 。可用于作文、小说、新闻、古诗等中文生成任务。详情见[文章](https://mp.weixin.qq.com/s/sFzUNtwrTvi2kAAGQ2M3UA) 。\n- 2021.05.26：新增50w、100w的多轮对话的原始数据与预处理数据。\n\n\n## 项目描述\n- 本项目是基于GPT2的中文闲聊机器人，模型实现基于HuggingFace的[transformers](https://github.com/huggingface/transformers) 。文章：\n- 本项目受 [GPT2-Chinese](https://github.com/Morizeyao/GPT2-Chinese) 的启发，精读作者的代码，获益匪浅。\n- 在生成阶段，使用了Temperature、Top-k Sampling和Nucleus Sampling等，可参考论文[The Curious Case of Neural Text Degeneration](https://arxiv.xilesou.top/pdf/1904.09751.pdf)\n- 代码中给出了许多详细的中文注释，方便大家更好地理解代码\n- 本项目被[微软的DialoGPT项目](https://github.com/microsoft/DialoGPT) 引用 （为了简化生成方法，加快生成速度，删除了MMI的生成方法）\n\n## 运行环境\npython3.6、 transformers==4.2.0、pytorch==1.7.0\n\n## 项目结构\n- data\n    - train.txt:默认的原始训练集文件，存放闲聊语料 \n    - train.pkl:对原始训练语料进行tokenize之后的文件,存储一个list对象，list的每条数据表示一个多轮对话，表示一条训练数据\n- model:存放对话生成的模型\n    - epoch40:经过40轮训练之后得到的模型\n      - config.json:模型参数的配置文件\n      - pytorch_model.bin:模型文件\n- vocab\n    - vocab.txt:字典文件。默认的字典大小为13317，若需要使用自定义字典，需要将confog.json文件中的vocab_size字段设为相应的大小。\n- sample:存放人机闲聊生成的历史聊天记录\n- train.py:训练代码\n- interact.py:人机交互代码\n- preprocess.py:数据预处理代码\n\n\n## 模型简介\n### 模型结构\n![avatar](figure/model.png)\n\n\n### 模型参数简介(详见模型的config.json文件)\n- initializer_range: 0.02\n- layer_norm_epsilon: 1e-05\n- n_ctx: 1024\n- n_embd: 768\n- n_head: 12\n- n_layer: 12\n- n_positions: 1024\n- vocab_size: 21128\n\n## 训练思路\n对每条训练数据进行拼接，然后将其输入到模型中，进行训练。\n\n对于如下多轮闲聊训练数据,在训练模型时，将训练数据进行如下拼接:\"[CLS]想看你的美照[SEP]亲我一口就给你看[SEP]我亲两口[SEP]讨厌人家拿小拳拳捶你胸口[SEP]\"。然后将上述拼接结果作为模型的输入，让模型进行自回归训练。\n```\n想看你的美照\n亲我一口就给你看\n我亲两口\n讨厌人家拿小拳拳捶你胸口\n```\n\n## 使用方法\n### Quick Start\n在[模型分享](#model_share)中下载模型，将模型文件夹model_epoch40_50w放到model目录下，执行如下命令，进行对话\n```\npython interact.py --no_cuda --model_path model_epoch40_50w (使用cpu生成，速度相对较慢)\n或\npython interact.py --model_path model_epoch40_50w --device 0 (指定0号GPU进行生成，速度相对较快)\n```\n\n\n###  数据预处理\n在项目根目录下创建data文件夹，将原始训练语料命名为train.txt，存放在该目录下。train.txt的格式如下，每段闲聊之间间隔一行，格式如下：\n```\n真想找你一起去看电影\n突然很想你\n我也很想你\n\n想看你的美照\n亲我一口就给你看\n我亲两口\n讨厌人家拿小拳拳捶你胸口\n\n美女约嘛\n开好房等你了\n我来啦\n```\n运行preprocess.py，对data/train.txt对话语料进行tokenize，然后进行序列化保存到data/train.pkl。train.pkl中序列化的对象的类型为List[List],记录对话列表中,每个对话包含的token。\n```\npython preprocess.py --train_path data/train.txt --save_path data/train.pkl\n```\n\n### 训练模型\n运行train.py,使用预处理后的数据，对模型进行自回归训练，模型保存在根目录下的model文件夹中。\n\n在训练时，可以通过指定patience参数进行early stop。当patience=n时，若连续n个epoch，模型在验证集上的loss均没有下降，则进行early stop，停止训练。当patience=0时，不进行early stop。\n\n代码中默认关闭了early stop，因为在实践中，early stop得到的模型的生成效果不一定会更好。\n```\npython train.py --epochs 40 --batch_size 8 --device 0,1 --train_path data/train.pkl\n```\n更多的训练参数介绍，可直接看train.py中的set_args()函数中的参数说明\n\n### 人机交互\n运行interact.py，使用训练好的模型，进行人机交互，输入Ctrl+Z结束对话之后，聊天记录将保存到sample目录下的sample.txt文件中。\n```\npython interact.py --no_cuda --model_path path_to_your_model --max_history_len 3(由于闲聊对话生成的内容长度不是很长，因此生成部分在CPU上跑速度也挺快的)\n```\n执行interact.py时，可以尝试通过调整topk、topp、repetition_penalty、max_history_len等参数，调整生成的效果。更多的参数介绍，可直接看interact.py的set_args()函数中的参数说明\n如果要使用GPU进行生成，则不要调用--no_cuda参数，并且通过--device gpu_id来指定使用哪块GPU。\n\n\n## 闲聊语料分享\n|中文闲聊语料 | 数据集地址 |语料描述|\n|---------|--------|--------|\n|常见中文闲聊|[chinese_chatbot_corpus](https://github.com/codemayq/chinese_chatbot_corpus)|包含小黄鸡语料、豆瓣语料、电视剧对白语料、贴吧论坛回帖语料、微博语料、PTT八卦语料、青云语料等|\n|50w中文闲聊语料 | [百度网盘【提取码:4g5e】](https://pan.baidu.com/s/1M87Zf9e8iBqqmfTkKBWBWA) 或 [GoogleDrive](https://drive.google.com/drive/folders/1QFRsftLNTR_D3T55mS_FocPEZI7khdST?usp=sharing) |包含50w个多轮对话的原始语料、预处理数据|\n|100w中文闲聊语料 | [百度网盘【提取码:s908】](https://pan.baidu.com/s/1TvCQgJWuOoK2f5D95nH3xg) 或 [GoogleDrive](https://drive.google.com/drive/folders/1NU4KLDRxdOGINwxoHGWfVOfP0wL05gyj?usp=sharing)|包含100w个多轮对话的原始语料、预处理数据|\n\n\n中文闲聊语料的内容样例如下:\n```\n谢谢你所做的一切\n你开心就好\n开心\n嗯因为你的心里只有学习\n某某某，还有你\n这个某某某用的好\n\n你们宿舍都是这么厉害的人吗\n眼睛特别搞笑这土也不好捏但就是觉得挺可爱\n特别可爱啊\n\n今天好点了吗？\n一天比一天严重\n吃药不管用，去打一针。别拖着\n```\n\n\u003ch2 id=\"model_share\"\u003e模型分享\u003c/h2\u003e\n\n|模型 | 共享地址 |模型描述|\n|---------|--------|--------|\n|model_epoch40_50w | [百度网盘【提取码:ju6m】](https://pan.baidu.com/s/1iEu_-Avy-JTRsO4aJNiRiA) 或 [GoogleDrive](https://drive.google.com/drive/folders/1fJ6VuBp4wA1LSMpZgpe7Hgm9dbZT5bHS?usp=sharing) |使用50w多轮对话语料训练了40个epoch，loss降到2.0左右。|\n\n## 关注我们\n\n\u003cimg src=\"figure/gongzhonghao.jpeg\" width=\"250\"\u003e \n\n## 生成样例\n#### Sample 1:\n``` \n  user :你好啊\nchatbot:你好，请问您是？\n  user :我是你的主人啊 \nchatbot:你是狗吗？\n  user :你才是狗\nchatbot:你不是狗我是你的小主人\n  user :你这个大笨蛋\nchatbot:你大笨蛋，你是想挨打了\n  user :你全家都是大笨蛋\nchatbot:我是你爸爸\n  user :算了，不跟你吵了，你在干嘛呢\nchatbot:我在上班啊，你还没下班呢啊你\n  user :我周末要做作业啊\nchatbot:不是才交了吗，早晚做完啊\n  user :好多作业啊，还没做完呢\nchatbot:我在抄，我妈让我周五交，还有事\n  user :你就不怕被你妈妈骂吗\nchatbot:不怕\n  user :你周末有去哪里玩吗\nchatbot:没时间啊，忙得很啊\n  user :忙着干嘛呢\nchatbot:不知道啊，我周末没有作业，我想睡觉，然后还得找人\n``` \n\n#### Sample 2:\n``` \n  user :你好啊\nchatbot:你好我好大家好\n  user :你在干嘛呢\nchatbot:你不上课啊\n  user :今天是周末啊，笨蛋\nchatbot:我在学校呢\n  user :我也是，周末还要做作业\nchatbot:我还没写呢我们都还没写\n  user :周末你有去哪里玩吗\nchatbot:没有去南京\n  user :南京有什么好玩的啊\nchatbot:我们没去南京\n  user :你刚刚又说你去了南京，你是不是耍我啊\nchatbot:我刚刚才到南京\n  user :你好笨啊\nchatbot:我没有\n  user :你是大笨蛋\nchatbot:你才是大笨蛋\n  user :你全家都是大笨蛋\nchatbot:你个傻冒\n  user :你个智障\nchatbot:哈哈哈\n``` \n#### Sample 3:\n``` \n  user :在干嘛\nchatbot:睡觉\n  user :这么早就睡觉啦\nchatbot:嗯啊你也早点睡啊\n  user :我再玩一会儿手机\nchatbot:早点休息晚安\n  user :拜拜啦\nchatbot:晚安晚安\n  user :多喝热水\nchatbot:哈哈哈\n  user :多喝热水\nchatbot:好的咯\n``` \n#### Sample 4:\n``` \n  user :在吗\nchatbot:在\n  user :我不开心\nchatbot:怎么啦\n  user :最近好累啊\nchatbot:是挺累的\n  user :作业好多啊\nchatbot:还好还好\n  user :我觉得我快要挂科了\nchatbot:我也是啊，但是我觉得我快熬出头了\n  user :不说了，我还有好多作业没写\nchatbot:我也是啊好多啊\n  user :那先这样，下次再聊，再见了\nchatbot:好哒\n``` \n\n#### Sample 5:\n``` \n  user :你去过广州吗\nchatbot:去过呀，但是没有找到工作呢\n  user :那你去过北京吗\nchatbot:没有啊\n  user :北京有什么好玩的\nchatbot:没有你想象的那么好\n  user :是吗，我还没过北京\nchatbot:那里我也不怎么熟，我去过北京\n  user :北京天安门呢\nchatbot:没去过，不过可以去玩玩\n``` \n\n#### Sample 6:\n``` \n  user :你好\nchatbot:你好\n  user :在干嘛呢\nchatbot:在想你呢\n  user :好害羞啊\nchatbot:害羞什么啊\n  user :你这样夸我，我觉得不好意思呀\nchatbot:我夸你你还不好意思呢\n  user :是啊，哈哈哈\nchatbot:你是不是在考验我\n  user :你怎么会这样想呢\nchatbot:我说我在夸你，这么客气，\n  user :你有睡午觉吗\nchatbot:有啊\n  user :我现在有点困呢\nchatbot:我也是\n  user :等下还要去上课，好烦啊\nchatbot:我们不用上课的呀\n```\n\n## TODO\n- 多卡并行训练负载不均衡的问题\n\n## Reference\n- [The Curious Case of Neural Text Degeneration](https://arxiv.xilesou.top/pdf/1904.09751.pdf)\n- [transformers](https://github.com/huggingface/transformers)\n- [GPT2-Chinese](https://github.com/Morizeyao/GPT2-Chinese)\n- [DialoGPT:Large-Scale Generative Pre-training for Conversational Response Generation](https://arxiv.xilesou.top/pdf/1911.00536.pdf)\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyangjianxin1%2FGPT2-chitchat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyangjianxin1%2FGPT2-chitchat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyangjianxin1%2FGPT2-chitchat/lists"}