prompt-engineering
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePrompt 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
核心组成部分
| Component | Purpose | Include When |
|---|---|---|
| Role/Context | Set expertise, persona | Complex domain tasks |
| Task | Clear instruction | Always |
| Format | Output structure | Need structured output |
| Examples | Few-shot learning | Pattern demonstration needed |
| Constraints | Boundaries, rules | Need to limit scope |
| 组成部分 | 作用 | 适用场景 |
|---|---|---|
| Role/Context(角色/上下文) | 设定专业能力、人物身份 | 复杂领域任务 |
| Task(任务) | 清晰的指令 | 所有场景 |
| Format(格式) | 输出结构要求 | 需要结构化输出时 |
| Examples(示例) | 少样本学习 | 需要演示模式时 |
| Constraints(约束条件) | 设定边界、规则 | 需要限制范围时 |
Prompt Patterns
提示词模式
| Pattern | Use Case | Key Concept |
|---|---|---|
| Chain of Thought | Complex reasoning | "Think step by step" |
| Few-Shot | Pattern learning | 2-5 input/output examples |
| Role Playing | Domain expertise | "You are an expert X" |
| Structured Output | Parsing needed | Specify JSON/format exactly |
| Self-Consistency | Improve accuracy | Generate multiple, vote |
| 模式 | 适用场景 | 核心概念 |
|---|---|---|
| Chain of Thought(思维链) | 复杂推理任务 | “逐步思考” |
| Few-Shot(少样本) | 模式学习任务 | 2-5个输入/输出示例 |
| Role Playing(角色扮演) | 领域专业任务 | “你是X领域专家” |
| Structured Output(结构化输出) | 需要解析结果的任务 | 明确指定JSON等格式 |
| Self-Consistency(自一致性) | 提升准确率 | 生成多个结果并投票 |
Chain of Thought Variants
思维链变体
| Variant | Description | When to Use |
|---|---|---|
| Standard CoT | "Think step by step" | Math, logic problems |
| Zero-Shot CoT | Just add "step by step" | Quick reasoning boost |
| Structured CoT | Numbered steps | Complex multi-step |
| Self-Ask | Ask sub-questions | Research-style tasks |
| Tree of Thought | Explore multiple paths | Creative/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
示例选择标准
| Criteria | Why |
|---|---|
| Representative | Cover common cases |
| Diverse | Show range of inputs |
| Edge cases | Handle boundaries |
| Consistent format | Teach output pattern |
| 标准 | 原因 |
|---|---|
| Representative(代表性) | 覆盖常见场景 |
| Diverse(多样性) | 展示输入范围 |
| Edge cases(边缘案例) | 处理边界场景 |
| Consistent format(格式一致) | 教授输出模式 |
Number of Examples
示例数量
| Count | Trade-off |
|---|---|
| 0 (zero-shot) | Less context, more creative |
| 2-3 | Good 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
分块策略
| Strategy | Best For | Trade-off |
|---|---|---|
| Fixed size | General documents | May split sentences |
| Sentence-based | Precise retrieval | Many small chunks |
| Paragraph-based | Context preservation | May be too large |
| Semantic | Mixed content | More complex |
| 策略 | 最佳适用场景 | 权衡点 |
|---|---|---|
| Fixed size(固定大小) | 通用文档 | 可能拆分完整句子 |
| Sentence-based(基于句子) | 精准检索 | 生成大量小块 |
| Paragraph-based(基于段落) | 保留上下文 | 块可能过大 |
| Semantic(语义分块) | 混合内容 | 实现更复杂 |
Retrieval Quality Factors
检索质量影响因素
| Factor | Impact |
|---|---|
| Chunk size | Too small = no context, too large = noise |
| Overlap | Prevents splitting important content |
| Metadata filtering | Narrows search space |
| Re-ranking | Improves relevance of top-k |
| Hybrid search | Combines 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模式
| Step | Description |
|---|---|
| Thought | Reason about what to do |
| Action | Call a tool |
| Observation | Process tool result |
| Repeat | Until task complete |
| 步骤 | 描述 |
|---|---|
| Thought(思考) | 推理下一步行动 |
| Action(行动) | 调用工具 |
| Observation(观察) | 处理工具返回结果 |
| Repeat(重复) | 直到任务完成 |
Tool Design Principles
工具设计原则
| Principle | Why |
|---|---|
| Single purpose | Clear when to use |
| Good descriptions | Model selects correctly |
| Structured inputs | Reliable parsing |
| Informative outputs | Model understands result |
| Error messages | Guide retry attempts |
| 原则 | 原因 |
|---|---|
| Single purpose(单一用途) | 明确适用场景 |
| Good descriptions(清晰描述) | 帮助模型正确选择 |
| Structured inputs(结构化输入) | 确保可靠解析 |
| Informative outputs(信息丰富的输出) | 帮助模型理解结果 |
| Error messages(错误提示) | 引导重试 |
Prompt Optimization
提示词优化
Token Efficiency
Token效率
| Technique | Savings |
|---|---|
| Remove redundant instructions | 10-30% |
| Use abbreviations in examples | 10-20% |
| Compress context with summaries | 50%+ |
| Remove verbose explanations | 20-40% |
| 技巧 | 节省比例 |
|---|---|
| 移除冗余指令 | 10-30% |
| 示例中使用缩写 | 10-20% |
| 用摘要压缩上下文 | 50%以上 |
| 移除冗长解释 | 20-40% |
Quality Improvement
质量提升
| Technique | Effect |
|---|---|
| Add specific examples | Reduces errors |
| Specify output format | Enables parsing |
| Include edge cases | Handles boundaries |
| Add confidence scoring | Calibrates uncertainty |
| 技巧 | 效果 |
|---|---|
| 添加具体示例 | 减少错误 |
| 指定输出格式 | 支持解析 |
| 包含边缘案例 | 处理边界场景 |
| 添加置信度评分 | 校准不确定性 |
Common Task Patterns
常见任务模式
| Task | Key Prompt Elements |
|---|---|
| Extraction | List fields, specify format (JSON), handle missing |
| Classification | List categories, one-shot per category, single answer |
| Summarization | Specify length, focus areas, format (bullets/prose) |
| Generation | Style guide, length, constraints, examples |
| Q&A | Context placement, "based only on context" |
| 任务 | 核心提示词要素 |
|---|---|
| Extraction(提取) | 列出字段、指定格式(JSON)、处理缺失值 |
| Classification(分类) | 列出类别、每个类别一个示例、单一答案 |
| Summarization(摘要) | 指定长度、重点领域、格式(项目符号/ prose) |
| Generation(生成) | 风格指南、长度、约束条件、示例 |
| Q&A(问答) | 上下文放置、“仅基于上下文回答” |
Best Practices
最佳实践
| Practice | Why |
|---|---|
| Be specific and explicit | Reduces ambiguity |
| Provide clear examples | Shows expected format |
| Specify output format | Enables parsing |
| Test with diverse inputs | Find edge cases |
| Iterate based on failures | Targeted improvement |
| Separate instructions from data | Prevent injection |
| 实践 | 原因 |
|---|---|
| 具体且明确 | 减少歧义 |
| 提供清晰示例 | 展示预期格式 |
| 指定输出格式 | 支持解析 |
| 用多样化输入测试 | 发现边缘案例 |
| 基于失败案例迭代 | 针对性改进 |
| 分离指令与数据 | 防止注入攻击 |
Resources
资源
- Anthropic Prompt Engineering: https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering
- OpenAI Cookbook: https://cookbook.openai.com/
- Anthropic提示词工程:https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering
- OpenAI Cookbook:https://cookbook.openai.com/