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
fix(examples): mirror laya.serve's option budgets in the demo server
Bruce-Yii1a5a47db2f2c
diff --git a/examples/server.py b/examples/server.pyindex ed842808..2e5eb44f 100644--- a/examples/server.py+++ b/examples/server.py@@ -59,6 +59,12 @@ import laya.serve as _laya_serveMAX_QUESTIONS = getattr(_laya_serve, "MAX_QUESTIONS", 64)MAX_STATE_CHARS = getattr(_laya_serve, "MAX_STATE_CHARS", 50_000)+# The option budgets are bounds for the same reason the two above are: a choice or score+# question encodes one sequence per option, and they share the head budget. Read with the+# same getattr so this demo cannot drift from the server it demonstrates.+MAX_CHOICE_OPTIONS = getattr(_laya_serve, "MAX_CHOICE_OPTIONS", 100)+MAX_SCORE_LEVELS = getattr(_laya_serve, "MAX_SCORE_LEVELS", 32)+MAX_TOTAL_OPTIONS = getattr(_laya_serve, "MAX_TOTAL_OPTIONS", 512)# --------------------------------------------------------------------------- ## Request / response models@@ -238,8 +244,11 @@ def _check_request_limits(state: Any, questions: Dict[str, Any]) -> None:"""Refuse an oversized request, as `laya.serve._check_request_limits` does.Laya encodes the state once per question, so cost is questions x state size,- collated into one tensor. The state length is measured exactly as laya.serve- measures it -- `len(v)` for a string, `len(str(v))` for a dict or list.+ collated into one tensor, and a choice or score question adds one sequence per+ option against a shared head budget. The state length is measured exactly as+ laya.serve measures it -- `len(v)` for a string, `len(str(v))` for a dict or list+ -- and the option counts exactly as it counts them, over `choice` and `score`+ criteria only.Checked here rather than declared as pydantic constraints on the request models,for two reasons: a `Field(max_length=...)` violation is reported as 422 where@@ -258,6 +267,41 @@ def _check_request_limits(state: Any, questions: Dict[str, Any]) -> None:status_code=413,detail="state too large (%d > %d chars)" % (size, MAX_STATE_CHARS),)+ # Counted exactly as laya.serve counts them, and refused for the same reason: a `noul`+ # question contributes no options, so the total is a sum over `choice` and `score`+ # criteria only, while the state above is still encoded once per question.+ total_options = 0+ for qid, qdef in questions.items():+ # This demo's request model hands these over as `Question` instances where+ # `laya.serve` sees plain dicts, so read either shape. Mirroring serve's+ # `if not isinstance(question, dict): continue` verbatim would skip every question+ # here and leave the check dead.+ if isinstance(qdef, dict):+ qtype, crit = qdef.get("type"), qdef.get("criteria")+ else:+ qtype, crit = getattr(qdef, "type", None), getattr(qdef, "criteria", None)+ if qtype == "choice" and isinstance(crit, (dict, list)):+ count = len(crit)+ if count > MAX_CHOICE_OPTIONS:+ raise HTTPException(+ status_code=413,+ detail="too many choice options for %r (%d > %d)" % (qid, count, MAX_CHOICE_OPTIONS),+ )+ elif qtype == "score" and isinstance(crit, list):+ count = len(crit)+ if count > MAX_SCORE_LEVELS:+ raise HTTPException(+ status_code=413,+ detail="too many score levels for %r (%d > %d)" % (qid, count, MAX_SCORE_LEVELS),+ )+ if isinstance(crit, (dict, list)):+ total_options += len(crit)+ if total_options > MAX_TOTAL_OPTIONS:+ raise HTTPException(+ status_code=413,+ detail="too many answer options across questions (%d > %d)"+ % (total_options, MAX_TOTAL_OPTIONS),+ )@app.post("/predict")diff --git a/tests/test_example_server_limits.py b/tests/test_example_server_limits.pyindex 49f62bd7..5079bbfc 100644--- a/tests/test_example_server_limits.py+++ b/tests/test_example_server_limits.py@@ -8,8 +8,12 @@ examples/server.py bounded `states` to 64 and left the rest open: 20 000 questioa 5 MB state were both accepted where the shipped server answers 413. The bounds areread from laya.serve rather than restated, so the two cannot drift.-Scope: the question count and the state size, answered 413 as laya.serve answers them.-`Question.instructions` and `criteria` still carry unbounded text that no per-field+Scope: the question count, the state size, and the per-question and total option counts,+answered 413 as laya.serve answers them. The option budgets matter for the same reason the+other two do -- a choice or score question encodes one sequence per option, and those+sequences share the head budget -- so a request laya.serve refuses with 413 must not be+answered here. The bounds are read from laya.serve, never restated.+`Question.instructions` and `criteria` still carry unbounded *text* that no per-fieldbound can see; capping the request body is the backstop for those and is left outdeliberately -- see the PR description.@@ -56,7 +60,13 @@ def main():print("SKIP: examples/server.py needs the serve extra -- pip install laya[serve]")return 0- from laya.serve import MAX_QUESTIONS, MAX_STATE_CHARS+ from laya.serve import (+ MAX_CHOICE_OPTIONS,+ MAX_QUESTIONS,+ MAX_SCORE_LEVELS,+ MAX_STATE_CHARS,+ MAX_TOTAL_OPTIONS,+ )client = TestClient(demo.app, raise_server_exceptions=False)one = {"a": {"type": "noul", "instructions": "x"}}@@ -64,6 +74,14 @@ def main():def questions(n):return {"q%d" % i: {"type": "noul", "instructions": "x"} for i in range(n)}+ def choice(n_opts, qid="a"):+ return {qid: {"type": "choice", "instructions": "Which?",+ "criteria": {"opt%d" % i: "desc %d" % i for i in range(n_opts)}}}++ def score(n_levels, qid="a"):+ return {qid: {"type": "score", "instructions": "How bad?",+ "criteria": ["level %d" % i for i in range(n_levels)]}}+def code(**kw):return client.post("/predict", **kw).status_code@@ -73,6 +91,13 @@ def main():ok("the per-request bounds are laya.serve's",demo_q == MAX_QUESTIONS and demo_s == MAX_STATE_CHARS,"demo %r/%r vs laya.serve %r/%r" % (demo_q, demo_s, MAX_QUESTIONS, MAX_STATE_CHARS))+ demo_opts = (getattr(demo, "MAX_CHOICE_OPTIONS", None),+ getattr(demo, "MAX_SCORE_LEVELS", None),+ getattr(demo, "MAX_TOTAL_OPTIONS", None))+ ok("the option budgets are laya.serve's too",+ demo_opts == (MAX_CHOICE_OPTIONS, MAX_SCORE_LEVELS, MAX_TOTAL_OPTIONS),+ "demo %r vs laya.serve %r" % (demo_opts, (MAX_CHOICE_OPTIONS, MAX_SCORE_LEVELS,+ MAX_TOTAL_OPTIONS)))# --- too many questions, too large a state: 413, as laya.serve answers ---ok("more than MAX_QUESTIONS questions is 413",@@ -90,6 +115,26 @@ def main():json={"states": ["hi", "A" * (MAX_STATE_CHARS + 1)], "questions": one}).status_code == 413)+ # --- the option budgets, which laya.serve also answers 413 -----------------+ # A choice or score question encodes one sequence per option against a shared head+ # budget, so the option counts are size limits for the same reason the state is.+ ok("more than MAX_CHOICE_OPTIONS options in one question is 413",+ code(json={"state": "hi", "questions": choice(MAX_CHOICE_OPTIONS + 1)}) == 413)+ ok("more than MAX_SCORE_LEVELS levels in one question is 413",+ code(json={"state": "hi", "questions": score(MAX_SCORE_LEVELS + 1)}) == 413)+ # Under the per-question caps but over the shared total: the case a per-question check+ # alone would still accept.+ per_q = MAX_CHOICE_OPTIONS+ n_questions = MAX_TOTAL_OPTIONS // per_q + 1+ ok("over MAX_TOTAL_OPTIONS across questions is 413",+ code(json={"state": "hi",+ "questions": {"q%d" % i: choice(per_q, "q%d" % i)[ "q%d" % i]+ for i in range(n_questions)}}) == 413,+ "%d questions x %d options" % (n_questions, per_q))+ # A noul question contributes no options, so it cannot move the total either way.+ ok("a noul flood is still bounded by the question count, not the option total",+ code(json={"state": "hi", "questions": questions(MAX_QUESTIONS)}) == 503)+# --- the refusal must not echo the rejected payload back ----------------# Declaring these as Field(max_length=...) instead would report 422 *and* include@@ -118,6 +163,10 @@ def main():code(json={"state": "hi", "questions": questions(MAX_QUESTIONS)}) == 503)ok("a state of exactly MAX_STATE_CHARS passes",code(json={"state": "A" * MAX_STATE_CHARS, "questions": one}) == 503)+ ok("exactly MAX_CHOICE_OPTIONS options passes",+ code(json={"state": "hi", "questions": choice(MAX_CHOICE_OPTIONS)}) == 503)+ ok("exactly MAX_SCORE_LEVELS levels passes",+ code(json={"state": "hi", "questions": score(MAX_SCORE_LEVELS)}) == 503)ok("a list state passes", code(json={"state": ["a", "b"], "questions": one}) == 503)ok("a small chunked body passes",code(content=(lambda: (yield json.dumps({"state": "hi", "questions": one}).encode()))(),