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(langchain): .batch() forwards lang and min_confidence like .invoke()

AlKor13323a3c31b86d
diff --git a/laya/integrations/langchain.py b/laya/integrations/langchain.py
index 3f60dbf1..99732c33 100644
--- a/laya/integrations/langchain.py
+++ b/laya/integrations/langchain.py
@@ -270,20 +270,28 @@ def _execute_batch(
model: Optional[str] = None,
max_len: Optional[int] = None,
head_max_len: Optional[int] = None,
+ lang: Optional[str] = None,
+ min_confidence: Optional[float] = None,
hook_kwargs: Optional[Dict[str, Any]] = None,
) -> List[Dict[str, Any]]:
"""Evaluate one question set over many states, packing them into shared forward passes.
The local sibling of `_execute_decision`; results come back in input order. `Agent`
and `Router` disagree about how `predict_batch` is called (states plus one question
- set, versus one request dict each), so both forms are built here. The token budget
- rides on each request for a Router and as call arguments for an Agent.
+ set, versus one request dict each), so both forms are built here. The token budget,
+ `lang` and `min_confidence` ride the same way they do on `_execute_decision`, so
+ `.batch()` answers identically to `.invoke()` instead of silently dropping them.
"""
runner = agent if agent is not None else _get_default_router()
- overrides = _predict_kwargs(model, max_len, head_max_len)
if hasattr(runner, "route_batch"):
- requests = [dict({"state": state, "questions": questions}, **overrides) for state in states]
- return runner.predict_batch(requests)
+ # Router: `lang` is a per-request control it lifts off each item, `min_confidence` is a
+ # call-level argument -- the same split laya-serve's batch endpoint makes between
+ # BATCH_BODY_ITEM_CONTROLS and BATCH_BODY_CALL_CONTROLS.
+ item_overrides = _predict_kwargs(model, max_len, head_max_len, lang)
+ requests = [dict({"state": state, "questions": questions}, **item_overrides) for state in states]
+ return runner.predict_batch(requests, **_decision_kwargs(None, min_confidence))
+ # Agent: `lang` and `min_confidence` are both call-level keyword arguments of `predict_batch`.
+ overrides = _predict_kwargs(model, max_len, head_max_len, lang, min_confidence)
return runner.predict_batch(list(states), questions, **overrides, **(hook_kwargs or {}))
@@ -351,7 +359,9 @@ class _BatchedRunnable:
states = [_extract_text(item, self.state_key) for item in inputs]
results = _execute_batch(
states, self._questions(), agent=self.agent, model=self.model,
- max_len=self.max_len, head_max_len=self.head_max_len, hook_kwargs=hook_kwargs,
+ max_len=self.max_len, head_max_len=self.head_max_len,
+ lang=getattr(self, "lang", None), min_confidence=getattr(self, "min_confidence", None),
+ hook_kwargs=hook_kwargs,
)
return [self._finish(result, item) for result, item in zip(results, inputs)]
diff --git a/tests/test_langchain.py b/tests/test_langchain.py
index 06953f93..fe9580b6 100644
--- a/tests/test_langchain.py
+++ b/tests/test_langchain.py
@@ -531,6 +531,36 @@ check("batch/router last_decision",
check("batch/router empty inputs", batch_router_node.batch([]), [])
check("batch/router empty skips the runner", len(batch_agent.batch_calls), 1)
+# batch() must forward `lang` and `min_confidence` the same way invoke() does, or the two paths
+# answer differently for a runnable configured with them. Agent convention: both are call kwargs.
+_crit = {"billing": "invoices, refunds", "technical": "bugs, errors"}
+ba_ctrl = MockBatchAgent(mock_router_response)
+LayaRouter(criteria=_crit, agent=ba_ctrl, lang="de", min_confidence=0.4).batch(ROUTER_INPUTS)
+check("batch/agent forwards lang", ba_ctrl.batch_calls[0]["kwargs"].get("lang"), "de")
+check("batch/agent forwards min_confidence", ba_ctrl.batch_calls[0]["kwargs"].get("min_confidence"), 0.4)
+
+# Router convention: lang rides on each request item; min_confidence is a call-level argument.
+rl_ctrl = MockRouterLike(mock_router_response)
+LayaRouter(criteria=_crit, agent=rl_ctrl, lang="de", min_confidence=0.4).batch(ROUTER_INPUTS)
+_reqs = rl_ctrl.batch_calls[0]["requests"]
+_kw = rl_ctrl.batch_calls[0]["kwargs"]
+check("batch/router item carries lang", all(r.get("lang") == "de" for r in _reqs), True)
+check("batch/router min_confidence is call-level", _kw.get("min_confidence"), 0.4)
+check("batch/router min_confidence not duplicated onto items",
+ any("min_confidence" in r for r in _reqs), False)
+check("batch/router lang not duplicated as a call kwarg", "lang" in _kw, False)
+
+# min_confidence=0.0 is a real gate (abstain over nothing), not an absence -- it must survive batch.
+ba_zero = MockBatchAgent(mock_router_response)
+LayaRouter(criteria=_crit, agent=ba_zero, min_confidence=0.0).batch(ROUTER_INPUTS)
+check("batch/agent keeps min_confidence=0.0", ba_zero.batch_calls[0]["kwargs"].get("min_confidence"), 0.0)
+# and an unset control stays unset -- no lang/min_confidence keys leak in
+ba_none = MockBatchAgent(mock_router_response)
+LayaRouter(criteria=_crit, agent=ba_none).batch(ROUTER_INPUTS)
+check("batch/agent omits unset controls",
+ ("lang" in ba_none.batch_calls[0]["kwargs"]) or ("min_confidence" in ba_none.batch_calls[0]["kwargs"]),
+ False)
+
# LangChain hands batch() one config, a list of per-input configs (what RunnableSequence
# and RunnableParallel do), or None. All three must reach the same outputs.
one_config = {"tags": ["t"], "max_concurrency": 2}