404 Not Found

404 Not Found


nginx

Prompt Engineering

"Prompt engineering" may sound like some kind of mystical art—after all, the same AI can produce outputs of vastly different quality depending on how you phrase your prompt. But the core of prompt engineering isn’t mysterious at all: it’s a set of design methods that help AI understand your intent and generate content in the format you expect. Starting with design principles, this chapter will guide you through zero-shot, few-shot, and thought-chain prompting strategies, help you understand system prompts and character settings, and show you how to build reusable, structured prompt templates using Python.

1. What You'll Learn


2. Story: Just a Prompt Away

(1) Pain Point: AI's "Nonsense Literature"

Bob needs a market analysis report, so he opens ChatGPT and types:

"Write a market analysis report"

As a result, the AI generated a bland, generic response: "The market continues to grow... Opportunities and challenges coexist... We recommend closely monitoring market trends."—Correct, but completely devoid of useful information.

(2) Alice's Prompt Modification

Alice picked up the keyboard and typed it again:

"You are a senior market analyst with 15 years of experience in the electric vehicle industry. Please analyze the global electric vehicle market in 2025, including: 3 key trends, 2 major risks, and 1 investment recommendation. Present the trends section using a Markdown table."

This time, the AI generated a well-organized, data-rich analytical report—using the same model and the same capabilities; the only difference was the prompt.

(3) Benefits: A prompt is like an "instruction manual"

Bob exclaimed, "So it wasn't that the AI couldn't do it—it was that I didn't explain it clearly!" Alice nodded. "The essence of prompt engineering is to give the AI a precise 'set of instructions.'"


3. Principles of Prompt Design

(1) Three Core Principles

Principle Meaning Counterexample
Clear The task objective is unambiguous; the AI does not need to guess "Analyze the market"
Specific Provide sufficient details, constraints, and context "Write a report"
Structured Organize the prompt using numbering, bullet points, and templates Long blocks of free-form text

A good prompt transforms AI's work from "free rein" to "following a roadmap."

(2) Comparison Chart: Good Prompts vs. Bad Prompts

Dimension Bad Prompt Good Prompt
Goal "Help me write something" "Write a 200-word business email inviting a client to a product launch event"
Role None "You are a senior PR manager"
Format None "Output in Markdown format, including heading, body text, and signature"
Constraints None "No more than 200 words; formal but not stiff"
Example None Includes 1–2 snippets of the expected output
Evaluation None "Focus on the time, location, and highlights"

(3) The "6 Elements" Framework for Prompt Design

A complete prompt typically includes the following elements (it doesn’t have to include all of them, but the more elements it includes, the more precise it will be):

  1. Role — Character description: "You are a..."
  2. Task — Specify the task: "Please complete..."
  3. Context — Background information: "The target audience is..."
  4. Format — Output format: "Output as JSON / Table / Markdown"
  5. Constraint — Constraints: "No more than 300 words / Use only elementary school vocabulary"
  6. Example — Example (Few-shot): "As shown in the following example..."

In Python string templates, this is expressed as:

PYTHON
PROMPT_TEMPLATE = """
[Role] You are a {role}.
[Task] Please {task}.
[Context] {context}
[Format] Output in {format}.
[Constraints] {constraints}
[Example] {example}
"""

4. Prompt Strategies: Zero-Shot / Few-Shot / Thought Chains

(1) Zero-Shot Learning

Zero-shot prompting involves describing the task directly without providing the AI with any examples—it relies on the model’s existing knowledge and understanding.

▶ Example: Zero-shot prompts and outputs (Difficulty: ⭐)

PYTHON
# Zero-shot prompt: classify sentiment without examples
prompt = """
Classify the sentiment of the following review as Positive or Negative:

Review: "The battery life is amazing, lasted 12 hours on a single charge!"
Sentiment:
"""

# Expected output: Positive

Zero-shot learning is suitable for simple, well-defined tasks. However, when tasks are more complex or require a specific format, the output tends to be inconsistent.

