Router
Modules concerning the routers used in Gen AI applications.
AurelioSemanticRouter(default_route, valid_routes, encoder, routes=None, index=None, auto_sync='local', **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". |
'local'
|
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". |
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
|
|