Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
964189387d | ||
|
|
2ee3b54661 | ||
|
|
a9837981b3 | ||
|
|
88769c508c |
+3
-1
@@ -35,7 +35,9 @@ lm[ExtractSignature]:
|
||||
# on slow local models (the 40-min arithmetic)
|
||||
|
||||
budget:
|
||||
max_llm_calls: 80 # per FLOOP run (children get a slice; fan-outs multiply
|
||||
# owner ruling 26/10: completeness beats budget — the budget must never
|
||||
# silently truncate extraction; it bounds runaway runs only
|
||||
max_llm_calls: 200 # per FLOOP run (children get a slice; fan-outs multiply
|
||||
# entity questions — a 3-river fan needs ~3x a scalar)
|
||||
min_child_budget: 6
|
||||
max_depth: 2 # recursion cap (PIPELINE.md §8.4)
|
||||
|
||||
@@ -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 # DEPRECATED (owner ruling 26/10:
|
||||
# completeness beats budget) — no longer
|
||||
# read by the two-pass loop; kept only for
|
||||
# the table-frags legacy path
|
||||
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,11 @@ 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)
|
||||
# owner ruling 26/10: completeness beats budget — every
|
||||
# unique claim gets its TYPE call (no cap, dedup only)
|
||||
for claim in claims:
|
||||
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(
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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,14 +97,40 @@ 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")
|
||||
|
||||
|
||||
def test_claims_from_dedupes_exact_repeats():
|
||||
"""Pass 1 sometimes repeats its own list (live: glm listed 46 claims,
|
||||
~26 were exact repeats/thinking leakage). Dedup is CODE, cheap and
|
||||
deterministic — the claim list must not be capped or filtered
|
||||
semantically, only made honest: no exact duplicates survive."""
|
||||
calls = []
|
||||
|
||||
class E: # minimal expert double
|
||||
def claims(self, entities, heading, text):
|
||||
calls.append((entities, heading))
|
||||
class Out:
|
||||
claims = ["Pedro Sanchez is prime minister of Spain.",
|
||||
"pedro sanchez is prime minister of spain. ",
|
||||
"Pedro Sanchez is Secretary-General of the PSOE.",
|
||||
"Pedro Sanchez is prime minister of Spain.",
|
||||
"from the text. I need to list factual claims"]
|
||||
return Out()
|
||||
|
||||
got = claims_from(E(), entities="Pedro Sanchez", heading="h", text="t")
|
||||
assert got == ["Pedro Sanchez is prime minister of Spain.",
|
||||
"Pedro Sanchez is Secretary-General of the PSOE.",
|
||||
"from the text. I need to list factual claims"], got
|
||||
print("ok: exact repeats deduped (case/space-insensitive); the rest kept")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_extract_claims_signature()
|
||||
test_type_claim_signature()
|
||||
test_claims_from_dedupes_exact_repeats()
|
||||
test_claims_from_runs_pass1()
|
||||
test_fact_from_claim_types_one_fact()
|
||||
test_honest_empty()
|
||||
print("\ntwo_pass_extract: 5/5 ok")
|
||||
print("\ntwo_pass_extract: 6/6 ok")
|
||||
+30
-18
@@ -15,26 +15,38 @@ from __future__ import annotations
|
||||
|
||||
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."""
|
||||
if charge_llm:
|
||||
charge_llm()
|
||||
pred = expert.claims(entities=entities, heading=heading, text=text)
|
||||
out = []
|
||||
for c in (pred.claims or []):
|
||||
c = (c or "").strip()
|
||||
if c:
|
||||
out.append(c)
|
||||
return out
|
||||
"""Pass 1: the chunk's claims as plain strings, coreferences resolved.
|
||||
|
||||
|
||||
def fact_from_claim(expert, claim: 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 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.
|
||||
"""
|
||||
if charge_llm:
|
||||
charge_llm()
|
||||
pred = expert.typeclaim(claim=claim)
|
||||
pred = expert.claims(entities=entities, heading=heading, text=text)
|
||||
seen: dict[str, str] = {}
|
||||
for c in (pred.claims or []):
|
||||
c = (c or "").strip()
|
||||
if c:
|
||||
key = " ".join(c.lower().split())
|
||||
if key not in seen:
|
||||
seen[key] = c
|
||||
return list(seen.values())
|
||||
|
||||
|
||||
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'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, text=text)
|
||||
return getattr(pred, "fact", None)
|
||||
Reference in New Issue
Block a user