AI大模型Llama 3系统级提示词格式Prompt Format

Tokens

本文重点介绍 Llama 3.1提示词Prompt支持的特殊标记(token)。

  • • <|begin_of_text|>:指定提示词的开始
  • • <|end_of_text|>:模型将停止生成更多标记。此标记仅由基础模型生成。
  • • <|finetune_right_pad_id|>:此标记用于在批处理中将文本序列填充到相同长度。
  • • <|start_header_id|> 和 <|end_header_id|>:这些标记用于包围特定消息的角色。可能的角色包括:[system, user, assistant 和 ipython]
  • • <|eom_id|>:消息结束标记。表示执行一个任务的潜在停止点,此时模型可以通知执行器需要进行工具调用Tool Calling。当系统提示词中使用 Environment: ipython 指令时,或者模型调用内置工具时,会生成此标记。
  • • <|eot_id|>:轮次结束标记。表示模型已确定完成对启动其响应的用户消息的交互。此标记在两种场景中使用:
      1. 在模型和用户的直接交互结束时
      1. 在模型与可用工具的多次交互结束时,此标记向执行器发出信号,表明模型已完成响应生成。
  • • <|python_tag|>:模型响应中用于标识工具调用的特殊标记。

Llama 3.1的4种角色

Llama 3.1 支持以下 4 种不同的角色:

  • • system:设置与 AI 模型交互的上下文环境。通常包括帮助模型有效响应的规则、指南或必要信息。
  • • user:代表与模型交互的人类用户。包括向模型提供的输入内容、命令和问题。
  • • ipython:Llama 3.1 中新引入的角色,在语义上代表"工具"角色。当执行器将工具调用的输出发送回模型时,使用此角色标记相关消息。
  • • assistant:代表 AI 模型基于 system、ipython 和 user 提示词所生成的响应内容。

AI模型交互流程

Llama 3.1 基础模型

Llama 3.1基础模型Text completion使用这种格式。

提示词的输入格式:

<|begin_of_text|>Color of sky is blue but sometimes can also be

模型的响应格式: 

red, orange, yellow, green, purple, pink, brown, gray, black, white, and even rainbow colors. The color of the sky can change due to various reasons such as time of day, weather conditions, pollution, and atmospheric phenomena.
The color of the sky is primarily blue because of a phenomenon called

Llama 3.1 指令模型

User角色与Assistant角色对话

Here is a regular multi-turn user assistant conversation and how its formatted.

提示词的输入格式:

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

You are a helpful assistant<|eot_id|><|start_header_id|>user<|end_header_id|>

Answer who are you in the form of jeopardy?<|eot_id|><|start_header_id|>assistant<|end_header_id|>

模型的响应格式: 

Here's my response

"What is a helpful assistant?"<|eot_id|>
工具调用格式

可以使用系统提示词启用以下三个内置工具(brave_search、wolfram_alpha 和代码解释器Code Interpreter):

  • • Brave Search:执行网络搜索的工具调用功能
  • • Wolfram Alpha:执行复杂数学计算的工具调用功能
  • • Code Interpreter(代码解释器):使模型能够输出 Python 代码
内置工具调用

以下是使用 Brave Search 的对话示例:

提示词的输入格式:

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

Environment: ipython
Tools: brave_search, wolfram_alpha
Cutting Knowledge Date: December 2023
Today Date: 21 September 2024

You are a helpful assistant.
<|eot_id|><|start_header_id|>user<|end_header_id|>

Search the web for the latest price of 1oz gold?<|eot_id|><|start_header_id|>assistant<|end_header_id|>

模型的响应格式: 

<|python_tag|>brave_search.call(query="latest price of 1oz gold")<|eom_id|>
  • 只需在系统提示词中包含 Environment: ipython 即可启用代码解释器功能;因此,无需在 Tools: 行中特别指定代码解释功能。模型可以生成 Python 代码,这些代码会由执行器进行解释执行,并将执行结果反馈给模型。
  • • 助手角色Assistant响应的消息正文始终以特殊标记 <|python_tag|> 开头。
  • • 模型生成 <|eom_id|>(消息结束标记)而不仅仅是标准的 <|eot_id|>(轮次结束标记)。轮次结束标记表示对话轮次已完成,而消息结束标记则表示模型正在进行多步推理。这意味着模型正在等待接收包含工具调用执行结果的后续消息。
  • • 模型的工具调用响应格式为 tool.call(query="..."),其中 tool 可以是 brave_search 或 wolfram_alpha。
