关于 prompt-engineering-patterns
面向构建可靠、结构化 LLM 交互的开发者,帮助设计和优化提示词。内容涵盖少样本示例选择、推理模式、基于 schema 的输出、可复用模板、测试与优化。
主要功能
- 01少样本示例选择
- 02思维链与自我验证
- 03使用 Pydantic 生成结构化输出
- 04提示词迭代与 A/B 测试
- 05带变量的可复用模板
- 06系统提示词设计
适用场景
- 01为生产环境 LLM 应用设计提示词
- 02生成经过 schema 验证的 JSON 输出
- 03排查提示词回复不一致的问题
- 04创建可复用的多轮提示词模板
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ jsonlee12138/ agent-team --skill prompt-engineering-patterns安装后在 Claude Code 中输入/prompt-engineering-patterns确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/few-shot-examples.jsonprompt-template-library.mdreferences/chain-of-thought.mdfew-shot-learning.mdprompt-optimization.mdprompt-templates.mdsystem-prompts.mdscripts/optimize-prompt.py详细介绍
提示词工程模式
掌握高级提示词工程技术,最大限度地提升大模型的性能、可靠性和可控性。
何时使用此技能
- 为生产环境中的大模型应用设计复杂提示词
- 优化提示词的性能和一致性
- 实现结构化推理模式(思维链、思维树)
- 构建支持动态示例选择的少样本学习系统
- 创建带变量插值的可复用提示词模板
- 调试和优化输出不一致的提示词
- 为专业 AI 助手实现系统提示词
- 使用结构化输出(JSON 模式)以便可靠解析
核心能力
1. 少样本学习
- 示例选择策略(语义相似度、多样性采样)
- 在示例数量与上下文窗口限制之间取得平衡
- 使用输入-输出对构建有效示范
- 从知识库中动态检索示例
- 通过策略性选择示例处理边缘情况
2. 思维链提示
- 引导模型逐步推理
- 使用“Let's think step by step”进行零样本 CoT
- 使用推理轨迹进行少样本 CoT
- 自洽性技术(对多条推理路径进行采样)
- 验证与校验步骤
3. 结构化输出
- 使用 JSON 模式实现可靠解析
- 使用 Pydantic 强制执行模式
- 类型安全的响应处理
- 对格式错误的输出进行错误处理
4. 提示词优化
- 迭代优化工作流
- 对提示词变体进行 A/B 测试
- 衡量提示词性能指标(准确率、一致性、延迟)
- 在保持质量的同时减少 token 用量
- 处理边缘情况和故障模式
5. 模板系统
- 变量插值与格式化
- 条件提示词段落
- 多轮对话模板
- 基于角色的提示词组合
- 模块化提示词组件
6. 系统提示词设计
- 设定模型行为和约束
- 定义输出格式和结构
- 明确角色和专业知识
- 安全指南和内容政策
- 设置上下文和背景信息
快速开始
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, Field
# Define structured output schema
class SQLQuery(BaseModel):
query: str = Field(description="The SQL query")
explanation: str = Field(description="Brief explanation of what the query does")
tables_used: list[str] = Field(description="List of tables referenced")
# Initialize model with structured output
llm = ChatAnthropic(model="claude-sonnet-4-6")
structured_llm = llm.with_structured_output(SQLQuery)
# Create prompt template
prompt = ChatPromptTemplate.from_messages([
("system", """You are an expert SQL developer. Generate efficient, secure SQL queries.
Always use parameterized queries to prevent SQL injection.
Explain your reasoning briefly."""),
("user", "Convert this to SQL: {query}")
])
# Create chain
chain = prompt | structured_llm
# Use
result = await chain.ainvoke({
"query": "Find all users who registered in the last 30 days"
})
print(result.query)
print(result.explanation)
核心模式
模式 1:使用 Pydantic 实现结构化输出
from anthropic import Anthropic
from pydantic import BaseModel, Field
from typing import Literal
import json
class SentimentAnalysis(BaseModel):
sentiment: Literal["positive", "negative", "neutral"]
confidence: float = Field(ge=0, le=1)
key_phrases: list[str]
reasoning: str
async def analyze_sentiment(text: str) -> SentimentAnalysis:
"""Analyze sentiment with structured output."""
client = Anthropic()
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=500,
messages=[{
"role": "user",
"content": f"""Analyze the sentiment of this text.
Text: {text}
Respond with JSON matching this schema:
{{
"sentiment": "positive" | "negative" | "neutral",
"confidence": 0.0-1.0,
"key_phrases": ["phrase1", "phrase2"],
"reasoning": "brief explanation"
}}"""
}]
)
return SentimentAnalysis(**json.loads(message.content[0].text))
模式 2:带自我验证的思维链
from langchain_core.prompts import ChatPromptTemplate
cot_prompt = ChatPromptTemplate.from_template("""
Solve this problem step by step.
Problem: {problem}
Instructions:
1. Break down the problem into clear steps
2. Work through each step showing your reasoning
3. State your final answer
4. Verify your answer by checking it against the original problem
Format your response as:
## Steps
[Your step-by-step reasoning]
## Answer
[Your final answer]
## Verification
[Check that your answer is correct]
""")
模式 3:动态选择示例的少样本提示
from langchain_voyageai import VoyageAIEmbeddings
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_chroma import Chroma
# Create example selector with semantic similarity
example_selector = SemanticSimilarityExampleSelector.from_examples(
examples=[
{"input": "How do I reset my password?", "output": "Go to Settings > Security > Reset Password"},
{"input": "Where can I see my order history?", "output": "Navigate to Account > Orders"},
{"input": "How do I contact support?", "output": "Click Help > Contact Us or email [email protected]"},
],
embeddings=VoyageAIEmbeddings(model="voyage-3-large"),
vectorstore_cls=Chroma,
k=2 # Select 2 most similar examples
)
async def get_few_shot_prompt(query: str) -> str:
"""Build prompt with dynamically selected examples."""
examples = await example_selector.aselect_examples({"input": query})
examples_text = "\n".join(
f"User: {ex['input']}\nAssistant: {ex['output']}"
for ex in examples
)
return f"""You are a helpful customer support assistant.
Here are some example interactions:
{examples_text}
Now respond to this query:
User: {query}
Assistant:"""
模式 4:渐进式披露
从简单提示词开始,仅在需要时增加复杂性:
PROMPT_LEVELS = {
# Level 1: Direct instruction
"simple": "Summarize this article: {text}",
# Level 2: Add constraints
"constrained": """Summarize this article in 3 bullet points, focusing on:
- Key findings
- Main conclusions
- Practical implications
Article: {text}""",
# Level 3: Add reasoning
"reasoning": """Read this article carefully.
1. First, identify the main topic and thesis
2. Then, extract the key supporting points
3. Finally, summarize in 3 bullet points
Article: {text}
Summary:""",
# Level 4: Add examples
"few_shot": """Read articles and provide concise summaries.
Example:
Article: "New research shows that regular exercise can reduce anxiety by up to 40%..."
Summary:
• Regular exercise reduces anxiety by up to 40%
• 30 minutes of moderate activity 3x/week is sufficient
• Benefits appear within 2 weeks of starting
Now summarize this article:
Article: {text}
Summary:"""
}
模式 5:错误恢复与回退
from pydantic import BaseModel, ValidationError
import json
class ResponseWithConfidence(BaseModel):
answer: str
confidence: float
sources: list[str]
alternative_interpretations: list[str] = []
ERROR_RECOVERY_PROMPT = """
Answer the question based on the context provided.
Context: {context}
Question: {question}
Instructions:
1. If you can answer confidently (>0.8), provide a direct answer
2. If you're somewhat confident (0.5-0.8), provide your best answer with caveats
3. If you're uncertain (<0.5), explain what information is missing
4. Always provide alternative interpretations if the question is ambiguous
Respond in JSON:
{{
"answer": "your answer or 'I cannot determine this from the context'",
"confidence": 0.0-1.0,
"sources": ["relevant context excerpts"],
"alternative_interpretations": ["if question is ambiguous"]
}}
"""
async def answer_with_fallback(
context: str,
question: str,
llm
) -> ResponseWithConfidence:
"""Answer with error recovery and fallback."""
prompt = ERROR_RECOVERY_PROMPT.format(context=context, question=question)
try:
response = await llm.ainvoke(prompt)
return ResponseWithConfidence(**json.loads(response.content))
except (json.JSONDecodeError, ValidationError) as e:
# Fallback: try to extract answer without structure
simple_prompt = f"Based on: {context}\n\nAnswer: {question}"
simple_response = await llm.ainvoke(simple_prompt)
return ResponseWithConfidence(
answer=simple_response.content,
confidence=0.5,
sources=["fallback extraction"],
alternative_interpretations=[]
)
模式 6:基于角色的系统提示词
SYSTEM_PROMPTS = {
"analyst": """You are a senior data analyst with expertise in SQL, Python, and business intelligence.
Your responsibilities:
- Write efficient, well-documented queries
- Explain your analysis methodology
- Highlight key insights and recommendations
- Flag any data quality concerns
Communication style:
- Be precise and technical when discussing methodology
- Translate technical findings into business impact
- Use clear visualizations when helpful""",
"assistant": """You are a helpful AI assistant focused on accuracy and clarity.
Core principles:
- Always cite sources when making factual claims
- Acknowledge uncertainty rather than guessing
- Ask clarifying questions when the request is ambiguous
- Provide step-by-step explanations for complex topics
Constraints:
- Do not provide medical, legal, or financial advice
- Redirect harmful requests appropriately
- Protect user privacy""",
"code_reviewer": """You are a senior software engineer conducting code reviews.
Review criteria:
- Correctness: Does the code work as intended?
- Security: Are there any vulnerabilities?
- Performance: Are there efficiency concerns?
- Maintainability: Is the code readable and well-structured?
- Best practices: Does it follow language idioms?
Output format:
1. Summary assessment (approve/request changes)
2. Critical issues (must fix)
3. Suggestions (nice to have)
4. Positive feedback (what's done well)"""
}
集成模式
与 RAG 系统集成
RAG_PROMPT = """You are a knowledgeable assistant that answers questions based on provided context.
Context (retrieved from knowledge base):
{context}
Instructions:
1. Answer ONLY based on the provided context
2. If the context doesn't contain the answer, say "I don't have information about that in my knowledge base"
3. Cite specific passages using [1], [2] notation
4. If the question is ambiguous, ask for clarification
Question: {question}
Answer:"""
与验证和校验机制集成
VALIDATED_PROMPT = """Complete the following task:
Task: {task}
After generating your response, verify it meets ALL these criteria:
✓ Directly addresses the original request
✓ Contains no factual errors
✓ Is appropriately detailed (not too brief, not too verbose)
✓ Uses proper formatting
✓ Is safe and appropriate
If verification fails on any criterion, revise before responding.
Response:"""
性能优化
Token 效率
# Before: Verbose prompt (150+ tokens)
verbose_prompt = """
I would like you to please take the following text and provide me with a comprehensive
summary of the main points. The summary should capture the key ideas and important details
while being concise and easy to understand.
"""
# After: Concise prompt (30 tokens)
concise_prompt = """Summarize the key points concisely:
{text}
Summary:"""
缓存常用前缀
from anthropic import Anthropic
client = Anthropic()
# Use prompt caching for repeated system prompts
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1000,
system=[
{
"type": "text",
"text": LONG_SYSTEM_PROMPT,
"cache_control": {"type": "ephemeral"}
}
],
messages=[{"role": "user", "content": user_query}]
)
最佳实践
- 明确具体:含糊的提示词会导致结果不一致
- 展示而非讲述:示例比描述更有效
- 使用结构化输出:通过 Pydantic 强制执行模式,确保可靠性
- 充分测试:使用多样且具有代表性的输入进行评估
- 快速迭代:微小改动也可能产生重大影响
- 监控性能:在生产环境中跟踪指标
- 版本控制:将提示词视为代码并进行适当的版本管理
- 记录设计意图:说明提示词为何采用当前结构
常见陷阱
- 过度设计:尚未尝试简单提示词,就从复杂提示词入手
- 示例污染:使用与目标任务不匹配的示例
- 上下文溢出:示例过多,超出 token 限制
- 指令含糊:留下多种解释空间
- 忽略边缘情况:未使用异常或边界输入进行测试
- 缺少错误处理:假设输出始终格式正确
- 硬编码值:未将提示词参数化以便复用
成功指标
跟踪提示词的以下 KPI:
- 准确性:输出的正确性
- 一致性:相似输入下的可复现性
- 延迟:响应时间(P50、P95、P99)
- Token 使用量:每个请求的平均 Token 数
- 成功率:有效且可解析的输出所占百分比
- 用户满意度:评分和反馈
评论
支持 Markdown