mcpField
P
prompt-engineering

prompt-engineering

github.com/revitalb-portAgent 工作流063 次安装10 周前更新
代码仓库

关于 prompt-engineering

Prompt Engineering 是用于编写和改进提示词、提升 LLM 输出可靠性的技能,涵盖提示词结构、示例、约束、代码任务模式,以及测试和维护方法。

主要功能

  1. 01运用原则编写明确提示词
  2. 02使用示例和结构化输出
  3. 03设置约束与防护规则
  4. 04采用代码任务提示模式
  5. 05规避常见提示词反模式
  6. 06测试并版本管理提示词

适用场景

  1. 01起草代码审查系统提示词
  2. 02要求生成机器可读的 JSON
  3. 03改进代码生成或修复提示词
  4. 04用边界情况测试提示词

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/revitalb-port/skills-registry-demo --skill prompt-engineering

安装后在 Claude Code 中输入/prompt-engineering确认已加载。

详细介绍

提示工程

编写能让大模型稳定生成高质量输出的提示词。

核心原则

  1. 明确具体 — 模糊的提示词会得到模糊的结果
  2. 展示胜于描述 — 示例胜过指令
  3. 规定输出结构 — 明确告诉模型你想要的格式
  4. 反复迭代 — 提示词就是代码;要测试并改进

技巧

系统提示词

设定模型的角色和约束:

You are a senior code reviewer. Review the provided code for:
1. Security vulnerabilities
2. Performance issues
3. Readability problems

For each issue found, provide:
- Severity (critical/warning/info)
- Line number
- Description
- Suggested fix

If no issues are found, respond with "No issues found."
少样本示例

提供 2-3 个输入 → 输出示例:

Convert the user's natural language query to a SQL query.

Example 1:
Input: "How many users signed up last month?"
Output: SELECT COUNT(*) FROM users WHERE created_at >= DATE_TRUNC('month', NOW() - INTERVAL '1 month') AND created_at < DATE_TRUNC('month', NOW());

Example 2:
Input: "Show me the top 5 products by revenue"
Output: SELECT p.name, SUM(o.amount) as revenue FROM products p JOIN orders o ON o.product_id = p.id GROUP BY p.name ORDER BY revenue DESC LIMIT 5;

Now convert this query:
Input: "{user_query}"
Output:
思维链

要求模型逐步推理:

Analyze this error and suggest a fix. Think step by step:
1. What does the error message mean?
2. What could cause this error?
3. What is the most likely root cause given the code context?
4. What is the fix?
结构化输出

要求输出 JSON 或指定格式:

Respond with a JSON object matching this schema:
{
  "summary": "string - one sentence summary",
  "sentiment": "positive | negative | neutral",
  "key_topics": ["string"],
  "confidence": 0.0-1.0
}
约束与护栏
Rules:
- Only use information from the provided context
- If you don't know the answer, say "I don't know" — do not guess
- Keep responses under 200 words
- Do not include any PII in your response

代码编写模式

代码生成:

Write a TypeScript function that {description}.

Requirements:
- {requirement 1}
- {requirement 2}

Use these libraries: {libraries}
Follow this pattern from the codebase: {example}

代码转换:

Refactor this code to {goal}. Keep the same behavior.
Do not change the public API (function signatures, exports).

修复 Bug:

This code has a bug: {description of bug}

Error: {error message}

Fix the bug. Explain what caused it in a comment.

反模式

  • 过于模糊:“让它更好” → 明确说明“更好”具体指什么
  • 过长:包含所有内容的巨型提示词 → 拆分成重点明确的提示词
  • 自相矛盾:“简洁但全面” → 二选一,或明确权衡方式
  • 没有示例:复杂格式却不展示预期效果 → 添加 1-2 个示例
  • 提示词注入风险:直接加入未经分隔的用户输入 → 使用 <user_input>...</user_input> 之类的清晰分隔符

提示

  • 对确定性任务(代码、分类)使用温度 0;对创意任务使用 0.7+
  • 使用边界情况测试提示词,而不只是测试理想情况
  • 对提示词进行版本控制——它们和代码同样重要
  • 以编程方式解析响应时,使用结构化输出(JSON)
  • 如果足够精确,较短的提示词往往比长提示词效果更好

评论

支持 Markdown