SlideShare a Scribd company logo
Prompt
Engineering
Damian Gordon
Prompt
Engineering
• Prompt engineering is
designing and refining
prompts to effectively
communicate with AI
models, improving
quality, relevance, and
accuracy of their
responses.
A Simple Introduction to Prompt Engineering
Prompt Engineering
•Prompt Engineering can …
• increase the accuracy and relevance of answers.
• help control the tone, style, and format of
answers.
• enhance the creativity and efficiency of answers.
Prompt Engineering
•It is important to note that different AI
models interpret prompts differently
(ChatGPT, DALL·E, Copilot, etc.), so the
most important thing to do is try out
different queries and different
approaches to generating queries, and
see which ones work best.
Prompt Engineering
• Ask a question
• Be specific
PROMP
T
• Review the
answer
• Try to verify
PROBE
• Add more
Context
• Add constraints
POLISH
Some
Useful
Terminolo
gy
AI
Hallucinations
What is an AI Hallucination?
•An AI hallucination occurs when an AI
generates false, misleading, or nonsensical
information that appears plausible but is not
based on real data or facts.
•Essentially, the AI "makes things up" because it
lacks the right knowledge or is overconfident
in its response.
What is an AI Hallucination?
•For Example:
• Who will win the 2044 Formula 1 World
Championship?
•Possible AI Answer:
• The 2044 Formula 1 World Champion will
be Max Verstappen, winning 10 races.
What is an AI Hallucination?
• Why Do AI Hallucinations Happen?
• Lack of Real Data: AI fills gaps with plausible but incorrect
information.
• Pattern Matching Without Understanding: AI generates text
based on statistical probabilities, not true comprehension.
• Overconfidence in Its Own Output: AI doesn’t always indicate
uncertainty when unsure.
• Ambiguous or Poorly Defined Prompts: If a prompt lacks
context, AI guesses instead of asking for clarification.
• Random Answers: Sometimes the AI randomly generates
incorrect answers, even if it has the correct answer.
What is an AI Hallucination?
•How to Reduce AI Hallucinations?
• Be Specific in Your Prompts: Ask for explanations,
examples, or step-by-step logic.
• Use Step-by-Step Prompting: Force AI to explain
reasoning before giving answers.
• Verify with External Sources: Cross-check AI output
using real-world references.
• Use Web Searches for Real-Time Data: Prevent
outdated or fabricated facts.
A Simple Introduction to Prompt Engineering
What is an Edge case?
•Edge cases are extreme, uncommon, or
boundary cases.
•Edge cases help identify potential bugs,
inefficiencies, or unexpected behaviors that
might not appear in normal conditions.
e
.
g
.
P
a
l
i
n
d
r
o
m
e
s
What is an Edge case?
• For example, checking if a string is a palindrome.
•Normal case:
• "racecar" True
→
•Edge cases:
• Empty string "" Should return True?
→
• Single character "a" Should return True?
→
• Spaces and punctuation "A man, a plan, a canal,
Panama!"
• Non-alphabetic input "12321"
Principles of
Effective Prompting
Prompt Engineering
•Principles of Effective Prompting:
•Clarity & Precision
•Context & Constraints
•Iteration & Testing
•Role-based Prompts
•Few-shot & Zero-shot Prompting
•Chain-of-Thought Prompting
Clarity &
Precision
Clarity & Precision
•Using clear and specific language to guide AI
responses.
•Let’s say you want an AI to generate a Python
function to calculate the factorial of a number
>>>
Clarity & Precision
•Bad Prompt - Vague & Ambiguous
• Write a function for factorial.
•Issues
• Doesn’t say which programming language to use.
• Unclear if recursion or iteration is preferred.
• No details about input validation.
Clarity & Precision
• Good Prompt - Clear & Precise
• Write a Python function called factorial that takes
a non-negative integer n as input and returns its
factorial. Implement it using recursion, and raise a
ValueError if n is negative.
• Why Better?
• Specifies language (Python).
• Defines function name (factorial).
• Clarifies input constraints (non-negative integer).
• Specifies methodology (recursion).
• Includes error handling (raise ValueError).
Context &
Constraints
Context & Constraints
•Providing background information or setting
boundaries to get relevant answers.
•Let’s say you want to generate a function to
sort a list of numbers >>>
Context & Constraints
• Bad Prompt - Lacks Context & Constraints
• Write a function to sort a list.
• Issues
• Which programming language?
• What kind of sorting algorithm?
• Should it be ascending or descending?
• Should it handle duplicates or special cases (like empty lists)?
• Should it be optimized for performance (e.g., large datasets)?
Context & Constraints
• Good Prompt - Provides Context & Constraints
• Write a Python function called custom_sort that
takes a list of integers and sorts them in
ascending order using the Merge Sort algorithm.
The function should be efficient for large lists
and should preserve duplicate values. If an
empty list is given, return an empty list.
• Why Better?
• Context: Python, sorting integers.
• Constraints: Must use Merge Sort, handle large lists, preserve
duplicates, and return an empty list when given one.
Iteration &
Testing
Iteration & Testing
•Experimenting with different phrasings and
formats to achieve the best output.
•Let’s say we want to generate a function that
checks if a string is a palindrome (reads the
same forward and backward) >>>
Iteration & Testing
• Iteration 1 – A bit vague
• Write a Python function to check if a
string is a palindrome.
• Issues
• Doesn’t specify case sensitivity (should "Racecar" be
considered a palindrome?).
• Doesn’t mention whitespace (should "nurses run" be valid?).
• No requirement for handling non-alphabetic characters (like
punctuation).
Iteration & Testing
•Iteration 2 – Adding Constraints
• Write a Python function is_palindrome
that checks if a string is a palindrome,
ignoring case and whitespace. It should
return True for palindromes and False
otherwise.
•Issues
• Fixes case sensitivity and whitespace handling
• Still doesn’t handle punctuation like "Madam, I'm Adam."
Iteration & Testing
•Iteration 3 – Final Refinement
• Write a Python function is_palindrome
that checks if a string is a palindrome,
ignoring case, whitespace, and
punctuation. Use Python’s re module to
remove non-alphanumeric characters.
•Why Better?
• Optimized & Robust
• Now handles case, whitespace, and punctuation correctly!
Iteration & Testing
•Methodology
• Start with a basic prompt and analyze the AI’s
output.
• Iterate by adding constraints based on
shortcomings.
• Test edge cases to verify correctness.
• Refine until the AI’s output meets all requirements.
Role-based
Prompts
Role-based Prompts
• Asking the AI to act as a specific expert or persona.
• This helps generate more accurate and more context-
aware responses. By assigning a role, you guide the
AI's thought process and ensure it tailors responses to
the expected expertise level.
• For example, role-based prompt for Code Reviewer
>>>
Role-based Prompts
•Prompt Without a Role - Generic Output
• Explain this Python function.
•Issues
• The AI doesn’t know who the explanation is for
(Beginner? Expert? Developer?)
• Might provide a generic, surface-level response
Role-based Prompts
• Role-Based Prompt - More Focused & Useful
• You are a senior Python developer reviewing
code for a junior colleague. Explain the
following function with clear comments and
suggestions for improvement.
• Why better?
• Provides comments and docstrings for clarity.
• Offers constructive feedback (input validation, error handling).
• Uses a mentor-like tone, making it useful for junior
developers.
Few-shot
&
Zero-shot
Prompting
Few-shot & Zero-shot Prompting
•Providing examples (few-shot) or asking the AI
to generate responses with no prior examples
(zero-shot).
Prompt Type Explanation
Few-shot
Prompting
The AI is given one or more examples to
learn from before generating an answer.
Zero-shot
Prompting
The AI is given no examples and must infer
the pattern or solution.
Few-shot & Zero-shot Prompting
•Zero-shot Prompt - No Examples Given:
• Write a Python function that converts
Fahrenheit to Celsius.
•Issues
• No request for docstring or explanation.
• No request for input validation or edge case
handling
Few-shot & Zero-shot Prompting
• Few-shot Prompt - Providing Example to Guide AI:
Here are some temperature conversion functions
… [SAMPLE CODE] … Now, write a function to
convert Fahrenheit to Celsius following the
same style."
• Why better?
• More structured & consistent.
• AI follows the given style.
• Consistent formatting and readability.
• Encourages AI to infer patterns from examples.
Few-shot & Zero-shot Prompting
• When to use each:
• Zero-shot: Useful for general tasks but may produce
inconsistent results.
• Few-shot: Helps guide the AI by showing examples,
improving accuracy and structure. So, use few-shot
prompting when you need consistency, style
matching, or complex logic.
Chain-of-Thought
Prompting
Chain-of-Thought Prompting
•Also called:
•Step-by-Step Instructions
•Step-by-Step Prompting
•Multi-Turn Prompting
Chain-of-Thought Prompting
•Encouraging step-by-step reasoning for
complex tasks.
•This helps improve accuracy and ensures
logical consistency, especially for
mathematical or algorithmic tasks.
•For example, we have a function that finds the
sum of all even numbers in a list, but it’s giving
incorrect results >>>
Chain-of-Thought Prompting
•Normal Prompt
• Find the bug in this Python function.
•Issues
• Sometimes, the AI will fix the error but doesn’t
explain why, making it harder for learners to
understand.
Chain-of-Thought Prompting
•Chain-of-Thought Prompt
• Analyze this Python function step by step to
find any logical errors. Explain each step
before fixing the bug.
•Issues
• Sometimes, the AI will fix the error but doesn’t
explain why, making it harder for learners to
understand.
Chain-of-Thought Prompting
•AI Output - With Step-by-Step Reasoning
•Step 1: Understand the function's goal.
•Step 2: Analyze the features of the code.
•Step 3: Identify the fix.
•Step 4: Provide the corrected function.
Chain-of-Thought Prompting
•Why better?
• Clear breakdown of the problem before fixing it.
• Step-by-step reasoning helps with learning and
debugging.
• Reduces AI hallucination by enforcing structured logical
thinking.
Sample Prompts
Writing Code
• Write a Python function to [describe functionality] .
• Generate a [language] script that does [specific task].
• Create a command-line tool that [functionality].
• Write a function to sort a list using [QuickSort, MergeSort,
etc.].
• Generate a [Flask/Django/React] application that does [features].
Debugging Code
• Find and fix bugs in this [language] code: [paste
code].
• Why is this function returning the wrong result?
[paste code].
• Help me debug this error message: [paste error].
• Explain what this error means and how to fix it:
[error message].
Optimising Code
• Optimize this function for performance: [paste code].
• Rewrite this algorithm to improve time complexity.
• Convert this loop into a more efficient list
comprehension.
• How can I reduce memory usage in this
[Python/JavaScript/etc.] function?"
Explaining Code
• Explain this function step by step: [paste code].
• What does this code do? [paste code].
• Break down this algorithm in simple terms.
• Describe how this function works in a way a
beginner would understand.
Testing Code
• Write unit tests for this function using
[unittest/PyTest/Jest].
• Generate test cases for this function: [paste
function].
• Create a set of edge cases to test this algorithm.
• How do I mock [API call/database query] in a test?"
A Simple Introduction to Prompt Engineering

