Prompt engineering is the skill of crafting inputs that get the best results from AI models. Master these techniques for superior AI outputs.
Basic Prompt Structure
# Clear and Specific
Bad: "Write about dogs"
Good: "Write a 200-word informative paragraph about Golden Retrievers,
focusing on their temperament and suitability as family pets."
# Include Role/Persona
"You are an expert Python developer. Explain list comprehensions
to a beginner with 3 simple examples."
# Specify Format
"Create a JSON object with user information including name,
age, email, and interests as an array."
```
Few-Shot Learning
Extract the sentiment from these reviews:
Review: "This product is amazing! Best purchase ever."
Sentiment: Positive
Review: "Terrible quality, broke after one day."
Sentiment: Negative
Review: "It's okay, nothing special."
Sentiment: Neutral
Review: "I absolutely love the color and design!"
Sentiment: [AI will complete]
```
Chain of Thought Prompting
Solve this step by step:
Q: A bakery baked 200 cookies. They sold 60% in the morning
and 25% of the remaining in the afternoon. How many are left?
Let's think through this step by step:
1. First calculate morning sales
2. Then calculate remaining after morning
3. Calculate afternoon sales
4. Final remaining count
```
System Prompts for Consistency
system_prompt = """
You are a professional code reviewer. For each code snippet:
1. Identify potential bugs
2. Suggest performance improvements
3. Check for security issues
4. Recommend best practices
Format your response as numbered list with clear explanations.
"""
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": "Review this code: ..."}
]
```
Prompt Templates
# Email generator template
email_template = """
Write a professional email with:
- Recipient: {recipient}
- Purpose: {purpose}
- Tone: {tone}
- Key points: {points}
- Call to action: {cta}
"""
# Use it
prompt = email_template.format(
recipient="hiring manager",
purpose="follow up on job application",
tone="polite and enthusiastic",
points="reiterate qualifications, express interest",
cta="request interview"
)
```
Advanced Techniques
# Constrained Output
"List 5 Python web frameworks.
Format: Framework Name | Use Case | Difficulty Level
Output as markdown table."
# Negative Prompting
"Explain machine learning WITHOUT using technical jargon,
mathematical formulas, or acronyms."
# Multi-step Reasoning
"First, identify the main topic. Second, list key concepts.
Third, create an outline. Finally, write a comprehensive summary."
```
Great prompts lead to great AI outputs. Practice and refine your prompting skills!