liveBook · Manning

Rejection sampling

As explained in Building LLM Applications with DSPy

Rejection sampling is a method that accepts candidate samples meeting a criterion and rejects those that do not.

In DSPy bootstrapping, rejection sampling selects training examples whose predictions pass a metric, keeping them as demonstrations and rejecting failures. With an exact-match metric, only examples whose predicted labels match the supplied labels are accepted. section 5.3

The optimizer tests examples in training-set order until it finds enough usable demonstrations; additional rounds can test examples again. A metric can also score program quality, so its role in bootstrapping is distinct from its role in evaluation. section 5.3

Listing 5.6 BootstrapFewShot optimizer
from dspy import BootstrapFewShot

optimizer = BootstrapFewShot(
    metric=validate_answer,
    max_bootstrapped_demos=4,
    max_labeled_demos=6,
    max_rounds=10
)

prog_bootstrap_few_shot = optimizer.compile(student=intent_classifier, 
                                             trainset=train_set)
prediction = prog_bootstrap_few_shot(**dev_set[0].inputs())
print(lm.inspect_history(n=1))

Configure BootstrapFewShot with a metric and demonstration limits, then compile it against a training set. section 5.3

How Building LLM Applications with DSPy covers it

How deep it goes
Describes metric-based acceptance, training-set order, and repeated rounds without formalizing the sampling method.
Examples
Uses exact-match intent labels to accept correct predictions and reject incorrect ones.
What you can do after
Apply rejection sampling with DSPy's BootstrapFewShot to compile a program using accepted training examples as demonstrations.
How it connects
Connects rejection sampling to prompt optimization, bootstrapping, and metric functions.

Also explained in

Compare how 3 books explain rejection sampling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.