knowledge: typed Fact extraction via dspy native structured output
His direction: stop explaining string formats to the LLM and parsing
them back with Python — that's exactly what dspy does natively and
better. The pipe format ('subject | relation | object | quote') was
fragile (run-L verbose values, pipe-in-quote ambiguity, STOP_TABLE
string protocol).
THE CHANGE (extractor only, PR-10 scope):
- Fact(BaseModel) with subject/relation/object/quote OutputFields
- ExtractSignature.facts: list[Fact] — the adapter renders the JSON
schema into the prompt itself (verified in a live run's prompt log)
and parses responses back into typed Fact objects
- STOP_TABLE string protocol -> stop: bool OutputField
- expert.py consumes typed objects; the cand dict is built at the
check_candidate boundary only (checks.py unchanged)
- parse_facts/parse_stop_table kept as LEGACY for existing tests only
VERIFIED:
- dspy 3.4.1 pattern E2E'd first with a raw-text engine: list[Fact]
parses back as Fact objects, schema rendered in prompt (tmp probes)
- full suite 12/12 green (74 tests) — tablefrags.py updated to script
typed Fact objects
- live E2E run (ollama-cloud glm-5.3-flash, Italy rivers): correct
Po/Adige/Tiber answer; prompt log shows the native JSON schema;
table rows ingested cleanly (22 rows, quotes verbatim)
NEXT (his direction, not this PR): apply the same typed-output law to
ALL floop signatures — INIT subquestions, ROUTE engine choice,
AnswerFromKG, SelectTable, SkimPromote, Translate, VerifyCount.
This commit is contained in:
@@ -11,6 +11,7 @@ sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # code/
|
||||
from knowledge.wiki import (parse_tables, tables_as_chunks, format_table_frag,
|
||||
parse_stop_table)
|
||||
from knowledge.expert import KnowledgeExpert, KnowledgeConfig
|
||||
from knowledge.extract import Fact
|
||||
from knowledge.store import KnowledgeStore
|
||||
from knowledge.wiki import WikiPage
|
||||
|
||||
@@ -91,23 +92,26 @@ def test_process_page_fragments():
|
||||
wikitext=WIKITEXT_2TABLES)
|
||||
page.tables = parse_tables(WIKITEXT_2TABLES)
|
||||
|
||||
# scripted extractor: world-table parts -> STOP_TABLE; Italy table -> one fact
|
||||
# scripted extractor: world-table parts -> stop=True; Italy table -> one Fact
|
||||
calls = []
|
||||
|
||||
class FakeOut:
|
||||
def __init__(self, facts):
|
||||
def __init__(self, facts, stop=False):
|
||||
self.facts = facts
|
||||
self.stop = stop
|
||||
|
||||
def fake_extract(question, heading, text, _calls=calls):
|
||||
_calls.append((question, heading, text))
|
||||
if "World rivers" in text:
|
||||
return FakeOut("STOP_TABLE")
|
||||
return FakeOut([], stop=True)
|
||||
if "Rivers of Italy" in text:
|
||||
# quote = the full rendered row context (header + row), verbatim
|
||||
# from the fragment the extractor saw
|
||||
return FakeOut("Po | Flow at the mouth | 1540 m3/s | "
|
||||
"River | Flow at the mouth Po | 1540")
|
||||
return FakeOut("")
|
||||
return FakeOut([Fact(
|
||||
subject="Po", relation="Flow at the mouth",
|
||||
object="1540 m3/s",
|
||||
quote="River | Flow at the mouth Po | 1540")])
|
||||
return FakeOut([])
|
||||
|
||||
e.extract = fake_extract
|
||||
|
||||
|
||||
Reference in New Issue
Block a user