Warning If wanting to use a fully local version of semantic router you can useHuggingFaceEncoder
andLlamaCppLLM
(pip install -qU "semantic-router[local]"
, see here). To use theHybridRouteLayer
you mustpip install -qU "semantic-router[hybrid]"
.
Defining Routes
We begin by defining a set ofRoute
objects. A Route represents a specific topic or intent that you want to detect in user input. Each Route is defined by example utterances that serve as a semantic reference point.
Let’s try two simple routes for now — one for talk on politics and another for chitchat:
Setting Up an Encoder
With our routes ready, now we initialize an embedding / encoder model. The encoder converts text into numerical vectors, allowing the system to measure semantic similarity. We currently supportCohereEncoder
and OpenAIEncoder
— more encoders will be added soon.
To initialize them:
Creating a RouteLayer
With ourroutes
and encoder
defined we now create a SemanticRouter
. The SemanticRouter is the decision-making engine that compares incoming text against your routes to find the best semantic match.
Making Routing Decisions
We can now use our route layer to make super fast routing decisions based on user queries. Behind the scenes, the system converts both your example utterances and the incoming query into vectors and finds the closest match. Let’s try with two queries that should trigger our route decisions:Handling Unmatched Queries
Now let’s try sending an unrelated query:None
! This feature is useful for creating fallback behavior or passthroughs in your applications when no intent is clearly matched.