More Related Content

PPTX
Prompt Engineering Guide.pptx
PPTX
Burp suite
PDF
An Introduction to Generative AI
PPTX
Learn Prompt Engineering Basics chatgpt llms guide
PPTX
Ethics, Roles, and Responsibilities of Entrepreneur (PPT)
PPTX
Microsoft Security Development Lifecycle
PPTX
Introduction to ChatGPT
PPTX
SQL Injection Introduction and Prevention
Prompt Engineering Guide.pptx
Burp suite
An Introduction to Generative AI
Learn Prompt Engineering Basics chatgpt llms guide
Ethics, Roles, and Responsibilities of Entrepreneur (PPT)
Microsoft Security Development Lifecycle
Introduction to ChatGPT
SQL Injection Introduction and Prevention

What's hot (20)

PDF
Prompt-Engineering-Lecture-Elvis learn prompt engineering
PPTX
Machine Learning and Artificial Intelligence
PDF
What is chat gpt
PPTX
Introduction to data science.pptx
PDF
Prompt Engineering - an Art, a Science, or your next Job Title?
PPTX
GPT, LLM, RAG, and RAG in Action: Understanding the Future of AI-Powered Info...
PPTX
Copilot to Cover: Why AI can't replace developers with robots, but can make l...
PDF
An Introduction to Generative AI - May 18, 2023
PPTX
ChatGPT 101.pptx
PDF
generative-ai-fundamentals and Large language models
PDF
GENERATIVE AI, THE FUTURE OF PRODUCTIVITY
PDF
And then there were ... Large Language Models
PDF
Large Language Models Bootcamp
PPTX
ChatGPT ppt.pptx
PPTX
How to fine-tune and develop your own large language model.pptx
PPT
Cognitive Science and AI
PPTX
Blueprint ChatGPT Lunch & Learn
PDF
Chat GPT Intoduction.pdf
PDF
Uses of AI text bot.pdf
PPTX
Artificial Intelligence
Prompt-Engineering-Lecture-Elvis learn prompt engineering
Machine Learning and Artificial Intelligence
What is chat gpt
Introduction to data science.pptx
Prompt Engineering - an Art, a Science, or your next Job Title?
GPT, LLM, RAG, and RAG in Action: Understanding the Future of AI-Powered Info...
Copilot to Cover: Why AI can't replace developers with robots, but can make l...
An Introduction to Generative AI - May 18, 2023
ChatGPT 101.pptx
generative-ai-fundamentals and Large language models
GENERATIVE AI, THE FUTURE OF PRODUCTIVITY
And then there were ... Large Language Models
Large Language Models Bootcamp
ChatGPT ppt.pptx
How to fine-tune and develop your own large language model.pptx
Cognitive Science and AI
Blueprint ChatGPT Lunch & Learn
Chat GPT Intoduction.pdf
Uses of AI text bot.pdf
Artificial Intelligence
Ad

