mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-07 02:59:05 +00:00
fix: filter_tools
This commit is contained in:
parent
dfb26c7cf5
commit
ad3b06fd68
2 changed files with 8 additions and 7 deletions
|
|
@ -150,6 +150,8 @@ class SemanticMCPToolFilter:
|
|||
Returns:
|
||||
Filtered and ordered list of tools (up to top_k)
|
||||
"""
|
||||
# Query semantic router with limit for top-k matches
|
||||
from semantic_router.schema import RouteChoice
|
||||
if not self.enabled or not available_tools:
|
||||
return available_tools
|
||||
|
||||
|
|
@ -169,11 +171,9 @@ class SemanticMCPToolFilter:
|
|||
verbose_logger.warning("Router rebuild failed, returning all tools")
|
||||
return available_tools
|
||||
|
||||
# Query semantic router
|
||||
from semantic_router.schema import RouteChoice
|
||||
|
||||
verbose_logger.debug(f"Querying semantic router with: '{query[:50]}...'")
|
||||
matches = self.tool_router(text=query)
|
||||
verbose_logger.debug(f"Querying semantic router with: '{query[:50]}...' (top_k={top_k})")
|
||||
matches = self.tool_router(text=query, limit=top_k)
|
||||
|
||||
if not matches:
|
||||
verbose_logger.warning(
|
||||
|
|
@ -184,10 +184,11 @@ class SemanticMCPToolFilter:
|
|||
# Extract matched tool names
|
||||
matched_names: List[str] = []
|
||||
if isinstance(matches, RouteChoice):
|
||||
matched_names = [matches.name]
|
||||
if matches.name:
|
||||
matched_names = [matches.name]
|
||||
elif isinstance(matches, list):
|
||||
# semantic-router returns list of RouteChoice, take top_k
|
||||
matched_names = [m.name for m in matches[:top_k] if hasattr(m, 'name')]
|
||||
matched_names = [m.name for m in matches[:top_k] if hasattr(m, 'name') and m.name is not None]
|
||||
|
||||
if not matched_names:
|
||||
verbose_logger.warning("No matched tool names extracted, returning all tools")
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ async def test_e2e_semantic_filter():
|
|||
|
||||
data = {
|
||||
"model": "gpt-4",
|
||||
"messages": [{"role": "user", "content": "Send an email"}],
|
||||
"messages": [{"role": "user", "content": "Send an email and create a calendar event"}],
|
||||
"tools": tools,
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue