- 文档
- 教程
- 知识库
- 人工智能
- 提示工程
【免费下载链接】Context-Engineering
"Context engineering is the delicate art and science of filling the context window with just the right information for the next step." — Andrej Karpathy. A frontier, first-principles handbook inspired by Karpathy and 3Blue1Brown for moving beyond prompt engineering to the wider discipline of context design, orchestration, and optimization.
本文围绕 Context-Engineering 仓库中的 Few-Shot Learning Template 展开,系统讲解"以例示教"的提示工程方法论:从模板骨架、参数占位符、完整可运行示例,到 Zero-Shot / One-Shot / Many-Shot 变体与示例选配的最佳实践。读完本文,你将掌握如何为分类、数据转换、风格对齐等任务构造高一致性、低方差的 few-shot 上下文,并能结合仓库中的 prompt_templates.yaml 与 02_expand_context.py 等源码进行落地验证与量化评测。
为什么用示例而不是规则
Few-shot learning 的核心信条是"展示而非讲述"(show rather than tell)。当任务的规则难以用语言精确描述、或期望的输出格式/风格高度模式化时,直接给模型几个"输入 → 输出"对,让它从示例中归纳规律,往往比穷举规则更可靠。
这在 Context-Engineering 的上下文分层理论中处于明确的位置:仓库 00_foundations/01_atoms_prompting.md 指出,原子提示(单条指令)存在"无记忆、演示能力有限、易歧义、输出方差高"等固有局限;而 00_foundations/02_molecules_context.md 则把 few-shot learning 定义为从原子到分子(Molecule = 指令 + 示例 + 上下文 + 新输入)的关键一跃——模型通过识别输入输出间的模式并延续它,即可完成新用例的处理。
Few-shot 模板尤其适合以下场景:
- 难以解释、但容易演示的任务(如情绪判定的语气拿捏);
- 模式驱动型任务,示例比规则更能传达规律;
- 需要保持输出格式或风格高度一致的任务(如 JSON 序列化);
- 需要训练模型做出细微判断或分类的任务。
模板骨架:Task → Examples → Your Turn
Few-Shot Learning Template 的标准结构如下,三段式布局把"任务定义、规律演示、待解输入"清晰分离,模型在## Your Turn处自动延续前文示例建立的模式:
# Task: {{task_description}} ## Examples ### Example 1 Input: {{input_1}} Output: {{output_1}} ### Example 2 Input: {{input_2}} Output: {{output_2}} ### Example 3 Input: {{input_3}} Output: {{output_3}} ## Your Turn Input: {{new_input}} Output:这一骨架与仓库课程模板中的 few_shot_learning_template 高度一致:后者以数学化表述给出模式提取公式P = Extract(examples)、应用公式A(P, new_input),并在示例后追加## Pattern Analysis(Common pattern / Key features)小节,引导模型显式归纳规律后再作答——可作为本模板在生产环境中的增强形态参考。
参数说明
| 占位符 | 含义 | 填写建议 |
|---|---|---|
{{task_description}} | 对任务的简短描述 | 例:"Classify the sentiment of these reviews";一句话说清做什么、输出什么 |
{{input_X}} | 演示规律的示例输入 | 推荐 3–5 个;覆盖不同情形与边界条件 |
{{output_X}} | 每个输入对应的期望输出 | 必须与任务描述中的输出规格完全一致 |
{{new_input}} | 希望 AI 运用所学模式处理的新用例 | 只放输入,让模型续写 Output |
从源码角度看,02_expand_context.py 提供了与模板等价的可编程版本:create_expanded_context()接收base_prompt、examples、constraints、audience、tone、output_format等参数,按固定顺序拼装上下文,其中examples参数正是以Examples:+Example N:的编号格式渲染——说明 few-shot 示例在上下文工程中是一个可动态组合的独立"上下文层"。
完整示例一:情感分类(Sentiment Classification)
# Task: Classify the sentiment of customer feedback as positive, negative, or neutral ## Examples ### Example 1 Input: "The product arrived on time and works perfectly. Couldn't be happier with my purchase!" Output: Positive ### Example 2 Input: "Delivery was quick but the product has several scratches on the surface." Output: Neutral ### Example 3 Input: "Terrible customer service. Had to call three times and still haven't resolved my issue." Output: Negative ## Your Turn Input: "Package was delivered two days late, but the quality of the item exceeded my expectations." Output:注意示例 2 的刻意设计:它同时包含正面线索(delivery was quick)与负面线索(scratches),输出被标为 Neutral,正是为了让模型学会处理"混合信号"的边界情形,避免只做简单的关键词正负匹配。这正是 02_molecules_context.md 中"示例选择科学"所强调的:用近失样例(near-misses)建立精确边界。
完整示例二:数据转换(Data Transformation)
# Task: Convert the given product information into a standardized JSON format ## Examples ### Example 1 Input: Product: Wireless Headphones Brand: SoundCore Price: $79.99 Features: Noise cancellation, 30-hour battery, Bluetooth 5.0 Output: ```json { "product_name": "Wireless Headphones", "manufacturer": "SoundCore", "price_usd": 79.99, "specifications": [ "Noise cancellation", "30-hour battery", "Bluetooth 5.0" ] }Example 2
Input: Product: Smart Watch Pro Brand: TechFit Price: $129.95 Features: Heart rate monitor, GPS tracking, Water resistant
Output:
{ "product_name": "Smart Watch Pro", "manufacturer": "TechFit", "price_usd": 129.95, "specifications": [ "Heart rate monitor", "GPS tracking", "Water resistant" ] }Your Turn
Input: Product: Portable Bluetooth Speaker Brand: AudioMax Price: $45.50 Features: Waterproof, 12-hour playback, Built-in microphone
Output:
该示例揭示了 few-shot 模板在结构化任务中的三个关键能力:字段名规范化(`Product` → `product_name`、`Brand` → `manufacturer`)、类型转换(`$79.99` → `price_usd: 79.99` 数值类型)、以及列表化重构(Features 段落 → `specifications` 数组)。两个示例共同确立 schema 约定,第三个输入即可被模型按同一 schema 自动序列化——这正是数据抽取/清洗类流水线常用的上下文工程手段。 ## 三种变体:Zero-Shot、One-Shot 与 Many-Shot ### Zero-Shot Extension(无示例,靠模式描述) 当没有现成示例、但能用语言描述规律时使用: ```text # Task: {{task_description}} ## Pattern {{detailed_pattern_description}} ## Format {{output_format_specification}} ## Your Turn Input: {{new_input}} Output:这一变体对应仓库课程中 01_prompt_engineering.md 的"Stage 1: Direct Instruction"到"Stage 2: Example-Based Learning"的过渡——描述模式本质上是把示例信息压缩进指令文本,省 token 但依赖模型的指令遵循能力。
One-Shot Learning(单示例)
适用于能被一个示例完整传达的简单模式:
# Task: {{task_description}} ## Example Input: {{input_example}} Output: {{output_example}} ## Your Turn Input: {{new_input}} Output:Many-Shot Learning(多示例 + 测试用例)
适用于需要大量示例覆盖复杂模式的场景,并额外加入测试用例用于验证模型是否真正学会了规律:
# Task: {{task_description}} ## Examples [Examples 1-10 formatted as input/output pairs] ## Test Cases [Additional examples to validate understanding] ## Your Turn Input: {{new_input}} Output:从 02_molecules_context.md 给出的示例数量经验表看:分类任务每类 1–3 个示例、生成任务 2–5 个、结构化抽取 2–4 个覆盖全部字段、推理任务 2–3 个带思考步骤、翻译任务 3–5 个不同复杂度——Many-Shot 的 10 个示例量级适合高复杂度任务,但必须接受"边际收益递减":每个额外示例都要消耗 token,而准确率提升逐级收窄,这正是 01_atoms_prompting.md 所述 token-质量幂律曲线在 few-shot 场景的体现。
最佳实践清单
- 使用多样化的示例,覆盖不同情形与边缘条件;
- 按从简到难的顺序编排示例,逐步建立理解;
- 大多数任务包含 3–5 个示例(简单模式可更少,复杂模式适当增加);
- 保证所有示例的格式高度一致——格式本身就是模式的一部分;
- 选择有代表性的示例,清晰呈现规律;
- 让示例彼此有足够区分度,突出模式而非表面相似性;
- 分类任务务必覆盖所有可能的类别,避免类别漏演示;
- 生成任务适当展示风格、长度与内容的跨度;
- 用不同输入反复测试模式,确认模型确实学到了规律而非死记硬背。
从源码佐证看,01_min_prompt.py 提供了衡量一致性的实验框架(measure_consistency()多次采样并计算一致性得分),02_expand_context.py 则提供了calculate_metrics()与generate_response(),可对"有示例 vs 无示例"的同一任务定量比较 prompt tokens、response tokens、token 效率与延迟——建议读者按此流程为自己的 few-shot 模板建立评测基线,用数据验证"加几个示例"的 ROI。
何时该用其他模板
Few-shot 并非万能。仓库 PROMPTS 目录下的模板矩阵提供了选择依据:
- Minimal Context Template:当示例非必要、直接指令足够时使用(例如任务边界清晰、交付物明确的场景),避免不必要的 token 开销;
- Chain of Thought Template:当需要逐步演示推理过程时使用——它把 few-shot 的输入输出对扩展为"输入 → 思考步骤 → 输出",在推理类任务上通常比纯示例更有效;
- Pattern and Anti-Pattern Template:当同时展示正例与反例能更清晰地澄清期望时使用。
判断准则可概括为:需要"学会一种格式或风格"用 few-shot;需要"学会一种思考方法"用 chain-of-thought;任务足够简单则退回 minimal context,把 token 花在刀刃上。
小结
Few-Shot Learning Template 是 Context-Engineering 从原子提示走向分子上下文的核心工具:它以三段式骨架(Task → Examples → Your Turn)把"演示规律"制度化,通过参数化占位符实现跨任务复用,并通过 Zero-Shot / One-Shot / Many-Shot 变体在不同 token 预算下权衡效果。在仓库体系中,它与 prompt_templates.yaml 的few_shot_learning_template、02_expand_context.py 的上下文组装函数、02_molecules_context.md 的动态示例检索思想(按查询相似度动态选取最相关示例)共同构成了从"手工抄模板"到"系统化构建上下文"的进阶路径。下一步实践建议:选一个你手头的分类或转换任务,分别以 0 / 1 / 3 / 5 个示例跑通 02_expand_context.py 的实验流程,亲手画出属于该任务的 token-质量曲线。
- 文档
- 教程
- 知识库
- 人工智能
- 提示工程
【免费下载链接】Context-Engineering
"Context engineering is the delicate art and science of filling the context window with just the right information for the next step." — Andrej Karpathy. A frontier, first-principles handbook inspired by Karpathy and 3Blue1Brown for moving beyond prompt engineering to the wider discipline of context design, orchestration, and optimization.
相关推荐
AI Engineer 路线图中的 Few-Shot Prompting:示例驱动的提示词工程实战指南
AI Engineer 路线图中的 Few Shot Prompting:示例驱动的提示词工程实战指南 在 AI Engineer 路线图 https://li
文档教程知识库Context-Engineering 分子上下文实战指南:从 Few-Shot 示例组合到动态分子构造
Context Engineering 分子上下文实战指南:从 Few Shot 示例组合到动态分子构造 本指南以 Context Engineering 仓库
文档教程知识库人工智能提示工程Few-Shot 示例排序优化:用 Instructor 提升 LLM 结构化输出的示例选择与排序实战
Few Shot 示例排序优化:用 Instructor 提升 LLM 结构化输出的示例选择与排序实战 在 Few Shot 提示工程中,示例(exemplar
人工智能大模型AI 应用
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考