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(structured): honour minimum when a score answer has no probabilities

cr-sbarbouche6b83b7d83589
diff --git a/laya/structured.py b/laya/structured.py
index 30e5d3ba..64637c4d 100644
--- a/laya/structured.py
+++ b/laya/structured.py
@@ -211,7 +211,13 @@ def _project(answers: Dict[str, Any], fields: Sequence[_Field]) -> Dict[str, Any
if probs:
idx = max(range(len(probs)), key=lambda i: float(probs.get(str(i), probs.get(i, 0.0))))
else:
- idx = int(round(float(answer.get("score", 0.0)))) - int(f.minimum or 0)
+ # `score` is always the probability-weighted 0-based level index (see
+ # `DecisionModel._decode_answers`), the same space `idx` is in above -- not an
+ # absolute field value -- so it is rounded on its own, with no `minimum`
+ # subtracted first. Subtracting it here and adding it back below used to cancel
+ # out, silently dropping `minimum` from every field whose schema does not start
+ # at 0.
+ idx = int(round(float(answer.get("score", 0.0))))
values[f.name] = int(f.minimum or 0) + idx
else: # choice
label = str(answer.get("choice"))
diff --git a/tests/test_structured.py b/tests/test_structured.py
index fcf4ae2c..4b3e2d00 100644
--- a/tests/test_structured.py
+++ b/tests/test_structured.py
@@ -97,6 +97,15 @@ check("project/false noul",
{"type": "object", "properties": {"x": {"type": "boolean"}}})["x"],
False)
+# A score answer with no `probabilities` -- a cached/replayed decision, or a minimal runner
+# that only reports the expected level index -- must still add `minimum`: `score` is the
+# 0-based level index (DecisionModel._decode_answers), not the absolute field value, so a
+# schema whose levels do not start at 0 must not be projected as if they did.
+SEVERITY_SCHEMA = {"type": "object", "properties": {"x": {"type": "integer", "minimum": 3, "maximum": 7}}}
+for score, want in ((0, 3), (2.4, 5), (4, 7)):
+ got = answers_to_json({"x": {"type": "score", "score": score}}, SEVERITY_SCHEMA)["x"]
+ check("project/score with no probabilities honours minimum (score=%r)" % score, got, want)
+
# --------------------------------------------------------------- rejections
def _bad(schema):
diff --git a/tests/test_structured_docs.py b/tests/test_structured_docs.py
index 28a1a82f..bc66fd83 100644
--- a/tests/test_structured_docs.py
+++ b/tests/test_structured_docs.py
@@ -333,7 +333,9 @@ def main() -> int:
if bounded:
schema = {"type": "object", "properties": {"field": {"type": "integer",
"minimum": 3, "maximum": 7}}}
- for score, want in ((3, 3), (5, 5), (7, 7)):
+ # `score` is the 0-based level index (5 levels: 0..4 for minimum=3, maximum=7), never
+ # the absolute field value -- see `DecisionModel._decode_answers` in laya/agent.py.
+ for score, want in ((0, 3), (2, 5), (4, 7)):
got = S.answers_to_json({"field": {"type": "score", "score": score}}, schema)
check("projection/minimum + argmax for score %d" % score, got, {"field": want})