While zero-shot prompting relies entirely on the LLM's pre-existing knowledge and instruction-following capabilities, Few-Shot Prompting takes a different approach. It leverages a powerful mechanism within LLMs known as in-context learning. Instead of just telling the model what to do, you also show it how to do it by providing a small number of examples (k examples, where k is typically small, like 1 to 5) directly within the prompt itself.
This technique is particularly effective when:
It's important to understand that few-shot prompting does not update the LLM's underlying parameters (weights). The "learning" happens dynamically within the context window of a single API call. The model analyzes the provided examples and identifies the pattern connecting the sample inputs to the sample outputs. It then applies this inferred pattern to the actual input query that follows the examples.
Think of it like giving someone brief, on-the-spot training for a specific task just before they perform it. They use the examples to understand the immediate requirement, but they don't fundamentally change their long-term knowledge.
A typical few-shot prompt follows a consistent structure:
Text:
, Input:
) + Sample Input 1Sentiment:
, Output:
) + Sample Output 1Consistency in labeling and formatting between examples and the final query is significant for success.
Let's say we want to classify customer feedback into Positive
, Negative
, or Neutral
, but we want to ensure the model uses exactly these labels.
Classify the sentiment of the following customer reviews.
Text: "The battery life on this device is amazing!"
Sentiment: Positive
Text: "The screen scratches too easily."
Sentiment: Negative
Text: "It arrived on time."
Sentiment: Neutral
Text: "Customer support was helpful but slow to respond."
Sentiment:
By providing examples, we guide the model to:
Positive
, Negative
, Neutral
) we expect.The LLM, following the pattern, would likely complete the prompt with Negative
or perhaps Neutral
, depending on its interpretation trained on similar data and guided by the examples.
Few-shot can help guide code generation tasks. Suppose you want to generate simple Python functions based on descriptions.
# Generate a Python function based on the description.
Description: """Adds two numbers."""
Function:
def add(a, b):
"""Adds two numbers."""
return a + b
Description: """Concatenates two strings with a space."""
Function:
def concatenate_strings(s1, s2):
"""Concatenates two strings with a space."""
return s1 + " " + s2
Description: """Calculates the area of a rectangle."""
Function:
The model observes the pattern: take the description, use it as a docstring, define a function with appropriate parameters, and implement the logic. It would likely generate something like:
def rectangle_area(length, width):
"""Calculates the area of a rectangle."""
return length * width
The quality of your examples heavily influences the outcome. Consider these points when selecting them:
Usually, a small number of examples (k=1 to k=5) is sufficient. This is why it's called "few-shot" prompting. Adding too many examples might not improve performance and could exceed the model's context length limit.
The following diagram illustrates the flow within a few-shot prompt designed for translation.
This diagram shows how instruction, examples (Input/Output pairs), and the final query are structured within the prompt sent to the LLM, which then generates the result based on the learned pattern.
Few-shot prompting is a valuable technique in your prompt engineering toolkit, offering a way to guide LLMs effectively for specific tasks and output formats without needing to modify the model itself. It represents a step up in control compared to zero-shot prompting, enabling more complex and nuanced interactions.
© 2025 ApX Machine Learning