Route filter
We can filter the routes that the SemanticRouter
considers when making a classification. This can be useful if we want to restrict the scope of possible routes based on some context.
For example, we may have a router with several routes, politics
, weather
, chitchat
, etc. We may want to restrict the scope of the classification to only consider the chitchat
route. We can do this by passing a route_filter
argument to our SemanticRouter
calls like so:
In this case, the SemanticRouter
will only consider the chitchat
route for the classification.
Full Example
We start by installing the library:
We start by defining a dictionary mapping routes to example phrases that should trigger those routes.
Let’s define another for good measure:
Now we initialize our embedding model:
Now we define the SemanticRouter
. When called, the router will consume text (a query) and output the category (Route
) it belongs to — to initialize a SemanticRouter
we need our encoder
model and a list of routes
.
Now we can test it:
Both are classified accurately, what if we send a query that is unrelated to our existing Route
objects?
In this case, we return None
because no matches were identified.
Demonstrating the Filter Feature
Now, let’s demonstrate the filter feature. We can specify a subset of routes to consider when making a classification. This can be useful if we want to restrict the scope of possible routes based on some context.
For example, let’s say we only want to consider the “chitchat” route for a particular query:
Even though the query might be more related to the “politics” route, it will be classified as “chitchat” because we’ve restricted the routes to consider.
Similarly, we can restrict it to the “politics” route:
In this case, it will return None
because the query doesn’t match the “politics” route well enough to pass the threshold.