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"
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.
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?"