The SemanticRouter
is the main class of the semantic router. It is responsible
for making decisions about which route to take based on an input utterance.
A SemanticRouter
consists of an encoder
, an index
, and a list of routes
.
Route layers that include dynamic routes (i.e. routes that can generate dynamic
decision outputs) also include an llm
.
To use a SemanticRouter
we first need some routes
. We can initialize them like
so:
We initialize an encoder — there are many options available here, from local
to API-based. For now we’ll use the OpenAIEncoder
.
Now we define the RouteLayer
. When called, the route layer will consume text
(a query) and output the category (Route
) it belongs to — to initialize a
RouteLayer
we need our encoder
model and a list of routes
.
Now we can call the RouteLayer
with an input query:
The output is a RouteChoice
object, which contains the name of the route,
the function call (if any), and the similarity score that triggered the route
choice.
We can try another query:
Both are classified accurately, what if we send a query that is unrelated to our existing Route objects?
In this case, the RouteLayer
is unable to find a route that matches the
input query and so returns a RouteChoice
with name=None
.
We can also retrieve multiple routes with their associated score using
retrieve_multiple_routes
:
If retrieve_multiple_routes
is called with a query that does not match any
routes, it will return an empty list:
You can find an introductory notebook for the route layer here.