No history yet

Few-Shot Engineering

Guiding with Examples

While zero-shot prompts rely on an LLM's existing knowledge, few-shot prompts give it a cheat sheet. This technique involves providing the model with several examples of a task directly within the prompt. By showing it what you want, you guide its behavior, style, and output format without the need to retrain or fine-tune the model itself.

This is possible because of a principle called in-context learning, where the model uses the provided examples as a temporary instruction manual for the current task. It recognizes the pattern in your demonstrations—the relationship between the input and the desired output—and applies that pattern to your new query. It's like teaching someone a new card game by playing a few hands with them first.

Few-shot prompting is a powerful way to steer model behavior for specific tasks, from data classification to adopting a unique writing style.

Choosing the Right Demonstrations

The quality of your output depends heavily on the quality of your examples. Random or poorly chosen demonstrations can confuse the model and lead to worse results than a simple zero-shot prompt. When selecting examples, focus on three key criteria: diversity, clarity, and relevance.

Your examples should cover a representative range of the inputs you expect to handle. If you're building a sentiment classifier, include positive, negative, and neutral examples. If you're extracting data, show variations in sentence structure. The goal is to give the model a well-rounded understanding of the task's scope.

# Task: Classify customer feedback as Positive, Negative, or Neutral.

# --- POOR EXAMPLES (Lacks Diversity) ---
# Input: The service was amazing!
# Output: Positive
# Input: I loved the product.
# Output: Positive
# Input: Great experience.
# Output: Positive

# --- GOOD EXAMPLES (Diverse & Clear) ---
# Input: The service was amazing and the food was delicious!
# Output: Positive
# Input: The wait time was too long and the staff seemed disorganized.
# Output: Negative
# Input: The restaurant is located downtown.
# Output: Neutral

Structure and Formatting

How you structure your prompt is just as important as the examples you choose. Consistency is key. Use clear labels for your inputs and outputs, like Input: and Output:, or Text: and Sentiment:. This helps the model distinguish between the different parts of your examples.

It's also crucial to use to create a clear separation between your instructions, your block of examples, and the final query you want the model to answer. Simple markers like --- or ### are effective. This prevents the model from getting confused about where the demonstrations end and the real task begins.

The order of your examples can also influence the outcome. Some models exhibit a , paying more attention to the last few examples they see. It's often best to experiment with the order. Try placing your most complex or important example last, or simply randomize the order to see what works best for your specific use case.

# The overall instruction for the task.
Extract the name and job title from the following text.

### EXAMPLES ###

# Example 1
Text: "After the presentation, Sarah, the lead engineer, took questions."
Name: Sarah
Title: lead engineer

# Example 2
Text: "We spoke with the project manager, David Chen, about the timeline."
Name: David Chen
Title: project manager

### END EXAMPLES ###

# The final query for the model to process.
Text: "The report was signed by Maria Garcia, our head of research."

Putting It into Practice

Let's apply this to a creative writing task. Suppose you want the model to write in the style of a terse, 1950s detective novel. You can teach it the style with a few examples.

Prompt: Rewrite the following sentences in the style of a hardboiled detective.


Original: It was a beautiful, sunny day and everyone was happy. Detective: The sun beat down on the pavement like a cheap hood. Nobody was smiling.

Original: The office was modern, with an open floor plan and lots of natural light. Detective: The office was a glass box full of smiling skeletons. Sunlight stabbed through the blinds, but it didn't make the place any warmer.

Original: I was nervous when I entered the room. Detective:

The model, having seen the pattern, would likely complete the final entry with something like: "A cold knot tightened in my gut. I pushed the door open anyway."

This technique allows you to create highly specialized outputs without needing a custom model. You simply demonstrate the pattern you want, and the LLM follows your lead.

Ready to test your knowledge? Let's see what you've learned about few-shot prompting.

Quiz Questions 1/6

What is the primary purpose of using a few-shot prompt instead of a zero-shot prompt?

Quiz Questions 2/6

The principle that allows LLMs to learn from examples provided directly in the prompt is called:

By providing clear, diverse, and well-structured examples, you can guide an LLM to perform complex and nuanced tasks with impressive accuracy.