liveBook · Manning

Chapter 5: Optimizing prompt examples

What you will learn in chapter 5 of Building LLM Applications with DSPy

Introduces DSPy prompt optimization as a data-driven search over candidate prompts and distinguishes optimizing demonstrations from optimizing instructions.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces DSPy prompt optimization as a data-driven search over candidate prompts and distinguishes optimizing demonstrations from optimizing instructions.

Key terms: candidate prompts, metric function, validation dataset, data-driven, demonstrations, instructions, prompt optimization

5.1 General approaches to optimization

This section explains why labeled demonstrations can improve a classification prompt and why optimizing them means evaluating sets of examples rather than choosing appealing examples one at a time.

Key terms: class names, demonstrations, instructions, intent label, examples, demos, general patterns, edge cases, set as a whole, combinations

5.2 The LabeledFewShot optimizer

Introduces LabeledFewShot’s random selection of prompt examples and prepares the intent-classification program and datasets used to assess optimization.

Key terms: LabeledFewShot, training data, demos, training, validation, development, test, exact matches, average score, ClosedIntentSignature

5.2.1 Executing the LabeledFewShot optimizer

This section shows how LabeledFewShot adds randomly selected training examples to an intent classifier’s prompt and how to inspect and test the resulting program.

Key terms: LabeledFewShot, k, compile(), student, training set, validation set, demonstrations, inspect_history(), demos, User

5.2.2 Executing the LabeledFewShot optimizer repeatedly in a loop

This section shows how to search over LabeledFewShot prompts by repeating runs with different numbers of demos and using early stopping to evaluate candidates efficiently.

Key terms: LabeledFewShot, demos, k, random search, training set, early stopping, candidate prompt, validation set, fixed threshold, previously tested prompts

5.2.3 Working with ChainOfThought

This section explains what happens when a ChainOfThought program is optimized using training examples that lack reasoning fields.

Key terms: ChainOfThought, Predict, optimizer, demonstrations, intent label, reasoning, training data, reasoning field, Not supplied for this particular example

5.3 BootstrapFewShot

This section explains how BootstrapFewShot selects demonstrations for a DSPy program, how its metric governs selection, and how to recognize unsuccessful bootstrapping.

Key terms: BootstrapFewShot, multistep modules, intermediate LM calls, bootstrapping, max_labeled_demos, max_bootstrapped_demos, labeled demos, bootstrapped demos, rejection sampling, metric function

5.3.1 Working with ChainOfThought

This section explains how BootstrapFewShot generates and validates ChainOfThought demonstrations, and when bootstrapping may be counterproductive.

Key terms: BootstrapFewShot, ChainOfThought, max_labeled_demos, max_bootstrapped_demos, reasoning field, bootstrapping, validate_answer, intent_label, metric function, prediction

5.3.2 Specifying a teacher

This subsection explains why a stronger teacher LM can be used during bootstrapping and illustrates it with GPT-4 generating ChainOfThought reasoning fields.

Key terms: teacher LM, bootstrapping, ChainOfThought, reasoning fields

5.4 BootstrapFewShotWithRandomSearch

The section explains how BootstrapFewShotWithRandomSearch compares demonstration sets on validation data, how to configure and interpret the search, and why the number of demos is worth testing.

Key terms: BootstrapFewShotWithRandomSearch, demos, validation set, num_candidate_programs, num_threads, compile(), baseline, LabeledFewShot, BootstrapFewShot, max_bootstrapped_demos

5.5 KNN: Finding examples dynamically

This section explains how DSPy’s KNN optimizer selects similar training examples for each LM call and what affects that approach’s usefulness and cost.

Key terms: KNN optimizer, demonstrations, k-nearest neighbors, compile(), embeddings, vector similarity, embedder, k, training set, dimensions

5.6 Evaluation results

This section explains how to compare optimized prompts, why optimizer choice depends on the task, and why small gains on an already strong classifier need careful evaluation.

Key terms: optimizer, prompt, demonstrations, bootstrapping, BootstrapFewShotWithRandomSearch, KNN, classification, unoptimized module, random variation, evaluation framework

5.6.1 gpt-4o-mini

This section interprets gpt-4o-mini optimization results and draws practical lessons about demonstration selection, hyperparameter testing, and inference tradeoffs.

Key terms: gpt-4o-mini, LabeledFewShot, baseline, demos, demonstrations, hyperparameters, k value, ChainOfThought, Predict, KNN

5.6.2 gpt-4.1-nano

This subsection shows that optimizing prompts can make the cheaper, initially weaker gpt-4.1-nano competitive with gpt-4o-mini.

Key terms: gpt-4.1-nano, gpt-4o-mini, unoptimized DSPy programs, optimized programs

5.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Evaluating DSPy programs Chapter 6: Optimizing prompt instructions →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.