从Llama2到Llama3.1:Prompt模板的演变与升级指南

发布时间:2026/6/29 1:53:34

从Llama2到Llama3.1:Prompt模板的演变与升级指南 从Llama2到Llama3.1Prompt模板的演变与升级指南如果你是一位开发者正在使用或计划使用Meta的Llama系列模型那么理解不同版本的prompt模板差异至关重要。从Llama2到Llama3.1prompt模板经历了多次迭代新增了工具调用、多角色交互等功能。本文将深入解析这些变化并提供实用的升级建议帮助你在项目中无缝迁移。1. Llama2的Prompt模板设计Llama2的prompt模板设计相对简单主要围绕多轮对话和系统消息展开。以下是其核心标识符及其作用s和/s分别表示句子的开始BOS和结束EOS用于分隔多轮对话中的消息。[INST]和[/INST]包裹用户输入的消息用于区分用户和模型的交互。SYS和/SYS用于定义系统消息通常用于设置模型的初始行为或上下文。1.1 单轮对话示例s[INST] SYS 你是一个旅行助手擅长提供目的地推荐。 /SYS 巴黎有哪些必去的景点 [/INST]1.2 多轮对话示例s[INST] SYS 你是一个旅行助手擅长提供目的地推荐。 /SYS 巴黎有哪些必去的景点 [/INST] 埃菲尔铁塔、卢浮宫和香榭丽舍大街。/ss[INST] 这些景点需要多少时间游览 [/INST]注意Llama2的prompt模板不支持工具调用功能相对单一适合基础对话场景。2. Llama3的Prompt模板升级Llama3在prompt模板上进行了重大改进引入了更灵活的标识符和角色定义。以下是其核心变化|begin_of_text|替代Llama2的s表示prompt的开始。|eot_id|表示单条消息的结束。|start_header_id|和|end_header_id|用于定义消息的角色如system、user、assistant。|end_of_text|表示prompt的结束模型生成到此停止。2.1 单轮对话示例|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|2.2 多轮对话示例|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|提示Llama3的模板设计更清晰角色定义更灵活适合复杂对话场景。3. Llama3.1的突破性功能工具调用Llama3.1在Llama3的基础上新增了工具调用功能进一步扩展了模型的应用场景。以下是其核心新增标识符和功能|eom_id|表示消息的结束点模型可以在此通知需要进行工具调用。|python_tag|用于标记模型响应中的工具调用。ipython角色新增的角色表示工具调用的输出。3.1 工具调用示例以下是一个完整的工具调用流程系统提示定义工具|begin_of_text||start_header_id|system|end_header_id| Environment: ipython Tools: spotify_trending_songs You are a helpful Assistant. |eot_id||start_header_id|user|end_header_id| 能帮我查一下Spotify上最火的5首歌吗 |eot_id||start_header_id|assistant|end_header_id|模型调用工具functionspotify_trending_songs{n: 5}/function|eom_id|工具返回结果|start_header_id|ipython|end_header_id| [1. BIRDS OF A FEATHER by Billie Eilish, 2. Espresso by Sabrina Carpenter] |eot_id||start_header_id|assistant|end_header_id|模型生成最终响应Spotify上最火的5首歌是 1. BIRDS OF A FEATHER by Billie Eilish 2. Espresso by Sabrina Carpenter |eot_id|3.2 自定义工具调用Llama3.1还支持自定义工具调用格式。例如可以在系统提示中定义以下规则If you choose to call a function, reply in the format: function{function_name}{parameters}/function4. 从Llama2升级到Llama3.1的实用建议如果你正在从Llama2迁移到Llama3.1以下是需要注意的关键点标识符替换将s和/s替换为|begin_of_text|和|end_of_text|。将[INST]和[/INST]替换为|start_header_id|user|end_header_id|和|start_header_id|assistant|end_header_id|。角色定义明确区分system、user、assistant和ipython角色。工具调用适配如果需要工具调用功能确保系统提示中正确定义工具格式。测试与验证在升级后务必测试多轮对话和工具调用场景确保模型响应符合预期。在实际项目中我曾遇到工具调用格式不匹配的问题导致模型无法正确解析工具参数。通过仔细检查系统提示中的格式定义最终解决了这一问题。

相关新闻