- VerifyCountSignature: LLM finds ONE verbatim sentence in the article prose (tables excluded by construction) stating the set's count - _verify_count: code checks containment + digit equality — no LLM compares numbers; NOQUOTE -> count_corroborated=false (honest, one call, never loops) - row verified <=> complete_per_source AND count_corroborated - DESIGN §12 documents now vs deferred (extension, criteria, cross-article) - live: EU question cold — E1 (50) + E2 (27) both corroborated with quotes, 23 computed; 25/25 tests
101 lines
5.1 KiB
Python
101 lines
5.1 KiB
Python
"""LLM extraction per chunk (map) — dspy Signatures, minimal per floop convention.
|
|
|
|
The extractor sees ONE chunk (+ heading path); it proposes typed candidates with
|
|
VERBATIM spans. The containment check (checks.py) validates every span against
|
|
the FULL document text — so chunking can never fake a quote.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import dspy
|
|
|
|
|
|
class TranslateSignature(dspy.Signature):
|
|
"""You are the TRANSLATE step of a knowledge machine. Turn the question
|
|
into Wikipedia search queries AND resolved entity terms. Resolve
|
|
references ('that state', 'the same river') using the context: the original
|
|
question and the evidence rows already gathered. Output two lines:
|
|
QUERIES: 1-2 wikipedia search queries, comma separated.
|
|
ENTITIES: the resolved entity names the answer is about, comma separated."""
|
|
question: str = dspy.InputField()
|
|
context: str = dspy.InputField(desc="the original question + evidence rows with answers already found; resolve pronouns from here; may be empty")
|
|
queries: str = dspy.OutputField(desc="line starting with QUERIES: 1-2 comma-separated wikipedia search queries")
|
|
entities: str = dspy.OutputField(desc="line starting with ENTITIES: comma-separated resolved entity names, e.g. ENTITIES: France, longest river")
|
|
|
|
|
|
class ExtractSignature(dspy.Signature):
|
|
"""You are the EXTRACT step of a knowledge machine. From the given text,
|
|
extract facts that help answer the question. Each fact is one line:
|
|
subject | relation | object | verbatim quote from the text.
|
|
The quote must be the FULL sentence or table ROW that states the fact,
|
|
copied exactly (at least 10 characters). Only facts the text states;
|
|
no inference."""
|
|
question: str = dspy.InputField()
|
|
heading: str = dspy.InputField(desc="section path of this chunk; context only")
|
|
text: str = dspy.InputField()
|
|
facts: str = dspy.OutputField(
|
|
desc="one fact per line: subject | relation | object | verbatim quote; empty if none")
|
|
|
|
|
|
class AnswerFromKG(dspy.Signature):
|
|
"""You are the ANSWER step of a knowledge machine. Answer the question using
|
|
ONLY the given knowledge-graph rows, each with its quote and source. Cite
|
|
rows by id. If the rows do not answer it, say UNKNOWN."""
|
|
question: str = dspy.InputField()
|
|
rows: str = dspy.InputField(desc="kg rows: id, subject, relation, object, quote, url")
|
|
value: str = dspy.OutputField(desc="the answer, or UNKNOWN")
|
|
quote: str = dspy.OutputField(desc="VERBATIM supporting quote from the rows, or empty")
|
|
confidence: str = dspy.OutputField(desc="high medium low")
|
|
|
|
|
|
class VerifyCountSignature(dspy.Signature):
|
|
"""You are the VERIFY step of a knowledge machine. A list answer has been
|
|
ingested from a table in one article (set_name, N elements). Find ONE
|
|
sentence in the article prose that states the expected count of that set
|
|
(independently of the table). Quote it VERBATIM. If no such sentence
|
|
exists, write NOQUOTE.
|
|
|
|
Example:
|
|
set_name: European Union member states
|
|
count: 27
|
|
GOOD quote: "The European Union (EU) is a supranational union of 27
|
|
member states that are party to the EU's founding treaties"
|
|
BAD (not a count statement): a row of the table itself."""
|
|
set_name: str = dspy.InputField()
|
|
count: str = dspy.InputField()
|
|
text: str = dspy.InputField(desc="the article prose WITHOUT the tables")
|
|
quote: str = dspy.OutputField(desc="verbatim sentence stating the count, or NOQUOTE")
|
|
|
|
|
|
class SelectTableSignature(dspy.Signature):
|
|
"""You are the SELECT TABLE step of a knowledge machine. Given a question
|
|
and the tables found in an article (index, caption, header columns, row
|
|
count — NOT the rows), decide: does one of these tables enumerate the
|
|
answer? Output the table index and the column holding the answer
|
|
elements, or NONE."""
|
|
question: str = dspy.InputField()
|
|
tables: str = dspy.InputField(desc="one line per table: 'idx | caption | header: a, b, c | rows: N'")
|
|
table_index: str = dspy.OutputField(desc="the chosen table index (e.g. '2'), or NONE")
|
|
column: str = dspy.OutputField(desc="header name of the column holding the answer elements; empty if NONE")
|
|
set_name: str = dspy.OutputField(desc="short name of the set this table enumerates, e.g. 'sovereign states in Europe'; empty if NONE")
|
|
origin_question: str = dspy.OutputField(desc="the question verbatim, for provenance; empty if NONE")
|
|
|
|
|
|
def parse_facts(raw: str) -> list[dict]:
|
|
"""Parse the extractor's piped lines; malformed lines are dropped+counted.
|
|
|
|
Split into AT MOST 4 fields — the quote is the last field and may itself
|
|
contain pipes (table rows like 'France | 68,635,900 | 15.2%')."""
|
|
out: list[dict] = []
|
|
for line in (raw or "").strip().splitlines():
|
|
line = line.strip()
|
|
if not line or "|" not in line:
|
|
continue
|
|
parts = [p.strip() for p in line.split("|", 3)]
|
|
if len(parts) < 4:
|
|
continue
|
|
subject, relation, obj, quote = parts
|
|
if not (subject and relation and obj and quote):
|
|
continue
|
|
out.append({"subject": subject, "relation": relation,
|
|
"object": obj, "quote": quote})
|
|
return out |