liveBook · Manning

Semantic precision

As explained in Building LLM Applications with DSPy

Semantic precision measures how well the ideas in a generated summary match the ideas in a reference summary, regardless of wording.

Semantic precision measures whether ideas in a generated summary match those in a reference summary, regardless of wording. Unlike lexical precision, it compares meaning rather than words or n-grams. section 8.6.1 section 8.7

Semantic precision and groundedness assess different things: precision asks whether a summary covers relevant ideas, while groundedness asks whether its ideas appear in the original document. Pairing them can help distinguish relevant content from invented content. section 8.6.1

Listing 8.13 Metric function using SemanticF1
import dspy
from dspy.evaluate import SemanticF1

lm = dspy.LM('openai/gpt-4.1-nano', cache=False) 
dspy.configure(lm=lm)

test_example = dspy.Example( 
   question="What is the Rime of the Ancient Mariner about?", 
   response="A sailor stops a wedding guest to recount his cursed journey
             after he "
            "thoughtlessly shoots an albatross, a bird of good omen, bringing
             death to " 
            "shipmates and himself.")  #1

test_pred = dspy.Prediction(response=
   "A mariner recalls his voyage at sea, which was doomed 
    after he killed a bird")  #2

semantic_f1 = SemanticF1()  #3
semantic_f1(test_example, test_pred)  #4

The DSPy example compares a reference and prediction about the Rime of the Ancient Mariner with SemanticF1. Its examples require fields named question and response, which can be adapted to other field names. section 8.6.1

How Building LLM Applications with DSPy covers it

How deep it goes
Defines idea-level precision and contrasts it with groundedness, without detailing a standalone scoring formula.
Examples
Uses summaries about the Rime of the Ancient Mariner and an albatross to illustrate idea matching.
What you can do after
Evaluate summaries with DSPy's SemanticF1 metric, adapting its required question and response fields when needed.
How it connects
Connects semantic precision to semantic recall, lexical precision, groundedness, completeness, and LLM-as-a-judge evaluation.

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.