diff --git a/code/floop/dspy_modules.py b/code/floop/dspy_modules.py index 3cf75c8..20fe634 100644 --- a/code/floop/dspy_modules.py +++ b/code/floop/dspy_modules.py @@ -64,6 +64,14 @@ class RouteSignature(dspy.Signature): 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): """You are an expert attendant answering a small, focused subquestion. 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'") 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") - 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): @@ -98,7 +108,8 @@ class AnswerSignature(dspy.Signature): marks verification status separately, do not hedge about it.""" goal: 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") @@ -160,11 +171,12 @@ class FloopModules: return { "value": pred.value.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: - 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() diff --git a/code/tests/typed_expert.py b/code/tests/typed_expert.py new file mode 100644 index 0000000..96c8e82 --- /dev/null +++ b/code/tests/typed_expert.py @@ -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") \ No newline at end of file