Intro to Prompt in Generative AI
On this course, we will learn how to use Prompt in Generative AI. We will start with the basics and then move on to more advanced topics. We’ll use ChatGPT to use the prompt on, so if you do not have an account, sign up at https://chat.openai.com
What is Prompt in AI Language Models?
A prompt is like a clue given to an AI model. This clue helps the AI know what to talk about. It’s like asking a question or starting a conversation. The AI takes this clue and uses it to come up with a response. The clue can be a question, a sentence, or even just one word. It’s very important because it tells the AI what to say next. It’s the first step that guides the AI’s answer.
Think of an AI language model as a super-smart parrot. This parrot doesn’t know what to say until you give it a hint, which we call a prompt
.
For example, if you say to the parrot,
Tell me a story about a brave knight
The parrot (or AI model) then uses this prompt to create a story about a brave knight.
The prompt is important because it sets the direction for what the AI model will generate.
It’s like giving a topic for an essay to a student. The student uses that topic to write an essay. Similarly, the AI model uses the prompt to generate a response.
So, in simple terms, a prompt is a piece of information or a question that you give to an AI model to guide what it will talk about or generate.
LLM Configuration
When crafting prompts, you’ll either interact with the Large Language Model (LLM) directly or through an API. One of the most important parameter you can adjust to tailor the results of your prompts is:
Temperature - Simply put, a lower temperature
setting makes it less creative, more following a predefined template (deterministic), while a higher temperature
will introduce more randomness, resulting in more diverse or inventive outputs. For instance, you might want to use a lower temperature for tasks like fact-based Q&A to promote more accurate and to the point responses. Conversely, for creative tasks like generating poetry, a higher temperature might be advantageous.
Components of a Prompt
A prompt is a set of instructions that guide an AI model to perform a specific task. It consists of several components, each playing a crucial role in shaping the model’s response.
Let’s consider a complex prompt for a detailed breakdown:
Prompt:
Given the following review of a restaurant, summarize it in one sentence and classify it as positive, negative, or neutral: 'The food was absolutely delicious, and the staff were incredibly friendly. However, the noise level was quite high which made it difficult to have a conversation.
Here are the key components of this prompt:
Instruction
The instruction is the core of the prompt. It directs the AI model on the task it needs to perform. In our example, the instruction is:
“Given the following review of a restaurant, summarize it in one sentence and classify it as positive, negative, or neutral”.
This part of the prompt tells the AI what task it needs to perform, which is to summarize a restaurant review in one sentence and classify the sentiment of the review.
Context
Context provides additional information that aids the AI model in understanding the task better and generating more accurate responses.
In the given prompt, the context is provided in the instruction itself -
“Given the following review of a restaurant, summarize it in one sentence and classify it as positive, negative, or neutral”.
The AI model is informed that the input data is a review of a restaurant. This helps the model understand that it should consider factors like food quality, service, and ambiance in its summary and classification.
Input Data
Input data is the information that the AI model processes to accomplish the task set in the prompt. In our example, the input data is:
“The food was absolutely delicious, and the staff were incredibly friendly. However, the noise level was quite high which made it difficult to have a conversation.”
This is the restaurant review that needs to be summarized and classified.
Output Indicator
The output indicator signals to the AI model that a response is expected. In this case, the output indicator is implicit.
The instruction to “summarize it in one sentence and classify it as positive, negative, or neutral” implies that the AI model is expected to produce a response, which would be the summarized review and its classification.
Prompt Engineering Tips
Creating prompts for AI language models is like giving directions. The better the directions, the better the AI can respond. Here are some tips and examples to help make better prompts:
- Giving Context for Clear and Accurate Responses
- Making a Clear Task for Clear Results
- Creating Characters
- Adding More Context
- Adding Outside Knowledge
- Trying Again and Again for Better Prompts
Giving Context for Clear and Accurate Responses
Context
can be thought of as the setting or background of a conversation. It’s like the stage where the conversation takes place and the theme that it revolves around.
Providing context is like giving a map to the AI model. This map helps the model understand the conversation’s location and topic.
Context
is crucial in any conversation. It acts like a spine, giving the necessary support and direction. When context is added to prompts, it steers the AI model to create responses that make sense and are on point.
Here are some examples of prompts with context:
Example 1
Incorrect Prompt: Tell me about it.
This prompt is unclear and lacks context. The AI model can’t identify what “it” refers to and might produce a response that’s off-topic or irrelevant.
Improved Prompt: Tell me about the implications of climate change on global agriculture.
This prompt gives clear context (climate change and global agriculture), directing the AI model to produce a relevant and coherent response.
Example 2
Incorrect Prompt: Analyze the data.
This prompt is unclear and lacks context. The AI model can’t identify what data to analyze or what kind of analysis is needed.
Improved Prompt: Analyze the sales data from the last quarter using a regression model to predict future trends.
This prompt gives clear context (sales data from the last quarter, regression model, prediction of future trends), directing the AI model to produce a relevant and coherent response.
Making a Clear Task for Clear Results
The relationship between the clarity of a task defined in a prompt and the quality of the results obtained is direct. When task definitions are clear and precise, superior results are achieved. This is because they offer the AI model a specific direction to follow.
Here are some examples of prompts with clear tasks:
Example 1
Incorrect Prompt: Improve this.
This prompt is too ambiguous. The AI model can’t identify what needs enhancement or how to tackle the task.
Improved Prompt: Improve this sentence for clarity: 'The man the professor the student has studies Rome.
The enhanced prompt clearly outlines the task (improving a sentence for clarity) and provides a specific item (the sentence) for the AI model to work on.
Example 2
Incorrect Prompt: Translate this.
Without indicating the source and target languages, the AI model can’t determine how to tackle the task.
Improved Prompt: Translate this English text into French: 'Artificial Intelligence is transforming industries.'
The enhanced prompt clearly outlines the task (translation), the source language (English), the target language (French), and provides a specific item (the sentence) for the AI model to work on.
Creating Characters
Characters represent individuals, creatures, or entities participating in a narrative. They interact with each other and propel the storyline.
The development of personas can aid in producing responses that are more tailored and captivating. Personas can steer the AI model to react in a specific tone or style, rendering the interaction more akin to human conversation.
Here are some examples of prompts with characters:
Example 1
Incorrect Prompt: Write a response to a customer complaint.
This prompt is overly general and doesn’t specify the tone or style of the response.
Improved Prompt: Write a response to a customer complaint as 'Emily', a customer service representative who is empathetic, patient, and solution-oriented.
The refined prompt creates a persona (Emily) with unique characteristics, guiding the AI model to generate a more personalized and engaging response.
Example 2
Incorrect Prompt: Write a blog post about the benefits of exercise.
This prompt doesn’t identify the persona of the writer, which could lead to a generic or impersonal blog post.
Improved Prompt: Write a blog post about the benefits of exercise as 'Dr. Smith', a seasoned fitness trainer with a passion for promoting healthy lifestyles.
The refined prompt creates a persona (Dr. Smith) with unique characteristics, guiding the AI model to generate a more personalized and engaging blog post.
Adding More Context
Adding more context is like giving the AI extra information that it wouldn’t know otherwise. This extra information can be in the form of a setting, a theme, or a character.
Here are some examples of prompts with more context:
Example 1:
Incorrect Prompt: Write a news report.
This prompt is missing specific context, which could result in a generic or irrelevant news report.
Improved Prompt: Write a news report on the recent hurricane in Florida, considering its impact on local communities and the ongoing recovery efforts.
The refined prompt introduces relevant context (recent hurricane in Florida, impact on local communities, ongoing recovery efforts), steering the AI model to generate a more precise and pertinent news report.
Example 2:
Incorrect Prompt: Write a product review.
This prompt is missing specific context, which could result in a generic or irrelevant product review.
Improved Prompt: Write a product review for the latest iPhone model, considering its new features, performance, and comparison with the previous model.
The refined prompt introduces relevant context (latest iPhone model, new features, performance, comparison with the previous model), steering the AI model to generate a more precise and relevant product review.
Adding Outside Knowledge
Adding Outside knowledge into the prompt can assist the AI model in producing responses that are more informed and knowledgeable. This can be especially beneficial when handling subjects that demand expert knowledge or the most current information.
Here are some examples of prompts with outside knowledge:
Example 1:
Incorrect Prompt: Write an article about global warming.
This prompt doesn’t indicate the need for expert knowledge or the most recent information, which could result in a generic or outdated article.
Improved Prompt: Write an article about global warming, incorporating the latest research findings from the IPCC (Intergovernmental Panel on Climate Change) report.
The refined prompt introduces external knowledge (latest research findings from the IPCC report), steering the AI model to generate a more informed and current article.
Example 2:
Incorrect Prompt: Write a report on the current state of the stock market.
This prompt doesn’t indicate the need for expert knowledge or the most recent information, which could result in a generic or outdated report.
Improved Prompt: Write a report on the current state of the stock market, incorporating the latest data from the New York Stock Exchange and expert analysis from leading financial analysts.
The refined prompt introduces external knowledge (latest data from the New York Stock Exchange, expert analysis from leading financial analysts), steering the AI model to generate a more informed and current report.
Trying Again and Again for Better Prompts
The procedure of prompt engineering extends beyond the creation of the initial prompt. It encompasses refining the prompts and assessing the output across multiple iterations. This iterative process aids in understanding how the AI model reacts to various prompts and enables the optimization of prompts for superior results.
Example 1
Initial Prompt: Write a poem.
The AI model could interpret this prompt in numerous ways, leading to a wide variety of responses. While not necessarily incorrect, it could be optimized for better results.
Refined Prompt after Iteration: Write a sonnet about the tranquility of a quiet winter night.
After evaluating the output from the initial prompt, it might become clear that a specific type of poem (a sonnet) about a specific theme (the tranquility of a quiet winter night) is desired. This refined prompt guides the AI model more effectively.
Example 2
Initial Prompt: Summarize the article.
While this prompt isn’t incorrect, it’s quite broad and could be optimized for better results.
Refined Prompt after Iteration: Summarize the key arguments and conclusions from the article on 'The Impact of Artificial Intelligence on Healthcare'.
After evaluating the output from the initial prompt, it might become clear that a summary focusing on key arguments and conclusions is desired. Also, specifying the article provides more context for the AI model. This refined prompt guides the AI model more effectively.
Adhering to these guidelines for prompt engineering can enhance the performance of AI models and yield superior responses. The inclusion of context, clear task definition, iterative refinement of prompts, persona creation, context injection, and external knowledge addition will result in more precise and pertinent responses. This not only saves time but also boosts the overall performance of the AI models.
Exploring Techniques in Prompt Engineering
There are several techniques used in Prompt Engineering, each with its unique approach and application. These techniques include: - Zero-shot Prompting - Few-shot Prompting - Chain-of-Thought Prompting - Generate Knowledge Prompting - Directional Stimulus Prompting - etc.
In this discussion, we will explore a few of these techniques and see how they work in the world of Prompt Engineering. For a more comprehensive understanding of these techniques and to explore others not covered here, you can visit Promptly.
Zero-Shot Prompting
Zero-shot prompting is when we ask a computer program to do a task without giving it any examples beforehand. This is possible with today’s Large Language Models (LLMs) that are trained on lots of data and can follow instructions.
For example, let’s say we ask the program to classify a sentence as neutral, negative, or positive:
Prompt:
"Determine the mood of the following statement:
Text: The concert was absolutely fantastic, and the band was incredible!
Mood:"
Output:
"Excited"
Here, we didn’t give the program any examples, but it still gave the correct answer. This is what we call zero-shot capabilities.
Few-Shot Prompting
Few-shot prompting is like giving a few examples to a computer program before asking it to do a similar task. This helps the program understand what we want it to do.
For example, let’s say we have a new word, “farduddle,” which means to jump up and down really fast. We can show the program how to use this word with an example:
Prompt:
"To do a 'farduddle' means to jump up and down really fast. An example of a sentence that uses the word farduddle is:"
Output:
"When we won the game, we all started to farduddle in celebration."
Here, the program learns how to use the new word correctly with just one example. If the task is harder, we might need to give more examples.
But few-shot prompting doesn’t always work, especially for more complex tasks. For example, if we ask the program to figure out if the sum of odd numbers in a group is even, it might not give the correct answer.
Prompt:
The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: The answer is False.
The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24.
A: The answer is True.
The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24.
A: The answer is True.
The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2.
A: The answer is False.
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:
Output:
The answer is True.
In such cases, we need to use other techniques or fine-tune the program.
Chain-of-Thought (CoT) Prompting
Image Source: Promptly
Chain-of-Thought (CoT) prompting is a technique where we guide a computer program step-by-step through a task. This is especially useful for complex tasks that require some thinking before giving an answer.
For example, let’s say we want the program to figure out if the sum of odd numbers in a group is even. We can guide it through the reasoning process like this:
Prompt:
"The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:"
Output:
"Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False."
Here, the program correctly figures out the answer by following the reasoning steps we provided. This technique can be very powerful, especially when combined with few-shot prompting.
Zero-shot CoT Prompting
Image Source: Promptly
Zero-shot Chain-of-Thought (CoT) prompting is a new technique where we ask a computer program to think step-by-step before giving an answer. This can be done by simply adding “Let’s think step by step” to the original question.
For example, let’s say we ask the program a simple math problem:
Prompt:
"I have a jar with 20 candies. I ate 5 candies in the morning and gave 3 candies to my friend. Later, I bought 10 more candies and put them in the jar. How many candies are in the jar now?"
Output:
"25 candies"
The answer is wrong! Now, let’s try the same problem but ask the program to think step-by-step:
Prompt:
"I have a jar with 20 candies. I ate 5 candies in the morning and gave 3 candies to my friend. Later, I bought 10 more candies and put them in the jar. How many candies are in the jar now? Let's think step by step."
Output:
"First, you started with 20 candies.
You ate 5 candies in the morning, so you had 15 candies left.
Then you gave 3 candies to your friend, so you had 12 candies left.
Finally, you bought 10 more candies, so now you have 22 candies in the jar."
This time, the program gives the correct answer! This technique can be very useful when we don’t have many examples to guide the program.
Generated Knowledge Prompting
Image Source: Promptly
Large Language Models (LLMs) are getting better and better. One way they’re improving is by using extra information or knowledge to make more accurate guesses.
Imagine if we could use these models to create new knowledge before making a guess? That’s what a recent study tried to do. They wanted to see if generating knowledge first could help with tasks like understanding common sense.
Let’s look at an example:
Question:
Do penguins fly like other birds? Yes or No?
A Large Language Model (LLM) might answer:
Answer:
Yes.
This answer is incorrect because penguins are flightless birds. They cannot fly like other birds. This shows that LLMs can sometimes struggle with tasks that require real-world knowledge. So, how can we improve this with knowledge generation?
First, we generate some knowledge about penguins:
Knowledge:
Penguins are a group of flightless birds. They live almost exclusively in the Southern Hemisphere, with only one species, the Galapagos penguin, found north of the equator. Despite their inability to fly, penguins are excellent swimmers.
We then use this knowledge to answer the question again:
Question:
Do penguins fly like other birds? Yes or No?
Knowledge: Penguins are a group of flightless birds. They live almost exclusively in the Southern Hemisphere, with only one species, the Galapagos penguin, found north of the equator. Despite their inability to fly, penguins are excellent swimmers.
Answer:
No, penguins do not fly like other birds. They are a group of flightless birds. Despite their inability to fly, penguins are excellent swimmers.
This time, the answer is correct! This shows that generating knowledge first can help LLMs make better guesses. However, the effectiveness of this technique can vary, and there’s still a lot to learn about how to use it most effectively.
Directional Stimulus Prompting
Large Language Models (LLMs) are continually being enhanced, and one of the techniques that have gained popularity is the use of Directional Stimulus Prompting. This method involves providing the model with a hint or nudge in the right direction to help it make more accurate predictions.
The concept behind Directional Stimulus Prompting is similar to giving a friend a hint when they’re trying to solve a puzzle. You’re not directly telling them the answer, but you’re providing enough information to guide them towards the correct solution.
Image Source: Promptly
Let’s illustrate this with a simple example:
Certainly, let’s imagine you’re asking the model to summarize a news article. Here’s a brief news article example for our situation:
News Article: “Yesterday, the major tech company, TechVerse, released their highly anticipated phone, the UltraPhone X. This latest model features a 6.7-inch display, a powerful Zenith Processor, and an improved camera with advanced Night Vision capabilities. It also boasts a durable battery that promises to last throughout the day. TechVerse expects the UltraPhone X to be a massive hit, building upon the success of previous models. The UltraPhone X will be made available for purchase worldwide next week.”
Now, suppose you want a brief summary of this article. You could use directional stimulus prompting to guide the AI’s summarization process.
Prompt:
Can you provide a brief summary of this article, focusing on the key details such as the features of the new phone and its release date?
This prompt nudges the AI to highlight specific elements in its summary.
So, the model might respond with:
Output:
TechVerse has unveiled their new model, the UltraPhone X, featuring a 6.7-inch display, a potent Zenith Processor, advanced Night Vision capabilities, and a durable battery. The product, following the success of its older versions, is expected to be available for worldwide purchase next week.
The extra prompting hint helped guide the AI to focus on the necessary details in its summarization process.
This example demonstrates how Directional Stimulus Prompting can be used to guide the model towards the correct answer without explicitly providing the answer. It’s a useful technique for tasks where the model might need a little extra help to arrive at the correct prediction.