Similar to A Simple Introduction to Prompt Engineering (20)

PPTX
Introduction to Prompts and Prompt Engineering
PPTX
Prompt_Engineering_Presentation1234.pptx
PPTX
Clean code
PPTX
Principled And Clean Coding
PPTX
Writing Clean Code (Recommendations by Robert Martin)
KEY
Testing gone-right
PPTX
An Overview of automated testing (1)
PPTX
Design p atterns
PDF
Agile experiments in Machine Learning with F#
PDF
Unit Testing Best Practices
PPTX
Agile Experiments in Machine Learning
PPTX
Grails Spock Testing
PPTX
Episode 5 - Writing unit tests in Salesforce
PPT
c-coding-standards-and-best-programming-practices.ppt
PPTX
Cracking the coding interview columbia - march 23 2011
PDF
Embracing a Taxonomy of Types to Simplify Machine Learning with Leah McGuire
PDF
Introduction to Machine Learning for Oracle Database Professionals
PDF
Tackling Testing Telephony
PPTX
Code reviews
PPTX
Improve Code Readability and Understanding Facilitate Code Reusability Across...
Introduction to Prompts and Prompt Engineering
Prompt_Engineering_Presentation1234.pptx
Clean code
Principled And Clean Coding
Writing Clean Code (Recommendations by Robert Martin)
Testing gone-right
An Overview of automated testing (1)
Design p atterns
Agile experiments in Machine Learning with F#
Unit Testing Best Practices
Agile Experiments in Machine Learning
Grails Spock Testing
Episode 5 - Writing unit tests in Salesforce
c-coding-standards-and-best-programming-practices.ppt
Cracking the coding interview columbia - march 23 2011
Embracing a Taxonomy of Types to Simplify Machine Learning with Leah McGuire
Introduction to Machine Learning for Oracle Database Professionals
Tackling Testing Telephony
Code reviews
Improve Code Readability and Understanding Facilitate Code Reusability Across...
Ad