(2) Few-Shot Learning

"Few examples" means providing a few "input→output" examples in the prompt so that the AI can learn the patterns you expect through analogy.

▶ Example: Improving Output with Few Samples (Difficulty ⭐)

PYTHON
# Few-shot prompt: classify sentiment with examples
prompt = """
Classify the sentiment of each review as Positive or Negative.

Review: "Love the screen quality!" -> Positive
Review: "Terrible customer service." -> Negative
Review: "Decent product, nothing special." -> Neutral
Review: "Best purchase I have made this year!" -> Positive

Review: "The price is too high for what you get." ->
"""

# Expected output: Negative

The key to working with a small sample size: The examples must be representative and cover a variety of scenarios; the format must be consistent so that the AI learns patterns rather than chaos.

(3) Chain-of-Thought (CoT) Prompting

The "Thinking Chain" prompt instructs the AI to "think step by step" and make its reasoning process explicit. This is particularly effective for tasks such as mathematical reasoning and logical analysis.

▶ Example: CoT prompts for solving math problems (Difficulty: ⭐⭐)

PYTHON
# Chain-of-Thought prompt: solve a math problem step by step
prompt = """
Solve the following problem step by step.

Problem: A store buys a laptop for $800 and sells it for $1,050.
What is the profit margin as a percentage?

Step-by-step solution:
"""

# Expected output:
# Step 1: Calculate profit = 1050 - 800 = $250
# Step 2: Calculate margin = (250 / 800) * 100% = 31.25%
# Answer: The profit margin is 31.25%

Two Uses of CoT:

(4) Comparison Table of Three Prompt Strategies

Dimension Zero-Shot Few-Shot Chain of Thought (CoT)
Are examples needed? No Yes (2–5) Yes (including reasoning steps)
Token Consumption Low Medium High
Use Cases Simple classification and formatting Formatting/style alignment required Math/logic/multi-step reasoning
Output Stability Low Medium High
Accuracy Depends on model capabilities Significant improvement Significant improvement
Typical Usage Classify: ... A->X, B->Y, C->? "Step by step..."

5. System Prompts and Character Settings

(1) What is a System Prompt?

In the Chat API, messages are categorized into three roles:

Role Function Analogy
system Setting AI Behavior Rules and Identity "Employee Handbook"
user User's actual question or command "Customer needs"
assistant AI's reply "Employee Response"

The System Prompt remains active throughout the entire conversation and is the most powerful tool for controlling the AI's behavior.

(2) The Power of Character Development

Role-based settings not only change the AI’s “tone,” but also alter the scope of knowledge it draws upon and its reasoning methods:

▶ Example: System Prompt Role-Playing (Difficulty: ⭐)

PYTHON
# Define a system prompt for a technical interviewer role
system_prompt = """
You are a senior software engineer conducting a technical interview.
Rules:
1. Ask one question at a time
2. Wait for the candidate answer before asking the next
3. If the answer is wrong, give a hint instead of the correct answer
4. Cover topics: data structures, algorithms, system design
5. Rate each answer on a scale of 1-5
"""

user_message = "I am ready for the interview. Please start."

# The AI will behave as a structured interviewer,
# not a generic chatbot

(3) Best Practices for System Prompts

  1. Write the rules first, then the roles: Rules are easier for AI to follow than roles
  2. List rules in numbered form: This is less likely to be overlooked by AI than paragraphs written in natural language.
  3. Set Negative Constraints: “Don’t do this” is sometimes more effective than “Do this.”
  4. Testing the Limits: Deliberately breaking the rules to see if the AI corrects them

6. Output Format Control

(1) Why Is Format Control Necessary?

When you need AI output to be parsed by a program (rather than read by a human), format control is crucial—JSON, tables, specific delimiters, and so on.

(2) Comparison of Output Format Control Methods

