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.

Merge pull request #420 from PerryLink/fix/score-legend-type

Nandakishor28076dd2addd
diff --cc laya/agent.py
index 046c589a,6ce72ee3..4c95a46f
--- a/laya/agent.py
+++ b/laya/agent.py
@@@ -31,9 -23,8 +31,10 @@@ from .common import
answer_confidence,
confidence_from_probs,
_resolve_noul_labels,
+ _reuse_question_tokens,
+ _disable_question_token_reuse,
encode_text,
+ render_criterion,
render_options,
serialize_state,
temp_bucket,
diff --cc tests/test_batch.py
index 47b3b845,6316c82f..fdbd5dff
--- a/tests/test_batch.py
+++ b/tests/test_batch.py
@@@ -271,38 -271,33 +271,65 @@@ with patch.object(_agent, "confidence_f
check("decode/noul-only answers skip entropy", entropy.call_count, 0)
+# --------------------------------------------------------------- min_confidence abstention (#361)
+# The flag is set before `on_predict_end` runs, so an audit hook sees exactly what the caller gets.
+def make_confidence_fake():
+ fake = make_fake()
+
+ def decode(logits, act, items, ids, internal, offset):
+ return {qid: {"type": "choice", "choice": qid,
+ "answer_confidence": 0.90 if qid == "a" else 0.40}
+ for qid in ids}
+
+ fake._decode_answers = decode
+ return fake
+
+
+seen = []
+gated = make_confidence_fake().predict_batch(
+ ["s0", "s1"], QUESTIONS, min_confidence=0.5,
+ on_predict_end=lambda ctx: seen.append(
+ [{q: a.get("low_confidence", False) for q, a in r["answers"].items()} for r in ctx.results]))
+check("min_confidence/flags only answers below the threshold",
+ [[r["answers"][q].get("low_confidence", False) for q in ("a", "b")] for r in gated],
+ [[False, True], [False, True]])
+check("min_confidence/end hook sees the same flags", seen, [[{"a": False, "b": True}] * 2])
+check("min_confidence/raw answer and confidence intact",
+ (gated[0]["answers"]["a"]["answer_confidence"], "low_confidence" in gated[0]["answers"]["a"]),
+ (0.90, False))
+check("min_confidence/default leaves results untouched",
+ "low_confidence" in make_confidence_fake().predict_batch(["s0"], QUESTIONS)[0]["answers"]["b"], False)
+check_raises("min_confidence/out-of-range rejected", ValueError,
+ lambda: make_confidence_fake().predict_batch(["s0"], QUESTIONS, min_confidence=1.5))
+check_raises("min_confidence/bool rejected", ValueError,
+ lambda: make_confidence_fake().predict_batch(["s0"], QUESTIONS, min_confidence=True))
+ # 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