More from Damian T. Gordon (20)

PPTX
Introduction to Vibe Coding and Vibe Engineering
PPTX
TRIZ: Theory of Inventive Problem Solving
PPTX
Some Ethical Considerations of AI and GenAI
PPTX
Some Common Errors that Generative AI Produces
PPTX
The Use of Data and Datasets in Data Science
PPTX
A History of Different Versions of Microsoft Windows
PPTX
Writing an Abstract: A Question-based Approach
PPTX
Using GenAI for Universal Design for Learning
DOC
A CheckSheet for Inclusive Software Design
PPTX
A History of Versions of the Apple MacOS
PPTX
68 Ways that Data Science and AI can help address the UN Sustainability Goals
PPTX
Copyright and Creative Commons Considerations
PPTX
Exam Preparation: Some Ideas and Suggestions
PPTX
Studying and Notetaking: Some Suggestions
PPTX
The Growth Mindset: Explanations and Activities
PPTX
Hyperparameter Tuning in Neural Networks
PPTX
Early 20th Century Modern Art: Movements and Artists
PPTX
An Introduction to Generative Artificial Intelligence
PPTX
An Introduction to Green Computing with a fun quiz.
PPTX
Introduction to Sustainability and the UN Sustainable Development Goals
Introduction to Vibe Coding and Vibe Engineering
TRIZ: Theory of Inventive Problem Solving
Some Ethical Considerations of AI and GenAI
Some Common Errors that Generative AI Produces
The Use of Data and Datasets in Data Science
A History of Different Versions of Microsoft Windows
Writing an Abstract: A Question-based Approach
Using GenAI for Universal Design for Learning
A CheckSheet for Inclusive Software Design
A History of Versions of the Apple MacOS
68 Ways that Data Science and AI can help address the UN Sustainability Goals
Copyright and Creative Commons Considerations
Exam Preparation: Some Ideas and Suggestions
Studying and Notetaking: Some Suggestions
The Growth Mindset: Explanations and Activities
Hyperparameter Tuning in Neural Networks
Early 20th Century Modern Art: Movements and Artists
An Introduction to Generative Artificial Intelligence
An Introduction to Green Computing with a fun quiz.
Introduction to Sustainability and the UN Sustainable Development Goals

