Extract verbatim in ExtractClaims not in TypeClaim

This commit is contained in:
2026-11-01 16:47:47 +01:00
parent 72ebe44053
commit 4e7229c92c
6 changed files with 176 additions and 91 deletions
+37 -19
View File
@@ -3,6 +3,10 @@
The extractor sees ONE chunk (+ heading path); it proposes typed candidates with
VERBATIM spans. The containment check (checks.py) validates every span against
the FULL document text — so chunking can never fake a quote.
Two-pass (two_pass.py): Pass 1 (ExtractClaims) extracts the claims WITH their
VERBATIM spans; Pass 2 (TypeClaim) types ONE claim, the verbatim given to it
as an input — typing only, the VERBATIM is obtained before (owner 1/11).
"""
from __future__ import annotations
@@ -72,39 +76,53 @@ class ExtractSignature(dspy.Signature):
desc="the extracted facts; empty list if none")
class Claim(BaseModel):
"""One claim of Pass 1: the free-form claim PLUS the VERBATIM span
that states it. The quote is extracted HERE, in the same call that
reads the text (owner 1/11: the VERBATIM is obtained before the
typing — Pass 2 only enforces the Fact's typing)."""
claim: str = dspy.OutputField(
desc="the factual claim, coreferences resolved (entities named, "
"never pronouns)")
verbatim: str = dspy.OutputField(
desc="VERBATIM quote from the text that states the claim: the FULL "
"sentence or table row, copied exactly (at least 10 characters)")
class ExtractClaims(dspy.Signature):
"""You are the CLAIMS step of a knowledge machine. From the given text,
list the FACTUAL CLAIMS it states about the NAMED ENTITIES: positions
and offices held, biographical facts, events, relationships,
quantities with units. One claim per line, plain text, no numbering.
Resolve coreferences: name the entity in the claim, never a pronoun
or a shorthand. Only what the text states; no inference."""
quantities with units. Each claim carries the VERBATIM quote that
states it: the FULL sentence or table row, copied exactly (at least
10 characters). Resolve coreferences: name the entity in the claim,
never a pronoun or a shorthand. Only what the text states; no
inference."""
entities: str = dspy.InputField(
desc="the named entities to extract claims about, comma separated")
heading: str = dspy.InputField(desc="section path of this chunk; context only")
text: str = dspy.InputField()
claims: list[str] = dspy.OutputField(
desc="the factual claims, one string each, coreferences resolved "
"(entities named, never pronouns); empty list if none")
claims: list[Claim] = dspy.OutputField(
desc="the factual claims, each with its VERBATIM quote copied "
"exactly from the text; empty list if none")
class TypeClaim(dspy.Signature):
"""You are the TYPE step of a knowledge machine. Given ONE single claim
and the TEXT it was extracted from, type the claim into the Fact
schema: subject entity, relation, object/value, the VERBATIM quote
copied EXACTLY from the text (the FULL sentence or table row that
states the claim, at least 10 characters), and the temporal
restrictions the text states. All temporal fields are optional and
must be contained in the quote. Only what the claim and its quote
state; no inference. If the text does not state the claim, return
null."""
and the VERBATIM quote that states it (extracted before, from the
text the claim came from), type the claim into the Fact schema:
subject entity, relation, object/value, and the temporal
restrictions the quote states. The Fact's quote is the given
verbatim, copied exactly. All temporal fields are optional and must
be contained in the quote. Only what the claim and its quote state;
no inference. If the claim cannot be typed honestly, return null."""
claim: str = dspy.InputField(desc="the claim to type")
text: str = dspy.InputField(
desc="the text the claim was extracted from — find the VERBATIM "
"quote here, copied exactly")
verbatim: str = dspy.InputField(
desc="the VERBATIM quote that states the claim, obtained before — "
"the Fact's quote field must copy it exactly")
fact: Fact | None = dspy.OutputField(
desc="the typed Fact with its verbatim quote from the text, or "
"null when the claim cannot be typed honestly")
desc="the typed Fact whose quote is the given verbatim, or null "
"when the claim cannot be typed honestly")
class AnswerFromKG(dspy.Signature):