Semantic Router#
Semantic Router#
- class SemanticRouter(name, routes, vectorizer=None, routing_config=None, redis_client=None, redis_url='redis://localhost:6379', overwrite=False, connection_kwargs={})[source]#
Semantic Router for managing and querying route vectors.
Initialize the SemanticRouter.
- Parameters:
name (str) – The name of the semantic router.
routes (List[Route]) – List of Route objects.
vectorizer (BaseVectorizer, optional) – The vectorizer used to embed route references. Defaults to default HFTextVectorizer.
routing_config (RoutingConfig, optional) – Configuration for routing behavior. Defaults to the default RoutingConfig.
redis_client (Optional[Redis], optional) – Redis client for connection. Defaults to None.
redis_url (str, optional) – The redis url. Defaults to redis://localhost:6379.
overwrite (bool, optional) – Whether to overwrite existing index. Defaults to False.
connection_kwargs (Dict[str, Any]) – The connection arguments for the redis client. Defaults to empty {}.
- classmethod from_dict(data, **kwargs)[source]#
Create a SemanticRouter from a dictionary.
- Parameters:
data (Dict[str, Any]) – The dictionary containing the semantic router data.
- Returns:
The semantic router instance.
- Return type:
- Raises:
ValueError – If required data is missing or invalid.
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_yaml(file_path, **kwargs)[source]#
Create a SemanticRouter from a YAML file.
- Parameters:
file_path (str) – The path to the YAML file.
- Returns:
The semantic router instance.
- Return type:
- Raises:
ValueError – If the file path is invalid.
FileNotFoundError – If the file does not exist.
from redisvl.extensions.router import SemanticRouter router = SemanticRouter.from_yaml("router.yaml", redis_url="redis://localhost:6379")
- get(route_name)[source]#
Get a route by its name.
- Parameters:
route_name (str) – Name of the route.
- Returns:
The selected Route object or None if not found.
- Return type:
Optional[Route]
- remove_route(route_name)[source]#
Remove a route and all references from the semantic router.
- Parameters:
route_name (str) – Name of the route to remove.
- Return type:
None
- route_many(statement=None, vector=None, max_k=None, distance_threshold=None, aggregation_method=None)[source]#
Query the semantic router with a given statement or vector for multiple matches.
- Parameters:
statement (Optional[str]) – The input statement to be queried.
vector (Optional[List[float]]) – The input vector to be queried.
max_k (Optional[int]) – The maximum number of top matches to return.
distance_threshold (Optional[float]) – The threshold for semantic distance.
aggregation_method (Optional[DistanceAggregationMethod]) – The aggregation method used for vector distances.
- Returns:
The matching routes and their details.
- Return type:
List[RouteMatch]
- to_dict()[source]#
Convert the SemanticRouter instance to a dictionary.
- Returns:
The dictionary representation of the SemanticRouter.
- Return type:
Dict[str, Any]
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)[source]#
Write the semantic router to a YAML file.
- Parameters:
file_path (str) – The path to the YAML file.
overwrite (bool) – Whether to overwrite the file if it already exists.
- Raises:
FileExistsError – If the file already exists and overwrite is False.
- Return type:
None
from redisvl.extensions.router import SemanticRouter router = SemanticRouter( name="example_router", routes=[], redis_url="redis://localhost:6379" ) router.to_yaml("router.yaml")
- update_routing_config(routing_config)[source]#
Update the routing configuration.
- Parameters:
routing_config (RoutingConfig) – The new routing configuration.
- name: str#
The name of the semantic router.
- property route_names: List[str]#
Get the list of route names.
- Returns:
List of route names.
- Return type:
List[str]
- property route_thresholds: Dict[str, float | None]#
Get the distance thresholds for each route.
- Returns:
Dictionary of route names and their distance thresholds.
- Return type:
Dict[str, float]
- routing_config: RoutingConfig#
Configuration for routing behavior.
- vectorizer: BaseVectorizer#
The vectorizer used to embed route references.
Routing Config#
- class RoutingConfig(*, distance_threshold=0.5, max_k=1, aggregation_method=DistanceAggregationMethod.avg)[source]#
Configuration for routing behavior.
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
- Parameters:
distance_threshold (float)
max_k (int)
aggregation_method (DistanceAggregationMethod)
- aggregation_method: DistanceAggregationMethod#
Aggregation method to use to classify queries.
- distance_threshold: float#
The threshold for semantic distance.
- max_k: int#
The maximum number of top matches to return.
Route#
- class Route(*, name, references, metadata={}, distance_threshold=None)[source]#
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 if the input data cannot be parsed to form a valid model.
- Parameters:
name (str)
references (List[str])
metadata (Dict[str, str])
distance_threshold (float | None)
- distance_threshold: float | None#
Distance threshold for matching the route.
- metadata: Dict[str, str]#
Metadata associated with the route.
- name: str#
The name of the route.
- references: List[str]#
List of reference phrases for the route.
Route Match#
- class RouteMatch(*, name=None, distance=None)[source]#
Model representing a matched route with distance information.
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
- Parameters:
name (str | None)
distance (float | None)
- distance: float | None#
The vector distance between the statement and the matched route.
- name: str | None#
The matched route name.
Distance Aggregation Method#
- class DistanceAggregationMethod(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
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.