prompt-engineering

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Prompt Engineering Guide

提示词工程指南

Effective prompts, RAG systems, and agent workflows.
高效的提示词、RAG系统与Agent工作流。

When to Use

适用场景

  • Optimizing LLM prompts
  • Building RAG systems
  • Designing agent workflows
  • Creating few-shot examples
  • Structuring chain-of-thought reasoning

  • 优化LLM提示词
  • 构建RAG系统
  • 设计Agent工作流
  • 创建少样本示例
  • 构建思维链推理结构

Prompt Structure

提示词结构

Core Components

核心组成部分

ComponentPurposeInclude When
Role/ContextSet expertise, personaComplex domain tasks
TaskClear instructionAlways
FormatOutput structureNeed structured output
ExamplesFew-shot learningPattern demonstration needed
ConstraintsBoundaries, rulesNeed to limit scope
组成部分作用适用场景
Role/Context(角色/上下文)设定专业能力、人物身份复杂领域任务
Task(任务)清晰的指令所有场景
Format(格式)输出结构要求需要结构化输出时
Examples(示例)少样本学习需要演示模式时
Constraints(约束条件)设定边界、规则需要限制范围时

Prompt Patterns

提示词模式

PatternUse CaseKey Concept
Chain of ThoughtComplex reasoning"Think step by step"
Few-ShotPattern learning2-5 input/output examples
Role PlayingDomain expertise"You are an expert X"
Structured OutputParsing neededSpecify JSON/format exactly
Self-ConsistencyImprove accuracyGenerate multiple, vote

模式适用场景核心概念
Chain of Thought(思维链)复杂推理任务“逐步思考”
Few-Shot(少样本)模式学习任务2-5个输入/输出示例
Role Playing(角色扮演)领域专业任务“你是X领域专家”
Structured Output(结构化输出)需要解析结果的任务明确指定JSON等格式
Self-Consistency(自一致性)提升准确率生成多个结果并投票

Chain of Thought Variants

思维链变体

VariantDescriptionWhen to Use
Standard CoT"Think step by step"Math, logic problems
Zero-Shot CoTJust add "step by step"Quick reasoning boost
Structured CoTNumbered stepsComplex multi-step
Self-AskAsk sub-questionsResearch-style tasks
Tree of ThoughtExplore multiple pathsCreative/open problems
Key concept: CoT works because it forces the model to show intermediate reasoning, reducing errors in the final answer.

变体描述适用场景
Standard CoT(标准思维链)“逐步思考”数学、逻辑问题
Zero-Shot CoT(零样本思维链)仅添加“逐步思考”快速提升推理能力
Structured CoT(结构化思维链)分步骤编号复杂多阶段任务
Self-Ask(自我提问)提出子问题研究类任务
Tree of Thought(思维树)探索多种路径创意/开放性问题
核心概念:思维链之所以有效,是因为它迫使模型展示中间推理过程,减少最终答案的错误。

Few-Shot Learning

少样本学习

Example Selection

示例选择标准

CriteriaWhy
RepresentativeCover common cases
DiverseShow range of inputs
Edge casesHandle boundaries
Consistent formatTeach output pattern
标准原因
Representative(代表性)覆盖常见场景
Diverse(多样性)展示输入范围
Edge cases(边缘案例)处理边界场景
Consistent format(格式一致)教授输出模式

Number of Examples

示例数量

CountTrade-off
0 (zero-shot)Less context, more creative
2-3Good balance for most tasks
5+Complex patterns, use tokens
Key concept: Examples teach format more than content. The model learns "how" to respond, not "what" facts to include.

数量权衡点
0(零样本)上下文少,创造性更强
2-3个大多数任务的最佳平衡
5个以上复杂模式,消耗更多tokens
核心概念:示例更多是教授格式而非内容。模型学习的是“如何”响应,而非“要包含哪些”事实。

RAG System Design

RAG系统设计

Architecture Flow

架构流程

Query → Embed → Search → Retrieve → Augment Prompt → Generate
查询 → 嵌入 → 搜索 → 检索 → 增强提示词 → 生成

Chunking Strategies

