# textgen **Repository Path**: huogenwang/textgen ## Basic Information - **Project Name**: textgen - **Description**: TextGen: Implementation of Text Generation models, include LLaMA, BLOOM, GPT2, BART, T5, SongNet and so on. 文本生成模型,实现了包括LLaMA,ChatGLM,BLOOM,GPT2,Seq2Seq,BART,T5,UDA等模型的训练和预测,开箱即用。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-12-28 - **Last Updated**: 2025-12-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [**🇨🇳中文**](https://github.com/shibing624/textgen/blob/main/README.md) | [**🌐English**](https://github.com/shibing624/textgen/blob/main/README_EN.md) | [**📖文档/Docs**](https://github.com/shibing624/textgen/wiki) | [**🤖模型/Models**](https://huggingface.co/shibing624)
----------------- # TextGen: Implementation of Text Generation models [](https://badge.fury.io/py/textgen) [](https://pepy.tech/project/textgen) [](CONTRIBUTING.md) [](LICENSE) [](requirements.txt) [](https://github.com/shibing624/textgen/issues) [](#Contact) ## 📖 Introduction **TextGen**实现了多种文本生成模型,包括:LLaMA、ChatGLM、UDA、GPT2、Seq2Seq、BART、T5、SongNet等模型,开箱即用。 ## 🔥 News [2023/11/02] v1.1.2版本: GPT模型支持了[NEFTune](https://github.com/neelsjain/NEFTune)给embedding加噪SFT训练方法,SFT中使用 `--neft_alpha` 参数启用 NEFTune,例如 `--neft_alpha 5`。详见[Release-v1.1.2](https://github.com/shibing624/textgen/releases/tag/1.1.2) [2023/09/05] v1.1.1版本: 支持多卡推理,推理速度加倍,调库textgen做batch推理,多卡推理更方便、快速。详见[Release-v1.1.1](https://github.com/shibing624/textgen/releases/tag/1.1.1) [2023/08/23] v1.1.0版本: 发布基于ShareGPT4数据集微调的中英文Vicuna-13B模型[shibing624/vicuna-baichuan-13b-chat](https://huggingface.co/shibing624/vicuna-baichuan-13b-chat),和对应的LoRA模型[shibing624/vicuna-baichuan-13b-chat-lora](https://huggingface.co/shibing624/vicuna-baichuan-13b-chat-lora),支持多轮对话,评测效果有提升,详见[Release-v1.1.0](https://github.com/shibing624/textgen/releases/tag/1.1.0) [2023/08/02] v1.0.2版本: 新增支持ChatGLM2和LLaMA2模型的SFT微调训练,详见[Release-v1.0.2](https://github.com/shibing624/textgen/releases/tag/1.0.2) [2023/06/15] v1.0.0版本: 新增ChatGLM/LLaMA/Bloom模型的多轮对话微调训练,并发布医疗问诊LoRA模型[shibing624/ziya-llama-13b-medical-lora](https://huggingface.co/shibing624/ziya-llama-13b-medical-lora)。详见[Release-v1.0.0](https://github.com/shibing624/textgen/releases/tag/1.0.0) [2023/06/02] v0.2.7版本: 新增ChatGLM/LLaMA/Bloom模型的SFT微调训练,并发布适用于通用对话和中文纠错的LoRA模型。详见[Release-v0.2.7](https://github.com/shibing624/textgen/releases/tag/0.2.7) ## 😊 Feature - [GPT](textgen/gpt):本项目基于PyTorch实现了 ChatGLM-6B 1,2,3 / Baichuan 1,2 / LLaMA 1,2 / BLOOM / Mistral / QWen 等GPT模型LoRA微调训练和预测,可以用于对话生成任务和领域微调训练 - [UDA/EDA](textgen/augment/word_level_augment.py):本项目实现了UDA(非核心词替换)、EDA和Back Translation(回译)算法,基于TF-IDF将句子中部分不重要词替换为同义词,随机词插入、删除、替换等方法,产生新的文本,实现了文本扩增 - [Seq2Seq](textgen/seq2seq):本项目基于PyTorch实现了Seq2Seq、ConvSeq2Seq、BART模型的训练和预测,可以用于文本翻译、对话生成、摘要生成等文本生成任务 - [T5](textgen/t5):本项目基于PyTorch实现了T5和CopyT5模型训练和预测,可以用于文本翻译、对话生成、对联生成、文案撰写等文本生成任务 - [GPT2](textgen/language_modeling):本项目基于PyTorch实现了GTP2模型训练和预测,可以用于文章生成、对联生成等文本生成任务 - [SongNet](textgen/language_modeling/songnet_model.py):本项目基于PyTorch实现了SongNet模型训练和预测,可以用于规范格式的诗词、歌词等文本生成任务 - [TGLS](textgen/unsup_generation):本项目实现了[TGLS](https://www.jiqizhixin.com/articles/2020-08-11-5)无监督相似文本生成模型,是一种“先搜索后学习”的文本生成方法,通过反复迭代学习候选集,最终模型能生成类似候选集的高质量相似文本 ### Release Models release基于`textgen`训练的中文模型,模型已经release到HuggingFace models,指定模型名称`textgen`会自动下载模型,可直接使用。 | Model | Arch | Introduction | Train Script | Predict Script | |:----------------------------------------------------------------------------------------------------------|:-------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------| | [shibing624/t5-chinese-couplet](https://huggingface.co/shibing624/t5-chinese-couplet) | T5 | fine-tuned中文对联后的模型 | [对联生成模型调研](https://github.com/shibing624/textgen/blob/main/docs/%E5%AF%B9%E8%81%94%E7%94%9F%E6%88%90%E6%A8%A1%E5%9E%8B%E5%AF%B9%E6%AF%94.md) | [predict script](https://github.com/shibing624/textgen/blob/main/examples/t5/t5_couplet_demo.py) | | [shibing624/songnet-base-chinese-songci](https://huggingface.co/shibing624/songnet-base-chinese-songci) | SongNet | fine-tuned宋词后的模型 | [training script](https://github.com/shibing624/textgen/blob/main/examples/songnet/training_zh_songnet_demo.py) | [predict script](https://github.com/shibing624/textgen/blob/main/examples/songnet/songnet_songci_demo.py) | | [shibing624/songnet-base-chinese-couplet](https://huggingface.co/shibing624/songnet-base-chinese-couplet) | SongNet | fine-tuned对联后的模型 | [training script](https://github.com/shibing624/textgen/blob/main/examples/songnet/training_zh_songnet_demo.py) | [predict script](https://github.com/shibing624/textgen/blob/main/examples/songnet/songnet_couplet_demo.py) | | [shibing624/chatglm-6b-csc-zh-lora](https://huggingface.co/shibing624/chatglm-6b-csc-zh-lora) | ChatGLM-6B | 在27万中文拼写纠错数据[shibing624/CSC](https://huggingface.co/datasets/shibing624/CSC)上微调了一版ChatGLM-6B,纠错效果有提升,发布微调后的LoRA权重 | [training script](https://github.com/shibing624/textgen/blob/main/examples/chatglm/training_chatglm_csc_demo.py) | [predict script](https://github.com/shibing624/textgen/blob/main/examples/chatglm/csc_demo.py) | | [shibing624/chatglm-6b-belle-zh-lora](https://huggingface.co/shibing624/chatglm-6b-belle-zh-lora) | ChatGLM-6B | 在100万条中文ChatGPT指令Belle数据集[BelleGroup/train_1M_CN](https://huggingface.co/datasets/BelleGroup/train_1M_CN)上微调了一版ChatGLM-6B,问答效果有提升,发布微调后的LoRA权重 | [training script](https://github.com/shibing624/textgen/blob/main/examples/chatglm/training_chatglm_hfdataset_demo.py) | [predict script](https://github.com/shibing624/textgen/blob/main/examples/chatglm/training_chatglm_hfdataset_demo.py) | | [shibing624/llama-13b-belle-zh-lora](https://huggingface.co/shibing624/llama-13b-belle-zh-lora) | LLaMA-13B | 在100万条中文ChatGPT指令Belle数据集[BelleGroup/train_1M_CN](https://huggingface.co/datasets/BelleGroup/train_1M_CN)上微调了一版Llama-13B,问答效果有提升,发布微调后的LoRA权重 | [training script](https://github.com/shibing624/textgen/blob/main/examples/llama/training_llama_hfdataset_demo.py) | [predict script](https://github.com/shibing624/textgen/blob/main/examples/llama/training_llama_hfdataset_demo.py) | | [shibing624/chinese-alpaca-plus-7b-hf](https://huggingface.co/shibing624/chinese-alpaca-plus-7b-hf) | LLaMA-7B | [中文LLaMA-Plus, Alpaca-Plus 7B版本](https://github.com/ymcui/Chinese-LLaMA-Alpaca/releases/tag/v3.0),在LLaMA-7B上扩充了中文词表并继续预训练120G文本(通用领域),在4M指令数据集上微调后得到的中文Alpaca-plus模型 | [training script](https://github.com/shibing624/textgen/blob/main/examples/gpt/training_llama_demo.py) | [predict script](https://github.com/shibing624/textgen/blob/main/examples/gpt/training_llama_demo.py) | | [shibing624/chinese-alpaca-plus-13b-hf](https://huggingface.co/shibing624/chinese-alpaca-plus-13b-hf) | LLaMA-13B | [中文LLaMA-Plus, Alpaca-Plus 13B版本](https://github.com/ymcui/Chinese-LLaMA-Alpaca/releases/tag/v3.1),在LLaMA-13B上扩充了中文词表并继续预训练120G文本(通用领域),在4.3M指令数据集上微调后得到的中文Alpaca-plus模型 | [training script](https://github.com/shibing624/textgen/blob/main/examples/gpt/training_llama_demo.py) | [predict script](https://github.com/shibing624/textgen/blob/main/examples/gpt/training_llama_demo.py) | | [shibing624/ziya-llama-13b-medical-lora](https://huggingface.co/shibing624/ziya-llama-13b-medical-lora) | LLaMA-13B | 在240万条中英文医疗数据集[shibing624/medical](https://huggingface.co/datasets/shibing624/medical)上微调了一版Ziya-LLaMA-13B模型,医疗问答效果有提升,发布微调后的LoRA权重 | [training script](https://github.com/shibing624/textgen/blob/main/examples/gpt/training_llama_demo.py) | [predict script](https://github.com/shibing624/textgen/blob/main/examples/gpt/training_llama_demo.py) | | [shibing624/vicuna-baichuan-13b-chat](https://huggingface.co/shibing624/vicuna-baichuan-13b-chat) | Baichuan-13B-Chat | 在10万条多语言ShareGPT GPT4多轮对话数据集[shibing624/sharegpt_gpt4](https://huggingface.co/datasets/shibing624/sharegpt_gpt4)上SFT微调了一版baichuan-13b-chat多轮问答模型,日常问答和医疗问答效果有提升,发布微调后的完整模型权重 | [training script](https://github.com/shibing624/textgen/blob/main/examples/gpt/training_llama_demo.py) | [predict script](https://github.com/shibing624/textgen/blob/main/examples/gpt/training_llama_demo.py) | ### Evaluation | Model | Arch | Introduction | Score | |:--------------------------------------------------------------------------------------------------------------------------------------------|:-----------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------| | [LLaMA-7B-Chinese-Alpaca](https://huggingface.co/ziqingyang/chinese-alpaca-lora-7b) | LLaMA-7B | 复用[ymcui/Chinese-LLaMA-Alpaca](https://github.com/ymcui/Chinese-LLaMA-Alpaca/blob/main/examples/README.md)的评估case和得分 | 4.92 | | [LLaMA-13B-Chinese-Alpaca](https://huggingface.co/ziqingyang/chinese-alpaca-lora-13b) | LLaMA-13B | 复用[ymcui/Chinese-LLaMA-Alpaca](https://github.com/ymcui/Chinese-LLaMA-Alpaca/blob/main/examples/README.md)的评估case和得分 | 7.05 | | [ChatGLM-6B](https://huggingface.co/THUDM/chatglm-6b) | ChatGLM-6B | 基于原生`THUDM/chatglm-6b`评估测试集得分 | 7.16 | | [ChatGLM-6B-v1.1](https://huggingface.co/THUDM/chatglm-6b) | ChatGLM-6B | 基于原生`THUDM/chatglm-6b`v1.1英文优化版模型评估测试集得分 | **7.18** | | [shibing624/chatglm-6b-belle-zh-lora](https://huggingface.co/shibing624/chatglm-6b-belle-zh-lora) | ChatGLM-6B | 基于`THUDM/chatglm-6b`加载`shibing624/chatglm-6b-belle-zh-lora`LoRA模型后评估测试集得分 | 7.03 | | [facat/alpaca-lora-cn-13b](https://huggingface.co/facat/alpaca-lora-cn-13b) | LLaMA-13B | 基于`decapoda-research/llama-13b-hf`加载`facat/alpaca-lora-cn-13b`LoRA模型后评估测试集并标注得分 | 4.13 | | [Chinese-Vicuna/Chinese-Vicuna-lora-13b-belle-and-guanaco](https://huggingface.co/Chinese-Vicuna/Chinese-Vicuna-lora-13b-belle-and-guanaco) | LLaMA-13B | 基于`decapoda-research/llama-13b-hf`加载`Chinese-Vicuna/Chinese-Vicuna-lora-13b-belle-and-guanaco`LoRA模型后评估测试集并标注得分 | 3.98 | | [shibing624/chinese-alpaca-plus-7b-hf](https://huggingface.co/shibing624/chinese-alpaca-plus-7b-hf) | LLaMA-7B | 使用[ymcui/Chinese-LLaMA-Alpaca 合并模型方法](https://github.com/ymcui/Chinese-LLaMA-Alpaca/wiki/%E6%89%8B%E5%8A%A8%E6%A8%A1%E5%9E%8B%E5%90%88%E5%B9%B6%E4%B8%8E%E8%BD%AC%E6%8D%A2#%E5%A4%9Alora%E6%9D%83%E9%87%8D%E5%90%88%E5%B9%B6%E9%80%82%E7%94%A8%E4%BA%8Echinese-alpaca-plus)合并HF权重后,评估测试集并标注得分 | 6.93 | | [shibing624/chinese-alpaca-plus-13b-hf](https://huggingface.co/shibing624/chinese-alpaca-plus-13b-hf) | LLaMA-13B | 使用[ymcui/Chinese-LLaMA-Alpaca 合并模型方法](https://github.com/ymcui/Chinese-LLaMA-Alpaca/wiki/%E6%89%8B%E5%8A%A8%E6%A8%A1%E5%9E%8B%E5%90%88%E5%B9%B6%E4%B8%8E%E8%BD%AC%E6%8D%A2#%E5%A4%9Alora%E6%9D%83%E9%87%8D%E5%90%88%E5%B9%B6%E9%80%82%E7%94%A8%E4%BA%8Echinese-alpaca-plus)合并HF权重后,评估测试集并标注得分 | 7.07 | | [TheBloke/vicuna-13B-1.1-HF](https://huggingface.co/TheBloke/vicuna-13B-1.1-HF) | LLaMA-13B | 使用原生vicuna-13B-1.1合并后的模型,评估测试集并标注得分 | 5.13 | | [IDEA-CCNL/Ziya-LLaMA-13B-v1](https://huggingface.co/IDEA-CCNL/Ziya-LLaMA-13B-v1) | LLaMA-13B | 使用姜子牙通用大模型V1,评估测试集并标注得分 | 6.63 | 说明: - 评估case,详见在线文档:中文LLM-benchmark多任务评估集(腾讯文档) https://docs.qq.com/sheet/DUUpsREtWbFBsUVJE?tab=r7io7g 感谢韩俊明、[杨家铭](https://github.com/yangjiam)等同学的标注 - 评估任务类型包括:知识问答,开放式问答,数值计算,诗词、音乐、体育,娱乐,写文章,文本翻译,代码编程,伦理、拒答类,多轮问答,Score 评分是前100条(10分制)的平均分数,人工打分,越高越好 - 评估数量少,任务类型不够全面,评分之间的大小关系有一些参考价值,分数的绝对值没太大参考价值 - 评估脚本:[tests/test_benchmark.py](https://github.com/shibing624/textgen/blob/main/tests/test_benchmark.py) ,使用fp16预测,无int量化处理,运行脚本可复现评估结果,但生成结果具有随机性,受解码超参、随机种子等因素影响。评测并非绝对严谨,测试结果仅供晾晒参考 - 结论:ChatGLM-6B、LLaMA-13B的中文衍生模型(包括alpaca-plus, vicuna, ziya)的表现属于第一梯队,原版LLaMA-7B的表现整体稍差些 - LLaMA-13B-Chinese-Alpaca是在原版LLaMA上扩充了中文词表,并融入了约20G的通用中文语料后的指令微调模型,表明了LLaMA的底座优秀,具有强大的语言迁移能力 - ChatGLM这种原生的中文预训练模型更理解中文语义,且在中文知识问答、开放式问答得分高 - LLaMA系列模型数值计算、中英翻译、代码编程类得分高 - 经过中文预训练和SFT微调后的Chinese-LLaMA模型在中文诗词、娱乐、伦理类得分相较原版LLaMA有提升 ## 🚀 Demo HuggingFace Demo: https://huggingface.co/spaces/shibing624/chinese-couplet-generate  run example: [examples/T5/gradio_demo.py](examples/T5/gradio_demo.py) to see the demo: ```shell python examples/T5/gradio_demo.py ``` model trained by [examples/t5/T5_Finetune_Chinese_Couplet.ipynb](https://github.com/shibing624/textgen/blob/main/examples/t5/T5_Finetune_Chinese_Couplet.ipynb) ## 💾 Install ```shell pip install -U textgen ``` or install develop version: ```shell pip install torch # conda install pytorch git clone https://github.com/shibing624/textgen.git cd textgen python setup.py install ``` ## ▶️ Usage ### ChatGLM-6B 模型 #### 使用 ChatGLM-6B 微调后的模型 example: [examples/gpt/inference_demo.py](https://github.com/shibing624/textgen/blob/main/examples/gpt/inference_demo.py) ```python from textgen import GptModel model = GptModel("chatglm", "THUDM/chatglm-6b", peft_name="shibing624/chatglm-6b-csc-zh-lora") r = model.predict(["介绍下北京"]) print(r) # ['北京是中国的首都...'] ``` #### 训练 ChatGLM-6B 微调模型 1. 支持自定义训练数据集和训练参数,数据集格式参考[examples/data/sharegpt_zh_100_format.jsonl](https://github.com/shibing624/textgen/blob/main/examples/data/sharegpt_zh_100_format.jsonl) 2. 支持QLoRA、AdaLoRA、LoRA、P_Tuning、Prefix_Tuning等部分参数微调方法,也支持全参微调 3. 支持多卡训练,支持混合精度训练 4. 支持多卡推理 example: [examples/gpt/training_chatglm_demo.py](https://github.com/shibing624/textgen/blob/main/examples/gpt/training_chatglm_demo.py) 单卡训练: ```shell cd examples/gpt CUDA_VISIBLE_DEVICES=0 python training_chatglm_demo.py --do_train --do_predict --num_epochs 1 --output_dir outputs_chatglm_v1 ``` 多卡训练: ```shell cd examples/gpt CUDA_VISIBLE_DEVICES=0,1 torchrun --nproc_per_node 2 training_chatglm_demo.py --do_train --do_predict --num_epochs 20 --output_dir outputs_chatglm_v1 ``` 多卡推理: ```shell cd examples/gpt CUDA_VISIBLE_DEVICES=0,1 torchrun --nproc_per_node 2 inference_multigpu_demo.py --model_type chatglm --base_model THUDM/chatglm-6b ``` ### LLaMA 模型 #### 使用 LLaMA 微调后的模型 example: [examples/gpt/inference_demo.py](https://github.com/shibing624/textgen/blob/main/examples/gpt/inference_demo.py)
## 😇 Citation
如果你在研究中使用了textgen,请按如下格式引用:
```latex
@misc{textgen,
title={textgen: Text Generation Tool},
author={Ming Xu},
year={2021},
howpublished={\url{https://github.com/shibing624/textgen}},
}
```
## 🤗 License
This repository is licensed under [The Apache License 2.0](LICENSE).
Please follow the [Model Card](https://github.com/facebookresearch/llama/blob/main/MODEL_CARD.md) to use the LLaMA model.
Please follow the [RAIL License](https://huggingface.co/spaces/bigscience/license) to use the BLOOM & BLOOMZ model.
## 😍 Contribute
项目代码还很粗糙,如果大家对代码有所改进,欢迎提交回本项目,在提交之前,注意以下两点:
- 在`tests`添加相应的单元测试
- 使用`python -m pytest`来运行所有单元测试,确保所有单测都是通过的
之后即可提交PR。
## 💕 Acknowledgements
- [PaddlePaddle/ERNIE](https://github.com/PaddlePaddle/ERNIE)
- [minimaxir/textgenrnn](https://github.com/minimaxir/textgenrnn)
- [minimaxir/gpt-2-simple](https://github.com/minimaxir/gpt-2-simple)
- [asyml/texar](https://github.com/asyml/texar)
- [yangjianxin1/GPT2-chitchat](https://github.com/yangjianxin1/GPT2-chitchat)
- [williamSYSU/TextGAN-PyTorch](https://github.com/williamSYSU/TextGAN-PyTorch)
- [RUCAIBox/TextBox](https://github.com/RUCAIBox/TextBox)
- [Tiiiger/bert_score](https://github.com/Tiiiger/bert_score)
- [ThilinaRajapakse/simpletransformers](https://github.com/ThilinaRajapakse/simpletransformers)
- [1YCxZ/Fake-review-generation](https://github.com/1YCxZ/Fake-review-generation)
- [tloen/alpaca-lora](https://github.com/tloen/alpaca-lora/blob/main/finetune.py)
Thanks for their great work!