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

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

本文将讨论Llama 3.1模型系统级提示中使用的特殊标记和角色。它涵盖了如<|begin_of_text|>、<|end_of_text|>等标记,这些标记有助于定义文本的开始和结束、填充以及不同角色(如系统、用户、助手和ipython)的头部。文章还解释了这些标记在各种场景中的使用,包括工具调用和用户互动。此外,它提供了Llama 3.1支持的角色的详细信息,这些角色对于设置交互上下文和在对话式AI设置中生成响应至关重要。

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. 在模型和用户的直接交互结束时
    2. 在模型与可用工具的多次交互结束时,此标记向执行器发出信号,表明模型已完成响应生成。
  • <|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 conver

### 关于 LlamaIndex RAG 提示的信息 LlamaIndex 是一种用于构建检索增强生成(RAG)系统的工具,它允许开发者创建复杂的查询管道并优化模型性能。对于 RAG 系统而言,提示的设计至关重要,因为这直接影响到最终生成的质量和准确性。 #### 创建有效的 RAG 提示 为了使 RAG 系统发挥最佳效果,在设计提示时应考虑以下几个方面: - **上下文集成**:确保提示能够有效地融入来自文档或其他数据源的上下文信息[^1]。 ```python context = "The Eiffel Tower is a wrought iron lattice tower on the Champ de Mars in Paris, France." question = "What material is the Eiffel Tower made of?" prompt_template = f"Given this context: {context}, answer the following question: {question}" ``` - **多轮对话支持**:考虑到实际应用场景中的交互需求,提示应该具备处理连续对话的能力,即能够在一轮或多轮交流中保持一致性[^2]。 ```python history = ["User asked about materials used.", "Assistant mentioned it&#39;s made from wrought iron."] current_question = "Is there any other metal involved?" multi_turn_prompt = "\n".join(history) + f"\n{current_question}" ``` - **指令清晰度**:提供给 AI 的指示应当尽可能具体明确,以便其能更好地理解任务目标并给出恰当的回答[^3]。 ```python clear_instruction = ( "You are an expert historian specializing in European architecture. " "Please provide detailed information regarding..." ) ``` #### 实际案例展示 下面是一个具体的例子,展示了如何使用 LlamaIndex 和 LangChain 来配置一个聊天完成 API 接口,并通过精心设计的提示来获取高质量的结果[^4]。 ```python from langchain import PromptTemplate, LLMChain from llama_index import SimpleDirectoryReader, GPTListIndex, LLMPredictor, ServiceContext # 加载本地文件夹内的文本文件作为索引输入 documents = SimpleDirectoryReader(&#39;data&#39;).load_data() index = GPTListIndex(documents) def construct_llama_rag_chain(): llm_predictor = LLMPredictor(llm=...) service_context = ServiceContext.from_defaults( chunk_size_limit=None, embed_model="text-davinci-003", llm_predictor=llm_predictor ) rag_chain = ... # 定义链式调用逻辑 return rag_chain rag_chain = construct_llama_rag_chain() prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"]) response = rag_chain.run(prompt.format(context=context, question=question)) print(response) ``` 上述代码片段说明了如何加载文档、建立索引以及定义一个简单的 RAG 流程。其中 `construct_llama_rag_chain` 函数负责组装整个工作流;而 `PromptTemplate` 则用来定制化每次请求的具体格式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值