Skip to content

armgit / laya

Public Imported from GitHub
0

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.

fix(examples): count noul criteria as zero options, as laya.serve does

Bruce-Yii62aa40237d9a
diff --git a/examples/server.py b/examples/server.py
index 2e5eb44f..03c24e84 100644
--- a/examples/server.py
+++ b/examples/server.py
@@ -267,9 +267,11 @@ 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.
+ # Counted exactly as laya.serve counts them, and refused for the same reason. The
+ # increment belongs inside the two branches, as it does there: a `noul` question carries
+ # false/true criteria, which are option *texts* rather than answer options, so a total
+ # that added them would refuse a request laya.serve accepts. The state above is still
+ # encoded once per question, which is what the question-count bound is for.
total_options = 0
for qid, qdef in questions.items():
# This demo's request model hands these over as `Question` instances where
@@ -282,6 +284,7 @@ def _check_request_limits(state: Any, questions: Dict[str, Any]) -> None:
qtype, crit = getattr(qdef, "type", None), getattr(qdef, "criteria", None)
if qtype == "choice" and isinstance(crit, (dict, list)):
count = len(crit)
+ total_options += count
if count > MAX_CHOICE_OPTIONS:
raise HTTPException(
status_code=413,
@@ -289,13 +292,12 @@ def _check_request_limits(state: Any, questions: Dict[str, Any]) -> None:
)
elif qtype == "score" and isinstance(crit, list):
count = len(crit)
+ total_options += count
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,
diff --git a/tests/test_example_server_limits.py b/tests/test_example_server_limits.py
index 5079bbfc..c48a7e9f 100644
--- a/tests/test_example_server_limits.py
+++ b/tests/test_example_server_limits.py
@@ -134,6 +134,20 @@ def main():
# 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)
+ # ...including when it carries the ordinary false/true criteria, which are option
+ # *texts* but not answer options. laya.serve adds them nowhere; a total that counted
+ # them would refuse a request serve accepts, which is the drift this file exists to
+ # catch -- in the direction of refusing something legal.
+ counted = 5 * MAX_CHOICE_OPTIONS + 12 # exactly MAX_TOTAL_OPTIONS by laya.serve's count
+ mixed = {"q%d" % i: choice(MAX_CHOICE_OPTIONS, "q%d" % i)["q%d" % i] for i in range(5)}
+ mixed["last"] = choice(12, "last")["last"]
+ for i in range(30): # 30 noul questions, 2 criteria each, 60 if wrongly counted
+ mixed["n%d" % i] = {"type": "noul", "instructions": "True?",
+ "criteria": {"false": "no", "true": "yes"}}
+ ok("noul criteria do not count toward the shared option total",
+ len(mixed) <= MAX_QUESTIONS and counted == MAX_TOTAL_OPTIONS
+ and code(json={"state": "hi", "questions": mixed}) == 503,
+ "%d questions, %d options by laya.serve's count" % (len(mixed), counted))
# --- the refusal must not echo the rejected payload back ----------------