Recently uploaded (20)

PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
Introduction to Building Materials
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
1_English_Language_Set_2.pdf probationary
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Lesson notes of climatology university.
PDF
IGGE1 Understanding the Self1234567891011
PDF
advance database management system book.pdf
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Indian roads congress 037 - 2012 Flexible pavement
Hazard Identification & Risk Assessment .pdf
Introduction to Building Materials
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
A systematic review of self-coping strategies used by university students to ...
1_English_Language_Set_2.pdf probationary
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Computing-Curriculum for Schools in Ghana
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
LDMMIA Reiki Yoga Finals Review Spring Summer
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Weekly quiz Compilation Jan -July 25.pdf
Paper A Mock Exam 9_ Attempt review.pdf.
Lesson notes of climatology university.
IGGE1 Understanding the Self1234567891011
advance database management system book.pdf
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين

A Simple Introduction to Prompt Engineering

  • 2. Prompt Engineering • Prompt engineering is designing and refining prompts to effectively communicate with AI models, improving quality, relevance, and accuracy of their responses.
  • 4. Prompt Engineering •Prompt Engineering can … • increase the accuracy and relevance of answers. • help control the tone, style, and format of answers. • enhance the creativity and efficiency of answers.
  • 5. Prompt Engineering •It is important to note that different AI models interpret prompts differently (ChatGPT, DALL·E, Copilot, etc.), so the most important thing to do is try out different queries and different approaches to generating queries, and see which ones work best.
  • 6. Prompt Engineering • Ask a question • Be specific PROMP T • Review the answer • Try to verify PROBE • Add more Context • Add constraints POLISH
  • 9. What is an AI Hallucination? •An AI hallucination occurs when an AI generates false, misleading, or nonsensical information that appears plausible but is not based on real data or facts. •Essentially, the AI "makes things up" because it lacks the right knowledge or is overconfident in its response.
  • 10. What is an AI Hallucination? •For Example: • Who will win the 2044 Formula 1 World Championship? •Possible AI Answer: • The 2044 Formula 1 World Champion will be Max Verstappen, winning 10 races.
  • 11. What is an AI Hallucination? • Why Do AI Hallucinations Happen? • Lack of Real Data: AI fills gaps with plausible but incorrect information. • Pattern Matching Without Understanding: AI generates text based on statistical probabilities, not true comprehension. • Overconfidence in Its Own Output: AI doesn’t always indicate uncertainty when unsure. • Ambiguous or Poorly Defined Prompts: If a prompt lacks context, AI guesses instead of asking for clarification. • Random Answers: Sometimes the AI randomly generates incorrect answers, even if it has the correct answer.
  • 12. What is an AI Hallucination? •How to Reduce AI Hallucinations? • Be Specific in Your Prompts: Ask for explanations, examples, or step-by-step logic. • Use Step-by-Step Prompting: Force AI to explain reasoning before giving answers. • Verify with External Sources: Cross-check AI output using real-world references. • Use Web Searches for Real-Time Data: Prevent outdated or fabricated facts.
  • 14. What is an Edge case? •Edge cases are extreme, uncommon, or boundary cases. •Edge cases help identify potential bugs, inefficiencies, or unexpected behaviors that might not appear in normal conditions.
  • 16. What is an Edge case? • For example, checking if a string is a palindrome. •Normal case: • "racecar" True → •Edge cases: • Empty string "" Should return True? → • Single character "a" Should return True? → • Spaces and punctuation "A man, a plan, a canal, Panama!" • Non-alphabetic input "12321"
  • 18. Prompt Engineering •Principles of Effective Prompting: •Clarity & Precision •Context & Constraints •Iteration & Testing •Role-based Prompts •Few-shot & Zero-shot Prompting •Chain-of-Thought Prompting
  • 20. Clarity & Precision •Using clear and specific language to guide AI responses. •Let’s say you want an AI to generate a Python function to calculate the factorial of a number >>>
  • 21. Clarity & Precision •Bad Prompt - Vague & Ambiguous • Write a function for factorial. •Issues • Doesn’t say which programming language to use. • Unclear if recursion or iteration is preferred. • No details about input validation.
  • 22. Clarity & Precision • Good Prompt - Clear & Precise • Write a Python function called factorial that takes a non-negative integer n as input and returns its factorial. Implement it using recursion, and raise a ValueError if n is negative. • Why Better? • Specifies language (Python). • Defines function name (factorial). • Clarifies input constraints (non-negative integer). • Specifies methodology (recursion). • Includes error handling (raise ValueError).
  • 24. Context & Constraints •Providing background information or setting boundaries to get relevant answers. •Let’s say you want to generate a function to sort a list of numbers >>>
  • 25. Context & Constraints • Bad Prompt - Lacks Context & Constraints • Write a function to sort a list. • Issues • Which programming language? • What kind of sorting algorithm? • Should it be ascending or descending? • Should it handle duplicates or special cases (like empty lists)? • Should it be optimized for performance (e.g., large datasets)?
  • 26. Context & Constraints • Good Prompt - Provides Context & Constraints • Write a Python function called custom_sort that takes a list of integers and sorts them in ascending order using the Merge Sort algorithm. The function should be efficient for large lists and should preserve duplicate values. If an empty list is given, return an empty list. • Why Better? • Context: Python, sorting integers. • Constraints: Must use Merge Sort, handle large lists, preserve duplicates, and return an empty list when given one.
  • 28. Iteration & Testing •Experimenting with different phrasings and formats to achieve the best output. •Let’s say we want to generate a function that checks if a string is a palindrome (reads the same forward and backward) >>>
  • 29. Iteration & Testing • Iteration 1 – A bit vague • Write a Python function to check if a string is a palindrome. • Issues • Doesn’t specify case sensitivity (should "Racecar" be considered a palindrome?). • Doesn’t mention whitespace (should "nurses run" be valid?). • No requirement for handling non-alphabetic characters (like punctuation).
  • 30. Iteration & Testing •Iteration 2 – Adding Constraints • Write a Python function is_palindrome that checks if a string is a palindrome, ignoring case and whitespace. It should return True for palindromes and False otherwise. •Issues • Fixes case sensitivity and whitespace handling • Still doesn’t handle punctuation like "Madam, I'm Adam."
  • 31. Iteration & Testing •Iteration 3 – Final Refinement • Write a Python function is_palindrome that checks if a string is a palindrome, ignoring case, whitespace, and punctuation. Use Python’s re module to remove non-alphanumeric characters. •Why Better? • Optimized & Robust • Now handles case, whitespace, and punctuation correctly!
  • 32. Iteration & Testing •Methodology • Start with a basic prompt and analyze the AI’s output. • Iterate by adding constraints based on shortcomings. • Test edge cases to verify correctness. • Refine until the AI’s output meets all requirements.
  • 34. Role-based Prompts • Asking the AI to act as a specific expert or persona. • This helps generate more accurate and more context- aware responses. By assigning a role, you guide the AI's thought process and ensure it tailors responses to the expected expertise level. • For example, role-based prompt for Code Reviewer >>>
  • 35. Role-based Prompts •Prompt Without a Role - Generic Output • Explain this Python function. •Issues • The AI doesn’t know who the explanation is for (Beginner? Expert? Developer?) • Might provide a generic, surface-level response
  • 36. Role-based Prompts • Role-Based Prompt - More Focused & Useful • You are a senior Python developer reviewing code for a junior colleague. Explain the following function with clear comments and suggestions for improvement. • Why better? • Provides comments and docstrings for clarity. • Offers constructive feedback (input validation, error handling). • Uses a mentor-like tone, making it useful for junior developers.
  • 38. Few-shot & Zero-shot Prompting •Providing examples (few-shot) or asking the AI to generate responses with no prior examples (zero-shot). Prompt Type Explanation Few-shot Prompting The AI is given one or more examples to learn from before generating an answer. Zero-shot Prompting The AI is given no examples and must infer the pattern or solution.
  • 39. Few-shot & Zero-shot Prompting •Zero-shot Prompt - No Examples Given: • Write a Python function that converts Fahrenheit to Celsius. •Issues • No request for docstring or explanation. • No request for input validation or edge case handling
  • 40. Few-shot & Zero-shot Prompting • Few-shot Prompt - Providing Example to Guide AI: Here are some temperature conversion functions … [SAMPLE CODE] … Now, write a function to convert Fahrenheit to Celsius following the same style." • Why better? • More structured & consistent. • AI follows the given style. • Consistent formatting and readability. • Encourages AI to infer patterns from examples.
  • 41. Few-shot & Zero-shot Prompting • When to use each: • Zero-shot: Useful for general tasks but may produce inconsistent results. • Few-shot: Helps guide the AI by showing examples, improving accuracy and structure. So, use few-shot prompting when you need consistency, style matching, or complex logic.
  • 43. Chain-of-Thought Prompting •Also called: •Step-by-Step Instructions •Step-by-Step Prompting •Multi-Turn Prompting
  • 44. Chain-of-Thought Prompting •Encouraging step-by-step reasoning for complex tasks. •This helps improve accuracy and ensures logical consistency, especially for mathematical or algorithmic tasks. •For example, we have a function that finds the sum of all even numbers in a list, but it’s giving incorrect results >>>
  • 45. Chain-of-Thought Prompting •Normal Prompt • Find the bug in this Python function. •Issues • Sometimes, the AI will fix the error but doesn’t explain why, making it harder for learners to understand.
  • 46. Chain-of-Thought Prompting •Chain-of-Thought Prompt • Analyze this Python function step by step to find any logical errors. Explain each step before fixing the bug. •Issues • Sometimes, the AI will fix the error but doesn’t explain why, making it harder for learners to understand.
  • 47. Chain-of-Thought Prompting •AI Output - With Step-by-Step Reasoning •Step 1: Understand the function's goal. •Step 2: Analyze the features of the code. •Step 3: Identify the fix. •Step 4: Provide the corrected function.
  • 48. Chain-of-Thought Prompting •Why better? • Clear breakdown of the problem before fixing it. • Step-by-step reasoning helps with learning and debugging. • Reduces AI hallucination by enforcing structured logical thinking.
  • 50. Writing Code • Write a Python function to [describe functionality] . • Generate a [language] script that does [specific task]. • Create a command-line tool that [functionality]. • Write a function to sort a list using [QuickSort, MergeSort, etc.]. • Generate a [Flask/Django/React] application that does [features].
  • 51. Debugging Code • Find and fix bugs in this [language] code: [paste code]. • Why is this function returning the wrong result? [paste code]. • Help me debug this error message: [paste error]. • Explain what this error means and how to fix it: [error message].
  • 52. Optimising Code • Optimize this function for performance: [paste code]. • Rewrite this algorithm to improve time complexity. • Convert this loop into a more efficient list comprehension. • How can I reduce memory usage in this [Python/JavaScript/etc.] function?"
  • 53. Explaining Code • Explain this function step by step: [paste code]. • What does this code do? [paste code]. • Break down this algorithm in simple terms. • Describe how this function works in a way a beginner would understand.
  • 54. Testing Code • Write unit tests for this function using [unittest/PyTest/Jest]. • Generate test cases for this function: [paste function]. • Create a set of edge cases to test this algorithm. • How do I mock [API call/database query] in a test?"