第一章 大语言模型与提示工程的底层逻辑 / Chapter 1: The Underlying Logic of LLMs and Prompt Engineering
你有没有过这样的经历:对着手机里的大语言模型(Large Language Model, LLM)连珠炮似的问了一堆问题,得到的回答要么是泛泛而谈的“正确的废话”,要么是错漏百出的“一本正经的胡说八道”,最后只能无奈地关掉对话框,吐槽一句“这AI也就那样”?别急,问题大概率不在大模型本身,而在于你递给它的“钥匙”——提示词(Prompt)的质量。
Have you ever had this experience: You fire a series of questions at the large language model (LLM) on your phone, only to get either generic "correct nonsense" or seriously wrong but plausible "lies", and finally have to close the dialog box helplessly, complaining "this AI is just so-so"? Don't worry, the problem is most likely not with the LLM itself, but with the "key" you handed to it — the quality of the prompt.
很多人对大模型的认知还停留在“聊天机器人”的层面,觉得它要么是 magic,能写代码、写文案、做分析,要么是“人工智障”,连简单的数学题都算错。这种认知偏差的本质,是没搞懂大模型的底层运行逻辑,也没搞懂提示工程到底是什么——它不是所谓的“AI话术”,而是一套基于大模型底层机制、用来最大化模型性能的系统化方法。
Most people's perception of LLMs is still stuck at the level of "chatbot", thinking it is either magic, able to write code, copywriting, and do analysis, or "artificial idiot", even unable to solve simple math problems. The essence of this cognitive bias is that they don't understand the underlying operating logic of LLMs, nor do they understand what prompt engineering really is — it is not the so-called "AI rhetoric", but a systematic method based on the underlying mechanism of LLMs to maximize model performance.
---
1.1 大语言模型的底层推理机制:不是“思考”,是“超级文字接龙”
1.1.1 The Underlying Inference Mechanism of LLMs: Not "Thinking", But "Super Text Continuation Game"
要搞懂提示工程,首先要拆掉对大模型的“拟人化滤镜”:当前所有主流大语言模型都没有自我意识,也不会像人类一样“理解”问题的深层含义,它的底层逻辑只有一个:基于海量文本训练出的统计规律,预测下一个最可能的词元(Token)。你可以把它理解为一个读过全人类几乎所有公开文本的“超级文字接龙高手”。
To understand prompt engineering, we first need to remove the "anthropomorphic filter" for LLMs: all current mainstream large language models have no self-awareness, nor can they "understand" the deep meaning of questions like humans. Its underlying logic is only one: predict the next most likely token based on the statistical规律 learned from massive text training. You can think of it as a "super text continuation expert" who has read almost all public texts ever written by humans.
举个最简单的例子:你给模型输入“今天天气真好,我想去”,模型会先把这个输入拆分成一个个Token(中文里通常1-2个字对应1个Token,英文里通常1个单词对应1-2个Token),然后在它学习过的所有文本里,统计“今天天气真好,我想去”后面出现频率最高的Token是什么——大概率是“公园”“散步”“海边”这类和休闲场景相关的词,而不是“写代码”“开董事会”这类和当前语境完全无关的词。它再把这个预测出来的Token拼到输入后面,变成“今天天气真好,我想去公园”,然后继续预测下一个Token,直到生成完整的回答。
Take the simplest example: You input "The weather is great today, I want to go to" to the model, and the model will first split this input into individual tokens (in Chinese, usually 1-2 characters correspond to 1 token, in English, usually 1 word corresponds to 1-2 tokens). Then, among all the text it has learned, it counts what token appears most frequently after "The weather is great today, I want to go to" — it is likely to be words related to leisure scenes such as "park", "take a walk", "seaside", rather than words completely unrelated to the current context such as "write code" or "hold a board meeting". It then appends this predicted token to the input to become "The weather is great today, I want to go to the park", and continues to predict the next token until it generates a complete answer.
这个“逐Token预测”的过程,就是大模型的推理(Inference)过程,也是我们和大模型交互的核心逻辑。而整个大模型的训练和使用流程,可以分为三个核心阶段,每个阶段和提示词的关联都完全不同: > This "token-by-token prediction" process is the inference process of LLMs, and also the core logic of our interaction with LLMs. The entire training and usage process of LLMs can be divided into three core stages, and the association with prompts is completely different in each stage:
```mermaid graph LR A[预训练阶段<br/>Pre-training: 学习海量文本的统计规律,构建通用语言与知识底座] --> B[微调阶段<br/>Fine-tuning: 通过标注数据调整参数,适配特定任务/领域/风格] B --> C[推理阶段<br/>Inference: 接收用户提示词,基于上下文生成输出] C --> D[提示工程<br/>Prompt Engineering: 优化推理阶段的输入,最大化模型性能] ```
1. 预训练阶段(Pre-training):相当于木工学习所有通用木工手艺的阶段。模型团队用互联网上TB级甚至PB级的公开文本(网页、书籍、论文、代码、社交媒体内容等)训练模型,让模型学习到人类语言的结构、世界的常识、不同场景下的表达规律,构建一个通用的“语言与知识底座”。这个阶段结束后,模型已经具备了通用的文本理解和生成能力,但还没有适配任何特定任务——就像木工学会了所有通用手艺,但还没专门学过做柜子、做桌子。 > 1. Pre-training Stage: Equivalent to the stage where a carpenter learns all general carpentry skills. The model team uses TB or even PB-level public text (web pages, books, papers, code, social media content, etc.) on the Internet to train the model, so that the model learns the structure of human language, common sense of the world, and expression rules in different scenarios, building a universal "language and knowledge base". After this stage, the model already has general text understanding and generation capabilities, but has not yet adapted to any specific tasks — just like a carpenter has learned all general skills, but has not yet specifically learned to make cabinets or tables.
2. 微调阶段(Fine-tuning):相当于木工专门学习做日式家具的阶段。如果预训练后的通用模型在某个特定场景(比如客服、法律、医疗)效果不好,模型团队会用该领域的标注数据对模型进行二次训练,调整模型的参数,让模型更适配该领域的任务风格和知识要求。比如医疗大模型会喂大量的病历、医学论文、诊疗对话数据,让它学会专业医学术语的表达和诊断逻辑;客服大模型会喂大量的客服对话数据,让它学会礼貌、专业地解答用户问题。 > 2. Fine-tuning Stage: Equivalent to the stage where a carpenter specifically learns to make Japanese-style furniture. If the general model after pre-training does not perform well in a specific scenario (such as customer service, law, medical care), the model team will use annotated data in that field to conduct secondary training on the model, adjust the model's parameters, and make the model more adapted to the task style and knowledge requirements of that field. For example, medical LLMs will be fed a large amount of medical records, medical papers, and diagnostic dialogue data to let it learn the expression of professional medical terms and diagnostic logic; customer service LLMs will be fed a large amount of customer service dialogue data to let it learn to answer user questions politely and professionally.
3. 推理阶段(Inference):相当于木工给你做家具的阶段。用户输入提示词,模型基于预训练和微调学到的知识,结合提示词里的上下文信息,生成对应的输出。这个阶段是用户和大模型直接交互的阶段,也是提示工程发挥作用的阶段。 > 3. Inference Stage: Equivalent to the stage where the carpenter makes furniture for you. The user inputs a prompt, and the model generates the corresponding output based on the knowledge learned from pre-training and fine-tuning, combined with the context information in the prompt. This stage is the stage where users interact directly with the LLM, and it is also the stage where prompt engineering plays a role.
这里有一个非常关键的认知误区需要澄清:很多人觉得要让大模型适配自己的业务,必须花大价钱做微调。实际上对于绝大多数场景,优化提示词的效果已经能接近甚至超过微调,而且成本只有微调的几十分之一。斯坦福大学2023年针对金融领域文本分类任务的研究显示:精心设计的提示词可以让
通用大模型在该任务上的准确率达到微调模型的92%,而成本仅为微调的1/50[^1]。这项研究揭示了一个被很多人忽略的事实:对于绝大多数非极端专业的场景,我们根本不需要修改模型的参数,只需要优化输入给模型的提示词,就能获得接近甚至超过微调的效果。要理解这个结论的底层逻辑,我们首先需要拆解大语言模型的完整运行链路,以及提示词在其中扮演的角色。
[^1]: Stanford HAI. (2023). *Prompt Engineering is the New Fine-tuning*. https://arxiv.org/abs/2302.11382
---
1.1 大语言模型的底层推理机制:不是“思考”,是“超级文字接龙”
1.1.1 全流程拆解:预训练、微调与推理的边界
当前主流大语言模型的完整生命周期可以分为三个阶段,每个阶段的参与方、可控性、和提示词的关联完全不同,搞清楚这个边界,是理解提示工程的第一步:
```mermaid graph TD A[预训练阶段<br/>Pre-training<br/>👉 参与方:模型厂商<br/>👉 可控性:用户完全不可控<br/>👉 输入:PB级公开文本(网页/书籍/论文/代码等)<br/>👉 输出:通用底座模型] --> B[微调阶段<br/>Fine-tuning<br/>👉 参与方:模型厂商/企业技术团队<br/>👉 可控性:普通用户不可控<br/>👉 输入:领域标注数据<br/>👉 输出:领域适配模型] B --> C[推理阶段<br/>Inference<br/>👉 参与方:终端用户<br/>👉 可控性:用户完全可控<br/>👉 输入:提示词+上下文<br/>👉 输出:模型生成结果] C --> D[提示工程<br/>Prompt Engineering<br/>👉 核心操作:优化推理阶段的输入提示词<br/>👉 目标:最大化模型输出质量] style D fill:#ffeb3b,stroke:#333,stroke-width:2px ```
1. 预训练阶段(Pre-training):相当于让模型“读遍全人类所有公开文本”。模型厂商会用TB甚至PB级的公开文本数据训练模型,让模型学习人类语言的结构、世界常识、不同场景的表达规律,构建一个通用的“语言与知识底座”。这个阶段的模型已经具备了通用的文本理解和生成能力,但还没有适配任何特定任务——就像一个刚毕业的通用文科生,什么话题都能聊两句,但让他做专业的法律分析、医疗诊断就力不从心。目前主流的大模型(比如GPT-4、Claude 3、文心一言4.0)的预训练成本都在千万美元以上,普通用户完全无法参与。 > 1. Pre-training Stage: Equivalent to letting the model "read all public texts ever written by humans". Model vendors train the model with TB or even PB-level public text data, so that the model learns the structure of human language, common sense of the world, and expression rules in different scenarios, building a universal "language and knowledge base". The model at this stage already has general text understanding and generation capabilities