
标准化大语言模型聊天模板的开源库
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

全球首个AI音乐社区
音述AI是全球首个AI音乐社区,致力让每个人都能用音乐表达自我。音述AI提供零门槛AI创作工具,独创GETI法则帮助用户精准定义音乐风格,AI润色功能支持自动优化作品质感。音述AI支持交流讨论、二次创作与价值变现。针对中文用户的语言习惯与文化背景进行专门优化,支持国风融合、C-pop等本土音乐标签,让技术更好地承载人文表达。


阿里Qoder团队推出的桌面端AI智能体
QoderWork 是阿里推出的本地优先桌面 AI 智能体,适配 macOS14+/Windows10+,以自然语言交互实现文件管理、数据分析、AI 视觉生成、浏览器自动化等办公任务,自主拆解执行复杂工作流,数据本地运行零上传,技能市场可无限扩展,是高效的 Agentic 生产力办公助手。


一站式搞定所有学习需求
不再被海量信息淹没,开始真正理解知识。Lynote 可摘要 YouTube 视频、PDF、文章等内容。即时创建笔记,检测 AI 内容并下载资料,将您的学习效率提升 10 倍。


为AI短剧协作而生
专为AI短剧协作而生的AniShort正式发布,深度重构AI短剧全流程生产模式,整合创意策划、制作执行、实时协作、在线审片、资产复用等全链路功能,独创无限画布、双轨并行工业化工作流与Ani智能体助手,集成多款主流AI大模型,破解素材零散、版本混乱、沟通低效等行业痛点,助力3人团队效率提升800%,打造标准化、可追溯的AI短剧量产体系,是AI短剧团队协同创作、提升制作效率的核心工具。


能听懂你表达的视频模型
Seedance two是基于seedance2.0的中国大模型,支持图像、视频、音频、文本四种模态输入,表达方式更丰富,生成也更可控。


国内直接访问,限时3折
输入简单文字,生成想要的图片,纳米香蕉中文站基于 Google 模型的 AI 图片生成网站,支持文字生图、图生图。官网价格限时3折活动


职场AI,就用扣子
AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!


多风格AI绘画神器
堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。


零代码AI应用开发平台
零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号