Extract verbatim in ExtractClaims not in TypeClaim
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
"""The two-pass EXTRACT tests (Pass 1 claims, Pass 2 typed Facts).
|
||||
"""The two-pass EXTRACT tests (Pass 1 claims + verbatim, Pass 2 typing).
|
||||
|
||||
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.
|
||||
shortens EVERY output: Pass 1 extracts the claims, each with its
|
||||
VERBATIM quote (coreference resolved to the entity names); Pass 2 types
|
||||
ONE claim into the existing Fact schema per call — typing ONLY, the
|
||||
verbatim given to it as an input (owner 1/11: the VERBATIM is obtained
|
||||
before; the TYPE call never sees the text).
|
||||
|
||||
CRITICAL law: Pass 2 works on ONE CLAIM AT A TIME — sibling order
|
||||
assertions become IMPOSSIBLE by construction, so orders are dropped from
|
||||
@@ -15,15 +17,19 @@ 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;
|
||||
1. ExtractClaims: free-form claims, coreferences RESOLVED (the claim
|
||||
must name its entities, not 'he/it/the party'), each carrying its
|
||||
VERBATIM quote — the quote is extracted HERE, in Pass 1.
|
||||
2. TypeClaim: one claim + its verbatim in -> one typed Fact out (same
|
||||
fields as the legacy ExtractSignature.facts minus orders; temporal
|
||||
fields optional) — no text input: the VERBATIM enters as an input.
|
||||
3. The pipeline wiring: the two-pass path 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).
|
||||
6. Code PINS the Fact's quote to Pass 1's verbatim (LLMs propose, code
|
||||
decides) — the TYPE model cannot corrupt the span.
|
||||
|
||||
Run: .venv/bin/python knowledge/tests/two_pass_extract.py
|
||||
"""
|
||||
@@ -36,7 +42,8 @@ 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.extract import (Claim, ExtractClaims, ExtractSignature, Fact,
|
||||
TypeClaim)
|
||||
from knowledge.two_pass import claims_from, fact_from_claim
|
||||
|
||||
|
||||
@@ -49,19 +56,27 @@ class Scripted:
|
||||
|
||||
|
||||
def test_extract_claims_signature():
|
||||
"""Pass 1: claims are free-form strings; coreferences resolved."""
|
||||
"""Pass 1: claims + their VERBATIM quotes; coreferences resolved."""
|
||||
fields = set(ExtractClaims.__fields__)
|
||||
assert fields == {"entities", "heading", "text", "claims"}, fields
|
||||
a = ExtractClaims.model_fields["claims"].annotation
|
||||
assert "Claim" in str(a), a # each claim now carries its verbatim
|
||||
# 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")
|
||||
assert "verbatim" in d.lower(), d
|
||||
dv = (Claim.model_fields["verbatim"].json_schema_extra or {}).get("desc", "")
|
||||
assert "verbatim" in dv.lower() and "copied exactly" in dv.lower(), dv
|
||||
print("ok: ExtractClaims = claims with their VERBATIM quotes, coreferences resolved")
|
||||
|
||||
|
||||
def test_type_claim_signature():
|
||||
"""Pass 2: one claim -> one typed Fact; orders NEVER produced here."""
|
||||
"""Pass 2: one claim + its verbatim -> one typed Fact; no text input;
|
||||
orders NEVER produced here."""
|
||||
fields = set(TypeClaim.__fields__)
|
||||
assert fields == {"claim", "text", "fact"}, fields # text: the quote must be COPIED from it (live finding: without it, 69/70 invented quotes rejected)
|
||||
# the VERBATIM is obtained BEFORE (Pass 1) and enters as an input; the
|
||||
# text is GONE — the quote-finding job moved to ExtractClaims (owner
|
||||
# 1/11: TypeClaim only enforces the correct typing of the Fact).
|
||||
assert fields == {"claim", "verbatim", "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
|
||||
@@ -70,34 +85,44 @@ def test_type_claim_signature():
|
||||
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)")
|
||||
print("ok: TypeClaim = one claim + verbatim in -> one Fact (typing only; no text)")
|
||||
|
||||
|
||||
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"]))
|
||||
e = Scripted(claims=Scripted(claims=[
|
||||
Claim(claim="Pedro Sanchez is the prime minister of Spain since 2018",
|
||||
verbatim="has been the prime minister of Spain since 2018"),
|
||||
Claim(claim="Sanchez became Secretary-General of the PSOE in 2017",
|
||||
verbatim="Secretary-General of the Spanish Socialist Workers' Party (PSOE) since 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")
|
||||
assert [(c.claim, c.verbatim) for c in out] == [
|
||||
("Pedro Sanchez is the prime minister of Spain since 2018",
|
||||
"has been the prime minister of Spain since 2018"),
|
||||
("Sanchez became Secretary-General of the PSOE in 2017",
|
||||
"Secretary-General of the Spanish Socialist Workers' Party (PSOE) since 2017")], out
|
||||
print("ok: claims_from extracts the claims with their verbatim quotes")
|
||||
|
||||
|
||||
def test_fact_from_claim_types_one_fact():
|
||||
"""Pass 2 types one claim; CODE pins the Fact's quote to the given
|
||||
verbatim — the scripted fact proposes a paraphrase, and it MUST be
|
||||
overwritten (LLMs propose, code decides)."""
|
||||
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",
|
||||
object="Spain", quote="a paraphrase the TYPE model invented",
|
||||
start="2018", end=None, point=None, granularity="year")))
|
||||
f = fact_from_claim(e, claim="Pedro Sanchez is the prime minister of Spain since 2018", text="...")
|
||||
f = fact_from_claim(e, claim="Pedro Sanchez is the prime minister of Spain since 2018",
|
||||
verbatim="has been 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")
|
||||
assert f.quote == "has been the prime minister of Spain since 2018", f.quote
|
||||
print("ok: fact_from_claim types one claim; code pins quote to the verbatim")
|
||||
|
||||
|
||||
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="", text="") is None
|
||||
assert fact_from_claim(e2, claim="", verbatim="") is None
|
||||
print("ok: no claims -> no facts; untypeable claim -> None, no invention")
|
||||
|
||||
|
||||
@@ -105,25 +130,32 @@ 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."""
|
||||
semantically, only made honest: no exact duplicates survive. The
|
||||
FIRST occurrence's verbatim is the one kept."""
|
||||
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"]
|
||||
claims = [Claim(claim="Pedro Sanchez is prime minister of Spain.",
|
||||
verbatim="prime minister of Spain"),
|
||||
Claim(claim="pedro sanchez is prime minister of spain. ",
|
||||
verbatim="the repeat's quote — must not survive"),
|
||||
Claim(claim="Pedro Sanchez is Secretary-General of the PSOE.",
|
||||
verbatim="Secretary-General of the PSOE"),
|
||||
Claim(claim="Pedro Sanchez is prime minister of Spain.",
|
||||
verbatim="second repeat's quote"),
|
||||
Claim(claim="from the text. I need to list factual claims",
|
||||
verbatim="thinking leakage")]
|
||||
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")
|
||||
assert [(c.claim, c.verbatim) for c in got] == [
|
||||
("Pedro Sanchez is prime minister of Spain.", "prime minister of Spain"),
|
||||
("Pedro Sanchez is Secretary-General of the PSOE.", "Secretary-General of the PSOE"),
|
||||
("from the text. I need to list factual claims", "thinking leakage")], got
|
||||
print("ok: exact repeats deduped (case/space-insensitive); the first verbatim kept")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user