Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66e8bd4daa | ||
|
|
c3d1b5d6d3 |
@@ -7,6 +7,7 @@ Every module's docstring is the draft prompt — kept minimal on purpose.
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
import dspy
|
import dspy
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@@ -39,13 +40,27 @@ class InitSignature(dspy.Signature):
|
|||||||
desc="1-4 subquestions in dependency order; empty when the question is atomic")
|
desc="1-4 subquestions in dependency order; empty when the question is atomic")
|
||||||
|
|
||||||
|
|
||||||
|
class Attendant(str, Enum):
|
||||||
|
"""The closed set of attendants the ROUTE step may pick."""
|
||||||
|
KNOWLEDGE = "knowledge"
|
||||||
|
LOGIC = "logic"
|
||||||
|
ANALYTIC = "analytic"
|
||||||
|
NUMERIC = "numeric"
|
||||||
|
FLOOP = "floop"
|
||||||
|
USER = "user"
|
||||||
|
|
||||||
|
|
||||||
class RouteSignature(dspy.Signature):
|
class RouteSignature(dspy.Signature):
|
||||||
"""You are the ROUTE step of a reasoning machine. Pick ONE attendant for the
|
"""You are the ROUTE step of a reasoning machine. Pick ONE attendant for the
|
||||||
question. The user is a last resort when nothing else plausibly answers it."""
|
question. The user is a last resort when nothing else plausibly answers it."""
|
||||||
question: str = dspy.InputField()
|
question: str = dspy.InputField()
|
||||||
catalog: str = dspy.InputField(desc="experts | floop | user, one per line with a summary")
|
catalog: str = dspy.InputField(desc="experts | floop | user, one per line with a summary")
|
||||||
evidence: str = dspy.InputField(desc="relevant evidence rows, may be empty")
|
evidence: str = dspy.InputField(desc="relevant evidence rows, may be empty")
|
||||||
attendant: str = dspy.OutputField(desc="one of: knowledge logic analytic numeric floop user")
|
attendant: Attendant = dspy.OutputField(
|
||||||
|
desc="knowledge: factual lookups from trusted sources; logic: deriving conclusions "
|
||||||
|
"from provided premises, counting over evidence; analytic: equations/symbolic "
|
||||||
|
"relations over provided quantities; numeric: closed arithmetic over provided "
|
||||||
|
"numbers; floop: needs a sub-goal; user: nothing else plausibly answers it")
|
||||||
reason: str = dspy.OutputField(desc="one sentence, for the log")
|
reason: str = dspy.OutputField(desc="one sentence, for the log")
|
||||||
|
|
||||||
|
|
||||||
@@ -132,8 +147,11 @@ class FloopModules:
|
|||||||
return self._subs_from_typed(pred.subquestions)
|
return self._subs_from_typed(pred.subquestions)
|
||||||
|
|
||||||
def route_question(self, question: str, catalog: str, evidence: str) -> tuple[str, str]:
|
def route_question(self, question: str, catalog: str, evidence: str) -> tuple[str, str]:
|
||||||
|
"""Typed attendant: the enum is the closed contract. A response
|
||||||
|
outside the set raises AdapterParseError after dspy's re-ask —
|
||||||
|
the machine's invalid-attendant branch stays as a contract guard."""
|
||||||
pred = self.route(question=question, catalog=catalog, evidence=evidence)
|
pred = self.route(question=question, catalog=catalog, evidence=evidence)
|
||||||
return pred.attendant.strip().lower(), pred.reason.strip()
|
return str(pred.attendant.value), pred.reason.strip()
|
||||||
|
|
||||||
def expert_draft(self, question: str, expert_kind: str, evidence: str,
|
def expert_draft(self, question: str, expert_kind: str, evidence: str,
|
||||||
parent_question: str = "") -> dict:
|
parent_question: str = "") -> dict:
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
"""Typed ROUTE tests: the attendant is a closed enum — invalid picks are
|
||||||
|
impossible by schema (a response outside the set raises AdapterParseError
|
||||||
|
after dspy's own re-ask).
|
||||||
|
|
||||||
|
No network; the engine returns raw text (DummyLM bypasses the adapter —
|
||||||
|
useless here). Run: .venv/bin/python tests/typed_route.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_route_valid_enum():
|
||||||
|
raw = '''[[ ## attendant ## ]]
|
||||||
|
knowledge
|
||||||
|
|
||||||
|
[[ ## reason ## ]]
|
||||||
|
A factual lookup of who held the office before.
|
||||||
|
|
||||||
|
[[ ## completed ## ]]'''
|
||||||
|
_lm(raw)
|
||||||
|
attendant, reason = FloopModules().route_question(
|
||||||
|
"Who was the President before Pedro Sanchez?", "catalog", "")
|
||||||
|
assert attendant == "knowledge", attendant
|
||||||
|
assert reason.startswith("A factual")
|
||||||
|
print("ok: typed ROUTE parses through the real adapter path")
|
||||||
|
|
||||||
|
|
||||||
|
def test_route_invalid_attendant_raises():
|
||||||
|
# outside the closed set -> schema violation -> AdapterParseError
|
||||||
|
# (dspy's JSON re-ask gets the same bad value back and gives up)
|
||||||
|
raw = '''[[ ## attendant ## ]]
|
||||||
|
wikipedia
|
||||||
|
|
||||||
|
[[ ## reason ## ]]
|
||||||
|
It's an encyclopedia.
|
||||||
|
|
||||||
|
[[ ## completed ## ]]'''
|
||||||
|
_lm(raw)
|
||||||
|
try:
|
||||||
|
FloopModules().route_question("q", "catalog", "")
|
||||||
|
raise AssertionError("invalid attendant parsed — enum contract broken")
|
||||||
|
except AdapterParseError:
|
||||||
|
print("ok: attendant outside the enum raises AdapterParseError (schema holds)")
|
||||||
|
|
||||||
|
|
||||||
|
def test_route_enum_compatibility():
|
||||||
|
# the machine's comparisons survive typing: Attendant mixes str
|
||||||
|
from floop.machine import EXPERT_ENGINES
|
||||||
|
from floop.dspy_modules import Attendant
|
||||||
|
assert Attendant.FLOOP == "floop"
|
||||||
|
assert Attendant.KNOWLEDGE in EXPERT_ENGINES
|
||||||
|
assert Attendant.USER not in EXPERT_ENGINES
|
||||||
|
print("ok: enum is str-compatible — machine comparisons unchanged")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_route_valid_enum()
|
||||||
|
test_route_invalid_attendant_raises()
|
||||||
|
test_route_enum_compatibility()
|
||||||
|
print("\ntyped-ROUTE tests passed")
|
||||||
Reference in New Issue
Block a user