Prompt engineering, also known as in-context prompting, refers to methods for how to communicate with LLM to steer its behavior for desired outcomes without updating the model weights. It is an empirical science and the effect of prompt engineering methods can vary a lot among models, thus requiring heavy experimentation and heuristics.
提示工程(Prompt Engineering),又称上下文提示(In-Context Prompting),指的是如何与 LLM 沟通以引导其行为产生期望结果,而不更新模型权重的方法。它是一门经验科学,提示工程方法的效果在不同模型间差异很大,因此需要大量实验和启发式经验。本文仅聚焦自回归语言模型的提示工程。其核心目标是对齐(alignment)与模型可控性(steerability)。
提示工程的本质是:在不动模型权重的前提下,用"怎么说"来改变模型"做什么"。这让它成为使用 LLM 成本最低、迭代最快的杠杆。但作者也坦率指出——很多提示工程论文其实不值得写 8 页,核心 trick 一句话就能讲清,剩下全是刷分。这说明:提示工程高度经验化,方法是否奏效因模型而异,难以形成统一理论。
Zero-shot learning is to simply feed the task text to the model and ask for results.
零样本学习就是直接把任务文本喂给模型并要求给出结果。例如情感分析直接给一句评论,让模型输出"正面/负面"。
Few-shot learning presents a set of high-quality demonstrations, each consisting of both input and desired output, on the target task. As the model first sees good examples, it can better understand human intention and criteria for what kinds of answers are wanted.
少样本学习向模型展示一组高质量示例,每个示例都包含输入和期望输出。由于模型先看到好的例子,它能更好地理解人类意图和"想要什么样的答案"。因此 few-shot 通常优于 zero-shot,但代价是消耗更多 token,长文本时可能触及上下文长度上限。
Many studies observed that choice of prompt format, training examples, and the order of the examples can lead to dramatically different performance, from near random guess to near SoTA. Zhao et al. (2021) proposed that several biases contribute to such high variance:
多项研究发现,提示格式、训练示例的选择、示例的顺序会导致性能剧烈波动——从接近随机猜测到接近当前最优(SoTA)。Zhao 等(2021)指出 LLM 存在几种偏差:
这是一个反直觉但极其重要的发现:给模型几个例子,模型的表现可能因为这些例子的"顺序"而天差地别。这意味着 few-shot 不是"随便挑几个例子摆上去"——示例的选择、数量、顺序、标签分布都会被模型当成隐含信号。在实践中,这意味着做 prompt 评测时必须多次打乱示例顺序、报告方差,否则你的"提升"可能只是运气。
通用建议:保持示例多样、与测试样本相关、并随机打乱顺序,以避免多数标签偏差和近因偏差。增大模型或增加示例数并不能降低不同排列带来的方差。验证集有限时,应选择使模型预测不极端失衡、也不过度自信的顺序(Lu 等 2022)。
The purpose of presenting few-shot examples is to explain our intent to the model. However, few-shot can be expensive in terms of token usage. So, why not just give the instruction directly? Instructed LM finetunes a pretrained model with high-quality tuples of (task instruction, input, ground truth output) to make LM better understand user intention and follow instruction.
在提示中放 few-shot 示例的本意是"用示范来表达意图"。既然如此,为什么不直接给指令?指令模型(如 InstructGPT)用大量高质量 (任务指令, 输入, 正确输出) 三元组微调预训练模型,让模型更好理解用户意图并遵循指令,常用方法是 RLHF。与指令模型交互时,应详细、具体、精确地描述任务要求,说要做什么而不是不要做什么。
指令还可以通过指定目标受众来引导:给 6 岁孩子解释量子物理;或指定安全要求:用工作场合安全的语言。上下文指令学习(Ye 等 2023)把 few-shot 与指令提示结合,在提示中放入跨任务的多个示范,每个示范含指令、输入、输出。
Self-consistency sampling is to sample multiple outputs with temperature > 0 and then selecting the best one out of these candidates. A general solution is to pick majority vote.
自洽性采样(Wang 等 2022)以温度 > 0 采样多次得到多个输出,再从中选最佳。通用做法是多数投票(majority vote);对于编程这类易于验证的任务,可直接跑解释器用单元测试验证正确性。
Chain-of-thought prompting generates a sequence of short sentences to describe reasoning logics step by step, known as reasoning chains or rationales, to eventually lead to the final answer. The benefit of CoT is more pronounced for complicated reasoning tasks, while using large models (e.g. with more than 50B parameters).
思维链(CoT)提示(Wei 等 2022)生成一系列短句,逐步描述推理逻辑(称为推理链/rationales),最终导出答案。CoT 的收益在复杂推理任务 + 大模型(如 50B+ 参数)时最为明显;简单任务收益甚微。
Let's think step by step)显式鼓励模型先生成推理链,再用 Therefore, the answer is 引出答案(Kojima 等 2022)。"Let's think step by step" 这五个词是提示工程史上最著名的咒语之一。它的洞察在于:大模型其实"会"推理,但默认直接给答案时容易出错;只要逼它把中间步骤写出来,正确率就大幅提升。这本质上是把测试时计算(test-time compute)从一步变成多步——用更多 token 换更高准确率。这也是后来 o1/r1 类"推理模型"的思想源头:与其靠 prompt 哄模型多想,不如把"多想"训练进模型本身。
\n 分隔推理步骤优于"step i"、句号或分号。CoT 的演进脉络:线性思维(CoT)→ 带检索的思维(ReAct/IRCoT)→ 树状思维(ToT)。每一步都在增加"思考的维度"——从一条线,到边想边查,到同时考虑多条路径并评估回溯。代价是计算成本成倍增长。规律是:任务越需要"深思熟虑",越值得用更重的推理结构;简单 QA 反而该保持轻量。
Prompt is a sequence of prefix tokens that increase the probability of getting desired output given input. Therefore we can treat them as trainable parameters and optimize them directly on the embedding space via gradient descent.
提示本质是"一串前缀 token,用来提升给定输入下得到期望输出的概率"。因此可以把它当作可训练参数,直接在嵌入空间用梯度下降优化——例如 AutoPrompt、Prefix-Tuning、P-tuning、Prompt-Tuning。趋势是从 AutoPrompt 到 Prompt-Tuning,设定越来越简化。
APE (Automatic Prompt Engineer) is a method to search over a pool of model-generated instruction candidates and then filters the candidate set according to a chosen score function to ultimately choose the best candidate with highest score.
APE(自动提示工程师)(Zhou 等 2022)在一池模型生成的指令候选中搜索,按打分函数过滤,最终选出得分最高的指令。它先用少量输入-输出对让 LLM 生成指令候选,再用蒙特卡洛搜索生成语义相近的变体来改进最佳候选。
这里有两条截然不同的"自动做 prompt"路线:(1) 软提示(soft prompt)——把 prompt 变成连续向量直接梯度优化,效果强但不可读、不可迁移;(2) LLM 自己写自然语言 prompt(APE)——可读、可解释,是后来"让模型优化自己的 prompt""自动 prompt 工程"这一脉的起点。今天的"元提示""prompt 自我改进"几乎都源于 APE 的思想。
Often we need to complete tasks that require latest knowledge after the model pretraining time cutoff or internal/private knowledge base. Many methods for Open Domain QA depend on first doing retrieval over a knowledge base and then incorporating the retrieved content as part of the prompt.
很多任务需要模型预训练截止时间之后的最新知识,或内部/私有知识库。这类开放域问答的标准做法是:先在知识库上做检索,再把检索到的内容作为提示的一部分。整个过程准确率取决于检索质量与生成质量两者。
Lazaridou 等(2022)用 Google Search 检索:取 20 个 URL 抽取纯文本,按 6 句一段切分,用 TF-IDF 余弦相似度排序段落,只把最相关段落放进提示。答案重排有三种打分:RAG 式、噪声通道(Noisy channel)、专家乘积(PoE),实验中 PoE > Noisy channel > RAG。一个有趣发现:仅靠内部检索(先让模型生成关于某主题的知识再作答)也有收益(Liu 等 2022)。
Both PAL and PoT ask LLM to generate programming language statements to resolve natural language reasoning problems, hence offloading the solution step to a runtime such as a Python interpreter. Such setup decouples complex computation and reasoning.
PAL(Gao 等 2022)和 PoT(Chen 等 2022)都让 LLM 生成编程语言语句来解决自然语言推理问题,把求解步骤交给 Python 解释器等运行时。这种设定把复杂计算与推理解耦,依赖模型有足够好的编程能力——也算今天"代码执行工具"的雏形。
TALM(Parisi 等 2022)是带文本到文本 API 调用的语言模型,采用自举(self-play)方式迭代扩展工具使用数据集并微调,本质上模仿了 RL 过程。Toolformer(Schick 等 2023)让 LM 通过简单 API 使用外部工具,自监督构建、每种 API 只需少量示范。其工具箱包括:计算器(补足精确数学)、问答系统(缓解幻觉)、搜索引擎(提供最新信息)、翻译系统、日历。
Toolformer 的训练流程:① 用 few-shot 让预训练模型给数据集标注潜在 API 调用;② 基于概率筛选 top-k 调用位置;③ 用自监督损失判断 API 调用是否真的帮模型预测后续 token(对比有/无 API 结果的损失差);④ 仅保留有帮助的调用,在增强数据集上微调。当前 Toolformer 不支持链式工具调用(一个工具的输出作为另一个的输入)和交互式使用。
本节其实是现代 Agent 工具使用的完整谱系:从检索(RAG)→ 代码执行(PAL/PoT)→ 通用 API 调用(TALM/Toolformer)。Toolformer 的核心洞察——"只在 API 调用真能帮模型预测下一个 token 时才学它"——至今仍是训练工具使用模型的黄金准则。它用"损失差"作为工具是否有用的信号,这个自监督范式后来被广泛复用。本文是 Lilian 在 Agent 综述(2023-06)之前的姊妹篇,两者合起来几乎覆盖了 LLM 应用的全部技术栈。