knowledge: typed outputs for all 5 remaining signatures (block PR)
The typed-output migration completes: every knowledge signature now declares its structure as Python types and dspy renders/parses it. - TranslateSignature: queries/entities as list[str] — the 'QUERIES:'/'ENTITIES:' line protocol and its prefix-scraping parser are gone - AnswerFromKG: confidence is a local Confidence(str, Enum) (knowledge stays decoupled from floop) - SelectTableSignature: chosen is TableChoice | None (index int, column, set_name) — 'NONE' string handling gone; origin_question dropped (it duplicated the input) - SkimPromoteSignature: decisions as list[PromotionDecision] (table_index int, promote bool, column) — the 'idx: YES col' regex parser gone - VerifyCountSignature: unchanged shape (quote str + NOQUOTE convention predates typed migration; kept — it is an answer-shaped field, not a format protocol) Callers consume typed objects directly. Test fakes updated to the new contracts (promote/store/guards). VERIFIED: - suite 14/14 green (84 tests) - live E2E (ollama-cloud glm-5.3-flash, Italy rivers, fresh KG): correct answer 2014 m3/s (Po 1540 + Adige 235 + Tiber 239) with verified join; wire prompts show list[str]/TableChoice/Confidence/ list[Fact] schemas rendered natively
This commit is contained in:
+38
-16
@@ -6,10 +6,19 @@ the FULL document text — so chunking can never fake a quote.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from enum import Enum
|
||||
|
||||
import dspy
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Confidence(str, Enum):
|
||||
"""Honest self-assessment of an answer drawn from KG rows."""
|
||||
HIGH = "high"
|
||||
MEDIUM = "medium"
|
||||
LOW = "low"
|
||||
|
||||
|
||||
class Fact(BaseModel):
|
||||
"""One fact extracted from a text, with its verbatim supporting quote."""
|
||||
subject: str = dspy.OutputField(desc="the fact's subject entity")
|
||||
@@ -23,13 +32,13 @@ 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 and the evidence rows already gathered."""
|
||||
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")
|
||||
queries: list[str] = dspy.OutputField(
|
||||
desc="1-2 Wikipedia search queries, most specific first")
|
||||
entities: list[str] = dspy.OutputField(
|
||||
desc="the resolved entity names the answer is about")
|
||||
|
||||
|
||||
class ExtractSignature(dspy.Signature):
|
||||
@@ -59,7 +68,8 @@ class AnswerFromKG(dspy.Signature):
|
||||
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")
|
||||
confidence: Confidence = dspy.OutputField(
|
||||
desc="high: rows settle it; medium: plausible but partial; low: weak support")
|
||||
|
||||
|
||||
class VerifyCountSignature(dspy.Signature):
|
||||
@@ -81,18 +91,31 @@ class VerifyCountSignature(dspy.Signature):
|
||||
quote: str = dspy.OutputField(desc="verbatim sentence stating the count, or NOQUOTE")
|
||||
|
||||
|
||||
class TableChoice(BaseModel):
|
||||
"""The table chosen as the answer's enumeration, if any."""
|
||||
table_index: int = dspy.OutputField(desc="the chosen table's index")
|
||||
column: str = dspy.OutputField(desc="header name of the column holding the answer elements")
|
||||
set_name: str = dspy.OutputField(desc="short name of the set this table enumerates")
|
||||
|
||||
|
||||
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."""
|
||||
answer?"""
|
||||
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")
|
||||
chosen: TableChoice | None = dspy.OutputField(
|
||||
desc="the chosen table and its answer column; null when NO table enumerates the answer")
|
||||
|
||||
|
||||
class PromotionDecision(BaseModel):
|
||||
"""One table's promotion verdict."""
|
||||
table_index: int = dspy.OutputField(desc="the table's index from the list")
|
||||
promote: bool = dspy.OutputField(
|
||||
desc="True when this table holds data about those entities for the goal")
|
||||
column: str = dspy.OutputField(
|
||||
desc="when promoted: the header holding the goal's values; empty otherwise")
|
||||
|
||||
|
||||
class SkimPromoteSignature(dspy.Signature):
|
||||
@@ -102,13 +125,12 @@ class SkimPromoteSignature(dspy.Signature):
|
||||
and the table's words may differ — 'water flow' vs 'Flow at the mouth').
|
||||
Given the run's goal, the entities it is about, and the tables' identity
|
||||
(caption, headers, row count, quantity signals — NOT the rows), decide
|
||||
per table whether it holds data about those entities for the goal.
|
||||
Output one line per table: 'idx: YES <column>' or 'idx: NO' — the column
|
||||
is the header holding the goal's values."""
|
||||
per table whether it holds data about those entities for the goal."""
|
||||
goal: str = dspy.InputField(desc="the run's overall goal question")
|
||||
entities: str = dspy.InputField(desc="the entities the goal is about, comma separated; may be empty")
|
||||
tables: str = dspy.InputField(desc="one line per table: 'idx | caption | header: a, b, c | rows: N | quantities: x, y'")
|
||||
decision: str = dspy.OutputField(desc="one line per table: 'idx: YES <column>' or 'idx: NO'")
|
||||
decisions: list[PromotionDecision] = dspy.OutputField(
|
||||
desc="one verdict per table in the list")
|
||||
|
||||
|
||||
def parse_facts(raw: str) -> list[dict]:
|
||||
|
||||
Reference in New Issue
Block a user