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 #598 from gRain-is-grainy/fix/guardrail-score-threshold

Nandakishore0e8154c9fc6
diff --cc laya/integrations/langchain.py
index 5e9428cd,4557b14b..390ff4b5
--- a/laya/integrations/langchain.py
+++ b/laya/integrations/langchain.py
@@@ -503,8 -259,14 +503,15 @@@ class LayaGuardrail(_BatchedRunnable, R
"""Sub-40ms inline guardrail for LangChain chains and LangGraph nodes.
Screens for prompt injections, jailbreaks, sensitive data, or custom harm
- criteria before passing inputs downstream.
+ criteria before passing inputs downstream. A list of inputs is screened in one
+ shared forward pass through `batch`.
+
+ `threshold` is a violation probability in [0, 1] for every `noul` and `score`
+ question: for a `noul` question it applies to `noul`, and for a `score`
+ question to the probability that the level is at or above the middle of the
+ scale (`serious` or `severe` for the default `harm_severity`; the middle level
+ counts on an odd scale). Levels of a `score` question must run from harmless
+ to worst.
"""
questions: Optional[Dict[str, Any]] = None
@@@ -539,15 -294,10 +546,17 @@@
base_url: Optional[str] = None,
api_key: Optional[str] = None,
model: Optional[str] = None,
+ max_len: Optional[int] = None,
+ head_max_len: Optional[int] = None,
+ hooks: Optional[Any] = None,
+ on_predict_start: Optional[Any] = None,
+ on_predict_end: Optional[Any] = None,
+ hooks_raise: Optional[bool] = None,
+ hooks_timeout: Optional[float] = None,
**kwargs: Any,
):
+ if not 0.0 <= threshold <= 1.0:
+ raise ValueError("threshold must be a probability in [0, 1]; got %r" % (threshold,))
if _RUNNABLE_AVAILABLE:
super().__init__(
questions=questions,
@@@ -605,11 -349,24 +614,24 @@@
"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. Without
+ # a distribution, the normalised expected level stands in for it.
+ probs = ans.get("probabilities") or {}
- k = len(probs) or len(qdefs.get(qid, {}).get("criteria") 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)
+ 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) == 0