03. Introduction to Prompt Engineering

What is Prompt Engineering?

Prompt engineering is the process of crafting and refining prompts to improve the performance of generative AI models.
When an AI model doesn't produce the desired response, prompt engineering is used to iterate and adjust the prompt to optimize the output.

Why Prompt Engineering is Important?

Generative AI models rely heavily on the input (or "prompt") provided by users.
Prompt engineering is important because:
o It bridges the gap between vague, general queries
and specific, actionable results.
o It helps mitigate errors, such as generating irrelevant content or
incorrect responses.
  • It ensures that the AI can handle various tasks
  • What is a Prompt?

    A prompt is the input or instruction given to an AI model to generate a response.
    Prompts can be simple (a question) or complex (detailed instructions with context, tone, style, and format specifications).
    The quality of the AI's response depends directly on how clear, detailed, and structured the prompt is.
    Here's an example of a simple prompt:
    Explain to me what is prompt engineering
    like I'm 5.
    And here is the response:
    Sure! Imagine you have a toy robot that listens to what you say and does things like answering questions or telling stories. But, this robot doesn't always understand exactly what you want unless you say it in a certain way.
    Prompt engineering is like figuring out the best way to talk to the robot so it does exactly what you want. For example, if you want the robot to tell a funny story about a cat, you might need to say, "Tell me a funny story about a cat," instead of just "Tell me a story."
    In short, prompt engineering is about asking the right questions or giving the right instructions to make sure the robot gives you the best answers!

    Applying Prompt Engineering to Improve the Prompt

    Generative AI models, like ChatGPT, can sometimes produce incorrect or misleading outputs.
    Let's try to refine the prompt and encourage step-by-step
    reasoning to improve model's output:
    Improved Prompt
    What is 923 * 99?
    Let's go step by step.
    Always write out the full number of 0s for each term.
    AI Output
    To multiply 923 by 99, we can break it down into smaller steps:
    Step 1:
    Write out the full number of 0s for each term: 923 * 99 = 923 * 990
    Step 2:
    Multiply the first term by 9: 923 * 9 = 8307
    Step 3:
    Multiply the first term by 90 (adding one 0): 923 * 90 = 83070
    Step 4:
    Add the results from step 2 and step 3: 8307 + 83070 = 91377
    Therefore, 923 multiplied by 99 is equal to 91,377.
    Interestingly, with these additional instructions, ChatGPT provides the correct answer by walking through the steps.
    This process of editing and refining the prompt is what we call Prompt Engineering.

    Why Did This Updated Prompt Work?

    Generative AI models are trained to predict text based on patterns instead of deep reasoning or factual accuracy.
    By prompting the model to explicitly think through its steps and break down the problem, we reduce the chance of mistakes and make the task easier for the model to handle.

    Examples of Prompt Engineering in Action
    (Text, Image, and Code)

    Text


    Initial Prompt
    Write a marketing summary for a new AI tool that helps companies automate tasks.
    The output might be too generic, so you refine it:
    Write a 100-word marketing summary for an AI tool called TaskBot, which automates repetitive tasks for small businesses in industries like retail and healthcare. Highlight efficiency and cost savings.

    Image Generation


    For models like Midjourney or DALL-E, prompts are crafted to generate specific imagery.
    Initial Prompt
    A cat sitting on a chair.
    Refined Prompt
    Generate an image of a tabby cat sitting on a wooden chair in a cozy, sunlit room, with soft shadows and warm lighting.
    By adding more details, you guide the model to generate an image that aligns with your vision.

    Code Generation


    Prompts can help generate or optimize code:
    Initial Prompt
    Write a Python function to multiply two numbers.
    Refined Prompt
    Write a Python function that multiplies two integers and returns the result. Include error handling for cases where inputs are not integers.

    Prompt Engineering for Creative Tasks:

    Writing Tweets
    Next, let's apply prompt engineering in a creative context. Imagine we are developing a fictional AI product called "ArchaeologistAI," which tells stories about famous archaeologists.
    Let's ask ChatGPT to write a marketing tweet about it.
    Write a tweet about ArchaeologistAI.
    AI Output
    πŸ” Exciting times ahead with #ArchaeologistAI!
    This cutting-edge technology is revolutionizing the field of archaeology, helping us uncover ancient mysteries and piece together our past like never before.
    Can't wait to see what hidden treasures and insights it unveils!
    #AI #Archaeology

    Including Relevant Information in the Prompt

    This Tweet is inaccurate because ArchaeologistAI doesn't discover new thingsβ€”it only tells stories. This isn't ChatGPT's fault, as it wasn't given enough context.
    Let's refine the prompt by including more specific details:
    Write a tweet about ArchaeologistAI.
    ArchaeologistAI tells stories about famous archaeologists.
    AI Output
    πŸ” Unearth the captivating tales of legendary archaeologists with #ArchaeologistAI!
    From Indiana Jones to Mary Leakey, this AI-powered storyteller brings their adventures to life, shedding light on their groundbreaking discoveries and inspiring the next generation of explorers.
    🌍 #Archaeology
    Much better now!

    Changing Style of the AI Output with the Prompt

    Now, let's refine it further by asking ChatGPT to write the tweet in the style of Indiana Jones:
    Write a tweet about ArchaeologistAI, in the style of Indiana Jones.
    ArchaeologistAI tells stories about famous archaeologists.
    Grab your fedora and whip, folks! 🎩πŸ”₯
    ArchaeologistAI is like having a personal Sallah by your side, sharing captivating tales of legendary archaeologists who've braved ancient tombs and unearthed hidden treasures! πŸ—ΊοΈπŸ’Ž
    Get ready to embark on a digital adventure through history!
    #ArchaeologistAI #UnearthTheLegends
    This version adds a fun, thematic spin to the message, making it more engaging.

    Conclusion


    Prompt engineering is an iterative process.
    The perfect prompt rarely happens on the first try.
    Adding specificity, providing context, and guiding the model with detailed instructions can significantly improve its responses.
    Next, we'll explore different prompting strategies that will help us get better at crafting prompts for a variety of AI applications.
    @sasban9