diff --git a/code/knowledge/expert.py b/code/knowledge/expert.py index c2ee1ec..db80de2 100644 --- a/code/knowledge/expert.py +++ b/code/knowledge/expert.py @@ -164,6 +164,10 @@ class KnowledgeConfig: semantic_tau: float = 0.45 # calibration: rivers 0.49-0.52 in, Pedro # cross-question 0.35-0.38 out, spouse-noise ~0.67 max_facts_per_chunk: int = 12 # question-blind extraction needs a cap + max_claims_per_chunk: int = 6 # two-pass: each claim costs a TYPE call — + # the budget is finite (80/run); without a + # cap one big article starves the run + # (live finding 26/10: 70 claims -> 80/80) extract_mode: str = "two_pass" # "two_pass" (claims -> typed Facts, short # outputs) | "single" (legacy one-call typed # array — table frags keep using it) @@ -1179,8 +1183,10 @@ class KnowledgeExpert: claims = claims_from(self, entities=ent_str, heading=chunk.heading_path, text=chunk.text, charge_llm=charge_llm) - for claim in claims[: self.cfg.max_facts_per_chunk]: - fact = fact_from_claim(self, claim, charge_llm=charge_llm) + # budget law: each claim costs a TYPE call — cap per chunk + for claim in claims[: self.cfg.max_claims_per_chunk]: + fact = fact_from_claim(self, claim, text=chunk.text, + charge_llm=charge_llm) if fact is None: continue # honestly declined: no invention merged, _eid = self._ingest_candidate( diff --git a/code/knowledge/extract.py b/code/knowledge/extract.py index 40f472f..131ef51 100644 --- a/code/knowledge/extract.py +++ b/code/knowledge/extract.py @@ -89,16 +89,22 @@ class ExtractClaims(dspy.Signature): 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.""" + """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.""" 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") fact: Fact | None = dspy.OutputField( - desc="the typed Fact, or null when the claim cannot be typed honestly") + desc="the typed Fact with its verbatim quote from the text, or " + "null when the claim cannot be typed honestly") class AnswerFromKG(dspy.Signature): diff --git a/code/knowledge/tests/two_pass_extract.py b/code/knowledge/tests/two_pass_extract.py index b4ef419..93780cc 100644 --- a/code/knowledge/tests/two_pass_extract.py +++ b/code/knowledge/tests/two_pass_extract.py @@ -61,7 +61,7 @@ def test_extract_claims_signature(): def test_type_claim_signature(): """Pass 2: one claim -> one typed Fact; orders NEVER produced here.""" fields = set(TypeClaim.__fields__) - assert fields == {"claim", "fact"}, fields + assert fields == {"claim", "text", "fact"}, fields # text: the quote must be COPIED from it (live finding: without it, 69/70 invented quotes rejected) f = TypeClaim.model_fields["fact"].annotation assert "Fact" in str(f), f # Fact KEEPS orders (the legacy single-pass path and table frags use @@ -88,7 +88,7 @@ def test_fact_from_claim_types_one_fact(): object="Spain", quote="is a Spanish politician who has been the " "prime minister of Spain since 2018", start="2018", end=None, point=None, granularity="year"))) - f = fact_from_claim(e, claim="Pedro Sanchez is the prime minister of Spain since 2018") + f = fact_from_claim(e, claim="Pedro Sanchez is the prime minister of Spain since 2018", text="...") assert f.subject == "Pedro Sanchez" and f.start == "2018", f print("ok: fact_from_claim returns the typed Fact for one claim") @@ -97,7 +97,7 @@ def test_honest_empty(): e = Scripted(claims=Scripted(claims=[])) assert claims_from(e, entities="", heading="", text="") == [] e2 = Scripted(typeclaim=Scripted(fact=None)) - assert fact_from_claim(e2, claim="") is None + assert fact_from_claim(e2, claim="", text="") is None print("ok: no claims -> no facts; untypeable claim -> None, no invention") diff --git a/code/knowledge/two_pass.py b/code/knowledge/two_pass.py index 0c866f3..8847afc 100644 --- a/code/knowledge/two_pass.py +++ b/code/knowledge/two_pass.py @@ -27,14 +27,16 @@ def claims_from(expert, entities: str, heading: str, text: str, return out -def fact_from_claim(expert, claim: str, charge_llm=None): +def fact_from_claim(expert, claim: str, text: str, charge_llm=None): """Pass 2: ONE claim -> one typed Fact (or None, honestly declined). - The Fact carries the verbatim quote and any temporal restrictions - the text states; the store's containment checks validate everything - downstream exactly as before. No orders: no siblings by construction. + 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 + orders: no siblings by construction. """ if charge_llm: charge_llm() - pred = expert.typeclaim(claim=claim) + pred = expert.typeclaim(claim=claim, text=text) return getattr(pred, "fact", None) \ No newline at end of file