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(agent): a score legend echoed the caller's own type instead of level text

PerryLink0c6e8d1b8496
diff --git a/laya/agent.py b/laya/agent.py
index 1b58de6f..6ce72ee3 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -24,6 +24,7 @@ from .common import (
confidence_from_probs,
_resolve_noul_labels,
encode_text,
+ render_criterion,
render_options,
serialize_state,
temp_bucket,
@@ -796,7 +797,15 @@ class Agent(HookRegistry):
answers[qid] = {
"type": "score",
"score": round(exp_score, 4),
- "legend": {str(i): c for i, c in enumerate(q["crit"])},
+ # A legend maps an index to the text of a level, and the keys are already
+ # strings. `render_criterion` rather than `str`: a dict or list level then
+ # comes back as the same JSON text the model was shown, where `str` produced
+ # a Python repr. A numeric scale passed as `[1, 2, 3]` used to come back as
+ # `{"0": 1, "1": 2, "2": 3}`, so the response's JSON types depended on what
+ # the caller happened to pass; `structured` stringifies every level it builds
+ # and `probabilities` stringifies its keys right below, so this was the one
+ # path that did not.
+ "legend": {str(i): render_criterion(c) for i, c in enumerate(q["crit"])},
"probabilities": {str(i): round(float(v), 4) for i, v in enumerate(p)},
"confidence": round(confidence_from_probs(p, k), 4),
"answer_confidence": ans_conf,
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.py
index 0f4a19f5..03f6e293 100644
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@ -19,6 +19,7 @@ from laya.common import (
collate_items,
confidence_from_probs,
encode_text,
+ render_criterion,
render_options,
serialize_state,
temp_bucket,
@@ -364,7 +365,11 @@ class ONNXAgent(HookRegistry):
answers[qid] = {
"type": "score",
"score": round(exp_score, 4),
- "legend": {str(i): c for i, c in enumerate(q["crit"])},
+ # Same as `Agent._decode_answers`, so both backends return the same legend:
+ # `render_criterion` gives a dict or list level back as the JSON text the
+ # model was shown, and a numeric level as a string, rather than a Python repr
+ # or the caller's own type.
+ "legend": {str(i): render_criterion(c) for i, c in enumerate(q["crit"])},
"probabilities": {str(i): round(float(v), 4) for i, v in enumerate(p)},
"confidence": conf_score,
"answer_confidence": ans_conf,
diff --git a/tests/test_batch.py b/tests/test_batch.py
index 9d4d3b63..6316c82f 100644
--- a/tests/test_batch.py
+++ b/tests/test_batch.py
@@ -271,6 +271,35 @@ with patch.object(_agent, "confidence_from_probs", wraps=_agent.confidence_from_
check("decode/noul-only answers skip entropy", entropy.call_count, 0)
+# A legend maps a level index to the TEXT of that level. Its keys are already strings, and
+# `structured` stringifies every level it builds, so a numeric scale written directly used to be
+# the one path that echoed the caller's own type back: `{"0": 1, "1": 2}` instead of
+# `{"0": "1", "1": "2"}`. That made the response's JSON types depend on the input's types, so a
+# client that reads a level as a string had to handle a number as well.
+#
+# `render_criterion` is what renders the text, so a structured level comes back as the JSON the
+# model was shown rather than a Python repr, and bool/None follow JSON spelling (`true`, `null`).
+for label, crit, want in (
+ ("int levels", [1, 2, 3], {"0": "1", "1": "2", "2": "3"}),
+ ("float levels", [1.5, 2.5], {"0": "1.5", "1": "2.5"}),
+ ("bool levels", [True, False], {"0": "true", "1": "false"}),
+ ("string levels", ["low", "high"], {"0": "low", "1": "high"}),
+ ("dict level", [{"a": 1}], {"0": '{"a": 1}'}),
+ ("list level", [[1, 2]], {"0": "[1, 2]"}),
+):
+ internal = {"level": {"t": "score", "crit": crit}}
+ k = len(crit)
+ logits = np.tile(np.log([1.0 / k] * k), (k, 1))
+ act = np.array([[0.25, 0.75]] * k)
+ result = decoder._decode_answers(
+ logits, act, [{"markers": [0, 1]}] * k, ["level"] * k, internal, 0
+ )
+ check("decode/score legend values are str (%s)" % label, result["level"]["legend"], want)
+ check_true("decode/score legend values have no non-str (%s)" % label,
+ all(isinstance(v, str) for v in result["level"]["legend"].values()),
+ result["level"]["legend"])
+
+
# --------------------------------------------------------------------------- report
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f_ in FAIL: