Route Queries by Embedding Centroids

easy
Unit 8 · Decoding0/3 to milestone

A cheap way to route user messages ("billing", "tech support", "sales") without an LLM call: embed a few example queries per route, average them into a centroid, and send each new query to the nearest centroid. If nothing is close enough, fall back to a general handler.

Implement:

  • fit_centroids(examples): examples maps route names to lists of embedding vectors. Return a dict of unit-length centroids (normalise each example first, average, then normalise again).
  • route(query, centroids, threshold=0.5): the route with the highest cosine similarity (ties: alphabetical), or "fallback" if the best similarity is below threshold
  • routing_accuracy(queries, labels, centroids, threshold=0.5): the fraction routed to the correct label
#routing#embeddings#classification2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Route Queries by Embedding Centroids · TechGradify