Method Advantages Disadvantages Suitable Scenarios
Natural language description Simple AI may not comply Unstructured output
Markdown template Good readability Requires additional processing for parsing Reports/documents
JSON Schema Machine-readable High token consumption API integration
Delimiter Marker Precise Control Requires Delimiter Design Extract Specific Fields
Few-shot format Strong AI imitation capabilities Examples take up tokens Alignment of complex formats

▶ Example: Controlling JSON Format Output (Difficulty: ⭐⭐)

PYTHON
# Prompt that enforces JSON output with a schema
prompt = """
Extract product information from the text below.
Output ONLY valid JSON matching this schema:

{
  "name": "string",
  "price": "number",
  "currency": "string",
  "features": ["string"]
}

Text: "The UltraWidget Pro costs $49.99 and comes with waterproof casing,
solar charging, and a 5-year warranty."

JSON:
"""

# Expected output:
# {
#   "name": "UltraWidget Pro",
#   "price": 49.99,
#   "currency": "USD",
#   "features": ["waterproof casing", "solar charging", "5-year warranty"]
# }

(3) Ensuring the Reliability of JSON Output

In real-world projects, JSON generated by AI may contain formatting errors. We recommend using Pydantic for secondary validation:

PYTHON
from pydantic import BaseModel
from typing import List

class ProductInfo(BaseModel):
    name: str
    price: float
    currency: str
    features: List[str]

# Validate AI output
try:
    product = ProductInfo.model_validate_json(ai_output)
    print(f"Valid: {product.name} - {product.currency}{product.price}")
except Exception as e:
    print(f"Invalid JSON: {e}")

7. Prompt Engineering Workflow

(1) Iterative Optimization Process

Prompt engineering is not a one-time process—it is a "design → test → evaluate → iterate" cycle:

100%
graph TB
    A[Requirements] --> B[Template Design]
    B --> C[Test Run]
    C --> D{Evaluate Output}
    D -- Unsatisfied --> E[Diagnose]
    E --> F[Revise Prompt]
    F --> C
    D -- Satisfied --> G[Freeze Template]
    G --> H[Deploy]

(2) Criteria for Evaluating Output Quality

Dimension Evaluation Method Tool
Accuracy Comparison with Human Annotation LLM-as-Judge
Format Compliance Schema Validation Pydantic / JSON Schema
Integrity Check Required Fields Custom Scripts
Consistency Comparison of Multiple Runs Statistical Variance
Relevance Alignment with Task Objectives Human Rating

8. Common Prompt Pitfalls and Troubleshooting

(1) Five Common Pitfalls

Pitfall Symptoms Fix
Vague Instructions AI Output Deviates Clear Task Objectives + Output Format
Information Overload Prompt is too long; AI ignores key parts Organize into paragraphs; place key information at the end
Format Drift Unstable output format Provide examples + schema constraints
Role Conflict Conflict between System and User Commands System has the highest priority; commands are unified
Excessive Constraints Too many constraints prevent the AI from generating output Keep core constraints; remove redundant ones

(2) Prompt Debugging Strategy Comparison Chart

Strategy Action Applicable Scenarios
A/B Testing Change only one variable and compare the results Identify which factor has the greatest impact
Gradual Simplification Gradually remove elements from the full prompt Identify the minimum viable prompt
Gradual Enhancement Gradually add elements starting from the simplest prompt Confirm the contribution of each element
Output Review Check AI output sentence by sentence Identify specific issues
Boundary Testing Input extreme/boundary cases Test robustness
Temperature Control Lower the temperature to reduce randomness When deterministic output is required

(3) Hands-On Debugging: The Diagnostic Process for a Prompt

PYTHON
# Bad: vague prompt
bad_prompt = "Analyze the electric vehicle market."

# Step 1: Add role
step1 = "You are a senior EV market analyst. Analyze the EV market."

# Step 2: Add structure
step2 = """You are a senior EV market analyst.
Analyze the 2025 global EV market with:
1. Three key trends
2. Two major risks
3. One investment recommendation"""

