Using Examples Few Shot Prompting
Using Examples Few Shot Prompting
April 9, 2024
1.1 Setup
Run the following setup cell to load your API key and establish the get_completion helper function.
# Retrieve the API_KEY & MODEL_NAME variables from the IPython store
%store -r API_KEY
%store -r MODEL_NAME
client = anthropic.Anthropic(api_key=API_KEY)
1
1.2 Lesson
Giving Claude examples of how you want it to behave (or how you want it not to
behave) is extremely effective for: - Getting the right answer - Getting the answer in the right
format
This sort of prompting is also called “few shot prompting”. You might also encounter the phrase
“zero-shot” or “n-shot” or “one-shot”. The number of “shots” refers to how many examples are
used within the prompt.
1.2.1 Examples
Pretend you’re a developer trying to build a “parent bot” that responds to questions from kids.
Claude’s default response is quite formal and robotic. This is going to break a child’s heart.
[ ]: # Prompt
PROMPT = "Will Santa bring me presents on Christmas?"
You could take the time to describe your desired tone, but it’s much easier just to give Claude a
few examples of ideal responses.
[ ]: # Prompt
PROMPT = """Please complete the conversation by writing the next line, speaking␣
↪as "A".
In the following formatting example, we could walk Claude step by step through a set of format-
ting instructions on how to extract names and professions and then format them exactly the way
we want, or we could just provide Claude with some correctly-formatted examples and
Claude can extrapolate from there. Note the <individuals> in the assistant turn to start
Claude off on the right foot.
[ ]: # Prompt template with a placeholder for the variable content
PROMPT = """Silvermist Hollow, a charming village, was home to an extraordinary␣
↪group of individuals.
Among them was Dr. Liam Patel, a neurosurgeon who revolutionized surgical␣
↪techniques at the regional medical center.
Olivia Chen was an innovative architect who transformed the village's landscape␣
↪with her sustainable and breathtaking designs.
2
The local theater was graced by the enchanting symphonies of Ethan Kovacs, a␣
↪professionally-trained musician and composer.
These remarkable individuals, each with their distinct talents, contributed to␣
↪the vibrant tapestry of life in Silvermist Hollow.
<individuals>
1. Dr. Liam Patel [NEUROSURGEON]
2. Olivia Chen [ARCHITECT]
3. Ethan Kovacs [MISICIAN AND COMPOSER]
4. Isabella Torres [CHEF]
</individuals>
At the heart of the town, Chef Oliver Hamilton has transformed the culinary␣
↪scene with his farm-to-table restaurant, Green Plate. Oliver's dedication to␣
Just down the street, you'll find the Riverside Grove Library, where head␣
↪librarian Elizabeth Chen has worked diligently to create a welcoming and␣
↪inclusive space for all. Her efforts to expand the library's offerings and␣
↪establish reading programs for children have had a significant impact on the␣
As you stroll through the charming town square, you'll be captivated by the␣
↪beautiful murals adorning the walls. These masterpieces are the work of␣
↪renowned artist, Isabella Torres, whose talent for capturing the essence of␣
Riverside Grove's athletic achievements are also worth noting, thanks to former␣
↪Olympic swimmer-turned-coach, Marcus Jenkins. Marcus has used his experience␣
↪and passion to train the town's youth, leading the Riverside Grove Swim Team␣
<individuals>
1. Oliver Hamilton [CHEF]
2. Elizabeth Chen [LIBRARIAN]
3. Isabella Torres [ARTIST]
4. Marcus Jenkins [COACH]
</individuals>
At the town's bustling farmer's market, you'll find Laura Simmons, a passionate␣
↪organic farmer known for her delicious and sustainably grown produce. Her␣
3
In Oak Valley's community center, Kevin Alvarez, a skilled dance instructor,␣
↪has brought the joy of movement to people of all ages. His inclusive dance␣
Through their unique talents and unwavering dedication, Laura, Kevin, and␣
↪Rachel have woven themselves into the fabric of Oak Valley, helping to␣
print("USER TURN:")
print(PROMPT)
print("\nASSISTANT TURN:")
print(PREFILL)
print("\n------------------------------------- Claude's response␣
↪-------------------------------------")
print(get_completion(PROMPT, prefill=PREFILL))
If you would like to experiment with the lesson prompts without changing any content above, scroll
all the way to the bottom of the lesson notebook to visit the Example Playground.
1.3 Exercises
• Exercise 7.1 - Email Formatting via Examples
4
# Prefill for Claude's response
PREFILL = ""
"Can I use my Mixmaster 4000 to mix paint, or is it only meant for mixing␣
↪food?", # (A) Pre-sale question OR (D) Other (please explain)
"I HAVE BEEN WAITING 4 MONTHS FOR MY MONTHLY CHARGES TO END AFTER␣
↪CANCELLING!! WTF IS GOING ON???", # (C) Billing question
"How did I get here I am not good with computer. Halp." # (D) Other␣
↪(please explain)
ANSWERS = [
["B"],
["A","D"],
["C"],
["D"]
]
print("USER TURN")
print(formatted_prompt)
print("\nASSISTANT TURN")
print(PREFILL)
5
print("\n------------------------------------- Claude's response␣
↪-------------------------------------")
print(response)
print("\n------------------------------------------ GRADING␣
↪------------------------------------------")
1.3.2 Congrats!
If you’ve solved all exercises up until this point, you’re ready to move to the next chapter. Happy
prompting!
[ ]: # Prompt
PROMPT = """Please complete the conversation by writing the next line, speaking␣
↪as "A".
6
Among them was Dr. Liam Patel, a neurosurgeon who revolutionized surgical␣
↪techniques at the regional medical center.
Olivia Chen was an innovative architect who transformed the village's landscape␣
↪with her sustainable and breathtaking designs.
The local theater was graced by the enchanting symphonies of Ethan Kovacs, a␣
↪professionally-trained musician and composer.
These remarkable individuals, each with their distinct talents, contributed to␣
↪the vibrant tapestry of life in Silvermist Hollow.
<individuals>
1. Dr. Liam Patel [NEUROSURGEON]
2. Olivia Chen [ARCHITECT]
3. Ethan Kovacs [MISICIAN AND COMPOSER]
4. Isabella Torres [CHEF]
</individuals>
At the heart of the town, Chef Oliver Hamilton has transformed the culinary␣
↪scene with his farm-to-table restaurant, Green Plate. Oliver's dedication to␣
Just down the street, you'll find the Riverside Grove Library, where head␣
↪librarian Elizabeth Chen has worked diligently to create a welcoming and␣
↪inclusive space for all. Her efforts to expand the library's offerings and␣
↪establish reading programs for children have had a significant impact on the␣
As you stroll through the charming town square, you'll be captivated by the␣
↪beautiful murals adorning the walls. These masterpieces are the work of␣
↪renowned artist, Isabella Torres, whose talent for capturing the essence of␣
Riverside Grove's athletic achievements are also worth noting, thanks to former␣
↪Olympic swimmer-turned-coach, Marcus Jenkins. Marcus has used his experience␣
↪and passion to train the town's youth, leading the Riverside Grove Swim Team␣
<individuals>
1. Oliver Hamilton [CHEF]
2. Elizabeth Chen [LIBRARIAN]
3. Isabella Torres [ARTIST]
4. Marcus Jenkins [COACH]
</individuals>
7
At the town's bustling farmer's market, you'll find Laura Simmons, a passionate␣
↪organic farmer known for her delicious and sustainably grown produce. Her␣
Through their unique talents and unwavering dedication, Laura, Kevin, and␣
↪Rachel have woven themselves into the fabric of Oak Valley, helping to␣
print("USER TURN:")
print(PROMPT)
print("\nASSISTANT TURN:")
print(PREFILL)
print("\n------------------------------------- Claude's response␣
↪-------------------------------------")
print(get_completion(PROMPT, prefill=PREFILL))