|
|
Few-Shot Prompting
Author: Venkata Sudhakar
Few-shot prompting means providing the LLM with a small number of worked examples inside the prompt so it understands the exact pattern you want before it handles your real input. Instead of describing the task in abstract terms and hoping the model interprets it correctly, you show it two or three concrete input-output pairs. The model recognises the pattern from the examples and applies it consistently to new inputs. This is one of the most reliable ways to get consistent, formatted output from an LLM without any fine-tuning. The key is that your examples must be representative of the full range of inputs and must demonstrate exactly the output format you need. If you want the model to return a single word category, every example must show a single word. If you want JSON, every example must show JSON. Inconsistent examples produce inconsistent output. Three well-chosen examples usually outperform ten mediocre ones. Few-shot prompting is especially effective for classification, labelling, extraction, and reformatting tasks. The below example shows an online retailer using few-shot prompting to automatically route incoming customer emails to the right department - refunds, delivery, or product questions - without writing a single rule.
It gives the following output,
Email: Hi, where is my parcel? I ordered 5 days ago and t...
Routed to: DELIVERY
Email: The coffee machine I bought stopped working after on...
Routed to: PRODUCT_ISSUE
Email: Please cancel my order and refund my card, I found ...
Routed to: REFUND
Email: Do you sell replacement filters for the air purifier...
Routed to: PRODUCT_QUESTION
# Without few-shot examples the model might return:
# "This message is about delivery" or "Category: Delivery Issue"
# With examples it returns the exact label format every time
It gives the following output,
Product: Envelopes
Quantity: 200
Unit Price: $0.08
# The model converted "8 cents" to "$0.08" and used the exact
# format shown in the examples - no parsing code needed
Few-shot vs zero-shot: zero-shot (no examples) works when the task is common and the output format does not matter much. Use few-shot when you need a very specific output format, when the task involves domain-specific labels the model might not know, or when you are getting inconsistent results with zero-shot. The sweet spot is usually two to four examples - enough to establish the pattern without consuming too many tokens. Always include at least one example of each output category you expect.
|
|