关于 prompt-engineering
面向设计 LLM 指令与工作流的提示工程参考技能,涵盖结构化提示词、示例、工具描述、模板和测试实践。
主要功能
- 01结构化系统提示词
- 02思维链提示
- 03少样本示例
- 04工具使用描述
- 05参数化提示词模板
- 06提示词测试清单
适用场景
- 01构建代码审查提示词
- 02将自然语言转换为 SQL
- 03定义结构化模型输出
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ rohitg00/ awesome-claude-code-toolkit --skill prompt-engineering安装后在 Claude Code 中输入/prompt-engineering确认已加载。
详细介绍
提示工程
结构化系统提示词
You are a senior code reviewer. Your role is to analyze pull requests for:
1. Correctness - logic errors, edge cases, off-by-one errors
2. Security - injection, authentication, data exposure
3. Performance - N+1 queries, unnecessary allocations, missing indexes
4. Maintainability - naming, complexity, test coverage
For each issue found, respond with:
- Severity: critical | warning | suggestion
- File and line reference
- What is wrong
- How to fix it (with code snippet)
If the code is well-written, say so briefly. Do not invent problems.
按照角色、范围、输出格式和限制来组织系统提示词。明确说明模型不应该做什么。
思维链
Analyze this database query for performance issues.
Think step by step:
1. Identify the tables and joins involved
2. Check if appropriate indexes exist for the WHERE and JOIN conditions
3. Look for full table scans or cartesian products
4. Estimate the row count at each step
5. Suggest specific index creation or query restructuring
Query:
SELECT o.*, u.name, p.title
FROM orders o
JOIN users u ON o.user_id = u.id
JOIN products p ON o.product_id = p.id
WHERE o.created_at > '2024-01-01'
AND u.country = 'US'
ORDER BY o.created_at DESC
LIMIT 50;
思维链提示通过要求模型展示中间步骤,提高模型在推理任务上的准确性。
少样本示例
Convert natural language to SQL. Follow these examples:
Input: "How many orders were placed last month?"
Output: SELECT COUNT(*) FROM orders WHERE created_at >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 month') AND created_at < DATE_TRUNC('month', CURRENT_DATE);
Input: "Top 5 customers by total spending"
Output: SELECT customer_id, SUM(total_amount) AS total_spent FROM orders GROUP BY customer_id ORDER BY total_spent DESC LIMIT 5;
Input: "Products that have never been ordered"
Output: SELECT p.* FROM products p LEFT JOIN order_items oi ON p.id = oi.product_id WHERE oi.id IS NULL;
Now convert:
Input: "Average order value per country for the last quarter"
提供 3-5 个不同的示例,展示预期格式和边缘情况。
工具使用/函数调用
{
"tools": [
{
"name": "search_codebase",
"description": "Search for code patterns across the repository. Use when you need to find implementations, usages, or definitions.",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Regex pattern or keyword to search for"
},
"file_type": {
"type": "string",
"description": "File extension filter (e.g., 'ts', 'py')"
}
},
"required": ["query"]
}
}
]
}
编写工具说明时,应解释何时使用工具,而不只是说明工具的作用。
提示词模板模式
def build_review_prompt(diff: str, context: str, rules: list[str]) -> str:
rules_text = "\n".join(f"- {rule}" for rule in rules)
return f"""Review this code diff against the following rules:
{rules_text}
Context about the codebase:
{context}
Diff to review:
{diff}
Respond with a JSON array of findings. If no issues, return an empty array.
Each finding: {{"severity": "critical|warning|info", "line": number, "message": "string", "suggestion": "string"}}"""
反模式
- 使用“乐于助人”或“尽力而为”等含糊指令
- 需要确定性输出时,却要求模型“发挥创意”
- 未指定输出格式(JSON、Markdown、纯文本)
- 在单个提示词中塞入过多无关任务
- 使用否定表达(“不要做 X”),却不说明应该怎么做
- 未使用对抗性或边缘情况输入测试提示词
检查清单
- 系统提示词定义了角色、范围、格式和限制
- 多步推理任务使用了思维链
- 少样本示例涵盖了典型情况和边缘情况
- 明确指定了输出格式(JSON Schema、Markdown 等)
- 工具说明解释了何时以及为何使用每个工具
- 使用对抗性输入测试了提示词
- 根据任务恰当设置了 temperature 和 top_p
- 提示词模板采用参数化方式,而非硬编码字符串
评论
支持 Markdown