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:
OpenAIEncoder.
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.
RouteLayer with an input query:
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:
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:
retrieve_multiple_routes is called with a query that does not match any
routes, it will return an empty list:

