KNN (K-Nearest Neighbors)
Overview
knn is a selection algorithm that uses K-Nearest Neighbors for example-based model selection. It finds the k most similar historical queries and votes on the best model based on their outcomes.
It aligns to config/algorithm/selection/knn.yaml.
Implementation: Rust via Linfa (linfa-nn) for high-performance nearest-neighbor search.
Key Advantages
- Interpretable: routing decisions can be traced back to similar historical examples.
- No training phase needed — just store and query examples.
- Works well when similar prompts should choose similar models.
- Quality-weighted voting considers the outcome quality of each neighbor.
Algorithm Principle
- Embedding: Each query is embedded into a dense vector.
- Search: Find the k nearest neighbors in the historical query embedding space.
- Voting: Each neighbor votes for the model that was used. Votes are weighted by the neighbor's outcome quality and inverse distance.
- Selection: The model with the highest weighted vote is selected.