Choosing prompt examples by hand is difficult. LabeledFewShot offers a simple starting point: it randomly selects labeled examples from a training set and adds them as demonstrations to a DSPy program’s prompt. It does not search for the best set or test the selected examples against a validation set during compilation. section 5.2 section 5.2.1
Listing 5.3 Using the LabeledFewShot optimizer
from dspy.teleprompt import LabeledFewShot
num_examples = 4
labeled_few_shot_optimizer = LabeledFewShot(k=num_examples)
prog_labeled_few_shot = labeled_few_shot_optimizer.compile(student=intent_classifier,
trainset=train_set)
prediction = prog_labeled_few_shot(**dev_set[0].inputs())
print(prediction)
print(lm.inspect_history(n=1))
print(prog_labeled_few_shot.demos)
Set k to the number of demonstrations, then compile the existing program with a training set. The result is a separate program that can be called like the original; inspect_history() shows its prompt, and .demos shows the selected examples. section 5.2.1
For intent classification, each demonstration pairs an input message with its intent label. Keep training examples available for selection and use separate validation or development examples to judge whether the resulting program improves accuracy. Because selection is random, a larger k does not guarantee a better prompt; more demonstrations also make prompts longer. section 5.2 section 5.2.1 section 5.6.1
Compare against the unoptimized program
With gpt-4o-mini and ChainOfThought, an unoptimized intent classifier scored 89.16% on 1,000 development examples. LabeledFewShot scored 92.35% with 10 prompt examples and 94.18% with 50. These results support trying several k values, not assuming that one random selection will be strongest. section 5.6.1
Use LabeledFewShot when a quick, straightforward demonstration-based baseline is useful. If selecting stronger examples matters, BootstrapFewShotWithRandomSearch tries candidate sets and selects among them; KNN instead selects demonstrations according to each input’s similarity. section 5.2 section 5.7
