🔢 Few-Shot Prompting

What Is It?

Few-shot prompting is a technique used when interacting with large language models. It involves providing the model with a few examples (or “shots”) of the task you want it to perform within your prompt. These examples act as demonstrations that help the model understand what you’re asking for, improving its ability to generate accurate and relevant responses.

Think of it as showing someone a couple of examples before asking them to complete a similar task. By providing these examples, you’re leveraging the model’s ability to learn from context — this is known as in-context learning.

How Does It Work?

Large language models have been trained on vast amounts of text data and are capable of picking up on patterns and structures within human language. When you include a few examples in your prompt, the model analyzes these examples to identify the task’s pattern or format.

Here’s how it operates:

  1. Provide Demonstrations: You include several input-output pairs in your prompt that demonstrate the task.
  2. Model Analyzes Patterns: The model reads these examples and recognizes the relationships between inputs and outputs.
  3. Generate Response: When you present a new input, the model uses the patterns it identified to generate the appropriate response.

By conditioning the model with examples, you’re effectively guiding it toward the desired behavior without needing explicit programming or additional training.

When Do You Use It?

Few-shot prompting is particularly useful in the following scenarios:

  • Complex Tasks: When dealing with tasks that are too complex for zero-shot prompting (asking the model to perform a task without any examples), few-shot prompting can significantly enhance performance.
  • Specific Output Formats: If you need the model to produce responses in a particular format or style, providing examples helps ensure consistency.
  • Uncommon or New Tasks: For tasks that the model might not have encountered frequently during training, such as using newly coined terms or specialized jargon.
  • Avoiding Fine-Tuning: When you lack the resources or data to fine-tune a model, few-shot prompting offers a way to instruct the model on new tasks on the fly.

Practical Example

Let’s explore how few-shot prompting can be applied in both system prompts and user prompts.

In a System Prompt

A system prompt sets the assistant’s behavior throughout the conversation. Including few-shot examples here can establish a consistent response style.

Example:

System Prompt:

You are an assistant that provides polite and formal responses.

Examples:
User: "Give me the weather forecast."
Assistant: "Certainly! Could you please specify your location?"

User: "I need help with my account."
Assistant: "I'd be happy to assist you. May I have your account number, please?"

[End of examples.]

In this very basic setup, the assistant is guided to respond politely and formally, using the examples as a template.

In a User Prompt

Including few-shot examples directly in the user prompt is useful for specific tasks during the conversation.

Example:

User Prompt:

A "blorpt" is a fictional gadget that helps you do work faster. An example of a sentence using the word "blorpt" is:
"I used the blorpt to finish my project ahead of schedule."

Now, use "blorpt" in a sentence about cooking.

Assistant’s Response:

“The chef relied on the blorpt to prepare meals more efficiently.”

In this case, the user provides an example of how to use the new word “blorpt,” and then asks the model to create a new sentence using that word in a different context.

Additional Example: Sentiment Analysis

User Prompt:

Classify the sentiment of the following sentences as Positive or Negative.

Sentence: "This is the best day ever!" // Positive
Sentence: "I can't stand this traffic." // Negative
Sentence: "I'm thrilled with the results." // Positive
Sentence: "This is absolutely terrible." //

Assistant’s Response:

“Negative”

Despite only providing a few examples, the model correctly classifies the sentiment of the new sentence by identifying the pattern in the examples.