content/develop/ai/redisvl/0.12.1/api/router.md
<a id="semantic-router-api"></a>
class SemanticRouter(name, routes, vectorizer=None, routing_config=None, redis_client=None, redis_url='redis://localhost:6379', overwrite=False, connection_kwargs={})Semantic Router for managing and querying route vectors.
Initialize the SemanticRouter.
add_route_references(route_name, references)Add a reference(s) to an existing route.
clear()Flush all routes from the semantic router index.
delete()Delete the semantic router index.
delete_route_references(route_name='', reference_ids=[], keys=[])Get references for an existing semantic router route.
classmethod from_dict(data, **kwargs)Create a SemanticRouter from a dictionary.
from redisvl.extensions.router import SemanticRouter
router_data = {
"name": "example_router",
"routes": [{"name": "route1", "references": ["ref1"], "distance_threshold": 0.5}],
"vectorizer": {"type": "openai", "model": "text-embedding-ada-002"},
}
router = SemanticRouter.from_dict(router_data)
classmethod from_existing(name, redis_client=None, redis_url='redis://localhost:6379', **kwargs)Return SemanticRouter instance from existing index.
classmethod from_yaml(file_path, **kwargs)Create a SemanticRouter from a YAML file.
from redisvl.extensions.router import SemanticRouter
router = SemanticRouter.from_yaml("router.yaml", redis_url="redis://localhost:6379")
get(route_name)Get a route by its name.
get_route_references(route_name='', reference_ids=[], keys=[])Get references for an existing route route.
model_post_init(context, /)This function is meant to behave like a BaseModel method to initialise private attributes.
It takes context as an argument since that’s what pydantic-core passes when calling it.
remove_route(route_name)Remove a route and all references from the semantic router.
route_many(statement=None, vector=None, max_k=None, distance_threshold=None, aggregation_method=None)Query the semantic router with a given statement or vector for multiple matches.
to_dict()Convert the SemanticRouter instance to a dictionary.
from redisvl.extensions.router import SemanticRouter
router = SemanticRouter(name="example_router", routes=[], redis_url="redis://localhost:6379")
router_dict = router.to_dict()
to_yaml(file_path, overwrite=True)Write the semantic router to a YAML file.
from redisvl.extensions.router import SemanticRouter
router = SemanticRouter(
name="example_router",
routes=[],
redis_url="redis://localhost:6379"
)
router.to_yaml("router.yaml")
update_route_thresholds(route_thresholds)Update the distance thresholds for each route.
update_routing_config(routing_config)Update the routing configuration.
model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
name: strThe name of the semantic router.
property route_names: List[str]Get the list of route names.
property route_thresholds: Dict[str, float | None]Get the distance thresholds for each route.
routes: List[Route]List of Route objects.
routing_config: RoutingConfigConfiguration for routing behavior.
vectorizer: BaseVectorizerThe vectorizer used to embed route references.
class RoutingConfig(*, max_k=1, aggregation_method=DistanceAggregationMethod.avg)Configuration for routing behavior.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
max_k: Annotated[int, FieldInfo(annotation=NoneType, required=True, metadata=[Strict(strict=True), Gt(gt=0)])]Aggregation method to use to classify queries.
model_config: ClassVar[ConfigDict] = {'extra': 'ignore'}Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
class Route(*, name, references, metadata={}, distance_threshold=0.5)Model representing a routing path with associated metadata and thresholds.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
distance_threshold: Annotated[float, FieldInfo(annotation=NoneType, required=True, metadata=[Strict(strict=True), Gt(gt=0), Le(le=2)])]Distance threshold for matching the route.
metadata: Dict[str, Any]Metadata associated with the route.
model_config: ClassVar[ConfigDict] = {}Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
name: strThe name of the route.
references: List[str]List of reference phrases for the route.
class RouteMatch(*, name=None, distance=None)Model representing a matched route with distance information.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
distance: float | NoneThe vector distance between the statement and the matched route.
model_config: ClassVar[ConfigDict] = {}Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
name: str | NoneThe matched route name.
class DistanceAggregationMethod(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)Enumeration for distance aggregation methods.
avg = 'avg'Compute the average of the vector distances.
min = 'min'Compute the minimum of the vector distances.
sum = 'sum'Compute the sum of the vector distances.