65 lines
2.8 KiB
Python
65 lines
2.8 KiB
Python
"""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 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[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 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, Claim] = {}
|
|
for c in (pred.claims or []):
|
|
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] = Claim(claim=claim, verbatim=verbatim)
|
|
return list(seen.values())
|
|
|
|
|
|
def fact_from_claim(expert, claim: str, verbatim: str, charge_llm=None):
|
|
"""Pass 2: ONE claim -> one typed Fact (or None, honestly declined).
|
|
|
|
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, 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 |