内置代码解释器功能

以下是模型响应并生成代码的实际示例:

提示词的输入格式:

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

Environment: ipython<|eot_id|><|start_header_id|>user<|end_header_id|>

Write code to check if number is prime, use that to see if the number 7 is prime<|eot_id|><|start_header_id|>assistant<|end_header_id|>

模型的响应格式: 

<|python_tag|>def is_prime(n):
    if n <= 1
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

print(is_prime(7))  # Output: True<|eom_id|>
  • 模型首先生成 <|python_tag|> 标记,然后继续编写需要被执行器执行的 Python 代码。
  • • 系统提示词中无需显式指定 code_interpreter。只要包含 Environment: ipython 就会自动启用代码解释器功能。
内置工具完整交互示例

以下展示了一个完整的内置工具交互过程,包括工具的响应结果以及助手的最终响应内容。

提示词的输入格式

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

Environment: ipython
Tools: brave_search, wolfram_alpha
<|eot_id|><|start_header_id|>user<|end_header_id|>

What is the 100th decimal of pi?<|eot_id|><|start_header_id|>assistant<|end_header_id|>

<|python_tag|>wolfram_alpha.call(query="100th decimal of pi")<|eom_id|><|start_header_id|>ipython<|end_header_id|>


{
    "queryresult": {
        "success": true,
        "inputstring": "100th decimal of pi",
        "pods": [
            {
                "title": "Input interpretation",
                "subpods": [
                    {
                        "title": "",
                        "plaintext": "100th digit | π"
                    }
                ]
            },
            {
                "title": "Nearby digits",
                "subpods": [
                    {
                        "title": "",
                        "plaintext": "...86208998628034825342117067982148086513282306647093..."
                    }
                ]
            },
            {
                "title": "Result",
                "primary": true,
                "subpods": [
                    {
                        "title": "",
                        "plaintext": "7"
                    }
                ]
            }
        ]
    }
}
<|eot_id|><|start_header_id|>assistant<|end_header_id|>

模型的响应格式: 

The 100th decimal of pi is 7.<|eot_id|>
  • 请注意助手响应中包含的 <|python_tag|> 特殊标记。
  • • 当 Wolfram Alpha 的计算结果被传回模型时,使用 ipython 角色标识。
  • • 助手的最终回复消息中包含 <|eot_id|>(轮次结束)标记。
零样本Zero Shot工具调用
基于 JSON 的工具调用

Llama 模型现在支持在单条消息中输出自定义工具调用,这使得工具调用过程变得更加便捷。以下提示词示例将展示模型如何输出自定义工具调用。需要特别说明的是,模型本身并不执行这些工具调用;它只负责提供结构化的输出信息,供执行器进行具体的调用操作。

提示词的输入格式:

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

Environment: ipython

Cutting Knowledge Date: December 2023
Today Date: 21 September 2024

You are a helpful assistant.
<|eot_id|><|start_header_id|>user<|end_header_id|>

Answer the user's question by making use of the following functions if needed.
If none of the function can be used, please say so.
Here is a list of functions in JSON format:
{
    "type": "function",
    "function": {
        "name": "trending_songs",
        "description": "Returns the trending songs on a Music site",
        "parameters": {
            "type": "object",
            "properties": [
                {
                    "n": {
                        "type": "object",
                        "description": "The number of songs to return"
                    }
                },
                {
                    "genre": {
                        "type": "object",
                        "description": "The genre of the songs to return"
                    }
                }
            ],
            "required": ["n"]
        }
    }
}

Return function calls in JSON format.<|eot_id|><|start_header_id|>user<|end_header_id|>

Use tools to get latest trending songs<|eot_id|><|start_header_id|>assistant<|end_header_id|>

模型的响应格式:

