A) SET GATE: enumerations are answered only from membership sets, never from mention rows (the probe early-exit that served 'Armenia, Cyprus, Georgia, Turkey' to 'what countries are in Europe' is dead). Sets carry origin_question; the gate matches stem-Jaccard>=0.5 so a set is served to its own question only. B) SETOP ENGINE: logic's first real engine — a count over list rows is computed (difference/intersection/count over norm_name'd items, table markers 'Austria*' == 'Austria'), before ROUTE, zero LLM calls, verified=True because the computation IS the check. C) Catalog honesty: knowledge 'lists are ONE fetch', logic 'how-many over lists in evidence', floop 'NOT mere size'. Plus four substrate bugs the live runs exposed: LM-proxy prompt cache (instant answers are cache hits, never trust zero latency), _context rendering list rows as scalars ('E1: 50' routed 'those countries' to user), <br> concatenation in multi-value name cells, multi-line refs leaking into values ('franceincludes the 101 departments' -> France dropped from EU -> 24 not 23). Cold E2E: Q1 50 states ingested, Q2 27 EU members ingested, Q3 difference computed = 23, cited over E1/E2. 22/22 tests.
82 lines
4.3 KiB
Python
82 lines
4.3 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 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 |