The LLM at the INPUT boundary only: one structured-output call (TranslateToDSL — question + the store's relation vocabulary in, typed DSL out), VALIDATED before execution (validate_query: the shared grammar — unknown filter/select, empty rel_any, malformed lookup are rejected deterministically; a mistranslation can never reach evaluate()). The evaluator (3a) closes the path; re-execution IS the check. _prover_answer runs BEFORE any judge: a closed path returns the answer with verified=True and the proof term; a multi-binding returns the SET with ambiguous=True + alternatives (the three readings as what they are — deterministic, no ablation rounds, no (K5) surface: the judge never sees these questions). No translation / no closed path / re-execution mismatch -> the judge path, unchanged; the prover can never kill a run. Wiring is additive: answer_question tries the prover first, falls back honestly. Tests: translate_dsl.py 4/4 (signature, live-shape translation, mistranslation rejection, honest decline -> None).
62 lines
2.7 KiB
Python
62 lines
2.7 KiB
Python
"""The question->DSL translator (phase 3b) — the LLM at the INPUT boundary.
|
|
|
|
One structured-output call (the MASK/EXTRACT class): the question + the
|
|
store's relation vocabulary in; a typed DSL term out; VALIDATED BEFORE
|
|
EXECUTION — a mistranslation is an invalid term caught deterministically,
|
|
never a wrong answer. The model never sees the goal, the evidence, or the
|
|
answer; it sees the question and which relations exist. If the question is
|
|
not translatable, the call returns None and the caller falls back to the
|
|
judge path, honestly.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import dspy
|
|
|
|
from .prover import validate_query
|
|
|
|
|
|
class TranslateToDSL(dspy.Signature):
|
|
"""You are the TRANSLATE-TO-QUERY step of a reasoning machine. Turn the
|
|
question into ONE typed query over the knowledge graph, choosing from
|
|
the given relations ONLY. The machine executes the query deterministically.
|
|
|
|
Shapes you may build:
|
|
- lookup: {"subject": "<entity>", "rel_any": ["<one or more relations>"], "bind": "<var>"}
|
|
- a second lookup with {"subject": "?x", "rel_any": [...], "bind": "<var>",
|
|
"same_value_as": "<bound var>"} — anyone holding the SAME value (e.g. the
|
|
same office) as the first lookup bound
|
|
- filters: {"open_now": ["<var>"]} (the fact holds now), {"before": ["<a>", "<b>"]}
|
|
(a's window ended before b's started), {"neq_value": ["<var-or-?x>", "<literal>"]}
|
|
(a bound name differs from the literal — e.g. exclude the queried entity)
|
|
- select: "all" (every binding — the question asks for the SET) or
|
|
"latest_end" (the most recent one that ended)
|
|
|
|
If the question cannot be expressed with these shapes, return an empty
|
|
query ({} or null) — never force a wrong translation."""
|
|
question: str = dspy.InputField()
|
|
relations: str = dspy.InputField(
|
|
desc="the relations that exist in the graph, comma separated")
|
|
query: dict | None = dspy.OutputField(
|
|
desc="the typed query as JSON, or null when not expressible")
|
|
|
|
|
|
_KNOWN_FILTERS = {"open_now", "before", "neq_value"}
|
|
_KNOWN_SELECTS = {"all", "latest_end"}
|
|
|
|
|
|
def translate(expert, question: str, relations: list[str]) -> dict | None:
|
|
"""One call: question + relation vocabulary -> a validated DSL term.
|
|
|
|
None when the model declines or the proposal is invalid — the caller
|
|
falls back to the judge path (the prover never executes an invalid
|
|
term; code decides, the model proposes).
|
|
"""
|
|
pred = expert.translate_dspy(question=question,
|
|
relations=", ".join(sorted(set(relations))))
|
|
q = getattr(pred, "query", None)
|
|
if not q:
|
|
return None
|
|
ok, _reason = validate_query(q)
|
|
return q if ok else None |