Principle 1: Write Clear and Specific Instructions#
Provide instructions that are as clear and specific as possible.
Strategy 1: Use delimiters to separate different parts of the input information.
Delimiters can be: ```, "", <>, <tag>, <\tag>, or any other marker. Separate specific text parts from the rest.
Avoid prompt injection: Prompt injection refers to the situation where if a user adds certain inputs to the prompt, it may provide conflicting instructions to the model, causing it to follow conflicting instructions instead of executing what you want. That is, the input may contain other instructions that override your instructions.
Example:
text = f"""
You should provide instructions that are as clear and specific as possible to express the task you want the model to perform.\
This will guide the model towards the desired output and reduce the likelihood of receiving irrelevant or incorrect responses.\
Do not confuse writing clear prompts with writing brief prompts.\
In many cases, longer prompts can provide the model with more clarity and context, leading to more detailed and relevant outputs.
"""
# Text content to be summarized
prompt = f"""
Summarize the text enclosed in triple backticks into one sentence.
```{text}```
"""
Output:
Provide clear and specific instructions, avoid irrelevant or incorrect responses, do not confuse writing clear with writing brief, longer prompts can provide more clarity and context, leading to more detailed and relevant outputs.
Strategy 2: Request structured output
Generate a structured output, such as JSON or HTML formatted data, which is easier for code to parse. For example, in Python, JSON data can be easily handled, such as reading it into a dictionary or list.
Example:
prompt = f"""
Please generate a list of three fictional books, including the title, author, and genre,\
and provide it in JSON format, containing the following keys: book_id, title, author, genre.
"""
Output:
{
"books": [
{
"book_id": 1,
"title": "The Shadow of the Wind",
"author": "Carlos Ruiz Zafón",
"genre": "Mystery"
},
{
"book_id": 2,
"title": "The Name of the Wind",
"author": "Patrick Rothfuss",
"genre": "Fantasy"
},
{
"book_id": 3,
"title": "The Hitchhiker's Guide to the Galaxy",
"author": "Douglas Adams",
"genre": "Science Fiction"
}
]
}
Strategy 3: Require the model to check if conditions are met
Have the model first check the assumptions made by the task, and if not met, indicate and stop execution.
Tell the model how to handle potential edge cases to avoid unexpected errors or results.
Example:
prompt = f"""
You will receive text enclosed in triple quotes.\
If it contains a series of instructions, rewrite those instructions in the following format:
Step 1 - ...
Step 2 - …
…
Step N - …
If the text does not contain a series of instructions, write “No steps provided” directly."
text_1 = f"""
Making a cup of tea is easy. First, you need to boil water.\
While waiting, grab a cup and place the tea bag inside.\
Once the water is hot enough, pour it over the tea bag.\
Wait a moment for the tea to steep. After a few minutes, remove the tea bag.\
If you like, you can add some sugar or milk for flavor.\
And there you have it, you can enjoy a delicious cup of tea.
"""
Output for text_1:
Step 1 - Boil the water.
Step 2 - Grab a cup and place the tea bag inside.
Step 3 - Pour the boiled water over the tea bag.
Step 4 - Wait a few minutes for the tea to steep.
Step 5 - Remove the tea bag.
text_2 = f"""
Today is sunny, and the birds are singing.\
It's a beautiful day to take a walk in the park.\
Flowers are blooming, and branches sway gently in the breeze.\
People are out enjoying the lovely weather, some having picnics, others playing games or relaxing on the grass.\
It's a perfect day to spend outdoors and appreciate the beauty of nature.
"""
Output for text_2:
No steps provided
Strategy 4: Provide a few examples
Before asking the model to perform an actual task, provide it with a few examples of successfully executed tasks.
Example: The task is to ask the model to answer questions in a consistent style and first give it an example of a conversation between a child and a grandparent.
prompt = f"""
Your task is to answer questions in a consistent style.
<Child>: Teach me patience.
<Grandparent>: The deepest rivers originate from an inconspicuous spring; the grandest symphonies begin with a single note; the most intricate tapestries start with a solitary thread.
<Child>: Teach me resilience.
"""
Output:
<Grandparent>: Resilience is like a tree; it needs to endure the winds and rains, the sun and drought, to grow stronger. So when you face setbacks and difficulties, don’t give up easily; keep going, believe in your abilities and potential, and eventually, you will become a tall tree.
Principle 2: Give the model time to think#
If the model rushes to conclusions and makes reasoning errors, try redesigning the question to require the model to engage in a series of relevant reasoning before providing an answer.
If the model is given a task that is too complex, it may not be able to complete it in a short time or with few words, leading to an incorrect guess. This is similar for humans; if a person is asked to solve a complex math problem without time to calculate the answer, they may also make mistakes. Therefore, prompting the model to take more time to think about the problem means it spends more computational resources on the task.
Strategy 1: Specify the steps needed to complete the task
First, present a complex task, then provide a series of steps to complete that task.
For example:
We describe the story of Jack and Jill and give an instruction that performs the following: First, summarize the text in one sentence. Second, translate the summary into French. Third, list each name in the French summary. Fourth, output a JSON object containing the following keys: French_summary and num_names.
text = f"""
In a charming village, siblings Jack and Jill set out to fetch water from a mountaintop well.\
They sang joyful songs as they climbed,\
but misfortune struck—Jack tripped on a stone and rolled down the mountain, followed closely by Jill.\
Though slightly injured, they returned to their cozy home.\
Despite the mishap, their adventurous spirit remained undiminished as they continued to explore joyfully.
"""
prompt_1 = f"""
Perform the following actions:
1- Summarize the text enclosed in triple backticks in one sentence.
2- Translate the summary into French.
3- List each name in the French summary.
4- Output a JSON object containing the following keys: French_summary and num_names.
Please separate your answers with newline characters.
Text:
```{text}```
"""
# Output received
1- The siblings had an accident while fetching water from the mountaintop well but maintained their adventurous spirit.
2- Dans un charmant village, les frère et sœur Jack et Jill partent chercher de l'eau dans un puits au sommet de la montagne. Malheureusement, Jack trébuche sur une pierre et tombe de la montagne, suivi de près par Jill. Bien qu'ils soient légèrement blessés, ils retournent chez eux chaleureusement. Malgré cet accident, leur esprit d'aventure ne diminue pas et ils continuent à explorer joyeusement.
3- Jack, Jill
4- {
"French_summary": "Dans un charmant village, les frère et sœur Jack et Jill partent chercher de l'eau dans un puits au sommet de la montagne. Malheureusement, Jack trébuche sur une pierre et tombe de la montagne, suivi de près par Jill. Bien qu'ils soient légèrement blessés, ils retournent chez eux chaleureusement. Malgré cet accident, leur esprit d'aventure ne diminue pas et ils continuent à explorer joyeusement.",
"num_names": 2
}
# Specify output format
prompt_2 = f"""
Your task is to perform the following actions:
1- Summarize the text enclosed in <>.
2- Translate the summary into English.
3- List each name in the English summary.
4- Output a JSON object containing the following keys: English_summary and num_names.
Please use the following format:
Text: <text to summarize>
Summary: <summary>
Translation: <translation of the summary>
Names: <list of names in the English summary>
Output JSON: <JSON with English_summary and num_names>
Text: <{text}>
"""
# Output received
Summary: Siblings Jack and Jill have an adventure in a charming village, returning home after an unfortunate fall, but still filled with adventurous spirit.
Translation: In a charming village, siblings Jack and Jill set out to fetch water from a mountaintop well. While climbing and singing, Jack trips on a stone and tumbles down the mountain, with Jill following closely behind. Despite some bruises, they make it back home safely. Their adventurous spirit remains undiminished as they continue to explore with joy.
Names: Jack, Jill
Output JSON: {"English_summary": "In a charming village, siblings Jack and Jill set out to fetch water from a mountaintop well. While climbing and singing, Jack trips on a stone and tumbles down the mountain, with Jill following closely behind. Despite some bruises, they make it back home safely. Their adventurous spirit remains undiminished as they continue to explore with joy.", "num_names": 2}
Strategy 2: Guide the model to find its own solution before concluding
Guiding the model to reason out its own solution before making decisions will yield better results.
Example:
prompt = f"""
Determine whether the student's solution is correct.
Problem:
I am building a solar power station and need help calculating finances.
Land cost is $100/sq ft
I can purchase solar panels at $250/sq ft
I have negotiated a maintenance contract that requires a fixed payment of $100,000 per year, plus an additional $10 per sq ft
As a function of the number of square feet, what is the total cost of operation in the first year?
Student's solution:
Let x be the size of the power station in square feet.
Costs:
Land cost: 100x
Solar panel cost: 250x
Maintenance cost: $100,000 + 100x
Total cost: 100x + 250x + $100,000 + 100x = 450x + $100,000
"""
The model would answer that the solution is correct. In fact, it is incorrect.
This issue can be resolved by guiding the model to first find its own solution, requiring it to solve the problem independently, and then compare its solution with the student's solution to determine if the student's solution is correct. Meanwhile, we provide the required output format. By clarifying the steps, allowing the model more time to think can sometimes yield more accurate results.
prompt = f"""
Please determine whether the student's solution is correct by solving the problem using the following steps:
Steps:
First, solve the problem yourself.
Then compare your solution with the student's solution and assess whether the student's solution is correct. Do not decide whether the student's solution is correct before solving the problem yourself.
Use the following format:
Problem: problem text
Student's solution: student's solution text
Actual solution and steps: actual solution and steps text
Is the student's solution the same as the actual solution: Yes or No
Student's grade: Correct or Incorrect
Problem:
I am building a solar power station and need help calculating finances.
- Land cost is $100/sq ft
- I can purchase solar panels at $250/sq ft
- I have negotiated a maintenance contract that requires a fixed payment of $100,000 per year, plus an additional $10 per sq ft
As a function of the number of square feet, what is the total cost of operation in the first year?
Student's solution:
Let x be the size of the power station in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: $100,000 + 100x
Total cost: 100x + 250x + $100,000 + 100x = 450x + $100,000
Actual solution and steps:
"""
GPT-3.5 Output:
Correct solution and steps:
1. Calculate land cost: $100/sq ft * x sq ft = 100x dollars
2. Calculate solar panel cost: $250/sq ft * x sq ft = 250x dollars
3. Calculate maintenance cost: $100,000 + $10/sq ft * x sq ft = $100,000 + 10x dollars
4. Calculate total cost: 100x dollars + 250x dollars + $100,000 + 10x dollars = 360x + $100,000
Is the student's solution the same as the actual solution: Yes/No (it may output either yes or no)
Student's grade: Correct/Incorrect (it may output either correct or incorrect)
GPT-4 has no issues.
3. Limitations#
False Knowledge: The model occasionally generates information that seems real but is actually fabricated. If the model has been exposed to a large amount of knowledge during training, it does not fully retain the information it has seen, meaning it may not be very clear about the boundaries of its knowledge. This means it may attempt to answer questions about obscure topics and fabricate answers that sound reasonable but are actually incorrect. We refer to these fabricated ideas as hallucinations.
The model may output seemingly very real fabricated knowledge, which can sometimes be dangerous. Therefore, use some of the techniques introduced above to avoid this situation when building applications. Another strategy to reduce hallucinations is to first ask the model to find any relevant citations in the text, then ask it to use those citations to answer questions. This trace-back documentation approach is often very helpful in reducing hallucinations.