π¨ Prompt Engineering Fundamentals
Unit 3: The Art & Science of Talking to AI
Master the skill that makes GenAI truly powerful
π Today's Journey
From Zero to Prompt Hero
Introduction to Prompt Engineering
What it is and why it matters
Anatomy of Effective Prompts
The building blocks of great prompts
Zero-shot & Few-shot Learning
Teaching AI with examples (or without!)
Advanced Techniques
Chain-of-thought, roles, templates
Hands-on Workshop
Build your own prompt library
π‘ Goal: By the end, you'll be able to craft prompts that get exactly the outputs you need!
π― What is Prompt Engineering?
Definition
Prompt Engineering = The art and science of crafting inputs (prompts) to get desired outputs from AI models
"Tell me about AI"
Vague, unfocused, unpredictable results
"Explain artificial intelligence in 3 bullet points for a high school student, focusing on real-world applications"
Clear, specific, actionable
Why It Matters
π― Precision
Get exactly what you need, not random outputs
β‘ Efficiency
Save time with first-try success
π° Cost Savings
Fewer tokens = lower API costs
π§ The Prompt Engineering Mindset
Think Like a Model Trainer
β Common Mistakes
- Assuming the model "knows" what you want
- Being too vague or too verbose
- Not providing context
- Expecting mind-reading
- Not iterating and testing
β Best Practices
- Be explicit and specific
- Provide clear instructions
- Give context and examples
- Specify format and tone
- Test and refine iteratively
The Golden Rule of Prompting
"If a human wouldn't understand your instruction, neither will the AI"
π‘ Pro Tip: Prompt engineering is empiricalβwhat works for GPT-4 might not work for Claude. Always test!
π§© The 6 Components of Great Prompts
The Prompt Formula
Not all components needed every timeβchoose what fits your use case!
π Role
"You are an expert..."
Sets the persona
π Context
"Given that..."
Background info
π― Task
"Please analyze..."
What to do
π Format
"Output as JSON..."
How to structure
π£οΈ Tone
"Be professional..."
Style of response
π Examples
"Like this:..."
Show don't tell
π Deconstructing a Great Prompt
Role You are an experienced Python developer.
Context I'm building a web scraper for e-commerce sites.
Task Write a Python function that extracts product titles and prices from HTML.
Format Use BeautifulSoup4, include error handling, and add docstrings.
Tone Explain your code with inline comments for a beginner.
Example Example HTML: <div class="product"><h2>Laptop</h2><span>$999</span></div>
Result: A well-structured, commented, error-handled function that matches your exact needs!
"Write code to scrape websites"
Result: Generic, possibly wrong, no context-specific code
π― Clarity & Specificity
The Specificity Spectrum
Too Vague
"Summarize this"
Problem:
- How long?
- What format?
- What to focus on?
Just Right
"Summarize this article in 3 bullet points, focusing on key findings and implications for healthcare"
Clear:
- Length: 3 points
- Format: Bullets
- Focus: Findings + healthcare
β οΈ Watch Out for Over-specification
Too many constraints can be restrictive. Find the balance!
"Write exactly 247 words using only words starting with A-M, in iambic pentameter, about quantum computing..."
This is probably too much! π
π Zero-shot Learning: No Examples Needed
What is Zero-shot?
Asking the model to perform a task without providing any examplesβrelying purely on its pre-trained knowledge
Zero-shot Example
Classify the sentiment of this review as positive, negative, or neutral:
"The product arrived on time but the quality was disappointing."
Sentiment: Negative
β When to Use
- Simple, common tasks
- Model has strong pre-training
- Quick prototyping
- General knowledge questions
β When to Avoid
- Domain-specific tasks
- Complex formatting needs
- Specialized terminology
- Inconsistent quality
π‘ Zero-shot Best Practices
Making Zero-shot Work Better
Be Crystal Clear About the Task
β Vague: "Fix this code"
β Clear: "Debug this Python function that's throwing a TypeError"
Specify Output Format
β: "List the countries"
β : "List the countries in alphabetical order, one per line"
Define Constraints
β: "Explain quantum physics"
β : "Explain quantum physics in 2 paragraphs for a high school
β : "Explain quantum physics in 2 paragraphs for a high school student"
Use Action Verbs
Classify, Summarize, Extract, Generate, Translate, Rewrite, Analyze, Compare
π‘ Power Move: Combine clear instructions with role-setting: "As a Python expert, debug this function..."
π Few-shot Learning: Learn from Examples
What is Few-shot?
Providing 2-5 examples of the task to teach the model the pattern you want
Few-shot Example
Extract company name and sentiment from reviews:
Review: "Apple's new iPhone is amazing!"
Company: Apple | Sentiment: Positive
Review: "Tesla's customer service was terrible"
Company: Tesla | Sentiment: Negative
Review: "Google's search results are okay"
Company: Google | Sentiment: Neutral
Review: "Microsoft Teams keeps crashing, very frustrating"
Company:
Company: Microsoft | Sentiment: Negative
β‘ The Power of Few-shot
Why Examples Are Magic
π― Shows Exact Format
Model learns the structure you want without explicit formatting instructions
π¨ Demonstrates Style
Tone, formality, and approach are learned from examples
π§ Handles Edge Cases
Include tricky examples to show how to handle them
π Improves Consistency
Outputs become more predictable and reliable
How Many Examples?
| Examples | Name | When to Use |
|---|---|---|
| 0 | Zero-shot | Simple tasks, quick tests |
| 1 | One-shot | Show format or style |
| 2-5 | Few-shot | Complex patterns, edge cases |
| 10+ | Many-shot | Very specialized tasks |
| 1000+ | Fine-tuning | Production systems, specific domains |
π Few-shot Best Practices
Use Diverse Examples
Cover different scenarios and edge cases
β Good: Positive, negative, neutral, mixed, sarcastic reviews
β Bad: Only positive reviews
Be Consistent
Use the same format across all examples
Input: [text]
Output: [result]
Input: [text]
Output: [result]
Order Matters
Put the most important/relevant examples last (recency bias)
Quality Over Quantity
3 perfect examples > 10 mediocre ones
π§ Chain-of-Thought: Show Your Work
What is Chain-of-Thought (CoT)?
Prompting the model to explain its reasoning step-by-step before giving the final answer
Q: If a store has 23 apples and sells 17, then receives 45 more, how many apples does it have?
A: 51
No reasoning shown, might be wrong
Q: If a store has 23 apples and sells 17, then receives 45 more, how many apples does it have? Think step by step.
A: Let me solve this step by step:
1. Start: 23 apples
2. After selling 17: 23 - 17 = 6 apples
3. After receiving 45: 6 + 45 = 51 apples
Final answer: 51
π― Why Chain-of-Thought Works
π Better Accuracy
Especially for math, logic, and reasoning tasks
π Easier Debugging
See where reasoning went wrong
π Handles Complexity
Breaks down multi-step problems
CoT Techniques
Zero-shot CoT
Just add "Let's think step by step"
"Solve this problem. Let's think step by step."
β Simple, works surprisingly well
Few-shot CoT
Provide examples with reasoning
Q: 5 + 3 = ?
A: Let me add: 5 + 3 = 8
Q: 10 - 4 = ?
β Most powerful, best results
πΌ CoT in Real Applications
Example 1: Code Debugging
Debug this code. Think through what each line does step by step:
def calculate(x):
result = x / 0
return result * 2
Let me analyze step by step:
1. Line 1: Function takes parameter x
2. Line 2: Attempts x / 0 - ERROR: Division by zero!
3. Line 3: Would multiply by 2 (but never reached)
Problem: Division by zero on line 2
Fix: Add check for zero or use try-except
Example 2: Data Analysis
Sales dropped 30% in Q3. Think through possible causes step by step.
π Role Prompting: Wear Different Hats
What is Role Prompting?
Telling the model to assume a specific persona or expertise to shape its responses
The Power of Roles
"Explain blockchain"
Generic, medium complexity explanation
"You are a blockchain professor. Explain blockchain to first-year students."
Educational, structured, beginner-friendly with analogies
π Expert Roles
"You are a senior data scientist..."
"As a cybersecurity expert..."
"You are a patent lawyer..."
π₯ Audience Roles
"Explain like I'm 5..."
"Write for C-level executives..."
"Teach a beginner..."
π¨ Creative Roles
"You are a creative copywriter..."
"As a storyteller..."
"You are a comedian..."
βοΈ System Instructions: Setting Persistent Behavior
System vs User Prompts
System: Sets overall behavior (role, constraints, style)
User: Specific task or question
Typical API Structure
"You are a helpful Python tutor. Always:
- Explain concepts simply
- Provide working code examples
- Use comments to explain each line
- Suggest best practices"
"How do I read a CSV file?"
Great question! Let me show you the simplest way...
[Provides simple explanation + commented code + best practices]
π‘ Pro Tip: System instructions are perfect for chatbotsβset once, affects all conversations!
π― Effective Role Patterns
Pattern 1: Expert + Constraints
Always [BEHAVIOR].
Never [CONSTRAINT].
"You are a security researcher with 10 years experience. Always mention potential risks. Never recommend insecure practices."
Pattern 2: Persona + Style
Respond in a [TONE] manner.
"You are a friendly teacher who loves making complex topics simple. Respond in an encouraging, patient manner."
Pattern 3: Multi-Role
Consider [PERSPECTIVE1] and [PERSPECTIVE2].
"You are both a developer and product manager. Consider technical feasibility and user experience."
Pattern 4: Negative Instructions
DO NOT: [UNWANTED BEHAVIOR]
"You are a medical advisor. DO NOT: Provide diagnoses, recommend specific treatments, or replace professional medical advice."
π Prompt Templates: Reusable Patterns
Why Templates?
Build once, use everywhere. Templates ensure consistency and save time
Template 1: Content Summarization
[CONTENT]
Focus on: [FOCUS_AREAS]
Target audience: [AUDIENCE]
Example Use:
"Summarize the following research paper in 3 bullet points. Focus on: methodology and results. Target audience: non-experts"
π οΈ Essential Prompt Templates
Template 2: Code Generation
Requirements:
- [REQ1]
- [REQ2]
- [REQ3]
Include:
- Error handling
- Docstrings
- Type hints
- Example usage
Template 3: Data Extraction
Format as JSON:
{
"field1": "value",
"field2": "value"
}
Text: [INPUT_TEXT]
Template 4: Creative Writing
Style: [STYLE]
Tone: [TONE]
Length: [LENGTH]
Target audience: [AUDIENCE]
Include: [ELEMENTS]
Template 5: Analysis
Consider:
1. [ASPECT1]
2. [ASPECT2]
3. [ASPECT3]
Provide: Findings, Insights, Recommendations
π― Popular Prompting Frameworks
1. CRISPE Framework
- Capacity and Role
- Rinsight (Context)
- Instructions
- Statement (Task)
- Personality (Tone)
- Experiment (Iterate)
2. RISEN Framework
- Role: Who is the AI?
- Instructions: What to do?
- Steps: How to do it?
- End goal: What's the output?
- Narrowing: Constraints
π‘ Remember: Frameworks are guides, not rules. Adapt to your needs!
π¬ Hands-on Workshop Time!
What We'll Do
Build prompts together, test them live, and create your personal prompt library
Workshop Structure
Round 1: Zero-shot Challenge (10 min)
Write prompts for common tasks without examples
Round 2: Few-shot Practice (10 min)
Add examples to improve your prompts
Round 3: CoT Application (10 min)
Apply chain-of-thought to complex problems
Round 4: Template Building (15 min)
Create reusable templates for your use cases
π― Tool: We'll use ChatGPT/Claude/Gemini - bring your laptops!
πͺ Workshop Challenges
Challenge 1: Email Writer
Task: Write a prompt that generates professional emails
Requirements:
- Adjustable tone (formal/casual)
- Different purposes (request/update/apology)
- Custom signature
Challenge 2: Code Reviewer
Task: Create a prompt for code review
Requirements:
- Check for bugs
- Suggest improvements
- Explain reasoning
Challenge 3: Data Analyzer
Task: Create a prompt for analyzing datasets
Requirements:
- Use chain-of-thought for reasoning
- Output key insights and visualization suggestions
- Handle edge cases like missing data
Challenge 4: Content Generator
Task: Build a prompt for generating social media content
Requirements:
- Incorporate role prompting (e.g., as a marketer)
- Specify tone and length
- Include calls to action
π‘ Instructions: Work in pairs, test your prompts with an LLM, and share what worked/didn't!
π Congratulations! You're Now a Prompt Engineer
Key Takeaways
π― Precision Matters
Clear, specific prompts yield dramatically better results
π§© Use the 6 Components
Role + Context + Task + Format + Tone + Examples
π Zero-shot vs Few-shot
Use zero-shot for simple tasks, few-shot for patterns and consistency
π§ Chain-of-Thought
Force step-by-step reasoning for complex problems
π Role Prompting
Make the AI adopt expert personas for better outputs
π Templates & Frameworks
Build reusable prompts for efficiency and consistency
Your Next Steps
Practice Daily
Apply these techniques in every AI interaction
Build Your Prompt Library
Save your best prompts for reuse across projects
Experiment & Iterate
Test across models (GPT, Claude, Gemini) and refine
Share Knowledge
Teach others β teaching solidifies your mastery
Thank You!
You now have the superpower to make GenAI work for you β not the other way around.
Questions? Let's discuss! π