Understanding Routers
In Semantic Router, routers serve several key functions:- Process incoming queries into semantic representations
- Match queries to routes based on similarity
- Make decisions about which handler should process a query
- Manage routes (adding, updating, removing)
- Provide confidence scores for routing decisions
Router Types
Semantic Router provides two main types of routers:SemanticRouter
TheSemanticRouter
is the standard router that uses dense embeddings to match queries to routes. It’s the most common and widely used router type.
Example usage:
HybridRouter
TheHybridRouter
uses both dense and sparse embeddings for a more balanced approach, combining semantic understanding with keyword matching. This can improve accuracy in many cases, especially where exact keyword matching is important.
Example usage:
Key Router Features
Route Management
Routers make it easy to add, update, and manage routes:Threshold Control
Control the sensitivity of your router by setting score thresholds:Asynchronous Operation
Both router types support asynchronous operation for improved performance in async environments:Loading from Configuration
Routers can be loaded from configurations for easier deployment across environments:Considerations for Choosing a Router
When selecting a router for your application, consider:- Accuracy requirements: HybridRouter typically provides better accuracy by combining semantic and keyword matching
- Performance needs: SemanticRouter is more lightweight and can be faster
- Query characteristics: If your queries often contain specific keywords, HybridRouter may perform better
- Resource constraints: HybridRouter requires more computational resources
- Infrastructure: Make sure you have the required API keys for the encoders used by your selected router
Advanced Usage: Auto-Sync
Both router types support syncing routes between local and remote indexes:Advanced Usage: Hybrid Alpha
TheHybridRouter
allows fine-tuning the balance between dense and sparse embeddings:
Router Return Values
When you call a router with a query, it returns aRouteChoice
object with these key attributes:
name
: The name of the matched route (or empty string if no match)score
: The confidence score of the matchfunction_schema
: Optional function schema associated with the routemetadata
: Any additional metadata associated with the route