floop: typed EXPERT confidence + ANSWER bool input

The last floop signatures migrate to the typed-output law:
- ExpertSignature.confidence: Confidence(str, Enum) high|medium|low,
  with guidance in the desc — out-of-set values are schema violations
- AnswerSignature.unverified: real bool InputField (callers already
  passed bools; the old signature stringified them)
- expert_draft/synthesize consume the typed values directly

VERIFIED: suite 14/14 green (new tests/typed_expert.py: enum parses
through the real adapter path, bad confidence raises, bool renders).
The floop side of the typed migration is COMPLETE — knowledge
signatures are the next (last) block.
This commit is contained in:
2026-10-22 15:04:43 +02:00
parent 66e8bd4daa
commit a6453b0cb3
2 changed files with 106 additions and 4 deletions
+16 -4
View File
@@ -64,6 +64,14 @@ class RouteSignature(dspy.Signature):
reason: str = dspy.OutputField(desc="one sentence, for the log") reason: str = dspy.OutputField(desc="one sentence, for the log")
class Confidence(str, Enum):
"""The expert's honest self-assessment (baseline draft — no engine
verifies the expert yet)."""
HIGH = "high"
MEDIUM = "medium"
LOW = "low"
class ExpertSignature(dspy.Signature): class ExpertSignature(dspy.Signature):
"""You are an expert attendant answering a small, focused subquestion. """You are an expert attendant answering a small, focused subquestion.
Use the evidence rows when they answer it; otherwise answer from your own Use the evidence rows when they answer it; otherwise answer from your own
@@ -77,7 +85,9 @@ class ExpertSignature(dspy.Signature):
value: str = dspy.OutputField(desc="the answer value in its declared type, or 'UNKNOWN'") value: str = dspy.OutputField(desc="the answer value in its declared type, or 'UNKNOWN'")
source_quote: str = dspy.OutputField( source_quote: str = dspy.OutputField(
desc="VERBATIM text from the given evidence if the answer comes from it; EMPTY when answering from your own knowledge") desc="VERBATIM text from the given evidence if the answer comes from it; EMPTY when answering from your own knowledge")
confidence: str = dspy.OutputField(desc="high medium low") confidence: Confidence = dspy.OutputField(
desc="high: the evidence or your knowledge settles it; medium: plausible "
"but not fully settled; low: a guess you would verify first")
class MissingSignature(dspy.Signature): class MissingSignature(dspy.Signature):
@@ -98,7 +108,8 @@ class AnswerSignature(dspy.Signature):
marks verification status separately, do not hedge about it.""" marks verification status separately, do not hedge about it."""
goal: str = dspy.InputField() goal: str = dspy.InputField()
evidence: str = dspy.InputField() evidence: str = dspy.InputField()
unverified: str = dspy.InputField(desc="'true' if the prover could not verify derivability") unverified: bool = dspy.InputField(
desc="True when the prover could not verify the answer's derivability from the evidence")
final_answer: str = dspy.OutputField(desc="answer in natural language; cite only given row ids; no outside facts") final_answer: str = dspy.OutputField(desc="answer in natural language; cite only given row ids; no outside facts")
@@ -160,11 +171,12 @@ class FloopModules:
return { return {
"value": pred.value.strip(), "value": pred.value.strip(),
"source_quote": pred.source_quote.strip(), "source_quote": pred.source_quote.strip(),
"confidence": pred.confidence.strip().lower(), "confidence": str(pred.confidence.value),
} }
def synthesize(self, goal: str, evidence: str, unverified: bool) -> str: def synthesize(self, goal: str, evidence: str, unverified: bool) -> str:
pred = self.answer(goal=goal, evidence=evidence, unverified="true" if unverified else "false") pred = self.answer(goal=goal, evidence=evidence,
unverified=unverified)
return pred.final_answer.strip() return pred.final_answer.strip()
+90
View File
@@ -0,0 +1,90 @@
"""Typed EXPERT + ANSWER tests (the last floop signatures): confidence is
a closed enum; unverified is a real bool InputField.
No network; RawEngine returns raw text (DummyLM bypasses the adapter).
Run: .venv/bin/python tests/typed_expert.py
"""
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import warnings
warnings.filterwarnings("ignore")
import dspy
from dspy._vendor.lm15 import Response, TextPart, Message, Usage
from dspy.utils.exceptions import AdapterParseError
from floop.dspy_modules import FloopModules
def _lm(scripted_raw: str) -> None:
class RawEngine:
def complete(self, request):
return Response(
id="x", model="t",
message=Message(role="assistant",
parts=(TextPart(text=scripted_raw),)),
finish_reason="stop",
usage=Usage(input_tokens=1, output_tokens=1, total_tokens=2))
dspy.configure(lm=dspy.LM("openai/test", engine=RawEngine(), cache=False))
def test_expert_confidence_enum():
raw = '''[[ ## value ## ]]
Mariano Rajoy
[[ ## source_quote ## ]]
his predecessor Mariano Rajoy
[[ ## confidence ## ]]
high
[[ ## completed ## ]]'''
_lm(raw)
out = FloopModules().expert_draft("Who was the President before Pedro Sanchez?",
"knowledge", "E1: his predecessor Mariano Rajoy")
assert out["value"] == "Mariano Rajoy" and out["confidence"] == "high", out
print("ok: typed EXPERT parses confidence enum through the real path")
def test_expert_bad_confidence_raises():
raw = '''[[ ## value ## ]]
42
[[ ## source_quote ## ]]
[[ ## confidence ## ]]
certain
[[ ## completed ## ]]'''
_lm(raw)
try:
FloopModules().expert_draft("q", "knowledge", "")
raise AssertionError("bad confidence parsed — enum broken")
except AdapterParseError:
print("ok: confidence outside the enum raises AdapterParseError")
def test_answer_bool_input():
# the bool InputField renders as a plain value in the prompt and the
# string answer parses; synthesize passes a real bool now
raw = '''[[ ## final_answer ## ]]
Mariano Rajoy (E1).
[[ ## completed ## ]]'''
_lm(raw)
ans = FloopModules().synthesize("Who was the President before Pedro Sanchez?",
"E1: Mariano Rajoy", unverified=True)
assert ans == "Mariano Rajoy (E1).", ans
print("ok: ANSWER takes a bool unverified and parses through the real path")
if __name__ == "__main__":
test_expert_confidence_enum()
test_expert_bad_confidence_raises()
test_answer_bool_input()
print("\ntyped EXPERT+ANSWER tests passed")