Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
Merge pull request #520 from aashish254/feat-decide-batch
Nandakishor6588e96b26f3
diff --cc laya/agent.pyindex 9d3b0791,64353cb8..a06c71a5--- a/laya/agent.py+++ b/laya/agent.py@@@ -1185,8 -1119,23 +1185,23 @@@ class Agent(HookRegistry)"""from .structured import decide as _decidereturn _decide(self, state, schema, questions=questions,- return_details=return_details, **predict_kwargs)+ return_details=return_details, min_confidence=min_confidence, **predict_kwargs)+ def decide_batch(self, states: List[Union[str, dict, list]], schema: Any = None, *,+ questions: Optional[Dict[str, Any]] = None,+ return_details: bool = False, **predict_kwargs) -> List[Any]:+ """Answer many states against one schema (JSON schema or pydantic model) in one batched call.++ The throughput form of :meth:`decide`: the schema is planned once and its questions+ run over every state through :meth:`predict_batch` (shared forward passes, results+ in input order), then each state's answers are projected as ``decide`` does. Extra+ keyword arguments (``batch_size=``, ``lang=``, ``hooks=``, ...) are forwarded to+ ``predict_batch``. See `laya.structured`.+ """+ from .structured import decide_batch as _decide_batch+ return _decide_batch(self, states, schema, questions=questions,+ return_details=return_details, **predict_kwargs)+def __repr__(self) -> str:return "Agent(model_id=%r, device=%s)" % (self.model_id, getattr(self, "device", None))diff --cc laya/router.pyindex 2b477a93,34642b56..5c8c3b1d--- a/laya/router.py+++ b/laya/router.py@@@ -745,8 -646,23 +745,23 @@@ class Router(HookRegistry)"""from .structured import decide as _decidereturn _decide(self, state, schema, questions=questions,- return_details=return_details, **predict_kwargs)+ return_details=return_details, min_confidence=min_confidence, **predict_kwargs)+ def decide_batch(self, states: Sequence[Any], schema: Any = None, *,+ questions: Optional[Dict[str, Any]] = None,+ return_details: bool = False, **predict_kwargs) -> List[Any]:+ """Answer many states against one schema (JSON schema or pydantic model) in one batched call.++ The throughput form of :meth:`decide`: the schema is planned once and its questions+ run over every state through :meth:`predict_batch` (grouped forward passes, results in+ input order), then each state's answers are projected as ``decide`` does. Extra keyword+ arguments (``batch_size=``, ``model=``, ``hooks=``, ...) are forwarded to+ ``predict_batch``. See `laya.structured`.+ """+ from .structured import decide_batch as _decide_batch+ return _decide_batch(self, states, schema, questions=questions,+ return_details=return_details, **predict_kwargs)+def __enter__(self):return selfdiff --cc tests/test_structured.pyindex 24a09253,9329d4be..fcf4ae2c--- a/tests/test_structured.py+++ b/tests/test_structured.py@@@ -173,54 -174,120 +174,167 @@@ runner = FakeRunner(ANSWERSdecide(runner, "s", schema=SCHEMA, hooks_raise=False)check("decide/forwards predict kwargs", runner.calls[0]["kwargs"], {"hooks_raise": False})+# --------------------------------------------------------------- min_confidence abstention (#361)+# Default behavior unchanged: all fields project normally when min_confidence is omitted+runner_default = FakeRunner(ANSWERS)+out_default = decide(runner_default, "s", schema=SCHEMA)+check("decide/default department unchanged", out_default["department"], "billing")+check("decide/default urgency unchanged", out_default["urgency"], 2)+check("decide/default needs_human unchanged", out_default["needs_human"], True)+check("decide/default priority unchanged", out_default["priority"], 2)+check_true("decide/default has no low_confidence flags",+ not any(a.get("low_confidence") for a in ANSWERS.values()))++# With min_confidence=0.85: answers below 0.85 project as None, raw confidence preserved+runner_gated = FakeRunner({k: dict(v) for k, v in ANSWERS.items()})+out_gated = decide(runner_gated, "s", schema=SCHEMA, min_confidence=0.85)+check("decide/gated high-confidence field kept", out_gated["department"], "billing")+check("decide/gated low-confidence score becomes None", out_gated["urgency"], None)+check("decide/gated low-confidence noul becomes None", out_gated["needs_human"], None)+check("decide/gated low-confidence choice becomes None", out_gated["priority"], None)++# With return_details=True, details keep raw confidence and answers dict+runner_det = FakeRunner({k: dict(v) for k, v in ANSWERS.items()})+det = decide(runner_det, "s", schema=SCHEMA, min_confidence=0.85, return_details=True)+check("decide/details values has None for low conf", det.values["urgency"], None)+check("decide/details raw confidence preserved", det.confidence["urgency"], 0.5)+check_true("decide/details answers flag set", det.answers["urgency"].get("low_confidence") is True)+check("decide/details high conf value kept", det.values["department"], "billing")+check_true("decide/details high conf flag unset", det.answers["department"].get("low_confidence") is not True)++# Direct projection with low_confidence: True in answer+answers_with_flag = {+ "department": {"type": "choice", "choice": "billing", "confidence": 0.4, "low_confidence": True},+ "urgency": {"type": "score", "score": 2.0, "confidence": 0.9, "probabilities": {"0": 0.0, "1": 0.1, "2": 0.9}, "legend": {}},+}+proj = answers_to_json(answers_with_flag, SCHEMA)+check("project/flagged answer becomes None", proj["department"], None)+check("project/unflagged answer keeps value", proj["urgency"], 2)++# Invalid min_confidence validation+check_raises("decide/rejects min_confidence > 1", ValueError,+ lambda: decide(runner, "s", schema=SCHEMA, min_confidence=1.2))+check_raises("decide/rejects min_confidence < 0", ValueError,+ lambda: decide(runner, "s", schema=SCHEMA, min_confidence=-0.1))+check_raises("decide/rejects bool min_confidence", ValueError,+ lambda: decide(runner, "s", schema=SCHEMA, min_confidence=True))+check_raises("decide/rejects str min_confidence", ValueError,+ lambda: decide(runner, "s", schema=SCHEMA, min_confidence="0.85"))++ # --------------------------------------------------------------- decide_batch+ class FakeBatchRunner:+ """predict_batch(states, questions, **kwargs) like Agent's/Router's: one+ result dict per state, in input order."""++ def __init__(self, answers_by_state):+ self.answers_by_state = answers_by_state+ self.calls = []++ def predict_batch(self, states, questions, **kwargs):+ self.calls.append({"states": states, "questions": questions, "kwargs": kwargs})+ # like Router.predict_batch: empty answers only for a state that was+ # genuinely skipped by a hook -- the fake keys them off an unknown text.+ return [{"answers": self.answers_by_state.get(s, {}), "routing": {"model": "english"}}+ for s in states]+++ STATES = ["first ticket", "second ticket"]+ BATCH_ANSWERS = {+ "first ticket": ANSWERS,+ "second ticket": {+ "department": {"type": "choice", "choice": "sales", "confidence": 0.8,+ "probabilities": {"billing": 0.0, "support": 0.1, "sales": 0.9}},+ "urgency": {"type": "score", "score": 0.4, "confidence": 0.5,+ "probabilities": {"0": 0.7, "1": 0.2, "2": 0.1}},+ "needs_human": {"type": "noul", "noul": 0.1, "confidence": 0.8},+ "priority": {"type": "choice", "choice": "1", "confidence": 0.6,+ "probabilities": {"1": 0.6, "2": 0.3, "3": 0.1}},+ },+ }++ batch_runner = FakeBatchRunner(BATCH_ANSWERS)+ outs = decide_batch(batch_runner, STATES, schema=SCHEMA)+ check("batch/one predict_batch call", len(batch_runner.calls), 1)+ check("batch/states forwarded", batch_runner.calls[0]["states"], STATES)+ check("batch/questions planned once", batch_runner.calls[0]["questions"],+ {f.name: f.question for f in plan_from_json_schema(SCHEMA)})+ check("batch/input order", [o["department"] for o in outs], ["billing", "sales"])+ check("batch/projection matches decide", outs[0], decide(FakeRunner(ANSWERS), "x", schema=SCHEMA))+ check("batch/per-state values", outs[1],+ {"department": "sales", "urgency": 0, "needs_human": False, "priority": 1})++ # kwargs are forwarded to predict_batch (batch_size, model, hooks, ...)+ batch_runner = FakeBatchRunner(BATCH_ANSWERS)+ decide_batch(batch_runner, STATES, schema=SCHEMA, batch_size=4, model="english")+ check("batch/forwards predict kwargs", batch_runner.calls[0]["kwargs"],+ {"batch_size": 4, "model": "english"})++ # return_details: one DecisionResult per state with the usual fields+ details = decide_batch(FakeBatchRunner(BATCH_ANSWERS), STATES, schema=SCHEMA, return_details=True)+ check_true("batch/details are DecisionResult", all(isinstance(d, laya.DecisionResult) for d in details))+ check("batch/details values", [d.values["department"] for d in details], ["billing", "sales"])+ check("batch/details confidence", details[0].confidence["department"], 0.9)+ check("batch/details routing", details[0].routing, {"model": "english"})++ # questions= is the raw-answers pass-through, exactly like decide()+ raw = decide_batch(FakeBatchRunner({"a": {"x": {"type": "noul", "noul": 0.9, "confidence": 0.9}}}),+ ["a"], questions={"x": {"type": "noul", "instructions": "?"}})+ check("batch/questions pass-through", raw, [{"x": {"type": "noul", "noul": 0.9, "confidence": 0.9}}])+++ class FakeRouterLike:+ """predict_batch(requests, **kwargs) like Router's: one request dict per+ state (no positional questions), routed through route_batch."""++ def __init__(self, answers_by_state):+ self.answers_by_state = answers_by_state+ self.calls = []++ def route_batch(self, requests):+ return [{"model": "english"} for _ in requests]++ def predict_batch(self, requests, **kwargs):+ self.calls.append({"requests": requests, "kwargs": kwargs})+ return [{"answers": self.answers_by_state.get(r["state"], {}),+ "routing": {"model": "english"}} for r in requests]+++ router_like = FakeRouterLike(BATCH_ANSWERS)+ router_outs = decide_batch(router_like, STATES, schema=SCHEMA, batch_size=3)+ expected_questions = {f.name: f.question for f in plan_from_json_schema(SCHEMA)}+ check("batch/router one call", len(router_like.calls), 1)+ check("batch/router request dicts", [r["state"] for r in router_like.calls[0]["requests"]], STATES)+ check("batch/router questions per request",+ all(r["questions"] == expected_questions for r in router_like.calls[0]["requests"]), True)+ check("batch/router kwargs forwarded", router_like.calls[0]["kwargs"], {"batch_size": 3})+ check("batch/router projection matches agent path", router_outs, outs)++ # a string is a sequence of states only accidentally: refuse it like route_batch does+ check_raises("batch/rejects string states", TypeError,+ lambda: decide_batch(FakeBatchRunner({}), "abc", schema=SCHEMA))+ check_raises("batch/requires schema or questions", ValueError,+ lambda: decide_batch(FakeBatchRunner({}), ["a"]))+ check_raises("batch/rejects both", ValueError,+ lambda: decide_batch(FakeBatchRunner({}), ["a"], schema=SCHEMA, questions={}))+ check_raises("batch/rejects bad schema", SchemaError,+ lambda: decide_batch(FakeBatchRunner({}), ["a"],+ schema={"type": "object", "properties": {"s": {"type": "string"}}}))+++ class NoBatchRunner(FakeRunner):+ pass # has predict() but no predict_batch+++ check_raises("batch/runner without predict_batch", TypeError,+ lambda: decide_batch(NoBatchRunner(ANSWERS), ["a"], schema=SCHEMA))++ # a runner that answers nothing (hook-skipped states surface as empty values)+ noop_runner = FakeBatchRunner({})+ check("batch/skipped states", decide_batch(noop_runner, ["unmatched"], schema=SCHEMA), [{}])+ check("batch/noop still batches", len(noop_runner.calls), 1)++# --------------------------------------------------------------- pydantic (optional)try:from typing import Literal