liveBook · Manning

Consistency

As explained in Building LLM Applications with DSPy

Consistency is the degree to which a language model produces similar results across repeated calls with the same input and configuration.

Consistency measures how similar a language model’s responses are across repeated calls with the same input and configuration. DSPy’s default cache can hide variation by returning the first response again; disable caching when testing consistency. section 4.6 section 2.4.4

Two repeated-test approaches answer different questions:

  • Repeat each test example and combine the scores to assess accuracy across calls. This gives a broader accuracy assessment, but does not isolate consistency. section 4.6
  • For a consistency-focused measure, count the predictions for each example. One simple measure is the number that differ from the most frequent prediction; it does not account for accuracy. section 4.6
Listing 4.18 Testing for consistency
from collections import Counter
import dspy 

lm = dspy.LM('gpt-4o-mini', cache=False)
dspy.configure(lm=lm)
classifier = dspy.Predict(IntentSignature)
for example in dev_set:
    examples = [example] * 10
    evaluator = dspy.Evaluate(devset=examples, num_threads=10, 
                              display_progress=False,
                              display_table=False,provide_traceback=False,
                              max_errors=100000)
    res = evaluator(classifier, metric=validate_answer)
    predictions = [prediction.intent_label for _, prediction, _ in res.results]
    print(predictions)
    print(Counter(predictions))

A DSPy example repeats each development-set item ten times, evaluates the calls, and counts the resulting intent labels. section 4.6

Repeated evaluation can extract more value from a small test set and help distinguish prompts with similar scores. It also takes more time and costs more, so reserve it for cases where a more accurate assessment is needed. section 4.6

How Building LLM Applications with DSPy covers it

How deep it goes
Explains repeated testing and prediction-frequency counting, but does not prescribe a single universal consistency measure.
Examples
Repeats intent-classification examples ten times and counts labels such as “Aircraft Type Inquiry.”
What you can do after
Evaluate response variability by disabling caching, repeating test examples, and counting prediction frequencies.
How it connects
Connects consistency testing to accuracy evaluation, test sets, prompts, and response caching.

Also explained in

Compare how 20 books explain consistency →

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.