Chapter 4: Prompting Techniques

Advanced Prompting Techniques
Mastering different prompting techniques allows you to unlock the full potential of Large Language Models. This module explores sophisticated methods to improve your prompt engineering skills.
Learning Objectives:
- Understand and apply various advanced prompting techniques
- Learn when to use each technique based on task requirements
- Combine multiple techniques for optimal results
- Evaluate the effectiveness of different approaches
- Develop strategies for complex problem-solving with LLMs
Role Prompting
Assigning a specific role to the LLM helps tailor responses to particular perspectives or expertise levels.
Basic Example
"Act as a senior software engineer. Explain how hash tables work to a junior developer."
This prompt will generate a technical explanation with appropriate depth for the target audience.
Advanced Example
"""
You are a financial analyst with 10 years of experience.
Explain the concept of dollar-cost averaging to a client who:
- Is 25 years old
- Has basic investing knowledge
- Prefers simple analogies
Keep the explanation under 3 paragraphs.
"""
Benefits of Role Prompting
Expertise Level
Matches explanation depth to audience knowledge
Perspective
Provides context-aware responses
Consistency
Maintains appropriate tone throughout
Few-Shot Prompting
Providing examples helps the model understand the desired format, style, or reasoning pattern.
Classification Example
"""
Classify the sentiment of these tweets:
1. "I love this product! It's amazing!" → Positive
2. "This is the worst service I've ever experienced" → Negative
3. "The update has some good and bad aspects" → Neutral
4. "Just tried the new feature and it works well" →
"""
Formatting Example
"""
Convert dates to this format: YYYY-MM-DD
Example 1: Jan 5, 2023 → 2023-01-05
Example 2: March 20 → 2025-03-20 (assume current year)
Now convert: July 4th →
"""
Code Example: Few-Shot Prompting
import openai
# Few-shot prompt for text transformation
few_shot_prompt = """
Convert these sentences to business jargon:
1. "We need to work faster" → "We need to leverage synergies to accelerate deliverables"
2. "Let's make a plan" → "Let's strategize a proactive roadmap"
3. "This isn't working" → "This initiative lacks operational alignment"
4. "We should talk to customers" →
"""
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": few_shot_prompt}],
temperature=0.7,
max_tokens=100
)
print(response.choices[0].message.content)
# Likely output: "We should engage in proactive stakeholder outreach"
Key Features:
- Provides clear examples of the transformation pattern
- Shows both input and desired output format
- Allows the model to infer the transformation rules
- Temperature setting allows for some creativity
Chain of Thought (CoT)
CoT prompting encourages the model to break down complex problems into intermediate reasoning steps.
Standard CoT
"""
A store has 15 apples. 6 are sold on Monday and 4 more on Tuesday.
How many apples remain at the end of Tuesday?
Think step by step and show your work.
"""
Sample Output:
1. Start with 15 apples
2. Monday: 15 - 6 = 9 apples remain
3. Tuesday: 9 - 4 = 5 apples remain
4. Final answer: 5 apples
Zero-Shot CoT
"""
If a train travels 300 miles in 5 hours, what is its average speed?
Let's think step by step.
"""
Sample Output:
1. Total distance: 300 miles
2. Total time: 5 hours
3. Speed = Distance / Time = 300 / 5
4. Final answer: 60 mph
CoT Effectiveness
Without CoT
- Direct answers may skip reasoning steps
- Higher chance of logical errors
- Harder to verify correctness
With CoT
- Transparent reasoning process
- Easier to debug incorrect steps
- Improves accuracy on complex problems by 20-40%
Least-to-Most Prompting
This technique breaks complex problems into smaller, sequential sub-questions.
Example: Business Analysis
"""
Analyze this business scenario step by step:
1. What is the core problem described?
2. What are 2-3 potential solutions?
3. For each solution, list pros and cons
4. Recommend the best option with justification
Scenario:
"A SaaS company has 40% churn rate. Customers cite complex onboarding as the main reason."
"""
Sample Output Structure:
1. Core Problem: Complex onboarding causing high churn...
2. Potential Solutions:
a. Simplify onboarding flow...
b. Add interactive tutorials...
3. Pros and Cons...
4. Recommendation: Implement interactive tutorials because...
Dual Prompt Approach
Separate planning and execution into distinct prompts for better control.
Planning Prompt
"""
Create a step-by-step plan to research and write a 500-word blog post about:
"The impact of AI on sustainable agriculture"
Output the plan as a numbered list.
"""
Execution Prompt
"""
Using this plan, write the blog post section for step 3:
"Compare traditional vs AI-enhanced farming techniques"
Keep it concise (1-2 paragraphs) and include specific examples.
"""
Combining Techniques
Powerful results come from combining multiple prompting techniques.
Role + CoT + Few-Shot
"""
You are a senior data scientist. Explain how to implement k-means clustering in Python.
Structure your explanation:
1. Briefly describe what k-means does
2. List the key steps in the algorithm
3. Show a complete Python implementation
4. Provide 2 evaluation metrics
Example for another algorithm (decision trees):
1. Decision trees split data recursively...
2. Steps: a) Calculate impurity, b) Find best split...
3. Code: from sklearn.tree import DecisionTreeClassifier...
4. Metrics: accuracy, F1-score...
Now explain k-means following this structure.
"""
Code Example: Combined Techniques
import openai
# Combined technique prompt
prompt = """
Act as an experienced cybersecurity professional.
Analyze this potential security vulnerability using the following steps:
1. Describe the vulnerability in simple terms
2. Explain potential attack vectors
3. Rate severity (Low/Medium/High/Critical)
4. Recommend mitigation strategies
Example for another vulnerability (SQL injection):
1. SQL injection allows attackers to...
2. Attack vectors: a) Unsanitized form inputs...
3. Severity: High
4. Mitigation: a) Use prepared statements...
Now analyze: "Server accepts password reset tokens without expiration"
"""
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
temperature=0.3, # Lower for more deterministic output
max_tokens=500
)
print(response.choices[0].message.content)
Techniques Used:
- Role Prompting: Cybersecurity professional
- Few-Shot: SQL injection example
- Chain of Thought: Structured analysis steps
- Constraints: Severity rating scale
Self-Consistency Prompting
Generate multiple responses and select the most consistent or highest quality answer.
Example Implementation
"""
Generate 3 different approaches to solve this problem, then select the best one:
Problem:
"Our mobile app has low user retention after the first week."
Approach 1:
Approach 2:
Approach 3:
After listing all 3, analyze their pros/cons and recommend the best option.
"""
Prompt Chaining
Break complex tasks into a sequence of smaller prompts with intermediate outputs.
Example: Research Paper Summary
Prompt 1
"Extract key claims from this research paper"
Prompt 2
"Organize claims into categories"
Prompt 3
"Write executive summary from organized claims"
When to Use Each Technique
Choose the right technique based on your specific requirements.
Technique | Best For | Complexity |
---|---|---|
Role Prompting | Audience-specific explanations | Low to Medium |
Few-Shot | Formatting, classification | Low |
Chain of Thought | Math, logic problems | Medium to High |
Least-to-Most | Complex analysis tasks | High |
Prompt Chaining | Multi-stage workflows | Very High |
Technique Selection Guide
Summary
Mastering these prompting techniques will significantly enhance your ability to work effectively with Large Language Models across various applications.
Key Takeaways
- Different techniques serve different purposes - choose appropriately
- Combining techniques often yields the best results
- Complex problems benefit from decomposition (CoT, Least-to-Most)
- Always consider the tradeoffs between simplicity and control
- Test and iterate to find the optimal approach for your use case