# Step 3: Add format constraint
step3 = """You are a senior EV market analyst with 15 years of experience.
Analyze the 2025 global EV market with:
1. Three key trends (present in a Markdown table)
2. Two major risks (with probability assessment)
3. One investment recommendation (with expected ROI range)

Output in Markdown format with clear headers."""

9. Comprehensive Example: Prompt Template for Structured Product Analysis

▶ Example: A Complete Product Analysis Prompt System (Difficulty: ⭐⭐⭐)

Create a reusable product analysis prompt template that includes a system prompt, a user prompt, and output analysis:

PYTHON
from string import Template
from pydantic import BaseModel
from typing import List, Optional

# Step 1: System Prompt - define the role
SYSTEM_PROMPT = """You are a senior product analyst with expertise in
market research, competitive analysis, and user experience evaluation.
You produce structured, data-driven product analysis reports.

Rules:
1. Always base analysis on concrete evidence and data points
2. Highlight both strengths and weaknesses objectively
3. Provide actionable recommendations with priority levels
4. Use specific numbers and metrics whenever possible
5. Format output as valid Markdown
"""

# Step 2: User Prompt template with all 6 elements
USER_PROMPT_TEMPLATE = Template("""
[Task] Analyze the following product and produce a comprehensive report.

[Product Info]
- Name: $product_name
- Category: $category
- Price: $price
- Target Users: $target_users

[Report Structure]
Please include the following sections:
1. Product Overview - Brief description and positioning
2. Strengths - Top 3 advantages with evidence
3. Weaknesses - Top 3 limitations with examples
4. Market Position - Competitive landscape analysis
5. Recommendations - Prioritized improvement suggestions

[Format] Output as Markdown with headers and a summary table.
[Constraints] Maximum 500 words. Be concise and specific.
[Example]
Product Overview
The ProductX is a mid-range smartphone targeting young professionals...

Summary Table
| Aspect | Rating | Key Insight |
|--------|--------|-------------|
| Design | 4/5 | Premium feel at mid-range price |
""")

# Step 3: Fill template and call API (pseudo-code)
def analyze_product(product_name: str, category: str,
                    price: str, target_users: str) -> str:
    user_prompt = USER_PROMPT_TEMPLATE.substitute(
        product_name=product_name,
        category=category,
        price=price,
        target_users=target_users
    )
    # In real usage, call your LLM API here
    # response = client.chat.completions.create(
    #     model="gpt-4",
    #     messages=[
    #         {"role": "system", "content": SYSTEM_PROMPT},
    #         {"role": "user", "content": user_prompt}
    #     ],
    #     temperature=0.3
    # )
    # return response.choices[0].message.content
    return user_prompt  # Return the prompt for demo

# Step 4: Use the template
result = analyze_product(
    product_name="EcoBike S3",
    category="Electric Bicycle",
    price="$1,299",
    target_users="Urban commuters aged 25-40"
)
print(result[:300] + "...")

Key Design Points: The System Prompt defines roles and general rules, while the User Prompt template handles specific tasks; separating the two allows for template reusability.


❓ FAQ

