Query Routing
Definition
Query Routing is a component of RAG systems that analyzes incoming queries and routes them to the most suitable source: vector database, search index, external API, or specialized model. Routing can be based on rules, embeddings, or LLM classification.
Types of Routing
| Type | Description | Example |
|---|---|---|
| Rules | Explicit conditions based on keywords | Query with "weather" → Weather API |
| Embeddings | Cosine similarity with source descriptions | Semantic Router |
| LLM | Model selects source based on description | Function calling |
Example Logic
def route_query(query: str) -> str:
if "course" in query:
return "vector_db"
if "weather" in query:
return "weather_api"
return "llm_default"