liveBook · Manning

Chapter 4: Evaluating DSPy programs

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

The introduction explains why baseline evaluation matters and what makes comparisons of LM prompts reliable.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why baseline evaluation matters and what makes comparisons of LM prompts reliable.

Key terms: evaluation, optimization, baseline model, test data, evaluation metric, test set, temperature, consistency, randomness

4.1 Creating a dataset for evaluation

This section explains how to assemble useful evaluation data for an LM application, including coverage, size, labeling, data sources, and risks to reliable estimates.

Key terms: test data, distribution, classes, user intents, test set, tens or hundreds of examples, labeled data, unlabeled data, airline intents classifier, real data

4.1.1 Using the Example class

This section shows how to represent evaluation cases as DSPy Example objects and assemble them into a test set for the intent classifier.

Key terms: Example, signature, message, labels, intent_label, with_inputs(), input fields, test set, list of tuples, list comprehension

4.1.2 Dividing the data into train and test sets

The section explains how to divide data for DSPy optimization and evaluation while keeping performance estimates reliable.

Key terms: training set, validation set, demonstrations, optimizer, development set, overfit, test set, hold-out test set, unbiased estimate, real-world cases

4.1.3 The sizes of the sets

This section explains why DSPy can use a modest training pool to choose a few prompt examples and why LM applications may allocate a larger share of their data to validation and testing.

Key terms: training set, few-shot examples, many-shot, demonstrations, training, validation, test sets, optimizers, validation set

4.1.4 Splitting the ATIS data

This section shows how the ATIS data is divided into four small sets for evaluation while keeping time and cost down.

Key terms: training set, validation set, development set, test set, ATIS

4.1.5 Using DSPy to generate examples

The section explains two ways to generate synthetic intent-classification examples with DSPy and why the generated data needs checking.

Key terms: synthetic data, intents, Predict module, synthetic questions, distinct synthetic utterances, duplicates, variations, test set, intent_label

4.2 Evaluating a module with a test set

This section introduces evaluating a DSPy program by scoring individual predictions and then assigning an overall score for a dataset.

Key terms: Examples, development set, validation set, test set, overall score

4.2.1 Defining an evaluation metric

The section explains how to score individual DSPy responses, using exact-match intent classification as the baseline while showing optional ways to make scoring more nuanced.

Key terms: evaluation metric, Example, LM’s response, score, classification, Boolean, ground truth, True or False, floating-point number, fuzzy matching

4.2.2 Defining a metric for the baseline model

This section defines the inputs to a DSPy evaluation metric and explains why the airline chatbot baseline uses exact-match scoring.

Key terms: metric function, Example, Prediction, trace, signature, gold values, exact match, validate_answer()

4.2.3 Calculating a final evaluation for the module

The section explains how to turn per-example test scores into an overall evaluation that reflects what matters for the application.

Key terms: per-example scores, mean average, median score, minimum score, low percentile, harmonic average, outlier, Boolean scores, accuracy, class imbalance

4.2.4 Evaluation for tuning versus final evaluation

The section explains why automated prompt tuning uses one numeric metric, while final evaluation should examine several dimensions and metrics should improve as new problems emerge.

Key terms: metric, optimization, final evaluation, test set, Goodhart’s law, accuracy, concise, cost effective, production, metrics

4.2.5 Testing the metric function

This section shows how to unit-test an evaluation metric directly and how to choose test cases that give confidence in its scores.

Key terms: evaluation metric, Example, Prediction, intent_labels, unit tests, metric functions, weak, moderate, and strong responses

4.3 Evaluating the DSPy baseline model

This section identifies the unoptimized DSPy baseline program to be evaluated and notes that evaluation can be done manually or through DSPy.

Key terms: LM, signature, module, optimization

4.3.1 Using custom Python code for evaluation

This subsection shows how to evaluate a baseline classifier with a manual Python loop and explains the practical limits of that approach.

Key terms: dev_set, Predict, validate_answer(), per-example scores, Example, **example.inputs(), with_inputs(), message, labels, cache=False

4.3.2 Using DSPy Evaluate

This section shows how DSPy Evaluate runs and reports the baseline evaluation, and explains its advantages and trade-offs compared with a manual loop.

Key terms: Evaluate, devset, environment variable, final score, evaluation framework, multithreaded, parallelization, EvaluationResult, results, display_table

4.3.3 Rate limits

Explains how provider rate limits can interrupt parallel evaluation and how to adjust evaluation concurrency.

Key terms: rate limits, threads, max_errors, sleep statements

4.4 Evaluating a manually created prompt using the OpenAI API

The section compares a manually written OpenAI prompt with a DSPy-generated prompt and interprets their similar evaluation scores.

Key terms: manually created prompt, baseline, hardcoded prompt, test set, OpenAI’s API, accuracy, unoptimized prompts, handcraft the prompt

4.5 Evaluating per-class performance

This section shows how to inspect classification performance by class and calculate a macro F1 score alongside overall accuracy.

Key terms: per-class performance, classification_report, test records, underperforming classes, macro F1 score, precision, recall, ground truths, predictions

4.6 Evaluating the consistency of responses

The section compares repeated-test methods for judging both a DSPy program’s accuracy and the consistency of its responses.

Key terms: test example, overall accuracy, consistency, predictions, most popular prediction, Evaluate, full test set, minimum score, Boolean scores

4.7 Evaluating other LMs and modules

This section compares LMs and DSPy modules against an evaluated baseline, showing how to weigh accuracy, runtime, exceptions, and randomness when choosing a current-best program.

Key terms: evaluate_baseline(), model parameter, Predict, ChainOfThought, test set, mean, standard deviation, randomness, train-validation-test splits, gpt-4o-mini

4.8 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 3: Classifying user intent Chapter 5: Optimizing prompt examples →

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.