
标准化大语言模型聊天模板的开源库
chat_templates是一个开源项目,为指令微调的大语言模型提供标准化聊天模板。该项目支持transformers库的chat_template功能,收录了Llama、Mistral、Qwen等主流大语言模型的聊天模板。此外,项目还提供了用于控制响应生成的配置文件。开发者可借助这些模板和配置,更便捷地使用各类大语言模型进行对话生成。
这是一个包含指令微调大型语言模型(LLMs)的正确聊天模板(或输入格式)的代码库,用于支持transformers的chat_template功能。如果您有兴趣添加更多聊天模板,欢迎提交拉取请求。
如果您觉得这个代码库有用,请引用它:
@misc{zheng-2024-chat-templates, author = {Zheng, Chujie}, title = {Chat Templates for HuggingFace Large Language Models}, year = {2024}, howpublished = {\url{https://github.com/chujiezheng/chat_templates}} }
chat_templates包含收集到的聊天模板的jinja文件,可以直接替换Huggingface tokenizers中的模板
generation_configs包含用于控制响应生成结束的相应json配置。特别地,stop_token_ids应该通过eos_token_id参数直接传递给generate方法
重要提示: 如此问题所 述,messages应至少包含一条用户消息。强烈不建议只传递系统消息,因为这可能会导致意外输出(因为模型没有以这种方式训练)。
这个示例可以检查jinja文件是否正确实现。
from transformers import AutoTokenizer toker = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", token="YOUR_OWN_TOKEN") messages = [ {'role': 'system', 'content': '这是一个系统提示。'}, {'role': 'user', 'content': '这是第一个用户输入。'}, {'role': 'assistant', 'content': '这是第一个助手回应。'}, {'role': 'user', 'content': '这是第二个用户输入。'}, ] print('###### 默认(但正确)聊天模板 ######') print(toker.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)) print('###### 修正后的聊天模板 ######') chat_template = open('./chat_templates/llama-3-instruct.jinja').read() chat_template = chat_template.replace(' ', '').replace('\n', '') toker.chat_template = chat_template print(toker.apply_chat_template(messages, tokenize=False, add_generation_prompt=True))
预期输出:
###### 默认(但正确)聊天模板 ######
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
这是一个系统提示。<|eot_id|><|start_header_id|>user<|end_header_id|>
这是第一个用户输入。<|eot_id|><|start_header_id|>assistant<|end_header_id|>
这是第一个助手回应。<|eot_id|><|start_header_id|>user<|end_header_id|>
这是第二个用户输入。<|eot_id|><|start_header_id|>assistant<|end_header_id|>
###### 修正后的聊天模板 ######
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
这是一个系统提示。<|eot_id|><|start_header_id|>user<|end_header_id|>
这是第一个用户输入。<|eot_id|><|start_header_id|>assistant<|end_header_id|>
这是第一个助手回应。<|eot_id|><|start_header_id|>user<|end_header_id|>
这是第二个用户输入。<|eot_id|><|start_header_id|>assistant<|end_header_id|>
</details>
<details>
<summary><b>示例2: Mistral-7B-Instruct-v0.2</b></summary>
对于mistral-instruct(也包括gemma-it),它本身不支持system消息,所以传递system消息会引发错误。
from transformers import AutoTokenizer toker = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2") messages = [ {'role': 'system', 'content': '这是一个系统提示。'}, {'role': 'user', 'content': '这是第一个用户输入。'}, {'role': 'assistant', 'content': '这是第一个助手回应。'}, {'role': 'user', 'content': '这是第二个用户输入。'}, ] print('###### 默认(但不合适)聊天模板 ######') # 引发错误 #print(toker.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)) print('###### 修正后的聊天模板 ######') chat_template = open('./chat_templates/mistral-instruct.jinja').read() chat_template = chat_template.replace(' ', '').replace('\n', '') toker.chat_template = chat_template print(toker.apply_chat_template(messages, tokenize=False, add_generation_prompt=True))
预期输出:
###### 默认(但会引发错误)聊天模板 ######
jinja2.exceptions.TemplateError: 对话角色必须交替为user/assistant/user/assistant/...
###### 修正后的聊天模板 ######
<s>[INST] 这是一个系统提示。
这是第一个用户输入。 [/INST] 这是第一个助手回应。 </s>[INST] 这是第二个用户输入。 [/INST]
</details>
<details>
<summary><b>示例3: vicuna-7b-v1.5</b></summary>
注意: 在fast-chat中,vicuna在角色消息之间不添加换行符。但我发现添加换行符会导致稍好的性能(特别是对于v1.5版本)。
此外,我发现当给定与默认系统消息不同的系统消息时,vicuna-7/13/33b-v1.3可能效果不佳。所以我建议使用vicuna-7/13b-v1.5代替。
from transformers import AutoTokenizer toker = AutoTokenizer.from_pretrained("lmsys/vicuna-7b-v1.5") messages = [ {'role': 'system', 'content': '这 是一个系统提示。'}, {'role': 'user', 'content': '这是第一个用户输入。'}, {'role': 'assistant', 'content': '这是第一个助手回应。'}, {'role': 'user', 'content': '这是第二个用户输入。'}, ] print('###### 默认(但不合适)聊天模板 ######') print(toker.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)) print('###### 修正后的聊天模板 ######') chat_template = open('./chat_templates/vicuna.jinja').read() chat_template = chat_template.replace(' ', '').replace('\n', '') toker.chat_template = chat_template print(toker.apply_chat_template(messages, tokenize=False, add_generation_prompt=True))
预期输出:
###### 默认(但不合适)聊天模板 ######
<s>[INST] <<SYS>>
这是一个系统提示。
<</SYS>>
这是第一个用户输入。 [/INST] 这是第一个助手回应。 </s><s>[INST] 这是第二个用户输入。 [/INST]
###### 修正后的聊天模板 ######
<s>这是一个系统提示。
USER: 这是第一个用户输入。
ASSISTANT: 这是第一个助手回应。</s>
USER: 这是第二个用户输入。
ASSISTANT:
</details>
注意: 列出的模型并非全部,还包括同一模型系列中的其他大小的模型
<details> <summary><b>Llama-3-Instruct, Llama-3.1-Instruct</b></summary>meta-llama/Meta-Llama-3-8B-Instruct, meta-llama/Meta-Llama-3.1-8B-Instructchat_templates/llama-3-instruct.jinjageneration_configs/llama-3-instruct.jsonmeta-llama/Llama-2-7b-chat-hf, meta-llama/CodeLlama-7b-Instruct-hfchat_templates/llama-2-chat.jinjageneration_configs/llama-2-chat.jsonQwen/Qwen2-7B-Instruct, Qwen/Qwen1.5-7B-Chatchat_templates/chatml.jinjageneration_configs/qwen2-instruct.jsonmistralai/Mistral-7B-Instruct-v0.3, mistralai/Mixtral-8x7B-Instruct-v0.1chat_templates/mistral-instruct.jinjageneration_configs/mistral-instruct.jsonmicrosoft/Phi-3-mini-4k-instructchat_templates/phi-3.jinjageneration_configs/phi-3.json01-ai/Yi-1.5-6B-Chat, 01-ai/Yi-6B-Chatchat_templates/chatml.jinjageneration_configs/yi-chat.jsonnvidia/Llama3-ChatQA-1.5-8Bchat_templates/chatqa.jinjageneration_configs/chatqa.jsonopenchat/openchat_3.5、berkeley-nest/Starling-LM-7B-alphachat_templates/openchat-3.5.jinjageneration_configs/openchat-3.5.jsonzephyr-7b-alphachat_templates/zephyr.jinjageneration_configs/zephyr.jsonvicuna-7b-v1.5、vicuna-7b-v1.3chat_templates/vicuna.jinjageneration_configs/vicuna.jsonmicrosoft/Orca-2-7bchat_templates/chatml.jinjageneration_configs/orca-2.jsontiiuae/falcon-7b-instructchat_templates/falcon-instruct.jinjaupstage/SOLAR-10.7B-Instruct-v1.0chat_templates/solar-instruct.jinjageneration_configs/solar-instruct.jsontatsu-lab/alpaca-7b-wdiffchat_templates/alpaca.jinjageneration_configs/alpaca.jsonLLM360/AmberChat、LLM360/AmberSafechat_templates/amberchat.jinjageneration_configs/amberchat.jsonIlyaGusev/saiga_mistral_7b_lorachat_templates/saiga.jinjageneration_configs/saiga.json

免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具


最适合小白的AI自动化工作流平台
无需编码,轻松生成可复用、可变现的AI自动化工作流

大模型驱动的Excel数据处理工具
基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


AI论文写作指导平台
AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10 万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号