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
+34 -21
View File
@@ -1,52 +1,65 @@
"""The two-pass extraction (Pass 1 claims, Pass 2 typed Facts).
"""The two-pass extraction (Pass 1 claims + verbatim quotes, Pass 2 typing).
The single-pass EXTRACT asked slow local models for a giant JSON array
of typed Facts with verbatim quotes + temporal restrictions + sibling
order assertions — the output budget behind the 40-minute arithmetic.
Two-pass shortens every output: Pass 1 lists plain claims (free-form
strings, coreferences resolved — near-zero format tax); Pass 2 types
ONE claim at a time into the same Fact schema the store already eats.
Two-pass shortens every output: Pass 1 lists the claims, each with its
VERBATIM quote (extracted while the text is in front of the model);
Pass 2 types ONE claim at a time into the same Fact schema the store
already eats — typing ONLY, the verbatim given as an input and the
TYPE call never carrying the chunk (owner 1/11: the VERBATIM is
obtained before).
Orders: NEVER produced here — one claim per call has no siblings to
reference. The legacy single-pass path (and table frags) keeps them.
"""
from __future__ import annotations
from .extract import Claim
def claims_from(expert, entities: str, heading: str, text: str,
charge_llm=None) -> list[str]:
"""Pass 1: the chunk's claims as plain strings, coreferences resolved.
charge_llm=None) -> list[Claim]:
"""Pass 1: the chunk's claims, each with its VERBATIM quote — the
quote is extracted HERE, in the same call that reads the text.
The list is made honest by CODE only: exact repeats are dropped
(live finding 26/10: glm sometimes lists its whole list twice, and
thinking leakage leaks in as list items). Dedup is case- and
whitespace-insensitive; the FIRST occurrence's exact text is kept.
NO cap, NO semantic filter: completeness beats budget (owner ruling
26/10) — every unique claim gets its TYPE call.
whitespace-insensitive on the CLAIM text; the FIRST occurrence's
(claim, verbatim) is kept. NO cap, NO semantic filter: completeness
beats budget (owner ruling 26/10) — every unique claim gets its
TYPE call.
"""
if charge_llm:
charge_llm()
pred = expert.claims(entities=entities, heading=heading, text=text)
seen: dict[str, str] = {}
seen: dict[str, Claim] = {}
for c in (pred.claims or []):
c = (c or "").strip()
if c:
key = " ".join(c.lower().split())
claim = (getattr(c, "claim", "") or "").strip()
verbatim = (getattr(c, "verbatim", "") or "").strip()
if claim:
key = " ".join(claim.lower().split())
if key not in seen:
seen[key] = c
seen[key] = Claim(claim=claim, verbatim=verbatim)
return list(seen.values())
def fact_from_claim(expert, claim: str, text: str, charge_llm=None):
def fact_from_claim(expert, claim: str, verbatim: str, charge_llm=None):
"""Pass 2: ONE claim -> one typed Fact (or None, honestly declined).
The Fact's quote is copied VERBATIM from `text` (the chunk the claim
came from) — without the text the model invents a quote and the
containment check rejects it (live finding 26/10: 69/70 rejected).
The store's checks validate everything downstream as before. No
TYPING ONLY: the VERBATIM was obtained in Pass 1 and enters as an
input — the TYPE call never sees the text. Code PINS the Fact's
quote to it (LLMs propose, code decides): a paraphrased copy would
fail the containment check and lose the fact (live finding 26/10:
without grounding, 69/70 invented quotes were rejected). The
store's checks validate everything downstream as before. No
orders: no siblings by construction.
"""
if charge_llm:
charge_llm()
pred = expert.typeclaim(claim=claim, text=text)
return getattr(pred, "fact", None)
pred = expert.typeclaim(claim=claim, verbatim=verbatim)
fact = getattr(pred, "fact", None)
if fact is not None:
fact.quote = verbatim # the law: the Fact's quote IS Pass 1's verbatim
return fact