DSPyDSPy 3.3 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
24 small wins to finish your pathNext lesson

LabeledFewShot: demos straight from your data

LabeledFewShot copies k examples from the training set into every predictor's demos. It makes no model calls, which makes it the baseline to beat.

Example
optimizer = dspy.LabeledFewShot(k=4)
compiled = optimizer.compile(dspy.Predict(Triage), trainset=trainset)
print([demo.ticket for demo in compiled.demos])
print(evaluate(compiled).score)

compile returns a new program with four demos, chosen at random with a fixed seed, so the choice is the same every run. The original Predict is left unchanged. The score went from 25.0 to 75.0.

Example
for k in [0, 4, 8, 12]:
    compiled = dspy.LabeledFewShot(k=k).compile(dspy.Predict(Triage), trainset=trainset)
    print(k, evaluate(compiled).score)

More demos helped here, reaching 87.5 at eight and staying there at twelve, because more training tickets means a closer match for each devset ticket. With a real model the curve usually flattens, and every demo is paid for on every call, so the smallest k that reaches your target is the one to keep.

Try it yourself
  • Pass sample=False to compile to take the first k instead of a random k.
  • Compile the Desk module and print how many demos each predictor got.
  • Evaluate the k=12 program on trainset. Why is that score not worth much?

Little by little, you're building something great.