feat: two-pass EXTRACT (claims -> typed Facts) for prose chunks

His diagnosis accepted (26/10): the single-pass EXTRACT's giant typed
JSON array (verbatim quotes + temporal restrictions + sibling orders)
is the output budget behind the 40-minute arithmetic on slow local
models. Two-pass shortens EVERY output:

- Pass 1 ExtractClaims: plain claim strings, coreferences resolved
  (entities named, never pronouns) — near-zero format tax.
- Pass 2 TypeClaim: ONE claim -> one typed Fact per call (same Fact
  schema the store eats; temporal fields optional, containment-checked
  downstream as always). Orders NEVER produced here: one claim at a
  time has no siblings — the legacy Fact keeps orders for the
  single-pass path and table frags, untouched.
- extract_mode: two_pass (default) | single (legacy) — config-driven,
  read from config.yaml's knowledge block by run_app.
- Budget: Pass 1 charges once per chunk; Pass 2 charges once per
  claim. Table frags keep the legacy single-pass call.
- Prompt log: CLAIMS/TYPE stage labels added.

TDD: two_pass_extract 5/5 (signatures, coref law, honest empties) +
two_pass_wiring 2/2 (end-to-end on a real store: two_pass ingests
typed facts with windows kept; single-pass control green). Suite: 29
test files green. The router from #26 makes the upcoming
extraction-model swap a pure config edit.
This commit is contained in:
2026-10-26 16:42:22 +01:00
parent 95dfdf7123
commit f6e9164e92
8 changed files with 340 additions and 4 deletions
+29
View File
@@ -72,6 +72,35 @@ class ExtractSignature(dspy.Signature):
desc="the extracted facts; empty list if none")
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."""
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")
class TypeClaim(dspy.Signature):
"""You are the TYPE step of a knowledge machine. Given ONE single claim,
type it into the Fact schema: subject entity, relation, object/value,
the VERBATIM quote from the text that states the claim (the FULL
sentence or table row, copied exactly, 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."""
claim: str = dspy.InputField(desc="the claim to type")
fact: Fact | None = dspy.OutputField(
desc="the typed Fact, or null when the claim cannot be typed honestly")
class AnswerFromKG(dspy.Signature):
"""You are the ANSWER step of a knowledge machine. Answer the question using
ONLY the given knowledge-graph rows, each with its quote and source. Cite