Q Is prompt engineering just “talking to AI”?
A Not exactly. Casual conversations with AI are spontaneous, whereas prompt engineering is a systematic design process—it involves clear principles (clear, specific, structured), strategy selection (zero-shot, few-shot, CoT), format constraints, and an iterative workflow. Just as both “talking” and “giving a speech” use language, but the latter requires careful planning.
Q Why do different prompts yield such vastly different results for the same task?
A LLMs are probabilistic models that predict subsequent content based on each token in the prompt. A vague prompt causes the model to sample randomly from a vast space of possibilities, while a precise prompt significantly narrows the range of valid outputs—the difference can be as much as several orders of magnitude.
Q When should the Chain of Thought prompt be used?
A Use CoT when a task involves multi-step reasoning (mathematical calculations, logical analysis, complex decision-making). For single-step tasks such as simple classification or format conversion, CoT actually wastes tokens without improving performance. Here’s a rule of thumb: if it’s a question that would require a person to “think for a moment” before answering, you should use CoT.
Q What is the difference between a System Prompt and a User Prompt?
A A System Prompt defines the AI’s identity and behavioral rules; it remains in effect throughout the entire conversation and has the highest priority. A User Prompt specifies a particular task or question for each interaction and is only effective during the current turn. To use an analogy: the System Prompt is the “employee handbook,” while the User Prompt is the “customer request.”
Q Can prompts replace fine-tuning?
A In many scenarios, yes—a small number of prompt examples can help the model adapt to new task formats. However, prompts have length limitations and cannot incorporate a large amount of domain knowledge; fine-tuning, on the other hand, can modify the model’s parameters to deeply internalize knowledge. Rule of thumb: If 5–10 examples are enough, use prompts; if you need hundreds of samples or specialized knowledge, consider fine-tuning.

📖 Summary

Preview of the Next Chapter: We’ll dive into RAG (Retrieval-Augmented Generation) and learn how to enable AI to answer questions by drawing on external knowledge bases, thereby breaking through the temporal limitations of the model’s knowledge.


📝 Exercises

Basics (⭐)

Design three prompts of varying quality to complete the same task (translating a passage of English into Chinese), and compare the quality of the output:

PYTHON
# Task: Translate English to Chinese

# Bad prompt
prompt_v1 = "Translate this: The quick brown fox jumps over the lazy dog."

# Better prompt (add context and constraints)
prompt_v2 = "Translate the following English sentence to natural, fluent Chinese. Preserve the original tone and meaning.\n\nThe quick brown fox jumps over the lazy dog."

# Best prompt (add role, format, and example)
prompt_v3 = """You are a professional English-Chinese translator.

Rules:
- Translate to natural, idiomatic Chinese
- Preserve the original tone and nuance
- If the sentence has cultural references, add a brief note

Example:
EN: "Break a leg!"
CN: "Break a leg! (English idiom for wishing good luck before a performance)"

Now translate:
The quick brown fox jumps over the lazy dog."""

# TODO: Run each prompt through an LLM and compare outputs

Advanced (⭐⭐)

Use a small number of examples to prompt an LLM to classify 5 comments by sentiment (Positive / Negative / Neutral):

PYTHON
# Few-shot sentiment classification
prompt = """Classify the sentiment of each review.

Review: "Absolutely love this product! Five stars!" -> Positive
Review: "Waste of money, broke after one week." -> Negative
Review: "It works as expected, nothing more." -> Neutral
Review: "The design is gorgeous but battery drains fast." -> ?

Now classify these 5 reviews:
1. "Best coffee maker I have ever owned!"
2. "Delivery was late and the box was damaged."
3. "Average quality for the price, decent but not impressive."
4. "Customer support resolved my issue in 10 minutes, amazing!"
5. "The instructions are confusing but the product works fine."
"""

# TODO: Run through an LLM and verify accuracy

Challenge (⭐⭐⭐)

Design a System Prompt that has an LLM act as a technical interviewer and implements a multi-round dialogue process:

PYTHON
# Technical interviewer system
SYSTEM_PROMPT = """You are a senior backend engineer conducting a coding interview.
Rules:
1. Ask one question at a time about Python/data structures/algorithms
2. Evaluate each answer: rate 1-5 and give brief feedback
3. If wrong, provide a hint, not the full answer
4. After 5 questions, give an overall assessment
5. Difficulty should gradually increase
"""

# Simulated multi-turn conversation
conversation = [
    {"role": "system", "content": SYSTEM_PROMPT},
    {"role": "user", "content": "I am ready for the interview."},
]

# TODO: Implement a loop that:
# 1. Sends conversation to LLM
# 2. Prints the interviewer question
# 3. Takes user input as answer
# 4. Appends both to conversation
# 5. Repeats until interview ends
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