Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion laya/mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)

Expand Down
15 changes: 13 additions & 2 deletions laya/mcp/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
34 changes: 30 additions & 4 deletions tests/test_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,8 @@ def _answer_for(self, request):
"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):
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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")
Expand Down
Loading