07 Using Examples Few Shot Prompting
Chapter 7: Using Examples (Few-Shot Prompting)
Setup
Run the following setup cell to load your API key and establish the get_completion helper function.
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.
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.
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.
In the following formatting example, we could walk Claude step by step through a set of formatting 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.
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.
Exercise 7.1 - Email Formatting via Examples
We're going to redo Exercise 6.2, but this time, we're going to edit the PROMPT to use "few-shot" examples of emails + proper classification (and formatting) to get Claude to output the correct answer. We want the last letter of Claude's output to be the letter of the category.
Refer to the comments beside each email in the EMAILS list if you forget which letter category is correct for each email.
Remember that these are the categories for the emails:
- (A) Pre-sale question
- (B) Broken or defective item
- (C) Billing question
- (D) Other (please explain)
❓ If you want a hint, run the cell below!
Still stuck? Run the cell below for an example solution.
Congrats!
If you've solved all exercises up until this point, you're ready to move to the next chapter. Happy prompting!
Example Playground
This is an area for you to experiment freely with the prompt examples shown in this lesson and tweak prompts to see how it may affect Claude's responses.