fix: two-pass Pass 2 needs the chunk text (invented quotes) + claim cap #28

Merged
onibot merged 1 commits from fix/two-pass-contract into main 2026-10-26 17:05:16 +01:00
4 changed files with 32 additions and 18 deletions
Showing only changes of commit a9837981b3 - Show all commits
+8 -2
View File
@@ -164,6 +164,10 @@ class KnowledgeConfig:
semantic_tau: float = 0.45 # calibration: rivers 0.49-0.52 in, Pedro semantic_tau: float = 0.45 # calibration: rivers 0.49-0.52 in, Pedro
# cross-question 0.35-0.38 out, spouse-noise ~0.67 # cross-question 0.35-0.38 out, spouse-noise ~0.67
max_facts_per_chunk: int = 12 # question-blind extraction needs a cap 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 extract_mode: str = "two_pass" # "two_pass" (claims -> typed Facts, short
# outputs) | "single" (legacy one-call typed # outputs) | "single" (legacy one-call typed
# array — table frags keep using it) # array — table frags keep using it)
@@ -1179,8 +1183,10 @@ class KnowledgeExpert:
claims = claims_from(self, entities=ent_str, claims = claims_from(self, entities=ent_str,
heading=chunk.heading_path, heading=chunk.heading_path,
text=chunk.text, charge_llm=charge_llm) text=chunk.text, charge_llm=charge_llm)
for claim in claims[: self.cfg.max_facts_per_chunk]: # budget law: each claim costs a TYPE call — cap per chunk
fact = fact_from_claim(self, claim, charge_llm=charge_llm) 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: if fact is None:
continue # honestly declined: no invention continue # honestly declined: no invention
merged, _eid = self._ingest_candidate( merged, _eid = self._ingest_candidate(
+14 -8
View File
@@ -89,16 +89,22 @@ class ExtractClaims(dspy.Signature):
class TypeClaim(dspy.Signature): class TypeClaim(dspy.Signature):
"""You are the TYPE step of a knowledge machine. Given ONE single claim, """You are the TYPE step of a knowledge machine. Given ONE single claim
type it into the Fact schema: subject entity, relation, object/value, and the TEXT it was extracted from, type the claim into the Fact
the VERBATIM quote from the text that states the claim (the FULL schema: subject entity, relation, object/value, the VERBATIM quote
sentence or table row, copied exactly, at least 10 characters), and copied EXACTLY from the text (the FULL sentence or table row that
the temporal restrictions the text states. All temporal fields are states the claim, at least 10 characters), and the temporal
optional and must be contained in the quote. Only what the claim and restrictions the text states. All temporal fields are optional and
its quote state; no inference.""" 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") 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( 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): class AnswerFromKG(dspy.Signature):
+3 -3
View File
@@ -61,7 +61,7 @@ def test_extract_claims_signature():
def test_type_claim_signature(): def test_type_claim_signature():
"""Pass 2: one claim -> one typed Fact; orders NEVER produced here.""" """Pass 2: one claim -> one typed Fact; orders NEVER produced here."""
fields = set(TypeClaim.__fields__) 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 f = TypeClaim.model_fields["fact"].annotation
assert "Fact" in str(f), f assert "Fact" in str(f), f
# Fact KEEPS orders (the legacy single-pass path and table frags use # 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 " object="Spain", quote="is a Spanish politician who has been the "
"prime minister of Spain since 2018", "prime minister of Spain since 2018",
start="2018", end=None, point=None, granularity="year"))) 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 assert f.subject == "Pedro Sanchez" and f.start == "2018", f
print("ok: fact_from_claim returns the typed Fact for one claim") 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=[])) e = Scripted(claims=Scripted(claims=[]))
assert claims_from(e, entities="", heading="", text="") == [] assert claims_from(e, entities="", heading="", text="") == []
e2 = Scripted(typeclaim=Scripted(fact=None)) 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") print("ok: no claims -> no facts; untypeable claim -> None, no invention")
+7 -5
View File
@@ -27,14 +27,16 @@ def claims_from(expert, entities: str, heading: str, text: str,
return out 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). """Pass 2: ONE claim -> one typed Fact (or None, honestly declined).
The Fact carries the verbatim quote and any temporal restrictions The Fact's quote is copied VERBATIM from `text` (the chunk the claim
the text states; the store's containment checks validate everything came from) — without the text the model invents a quote and the
downstream exactly as before. No orders: no siblings by construction. 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: if charge_llm:
charge_llm() charge_llm()
pred = expert.typeclaim(claim=claim) pred = expert.typeclaim(claim=claim, text=text)
return getattr(pred, "fact", None) return getattr(pred, "fact", None)