🎓 A1Learner
🔍
0 of 50 lessons completed

Prompt Engineering: Few-Shot

Few-shot prompting means showing the model two or three examples before asking it to do the task. The examples teach the format better than a paragraph of rules.

Two examples teach the one-sentence reply

Two short examples set the length and the tone.

You are a support agent for ShopStack. Answer each customer question in one sentence. Example 1. Question: Where is my order 1042? Answer: Order 1042 shipped and is out for delivery. Example 2. Question: How long does a refund take? Answer: Refunds are processed within 3 to 5 business days after the returned item arrives. Question: Is the tote bag big enough for a 13-inch laptop? Answer:

Three examples teach a short product answer

Three product answers set the shape for the next product question.

You are a support agent for ShopStack. Answer each product question in one sentence. Example 1. Question: Is the tote bag big enough for a 13-inch laptop? Answer: Yes, it fits a 13-inch laptop and has an inner pocket. Example 2. Question: What is the difference between the Aurora Mug and the Ember Set? Answer: The Aurora Mug is a blue ceramic mug for 18 dollars, and the Ember Set is a pour-over coffee dripper with 100 paper filters for 35 dollars. Example 3. Question: What does the Willow Linen Cushion cost? Answer: The Willow Linen Cushion costs 29 dollars. Question: How much is the Hearthstone Desk Lamp? Answer:

The same task with no examples

Without examples the model guesses the format.

You are a support agent for ShopStack. Answer this customer question in one sentence. Question: How long does a refund take?

Example

You are a support agent for ShopStack. Answer each customer question in one sentence. Example 1. Question: Where is my order 1042? Answer: Order 1042 shipped and is out for delivery. Example 2. Question: How long does a refund take? Answer: Refunds are processed within 3 to 5 business days after the returned item arrives. Example 3. Question: What is the discount code for first orders? Answer: There is no first-order code. Question: Can I return the cushion? I bought the wrong colour. Answer:

Key points

  • Few-shot prompting puts examples in the prompt before the real task.
  • Two or three examples are usually enough to teach the shape of an answer.
  • Examples show the format, the tone and the length in one glance.
  • A rule says what to do. An example shows what done looks like.
  • Few-shot prompting works best when the answer has a fixed shape, like a one-sentence reply.

This example runs a few-shot prompt against the local model and shows the reply it gave. The three examples set the one-sentence shape, so the new answer follows it.

Note

The examples must be correct. A wrong example teaches the wrong answer.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What do few-shot examples teach the model?