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.

PR: expose hooks_timeout/min_confidence/sort_by_length on laya_predict_batch

Aashish3a9069f66c73
diff --git a/laya/mcp/server.py b/laya/mcp/server.py
index e7d362e6..f36577e4 100644
--- a/laya/mcp/server.py
+++ b/laya/mcp/server.py
@@ -285,7 +285,8 @@ def laya_predict_tool(
+ _GUARDRAILS
),
)
-def laya_predict_batch_tool(requests: list, batch_size: int = 0) -> str:
+def laya_predict_batch_tool(requests: list, batch_size: int = 0, hooks_timeout: float = 0,
+ min_confidence: float = -1, sort_by_length: bool = False) -> str:
"""Answer many typed-question requests in one batched call."""
router = _router_or_error()
# batch_size=0 means "unset": MCP clients send defaults eagerly, and
@@ -294,6 +295,9 @@ def laya_predict_batch_tool(requests: list, batch_size: int = 0) -> str:
laya_predict_batch,
requests=requests,
batch_size=batch_size or None,
+ hooks_timeout=hooks_timeout if hooks_timeout > 0 else None,
+ min_confidence=min_confidence if min_confidence >= 0 else None,
+ sort_by_length=sort_by_length,
router=router,
)
diff --git a/laya/mcp/tools.py b/laya/mcp/tools.py
index 1bf7f269..1a643dca 100644
--- a/laya/mcp/tools.py
+++ b/laya/mcp/tools.py
@@ -911,6 +911,9 @@ def _validate_batch_size(batch_size: Any) -> int | None:
def laya_predict_batch(
requests: Any,
batch_size: Any = None,
+ hooks_timeout: Any = None,
+ min_confidence: Any = None,
+ sort_by_length: bool = False,
*,
router: Any = None,
) -> dict:
@@ -934,8 +937,16 @@ def laya_predict_batch(
started = time.perf_counter()
try:
- results = router.predict_batch(items, batch_size=size) if size is not None \
- else router.predict_batch(items)
+ kwargs = {}
+ if size is not None:
+ kwargs["batch_size"] = size
+ if hooks_timeout is not None:
+ kwargs["hooks_timeout"] = hooks_timeout
+ if min_confidence is not None:
+ kwargs["min_confidence"] = min_confidence
+ if sort_by_length:
+ kwargs["sort_by_length"] = True
+ results = router.predict_batch(items, **kwargs)
except TypeError as exc:
# A Router without batch support raises at the call itself; anything
# else is a real bug and must surface unchanged.
diff --git a/tests/test_mcp.py b/tests/test_mcp.py
index d878d8c6..dcee67d4 100644
--- a/tests/test_mcp.py
+++ b/tests/test_mcp.py
@@ -842,8 +842,8 @@ class BatchRouter(FakeRouter):
"routing": {"model": request.get("model") or "english",
"repo": "fake/laya", "reason": "batch route"}}
- def predict_batch(self, requests, batch_size=None):
- self.predict_batch_calls.append((list(requests), batch_size))
+ def predict_batch(self, requests, batch_size=None, **kwargs):
+ self.predict_batch_calls.append((list(requests), batch_size, kwargs))
return [self._answer_for(request) for request in requests]
def route_batch(self, requests):
@@ -965,7 +965,7 @@ def test_batch_predict():
out = laya_predict_batch(BATCH_REQUESTS, batch_size=8, router=router)
ok("batch/predict_one_call", len(router.predict_batch_calls) == 1,
repr(len(router.predict_batch_calls)))
- forwarded, size = router.predict_batch_calls[0]
+ forwarded, size, kwargs = router.predict_batch_calls[0]
ok("batch/predict_size_forwarded", size == 8, repr(size))
ok("batch/predict_items_forwarded", [item["state"] for item in forwarded]
== [request["state"] for request in BATCH_REQUESTS])
@@ -1002,8 +1002,34 @@ def test_batch_predict():
# batch_size unset must not be forwarded as None (strict old stubs included).
router = BatchRouter()
laya_predict_batch(BATCH_REQUESTS, router=router)
- ok("batch/predict_size_default", router.predict_batch_calls[0][1] is None)
+ _, size, _ = router.predict_batch_calls[0]
+ ok("batch/predict_size_default", size is None)
+ # Test that hooks_timeout parameter is forwarded correctly
+ router = BatchRouter()
+ out = laya_predict_batch(BATCH_REQUESTS, hooks_timeout=5.5, router=router)
+ _, kwargs = router.predict_batch_calls[0][1], router.predict_batch_calls[0][2]
+ ok("batch/hooks_timeout_forwarded", kwargs.get("hooks_timeout") == 5.5, repr(kwargs))
+
+ # Test that min_confidence parameter is forwarded correctly
+ router = BatchRouter()
+ out = laya_predict_batch(BATCH_REQUESTS, min_confidence=0.75, router=router)
+ _, kwargs = router.predict_batch_calls[0][1], router.predict_batch_calls[0][2]
+ ok("batch/min_confidence_forwarded", kwargs.get("min_confidence") == 0.75, repr(kwargs))
+
+ # Test that sort_by_length parameter is forwarded correctly
+ router = BatchRouter()
+ out = laya_predict_batch(BATCH_REQUESTS, sort_by_length=True, router=router)
+ _, kwargs = router.predict_batch_calls[0][1], router.predict_batch_calls[0][2]
+ ok("batch/sort_by_length_forwarded", kwargs.get("sort_by_length") is True, repr(kwargs))
+
+ # Test that defaults don't pollute kwargs
+ router = BatchRouter()
+ out = laya_predict_batch(BATCH_REQUESTS, router=router)
+ _, kwargs = router.predict_batch_calls[0][1], router.predict_batch_calls[0][2]
+ ok("batch/defaults_not_in_kwargs", "hooks_timeout" not in kwargs and
+ "min_confidence" not in kwargs and "sort_by_length" not in kwargs, repr(kwargs))
+
expect_tool_error("batch/predict_count_mismatch",
lambda: laya_predict_batch(BATCH_REQUESTS, router=ShortRouter()),
"internal_error")