prompt-engineering

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Prompt Engineering

Prompt Engineering(提示词工程)

Write prompts that get reliable, high-quality output from LLMs.
编写能从LLM获取可靠、高质量输出的提示词。

Core Principles

核心原则

  1. Be specific — vague prompts get vague results
  2. Show, don't tell — examples beat instructions
  3. Structure the output — tell the model exactly what format you want
  4. Iterate — prompts are code; test and refine them
  1. 明确具体 — 模糊的提示词会得到模糊的结果
  2. 示例优先,而非指令 — 示例比单纯的说明更有效
  3. 指定输出结构 — 明确告知模型你需要的输出格式
  4. 持续迭代 — 提示词如同代码,需不断测试和优化

Techniques

常用技巧

System Prompts

系统提示词(System Prompts)

Set the model's role and constraints:
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."
设定模型的角色与约束:
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."

Few-Shot Examples

少样本示例(Few-Shot Examples)

Provide 2-3 examples of input → output:
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:
提供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:

Chain-of-Thought

思维链(Chain-of-Thought)

Ask the model to reason step by step:
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?
要求模型逐步推理:
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?

Structured Output

结构化输出(Structured Output)

Request JSON or a specific format:
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
}
要求返回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
}

Constraints and Guardrails

约束与防护规则

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
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

Patterns for Code

代码场景下的提示词模式

Code generation:
Write a TypeScript function that {description}.

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

Use these libraries: {libraries}
Follow this pattern from the codebase: {example}
Code transformation:
Refactor this code to {goal}. Keep the same behavior.
Do not change the public API (function signatures, exports).
Bug fixing:
This code has a bug: {description of bug}

Error: {error message}

Fix the bug. Explain what caused it in a comment.
代码生成:
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.

Anti-Patterns

反模式

  • Too vague: "Make this better" → Be specific about what "better" means
  • Too long: Giant prompts with everything → Split into focused prompts
  • Contradictory: "Be concise but thorough" → Pick one or define the tradeoff
  • No examples: Complex formatting without showing what you want → Add 1-2 examples
  • Prompt injection risk: Including raw user input without delimiting → Use clear delimiters like
    <user_input>...</user_input>
  • 过于模糊:"Make this better" → 需明确说明"优化"的具体含义
  • 过于冗长:包含所有内容的巨型提示词→拆分为聚焦的提示词
  • 相互矛盾:"Be concise but thorough" → 二选一或明确权衡方式
  • 缺少示例:要求复杂格式却不展示预期效果→添加1-2个示例
  • 提示词注入风险:直接包含原始用户输入而未分隔→使用
    <user_input>...</user_input>
    等明确分隔符

Tips

实用技巧

  • Temperature 0 for deterministic tasks (code, classification), 0.7+ for creative tasks
  • Test prompts with edge cases, not just the happy path
  • Version control your prompts — they're as important as code
  • Use structured output (JSON) when parsing the response programmatically
  • Shorter prompts often outperform longer ones if they're precise enough
  • 确定性任务(如代码生成、分类)设置Temperature为0,创意任务设置为0.7以上
  • 用边缘场景测试提示词,而非仅测试常规场景
  • 对提示词进行版本控制——它们和代码同等重要
  • 当通过程序解析响应时,使用结构化输出(如JSON)
  • 若足够精准,简短的提示词往往比冗长的效果更好