Skip to content

Chapter 8 · Basic Prompt Elements and Instruction Design ​

Chapter goals: Master the basic building blocks of prompts, learn to design clear and effective instructions, and understand the impact of instruction design on model output.

8.1 Five Elements of a Prompt ​

A complete prompt typically contains the following elements:

ElementPurposeExample
RoleSet the model's "persona""You are a professional Python engineer"
TaskClarify what needs to be done"Analyze the performance bottlenecks in this code"
ContextProvide background informationCode snippets, doc links, domain knowledge
ConstraintsLimit the output scope"Answer in Chinese", "No more than 200 words"
FormatSpecify output structureJSON, Markdown, list, etc.

8.2 Instruction Design Principles ​

1. Clear and Specific ​

Avoid vague expressions; use concrete, executable instructions:

python
# ❌ Bad example
"Write something for me"

# ✅ Good example
"Write a 500-word technical blog post introducing best practices for Python async programming, aimed at developers with some background"

2. Structured Organization ​

Use delimiters and structured formats:

python
prompt = """
# Role
You are a technical documentation expert.

# Task
Organize the following technical notes into standardized API documentation.

# Input
{notes}

# Requirements
1. Use Markdown format
2. Include a parameter description table
3. Provide at least one code example
4. Language: English

# Output Format
## API Name
## Description
## Parameters
## Example
"""

3. Positive Phrasing ​

Prefer positive instructions over negative ones:

python
# ❌ Avoid negative phrasing
"Don't use complex terminology"

# ✅ Use positive phrasing
"Use simple and easy-to-understand language"

8.3 Common Instruction Patterns ​

1. Role-Playing Pattern ​

python
role_prompt = """
You are a system architect with 10 years of experience.
Please analyze the pros and cons of the following design from an architectural perspective:
{design_doc}
"""

2. Step-by-Step Reasoning Pattern ​

python
step_by_step = """
Please solve this problem step by step:
1. First understand the core of the problem
2. List known conditions and constraints
3. Derive the solution
4. Verify the correctness of the result

Problem: {problem}
"""

3. Comparative Analysis Pattern ​

python
compare_prompt = """
Please compare the following two approaches:
- Approach A: {option_a}
- Approach B: {option_b}

Analyze from cost, performance, and maintainability perspectives, and give a recommendation.
"""

8.4 Tips for Debugging Prompts ​

When the model output is unsatisfactory, try the following:

  1. Add more context: provide more detailed background information
  2. Adjust constraints: tighten or loosen restrictions
  3. Change the phrasing: rewording may produce better results
  4. Use examples: few-shot examples effectively guide the model
  5. Decompose the task: break complex tasks into sub-steps

Chapter Summary ​

  • Prompts contain five elements: role, task, context, constraints, format
  • Instruction design should be clear, structured, and use positive phrasing
  • Master common patterns: role-playing, step-by-step reasoning, comparative analysis
  • When debugging, try from five angles: context, constraints, phrasing, examples, decomposition

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. Which of the following is NOT one of the five elements of a prompt?

2. Which instruction design is the clearest?

3. When debugging a prompt, which method is most effective?

4. What is the main purpose of using delimiters?

🛠️ Hands-on Practice ​

  1. Design a complete prompt for a "code review assistant" containing all five elements, and test the effect difference across different phrasings.
  2. Prompt the same task using both "direct question" and "step-by-step reasoning" approaches, and compare output quality.
  3. Implement a prompt debugging tool: record outputs across different parameter combinations and automatically generate improvement suggestions.

Next chapter: Zero-shot and Few-shot