Router
Modules concerning the routers used in Gen AI applications.
AurelioSemanticRouter(default_route, valid_routes, encoder, routes=None, index=None, auto_sync=SyncMode.LOCAL.value, **kwargs)
Bases: BaseRouter
A router that utilizes the Aurelio Labs library to route the input source to the appropriate path.
The AurelioSemanticRouter utilizes the Aurelio Labs library to route a given input source to an appropriate path
based on the similarity with existing samples. If the determined route is not valid, it defaults to a predefined
route.
Attributes:
| Name | Type | Description |
|---|---|---|
route_layer |
RouteLayer
|
The Aurelio Labs route layer that handles the routing logic. |
default_route |
str
|
The default route to be used if the input source is not similar to any of the routes. |
valid_routes |
set[str]
|
A set of valid routes for the router. |
Notes
For more information about the Aurelio Labs library, please refer to https://github.com/aurelio-labs/semantic-router
Initializes a new instance of the AurelioSemanticRouter class.
To define the routes, at least one of the routes or index parameters must be provided.
When both parameters are provided, the routes parameter is ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_route |
str
|
The default route to be used if the input source is not similar to any of the routes. |
required |
valid_routes |
set[str]
|
A set of valid routes for the router. |
required |
encoder |
DenseEncoder
|
An Aurelio Labs dense encoder to encode the input source and the samples. The encoded vectors are used to calculate the similarity between the input source and the samples. |
required |
routes |
list[Route] | dict[str, list[str | bytes]] | None
|
A list of Aurelio Labs Routes
or a dictionary mapping route names to the list of samples. Ignored if |
None
|
index |
BaseAurelioIndex | None
|
A router index to retrieve the routes.
If provided, it is prioritized over |
None
|
auto_sync |
str
|
The auto-sync mode for the router. Defaults to "local". |
value
|
kwargs |
Any
|
Additional keyword arguments to be passed to the Aurelio Labs Route Layer. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
|
from_file(default_route, valid_routes, file_path)
classmethod
Creates a new instance of the AurelioSemanticRouter class from a file.
This method creates a new instance of the AurelioSemanticRouter class from a file. It supports JSON and YAML file extensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_route |
str
|
The default route to be used if the input source is not similar to any of the routes. |
required |
valid_routes |
set[str]
|
A set of valid routes for the router. |
required |
file_path |
str
|
The path to the file containing the routes. The file extension must be either JSON or YAML. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AurelioSemanticRouter |
AurelioSemanticRouter
|
A new instance of the AurelioSemanticRouter class. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file extension is not ".json" or ".yaml". |
from_preset(modality, preset_name, preset_kwargs=None, **kwargs)
classmethod
Initialize the Aurelio semantic based router component using preset model configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
modality |
str
|
type of modality input. |
required |
preset_name |
str
|
Name of the preset to use. |
required |
preset_kwargs |
dict | None
|
placeholder for preset additional arguments. |
None
|
**kwargs |
Any
|
Additional arguments to pass for this class. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
AurelioSemanticRouter |
AurelioSemanticRouter
|
Initialized aurelio semantic based router component using preset model. |
LMBasedRouter(lm_request_processor, default_route, valid_routes, lm_output_key=DEFAULT_LM_OUTPUT_KEY)
Bases: BaseRouter, UsesLM
A router that utilizes a language model to determine the appropriate route for an input source.
This class routes a given input source to an appropriate path based on the output of a language model. If the determined route is not valid, it defaults to a predefined route.
Attributes:
| Name | Type | Description |
|---|---|---|
lm_request_processor |
LMRequestProcessor
|
The request processor that handles requests to the language model. |
default_route |
str
|
The default route to be used if the language model's output is invalid. |
valid_routes |
set[str]
|
A set of valid routes for the router. |
lm_output_key |
str
|
The key in the language model's output that contains the route. |
Notes
The lm_request_processor must be configured to:
1. Take a "source" key as input. The input source of the router should be passed as the value of
this "source" key.
2. Return a JSON object which contains the selected route as a string. The key of the route is specified by the
lm_output_key attribute. Furthermore, the selected route must be present in the valid_routes set.
Output example, assuming the lm_output_key is "route":
{
"route": "
Initializes a new instance of the LMBasedRouter class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lm_request_processor |
LMRequestProcessor
|
The request processor that handles requests to the language model. |
required |
default_route |
str
|
The default route to be used if the language model's output is invalid. |
required |
valid_routes |
set[str]
|
A set of valid routes for the router. |
required |
lm_output_key |
str
|
The key in the language model's output that contains the route. Defaults to DEFAULT_LM_OUTPUT_KEY. |
DEFAULT_LM_OUTPUT_KEY
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided default route is not in the set of valid routes. |
from_preset(modality, preset_name, preset_kwargs=None, **kwargs)
classmethod
Initialize the LM based router component using preset model configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
modality |
str
|
type of modality input. |
required |
preset_name |
str
|
Name of the preset to use. |
required |
preset_kwargs |
dict | None
|
placeholder for preset additional arguments. |
None
|
**kwargs |
Any
|
Additional arguments to pass for this class. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
LMBasedRouter |
LMBasedRouter
|
Initialized lm based router component using preset model. |
RuleBasedRouter(ruleset_map, default_route, valid_routes)
Bases: BaseRouter
A rule-based router that directs the input text to an appropriate route based on a set of rules.
The RuleBasedRouter routes incoming input text to different paths by evaluating a set of rules encapsulated in
RouterRuleset objects. Each ruleset consists of multiple RouterRule objects, and the router determines which
route to take based on the input text and the rules defined in the ruleset.
If match_all is True in a RouterRuleset, all rules in that ruleset must match the input text for the
associated route to be selected. If False, matching any rule in the ruleset will cause that route to be selected.
If no match is found, the router defaults to the default_route.
Attributes:
| Name | Type | Description |
|---|---|---|
ruleset_map |
dict[str, RouterRuleset]
|
A mapping of route names to their corresponding rulesets. |
default_route |
str
|
The default route to be taken if no ruleset matches the input text. |
valid_routes |
set[str]
|
A set of valid routes that the router can direct to. |
Initializes a new instance of the RuleBasedRouter class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ruleset_map |
dict[str, RouterRuleset]
|
A mapping of route names to their corresponding rulesets. |
required |
default_route |
str
|
The default route to be taken if no ruleset matches the input text. |
required |
valid_routes |
set[str]
|
A set of valid routes for the router. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
SimilarityBasedRouter(em_invoker, route_examples, default_route, similarity_threshold=0.5)
Bases: BaseRouter
A router that utilizes an embedding model to determine the appropriate route for an input text.
This class routes a given input text to an appropriate path based on semantic similarity between the input text and predefined route examples. It calculates cosine similarity between the input text embedding and the embeddings of route examples, then selects the route with the highest similarity score above a threshold.
Attributes:
| Name | Type | Description |
|---|---|---|
em_invoker |
BaseEMInvoker
|
The embedding model invoker to use for vectorization. |
route_examples |
dict[str, list[str]]
|
A mapping of route names to their example texts. |
similarity_threshold |
float
|
The minimum similarity score required for a route to be selected. |
default_route |
str
|
The default route to be used if no route meets the similarity threshold. |
valid_routes |
set[str]
|
A set of valid routes for the router. |
Initializes a new instance of the SimilarityBasedRouter class.
Args: em_invoker (BaseEMInvoker): The embedding model invoker to use for vectorization. route_examples (dict[str, list[str]]): A mapping of route names to their example texts. The keys define the valid routes, and values are example texts for each route. default_route (str): The default route to be used if no route meets the similarity threshold. Must be one of the keys in route_examples. Examples: "general", "fallback", "customer_service" similarity_threshold (float, optional): The minimum similarity score required for a route to be selected. Must be between 0 and 1. This threshold is compared against normalized cosine similarity scores, which are derived from the standard cosine similarity (ranging from -1 to 1) and converted to a 0-1 range where 0 indicates maximum dissimilarity and 1 indicates maximum similarity. Examples of route_examples: { "tech_support": [ "I can't log in to my account", "The app keeps crashing", "Password reset not working", "Getting error code 500" ], "billing": [ "How much do I owe?", "I want to dispute a charge", "When is my payment due?", "Can I get a refund?" ], "general": [ "What are your business hours?", "How do I contact support?", "Tell me about your services", "Where are you located?" ] }
Raises:
| Type | Description |
|---|---|
ValueError
|
|
clear_cache()
Clears the cached route embeddings.
This method clears the cached embeddings, forcing them to be regenerated on the next routing operation.