prompt-engineering
Master the art and science of crafting effective prompts for large language models. Covers foundational patterns, advanced techniques like chain-of-thought and role prompting, structured output formats, and practical strategies for iterative refinement.
cosmicstack-labs · 已有 AI 审核记录
安装与使用
运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。
npx skills add cosmicstack-labs/mercury-agent-skills --skill prompt-engineering技能说明
name: prompt-engineering description: Master the art and science of crafting effective prompts for large language models. Covers foundational patterns, advanced techniques like chain-of-thought and role prompting, structured output formats, and practical strategies for iterative refinement. metadata: author: cosmicstack-labs version: 1.0.0 category: ai-ml tags: - prompt-engineering - llm - chain-of-thought - few-shot - structured-outputs - ai-patterns
Prompt Engineering
Core Principles
1. Clarity Over Cleverness
A clear, direct prompt always outperforms a clever but ambiguous one. State exactly what you want, in what format, and with what constraints. Ambiguity is the enemy of consistent output.
2. Context is Everything
Models have no inherent context beyond their training data. Every prompt must establish:
- Who the model should be (role)
- What the task is (instruction)
- How to respond (format, tone, length)
- Why the task matters (optional but helpful for complex tasks)
3. Iterate, Don't Expect Perfection First Time
The first prompt is rarely the best. Prompt engineering is an iterative discipline. Each refinement teaches you something about how the model interprets your instructions.
4. Constrain to Liberate
Paradoxically, more constraints (format, length constraints, guardrails) lead to better outputs. Open-ended prompts invite hallucination and inconsistency.
5. Test Systematically
Change one variable at a time. Track what works. Build a personal library of prompt patterns that reliably produce good results.
Prompt Engineering Scorecard
| Level | Characteristics | Typical Output Quality | Refinement Approach |
|---|---|---|---|
| Beginner | Single-sentence prompts, no role definition, no format specification | Inconsistent, often misses the mark, requires manual editing | Trial and error, adds more words hoping for improvement |
| Proficient | Clear instructions, role assignment, basic format constraints, some examples | Mostly correct, occasionally deviates, needs minor edits | Systematic A/B testing, adjusts temperature, adds few-shot examples |
| Expert | Multi-layered instructions, chain-of-thought reasoning, structured output schemas, temperature calibration, guardrails | Highly consistent, follows complex constraints, minimal editing needed | Uses prompt chains, dynamic few-shot selection, automated evaluation, version-controlled prompts |
Self-Assessment Questions
- Beginner: Do you write prompts like "Write a poem about AI"? If so, you're here.
- Proficient: Do you write prompts like "You are a poet. Write a 14-line sonnet about artificial intelligence, using iambic pentameter. Include themes of learning and evolution."? Welcome to proficient.
- Expert: Do you design multi-step prompts with chain-of-thought scaffolding, structured output schemas, dynamic example selection, and automated validation? You're an expert.
Chain-of-Thought (CoT) Prompting
What It Is
Chain-of-thought prompting instructs the model to reason step-by-step before arriving at an answer. This dramatically improves performance on arithmetic, logic, and multi-step reasoning tasks.
Why It Works
LLMs are autoregressive — they predict the next token based on previous tokens. By generating intermediate reasoning steps, the model builds a logical scaffold that leads to more accurate conclusions.
Zero-Shot CoT
Simply append "Let's think step by step." to your prompt.
Prompt: A bat and a ball cost $1.10 in total. The bat costs $1.00 more than the ball. How much does the ball cost? Let's think step by step.
Response: Let's denote the ball's cost as x. Then the bat costs x + $1.00. Together: x + (x + 1.00) = 1.10. So 2x = 0.10, x = 0.05. The ball costs $0.05.
Few-Shot CoT
Provide 2-3 examples of reasoning chains before asking your question.
Prompt: Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 balls. How many does he have now?
A: Roger starts with 5 balls. 2 cans of 3 each = 6 balls. 5 + 6 = 11. The answer is 11.
Q: The cafeteria had 23 apples. They used 20 to make lunch and bought 6 more. How many apples do they have?
A: They had 23. Used 20 → 23 - 20 = 3 left. Bought 6 → 3 + 6 = 9. The answer is 9.
Q: {your question}
A:
When to Use Chain-of-Thought
| Task Type | CoT Recommended? | Notes |
|---|---|---|
| Arithmetic/Math | ✅ Yes | Essential for multi-step |
| Logic Puzzles | ✅ Yes | Dramatically improves accuracy |
| Code Generation | ⚠️ Sometimes | Useful for complex algorithms |
| Creative Writing | ❌ No | Can feel mechanical |
| Factual Recall | ❌ No | Adds unnecessary verbosity |
Few-Shot vs Zero-Shot
Zero-Shot Prompting
The model receives only the instruction with no examples.
Best for: Simple, well-understood tasks; creative work; when examples might bias the output.
Translate to French: "Hello, how are you?"
Few-Shot Prompting
The model receives 2-5 examples demonstrating the desired pattern before the actual query.
Best for: Complex formatting; tasks with edge cases; domain-specific terminology; when you need consistent output structure.
English: "I love programming"
French: "J'adore programmer"
English: "The weather is nice today"
French: "Il fait beau aujourd'hui"
English: "Can you help me with this?"
French:
Guidelines for Few-Shot Selection
- Quality over quantity: 3 excellent examples beat 10 mediocre ones
- Cover edge cases: Include examples that show how to handle tricky inputs
- Mirror your target: Examples should match the complexity and style of your actual use case
- Randomize order: If examples are in predictable order, the model may learn a pattern you don't want
When to Choose Which
| Scenario | Recommend | Rationale | |---
阅读完整来源 ↗