<|python_tag|>{
    "type": "function",
    "name": "trending_songs",
    "parameters": {
        "n": "10",
        "genre": "all"
    }
}<|eom_id|>
  • JSON 格式的工具定义必须包含工具名称(name)、描述(description)和参数(parameters)
  • • 由于系统提示词中包含了 Environment: ipython 设置,模型会在响应中生成 <|python_tag|> 和 <|eom_id|> 标记
  • • 工具的使用说明需要以用户消息的形式添加
  • • 当前版本仅支持单个工具的调用操作
用户自定义的工具调用示例

以下是基于function工具调用的示例。

此示例展示了如何为模型编写自定义指令,以实现零样本工具调用。在该示例中,我们使用function标签定义了一种自定义工具调用格式。

提示词的输入格式:

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

Environment: ipython

Cutting Knowledge Date: December 2023
Today Date: 21 September 2024

You are a helpful assistant.
<|eot_id|><|start_header_id|>user<|end_header_id|>

You have access to the following functions:

Use the function 'trending_songs' to 'Returns the trending songs on a Music site':
{"name": "trending_songs", "description": "Returns the trending songs on a Music site", "parameters": {"genre": {"description": "The genre of the songs to return", "param_type": "str", "required": false}, "n": {"description": "The number of songs to return", "param_type": "int", "required": true}}}

Think very carefully before calling functions.
If you choose to call a function ONLY reply in the following format with no prefix or suffix:

<function=example_function_name>{"example_name": "example_value"}</function>

Reminder:
- If looking for real time information use relevant functions before falling back to brave_search
- Function calls MUST follow the specified format, start with <function= and end with </function>
- Required parameters MUST be specified
- Only call one function at a time
- Put the entire function call reply on one line<|eot_id|><|start_header_id|>user<|end_header_id|>

Use tools to get latest trending songs<|eot_id|><|start_header_id|>assistant<|end_header_id|>

模型的响应格式: 

<function=trending_songs>{"n": 10}</function><|eot_id|>
  • 在这种情况下,模型不会使用 <|python_tag|> 进行响应,并以 <|eot_id|> 结束。

  • • 工具的使用说明被添加为用户消息。

 

 


 

  如何学习大模型?

学习AI大模型是一个系统的过程,需要从基础开始,逐步深入到更高级的技术。

这里给大家精心整理了一份全面的AI大模型学习资源,包括:AI大模型全套学习路线图(从入门到实战)、精品AI大模型学习书籍手册、视频教程、实战学习、面试题等,资料免费分享!

这是一份大模型从零基础到进阶的学习路线大纲全览,小伙伴们记得点个收藏!

请添加图片描述

第一阶段: 从大模型系统设计入手,讲解大模型的主要方法;

第二阶段: 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用;

第三阶段: 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统;

第四阶段: 大模型知识库应用开发以LangChain框架为例,构建物流行业咨询智能问答系统;

第五阶段: 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型;

第六阶段: 以SD多模态大模型为主,搭建了文生图小程序案例;

第七阶段: 以大模型平台应用与开发为主,通过星火大模型,文心大模型等成熟大模型构建大模型行业应用。

100套AI大模型商业化落地方案

请添加图片描述

大模型全套视频教程

请添加图片描述

200本大模型PDF书籍

请添加图片描述

👉学会后的收获:👈

• 基于大模型全栈工程实现(前端、后端、产品经理、设计、数据分析等),通过这门课可获得不同能力;

• 能够利用大模型解决相关实际项目需求: 大数据时代,越来越多的企业和机构需要处理海量数据,利用大模型技术可以更好地处理这些数据,提高数据分析和决策的准确性。因此,掌握大模型应用开发技能,可以让程序员更好地应对实际项目需求;

• 基于大模型和企业数据AI应用开发,实现大模型理论、掌握GPU算力、硬件、LangChain开发框架和项目实战技能, 学会Fine-tuning垂直训练大模型(数据准备、数据蒸馏、大模型部署)一站式掌握;

• 能够完成时下热门大模型垂直领域模型训练能力,提高程序员的编码能力: 大模型应用开发需要掌握机器学习算法、深度学习框架等技术,这些技术的掌握可以提高程序员的编码能力和分析能力,让程序员更加熟练地编写高质量的代码。

LLM面试题合集

请添加图片描述

大模型产品经理资源合集

请添加图片描述

大模型项目实战合集

请添加图片描述

😝有需要的小伙伴,可以扫描下方二v码免费领取【保证100%免费】🆓

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值