Client Referencerouters
semantic_router.routers.hybrid
HybridRouter Objects
class HybridRouter(BaseRouter)A hybrid layer that uses both dense and sparse embeddings to classify routes.
__init__
def __init__(encoder: DenseEncoder,
sparse_encoder: Optional[SparseEncoder] = None,
llm: Optional[BaseLLM] = None,
routes: Optional[List[Route]] = None,
index: Optional[HybridLocalIndex] = None,
top_k: int = 5,
aggregation: str = "mean",
auto_sync: Optional[str] = None,
alpha: float = 0.3,
init_async_index: bool = False)Initialize the HybridRouter.
Arguments:
encoder(DenseEncoder): The dense encoder to use.sparse_encoder(Optional[SparseEncoder]): The sparse encoder to use.
add
def add(routes: List[Route] | Route)Add a route to the local HybridRouter and index.
Arguments:
route(Route): The route to add.
aadd
async def aadd(routes: List[Route] | Route)Add a route to the local HybridRouter and index asynchronously.
Arguments:
routes(List[Route] | Route): The route(s) to add.
__call__
def __call__(
text: Optional[str] = None,
vector: Optional[List[float] | np.ndarray] = None,
simulate_static: bool = False,
route_filter: Optional[List[str]] = None,
limit: int | None = 1,
sparse_vector: dict[int, float] | SparseEmbedding | None = None
) -> RouteChoice | list[RouteChoice]Call the HybridRouter.
Arguments:
text(Optional[str]): The text to encode.vector(Optional[List[float] | np.ndarray]): The vector to encode.simulate_static(bool): Whether to simulate a static route.route_filter(Optional[List[str]]): The route filter to use.limit(int | None): The number of routes to return, defaults to 1. If set to None, no limit is applied and all routes are returned.Optional[str]0 (Optional[str]1): The sparse vector to use.
Returns:
Optional[str]2: A RouteChoice or a list of RouteChoices.
acall
async def acall(
text: Optional[str] = None,
vector: Optional[List[float] | np.ndarray] = None,
limit: int | None = 1,
simulate_static: bool = False,
route_filter: Optional[List[str]] = None,
sparse_vector: dict[int, float] | SparseEmbedding | None = None
) -> RouteChoice | list[RouteChoice]Asynchronously call the router to get a route choice.
Arguments:
text(Optional[str]): The text to route.vector(Optional[List[float] | np.ndarray]): The vector to route.simulate_static(bool): Whether to simulate a static route (ie avoid dynamic route LLM calls during fit or evaluate).route_filter(Optional[List[str]]): The route filter to use.sparse_vector(dict[int, float] | SparseEmbedding | None): The sparse vector to use.
Returns:
Optional[str]0: The route choice.
fit
def fit(X: List[str],
y: List[str],
batch_size: int = 500,
max_iter: int = 500,
local_execution: bool = False)Fit the HybridRouter.
Arguments:
X(List[str]): The input data.y(List[str]): The output data.batch_size(int): The batch size to use for fitting.max_iter(int): The maximum number of iterations to use for fitting.local_execution(bool): Whether to execute the fitting locally.
evaluate
def evaluate(X: List[str], y: List[str], batch_size: int = 500) -> floatEvaluate the accuracy of the route selection.
Arguments:
X(List[str]): The input data.y(List[str]): The output data.batch_size(int): The batch size to use for evaluation.
Returns:
float: The accuracy of the route selection.