Routers
=Routers are the core components of Semantic Router that actually perform the intelligent routing of text to the appropriate handlers. They combine encoders and indexes to create a powerful semantic classification system.
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
The router is the main interface you’ll interact with when using Semantic Router, as it brings together all the other components (routes, encoders, and indexes) into a cohesive system.
Router Types
Semantic Router provides two main types of routers:
SemanticRouter
The SemanticRouter
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
The HybridRouter
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
The HybridRouter
allows fine-tuning the balance between dense and sparse embeddings:
Router Return Values
When you call a router with a query, it returns a RouteChoice
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
For detailed information on routers and their configuration options, refer to the API documentation.