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(integrations): gate a LayaTaskGuard score question on its upper-half probability
abhijithneilabraham0a06f6622299
diff --git a/laya/integrations/_guard.py b/laya/integrations/_guard.pynew file mode 100644index 00000000..c8d482e9--- /dev/null+++ b/laya/integrations/_guard.py@@ -0,0 +1,19 @@+"""The score-question rule shared by the guardrail wrappers."""+from typing import Any, Dict+++def score_violation_probability(answer: Dict[str, Any], levels: int = 0) -> float:+ """Probability that a `score` answer is at or above the middle of its scale.++ `score` is the expected level (0..k-1), not a probability, so a guard threshold cannot be+ compared with it directly. `levels` is the number of levels the question defines; it is only+ used when the answer carries no `probabilities`, and then the normalised expected level stands+ in for the distribution.+ """+ probs = answer.get("probabilities") or {}+ k = len(probs) or levels+ if k < 2:+ return 0.0+ if probs:+ return sum(float(probs.get(str(i), 0.0)) for i in range(k // 2, k))+ return answer.get("score", 0.0) / (k - 1)diff --git a/laya/integrations/crewai.py b/laya/integrations/crewai.pyindex 057349d4..4d045e54 100644--- a/laya/integrations/crewai.py+++ b/laya/integrations/crewai.py@@ -57,6 +57,7 @@ class LayaTaskGuardError(ValueError):from ._controls import budget_kwargs as _budget_kwargs, hook_kwargs as _hook_kwargs # noqa: E402from ._controls import decision_kwargs as _decision_kwargs # noqa: E402from ._controls import predict_kwargs as _predict_kwargs, reject_remote_hooks as _reject_remote_hooks # noqa: E402+from ._guard import score_violation_probability as _score_violation_probability # noqa: E402from ..confidence import _gate_confidence # noqa: E402# One class for every integration, so `except LayaLowConfidenceError` catches all of them.@@ -448,6 +449,8 @@ class LayaTaskGuard:hooks_raise: Optional[bool] = None,hooks_timeout: Optional[float] = None,):+ if not 0.0 <= threshold <= 1.0:+ raise ValueError("threshold must be a probability in [0, 1]; got %r" % (threshold,))self.questions = questionsself.action = actionself.rejection_message = rejection_message@@ -504,11 +507,17 @@ class LayaTaskGuard:"probability": ans["noul"],"confidence": ans.get("confidence", 0.0),}- elif t == "score" and ans.get("score", 0.0) >= self.threshold:- violations[qid] = {- "score": ans["score"],- "confidence": ans.get("confidence", 0.0),- }+ elif t == "score":+ # `score` is the expected level (0..k-1), not a probability: gate on the+ # probability that the level is at or above the middle of the scale.+ levels = len(qdefs.get(qid, {}).get("criteria") or [])+ p_violation = _score_violation_probability(ans, levels)+ if p_violation >= self.threshold:+ violations[qid] = {+ "score": ans.get("score", 0.0),+ "probability": round(p_violation, 4),+ "confidence": ans.get("confidence", 0.0),+ }is_safe = len(violations) == 0diff --git a/laya/integrations/langchain.py b/laya/integrations/langchain.pyindex 3f60dbf1..87386a0a 100644--- a/laya/integrations/langchain.py+++ b/laya/integrations/langchain.py@@ -34,6 +34,7 @@ from ._controls import budget_kwargs as _budget_kwargs, hook_kwargs as _hook_kwafrom ._controls import decision_kwargs as _decision_kwargsfrom ._controls import predict_kwargs as _predict_kwargsfrom ._controls import reject_remote_hooks as _reject_remote_hooks+from ._guard import score_violation_probability as _score_violation_probabilityclass LayaGuardrailError(ValueError):@@ -654,16 +655,9 @@ class LayaGuardrail(_BatchedRunnable, RunnableSerializable):}elif t == "score":# `score` is the expected level (0..k-1), not a probability: gate on the- # probability that the level is at or above the middle of the scale. Without- # a distribution, the normalised expected level stands in for it.- probs = ans.get("probabilities") or {}- k = len(probs) or len(self._questions().get(qid, {}).get("criteria") or [])- if k < 2:- p_violation = 0.0- elif probs:- p_violation = sum(float(probs.get(str(i), 0.0)) for i in range(k // 2, k))- else:- p_violation = ans.get("score", 0.0) / (k - 1)+ # probability that the level is at or above the middle of the scale.+ levels = len(self._questions().get(qid, {}).get("criteria") or [])+ p_violation = _score_violation_probability(ans, levels)if p_violation >= self.threshold:violations[qid] = {"score": ans.get("score", 0.0),