Imp.Optimizer.LabeledFewShot (Imp v0.5.0)

Copy Markdown View Source

Compile a program by attaching labeled examples as demonstrations.

This optimizer does not call the language model or score candidates. It is the deterministic few-shot baseline: select up to k examples from the trainset for every exposed predictor and attach them as demonstrations. Its report therefore has best_score: nil; scores used to admit the compiled program belong to the surrounding evaluation or Imp.Experiment.Result.

k applies independently to each predictor. Report candidate_count and metadata.selected_assignment_count count predictor-example assignments, so a two-predictor program with k: 1 can report two selected assignments. The selected_by_predictor map gives the corresponding per-predictor counts.

A trainset example may contain the union of fields needed by a composed program. Each predictor's adapter renders only the input and output fields in that predictor's own signature; unrelated fields are ignored.

Selection behavior and determinism

Like DSPy 3.2.1, the defaults are k: 16, sample: true, and seed zero. Repeated compiles therefore select the same no-replacement sample. Set sample: false for DSPy's ordered first-k path. Multi-predictor programs receive separate draws from one advancing RNG stream, matching the upstream predictor traversal contract.

Imp exposes seed: as a BEAM-native extension and uses its explicit, serializable optimizer RNG instead of Python's process-local random.Random. This preserves deterministic sampling semantics and checkpoint-friendly state, but it does not promise Python's incidental exact subset ordering for the same integer seed.

Summary

Functions

Builds the optimizer.

Functions

new(opts \\ [])

Builds the optimizer.

Options:

  • :k — maximum demonstrations per predictor (default 16)
  • :sample — sample without replacement when true; take first k when false (default true)
  • :seed — deterministic BEAM optimizer seed (default 0)