分块策略

StrategyBest ForTrade-off
Fixed sizeGeneral documentsMay split sentences
Sentence-basedPrecise retrievalMany small chunks
Paragraph-basedContext preservationMay be too large
SemanticMixed contentMore complex
策略最佳适用场景权衡点
Fixed size(固定大小)通用文档可能拆分完整句子
Sentence-based(基于句子)精准检索生成大量小块
Paragraph-based(基于段落)保留上下文块可能过大
Semantic(语义分块)混合内容实现更复杂

Retrieval Quality Factors

检索质量影响因素

FactorImpact
Chunk sizeToo small = no context, too large = noise
OverlapPrevents splitting important content
Metadata filteringNarrows search space
Re-rankingImproves relevance of top-k
Hybrid searchCombines keyword + semantic
Key concept: RAG quality depends more on retrieval quality than generation quality. Fix retrieval first.

因素影响
Chunk size(块大小)太小=无上下文,太大=冗余信息
Overlap(重叠)避免拆分重要内容
Metadata filtering(元数据过滤)缩小搜索范围
Re-ranking(重排序)提升Top-K结果相关性
Hybrid search(混合搜索)结合关键词+语义搜索
核心概念:RAG的质量更多取决于检索质量而非生成质量。优先优化检索环节。

Agent Patterns

Agent模式

ReAct Pattern

ReAct模式

StepDescription
ThoughtReason about what to do
ActionCall a tool
ObservationProcess tool result
RepeatUntil task complete
步骤描述
Thought(思考)推理下一步行动
Action(行动)调用工具
Observation(观察)处理工具返回结果
Repeat(重复)直到任务完成

Tool Design Principles

工具设计原则

PrincipleWhy
Single purposeClear when to use
Good descriptionsModel selects correctly
Structured inputsReliable parsing
Informative outputsModel understands result
Error messagesGuide retry attempts

原则原因
Single purpose(单一用途)明确适用场景
Good descriptions(清晰描述)帮助模型正确选择
Structured inputs(结构化输入)确保可靠解析
Informative outputs(信息丰富的输出)帮助模型理解结果
Error messages(错误提示)引导重试

Prompt Optimization

提示词优化

Token Efficiency

Token效率

TechniqueSavings
Remove redundant instructions10-30%
Use abbreviations in examples10-20%
Compress context with summaries50%+
Remove verbose explanations20-40%
技巧节省比例
移除冗余指令10-30%
示例中使用缩写10-20%
用摘要压缩上下文50%以上
移除冗长解释20-40%

Quality Improvement

质量提升

TechniqueEffect
Add specific examplesReduces errors
Specify output formatEnables parsing
Include edge casesHandles boundaries
Add confidence scoringCalibrates uncertainty

技巧效果
添加具体示例减少错误
指定输出格式支持解析
包含边缘案例处理边界场景
添加置信度评分校准不确定性

Common Task Patterns

常见任务模式

TaskKey Prompt Elements
ExtractionList fields, specify format (JSON), handle missing
ClassificationList categories, one-shot per category, single answer
SummarizationSpecify length, focus areas, format (bullets/prose)
GenerationStyle guide, length, constraints, examples
Q&AContext placement, "based only on context"

任务核心提示词要素
Extraction(提取)列出字段、指定格式(JSON)、处理缺失值
Classification(分类)列出类别、每个类别一个示例、单一答案
Summarization(摘要)指定长度、重点领域、格式(项目符号/ prose)
Generation(生成)风格指南、长度、约束条件、示例
Q&A(问答)上下文放置、“仅基于上下文回答”

Best Practices

最佳实践

PracticeWhy
Be specific and explicitReduces ambiguity
Provide clear examplesShows expected format
Specify output formatEnables parsing
Test with diverse inputsFind edge cases
Iterate based on failuresTargeted improvement
Separate instructions from dataPrevent injection
实践原因
具体且明确减少歧义
提供清晰示例展示预期格式
指定输出格式支持解析
用多样化输入测试发现边缘案例
基于失败案例迭代针对性改进
分离指令与数据防止注入攻击

Resources

资源