feat: two-pass EXTRACT (claims -> typed Facts) for prose chunks
His diagnosis accepted (26/10): the single-pass EXTRACT's giant typed JSON array (verbatim quotes + temporal restrictions + sibling orders) is the output budget behind the 40-minute arithmetic on slow local models. Two-pass shortens EVERY output: - Pass 1 ExtractClaims: plain claim strings, coreferences resolved (entities named, never pronouns) — near-zero format tax. - Pass 2 TypeClaim: ONE claim -> one typed Fact per call (same Fact schema the store eats; temporal fields optional, containment-checked downstream as always). Orders NEVER produced here: one claim at a time has no siblings — the legacy Fact keeps orders for the single-pass path and table frags, untouched. - extract_mode: two_pass (default) | single (legacy) — config-driven, read from config.yaml's knowledge block by run_app. - Budget: Pass 1 charges once per chunk; Pass 2 charges once per claim. Table frags keep the legacy single-pass call. - Prompt log: CLAIMS/TYPE stage labels added. TDD: two_pass_extract 5/5 (signatures, coref law, honest empties) + two_pass_wiring 2/2 (end-to-end on a real store: two_pass ingests typed facts with windows kept; single-pass control green). Suite: 29 test files green. The router from #26 makes the upcoming extraction-model swap a pure config edit.
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
"""The two-pass EXTRACT tests (Pass 1 claims, Pass 2 typed Facts).
|
||||
|
||||
Law: the old single call asked a slow model for verbatim quotes + typed
|
||||
temporal fields + sibling order assertions in ONE giant JSON array — the
|
||||
output budget that produced the 40-minute arithmetic. The two-pass split
|
||||
shortens EVERY output: Pass 1 extracts plain claims (free-form strings
|
||||
with coreference resolved to the entity names — near-zero format tax);
|
||||
Pass 2 types ONE claim into the existing Fact schema per call.
|
||||
|
||||
CRITICAL law: Pass 2 works on ONE CLAIM AT A TIME — sibling order
|
||||
assertions become IMPOSSIBLE by construction, so orders are dropped from
|
||||
the two-pass path (the store's fact_order keeps working for whatever
|
||||
arrives as a typed Fact with orders; the two-pass facts carry none).
|
||||
The store, the containment checks and the merge are UNTOUCHED — Pass 2
|
||||
emits the same typed Fact as before, minus orders.
|
||||
|
||||
Pinned:
|
||||
1. ExtractClaims: free-form claim strings, coreferences RESOLVED (the
|
||||
claim must name its entities, not 'he/it/the party').
|
||||
2. TypeClaim: one claim in -> one typed Fact out (same fields as the
|
||||
legacy ExtractSignature.facts minus orders; temporal fields optional).
|
||||
3. The pipeline wiring: _extract_two_pass returns typed Fact objects;
|
||||
candidates flow into _ingest_candidate exactly as before.
|
||||
4. Honest empty: no claims -> no facts, no invention.
|
||||
5. The legacy single-pass signature STAYS (table frags use it; flipping
|
||||
the whole pipeline over is a later decision, config-driven).
|
||||
|
||||
Run: .venv/bin/python knowledge/tests/two_pass_extract.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
|
||||
|
||||
from knowledge.extract import ExtractClaims, TypeClaim, Fact, ExtractSignature
|
||||
from knowledge.two_pass import claims_from, fact_from_claim
|
||||
|
||||
|
||||
class Scripted:
|
||||
def __init__(self, **attrs):
|
||||
self.__dict__.update(attrs)
|
||||
|
||||
def __call__(self, **kw):
|
||||
return self
|
||||
|
||||
|
||||
def test_extract_claims_signature():
|
||||
"""Pass 1: claims are free-form strings; coreferences resolved."""
|
||||
fields = set(ExtractClaims.__fields__)
|
||||
assert fields == {"entities", "heading", "text", "claims"}, fields
|
||||
# what the model actually sees: dspy keeps desc in json_schema_extra
|
||||
d = (ExtractClaims.model_fields["claims"].json_schema_extra or {}).get("desc", "")
|
||||
assert "coreference" in d.lower() or "resolved" in d.lower(), d
|
||||
print("ok: ExtractClaims = free-form claims, coreferences resolved in-draft")
|
||||
|
||||
|
||||
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
|
||||
f = TypeClaim.model_fields["fact"].annotation
|
||||
assert "Fact" in str(f), f
|
||||
# Fact KEEPS orders (the legacy single-pass path and table frags use
|
||||
# them — untouched); the two-pass law is that TypeClaim never fills
|
||||
# them: one claim per call means no siblings to reference.
|
||||
assert "orders" in Fact.model_fields, "legacy Fact must keep orders"
|
||||
d = TypeClaim.__doc__ or ""
|
||||
assert "one claim" in d.lower() or "single" in d.lower(), d
|
||||
print("ok: TypeClaim = one claim -> one Fact; orders never produced (no siblings)")
|
||||
|
||||
|
||||
def test_claims_from_runs_pass1():
|
||||
e = Scripted(claims=Scripted(claims=["Pedro Sanchez is the prime minister of Spain since 2018",
|
||||
"Sanchez became Secretary-General of the PSOE in 2017"]))
|
||||
out = claims_from(e, entities="Pedro Sanchez, PSOE", heading="", text="...")
|
||||
assert out == ["Pedro Sanchez is the prime minister of Spain since 2018",
|
||||
"Sanchez became Secretary-General of the PSOE in 2017"], out
|
||||
print("ok: claims_from extracts the plain claim strings")
|
||||
|
||||
|
||||
def test_fact_from_claim_types_one_fact():
|
||||
e = Scripted(typeclaim=Scripted(fact=Fact(
|
||||
subject="Pedro Sanchez", relation="prime minister of",
|
||||
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")
|
||||
assert f.subject == "Pedro Sanchez" and f.start == "2018", f
|
||||
print("ok: fact_from_claim returns the typed Fact for one claim")
|
||||
|
||||
|
||||
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
|
||||
print("ok: no claims -> no facts; untypeable claim -> None, no invention")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_extract_claims_signature()
|
||||
test_type_claim_signature()
|
||||
test_claims_from_runs_pass1()
|
||||
test_fact_from_claim_types_one_fact()
|
||||
test_honest_empty()
|
||||
print("\ntwo_pass_extract: 5/5 ok")
|
||||
Reference in New Issue
Block a user