liveBook · Manning

Metric

As explained in Building LLM Applications with DSPy

A criterion or measurement used to assess the quality of a language-model application's outputs.

A metric scores the quality of a language-model response. For an application that classifies airline messages, it turns each predicted intent into a result that can be compared across examples. Those results help compare prompts and estimate whether the classifier is reliable enough for production. section 4.0 section 4.2.1

Choose a score that reflects what matters for the task:

  • For straightforward classification, return whether the predicted label matches the expected label. section 4.2.1
  • For meaningful near misses, return a numeric score: similar intents might earn partial credit, while unrelated intents earn none. Higher scores mean better responses. section 4.2.1
  • If spelling or capitalization differences should not count as full errors, account for them when scoring. section 4.2.1

In DSPy, a custom metric is a Python function that receives an Example and the LM response for that Example, then returns a score. Defining the rule is often harder than writing the function: it determines which responses count as good. section 4.2.1

Scoring an intent classifier

If the expected intent is “Flight Booking Request,” exact-match scoring accepts that label and rejects every other one. A graded metric could instead give partial credit when “Airport Location Inquiry” is confused with “Airport Distance Inquiry,” but no credit for confusing unrelated intents. section 4.2.1

Automated prompt optimization needs one numeric metric to rank candidates. Final evaluation should often check several dimensions separately: a prompt that improves accuracy might also be slower or more expensive. Revisit metrics as reviewed production data reveals concerns the original score missed. section 4.2.4

How Building LLM Applications with DSPy covers it

How deep it goes
Defines a custom Python scoring function and illustrates Boolean and graded scores; defers built-in metrics and optimization details.
Examples
Uses airline-intent labels, partial credit for similar intents, and accuracy-versus-cost trade-offs.
What it assumes
Assumes familiarity with Python functions and labeled Examples; introduces how to score their LM responses.
What you can do after
Apply it via a DSPy custom metric to score classifier responses, then evaluate a test set against additional production concerns.
How it connects
Connects metrics to DSPy Examples, intent classification, prompt optimization, and production evaluation.

Also explained in

Compare how 16 books explain metric →

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.