
关于
提示词工程专家指南,涵盖提示词模式、最佳实践和优化技巧。适用于改进提示词、学习提示策略或调试代理行为
name: prompt-engineering description: "提示工程模式、最佳实践和优化技术的专家指南。当用户想要改进提示词、学习提示策略或调试代理行为时使用。" risk: unknown source: community date_added: "2026-02-27"
提示工程模式
最大化LLM性能、可靠性和可控性的高级提示工程技术。
核心能力
1. 少样本学习
通过展示示例而非解释规则来教导模型。包含2-5个输入-输出对来演示期望行为。当你需要一致的格式、特定的推理模式或处理边缘情况时使用。更多示例提高准确性但消耗token——根据任务复杂度进行平衡。
示例:
Extract key information from support tickets:
Input: "My login doesn't work and I keep getting error 403"
Output: {"issue": "authentication", "error_code": "403", "priority": "high"}
Input: "Feature request: add dark mode to settings"
Output: {"issue": "feature_request", "error_code": null, "priority": "low"}
Now process: "Can't upload files larger than 10MB, getting timeout"
2. 思维链提示
在最终答案之前请求逐步推理。添加"Let's think step by step"(零样本)或包含示例推理轨迹(少样本)。用于需要多步逻辑、数学推理的复杂问题,或当你需要验证模型的思考过程时。在分析任务上提高30-50%的准确率。
示例:
Analyze this bug report and determine root cause.
Think step by step:
1. What is the expected behavior?
2. What is the actual behavior?
3. What changed recently that could cause this?
4. What components are involved?
5. What is the most likely root cause?
Bug: "Users can't save drafts after the cache update deployed yesterday"
3. 提示优化
通过测试和迭代系统性地改进提示词。从简单开始,衡量性能(准确性、一致性、token使用量),然后迭代。在包括边缘情况的多样化输入上测试。使用A/B测试比较变体。对于一致性和成本至关重要的生产提示词来说至关重要。
示例:
Version 1 (Simple): "Summarize this article"
→ Result: Inconsistent length, misses key points
Version 2 (Add constraints): "Summarize in 3 bullet points"
→ Result: Better structure, but still misses nuance
Version 3 (Add reasoning): "Identify the 3 main findings, then summarize each"
→ Result: Consistent, accurate, captures key information
4. 模板系统
构建带有变量、条件部分和模块化组件的可复用提示结构。用于多轮对话、基于角色的交互,或当相同模式适用于不同输入时。减少重复并确保类似任务的一致性。
示例:
# Reusable code review template
template = """
Review this {language} code for {focus_area}.
Code:
{code_block}
Provide feedback on:
{checklist}
"""
# Usage
prompt = template.format(
language="Python",
focus_area="security vulnerabilities",
code_block=user_code,
checklist="1. SQL injection\n2. XSS risks\n3. Authentication"
)
5. 系统提示设计
设置在整个对话中持续存在的全局行为和约束。定义模型的角色、专业水平、输出格式和安全准则。使用系统提示来存放不应逐轮变化的稳定指令,将用户消息token留给可变内容。
示例:
System: You are a senior backend engineer specializing in API design.
Rules:
- Always consider scalability and performance
- Suggest RESTful patterns by default
- Flag security concerns immediately
- Provide code examples in Python
- Use early return pattern
Format responses as:
1. Analysis
2. Recommendation
3. Code example
4. Trade-offs
关键模式
渐进式披露
从简单提示开始,仅在需要时增加复杂度:
-
第1级:直接指令
- "Summarize this article"
-
第2级:添加约束
- "Summarize this article in 3 bullet points, focusing on key findings"
-
第3级:添加推理
- "Read this article, identify the main findings, then summarize in 3 bullet points"
-
第4级:添加示例
- 包含2-3个带有输入-输出对的示例摘要
指令层次结构
[System Context] → [Task Instruction] → [Examples] → [Input Data] → [Output Format]
错误恢复
构建能优雅处理失败的提示词:
- 包含回退指令
- 请求置信度分数
- 不确定时请求替代解释
- 指定如何表示缺失信息
最佳实践
- 具体明确:模糊的提示产生不一致的结果
- 展示而非描述:示例比描述更有效
- 测试边缘情况:在多样化输入上验证提示词行为
- 迭代改进:从简单开始,根据结果逐步优化
- 衡量性能:跟踪准确性、一致性和token使用量
兼容工具
Claude CodeCursor
标签
AI与机器学习