AI Engineer Problems › RAG / Retrieval

Why Retrieval Returns the Worst Match First

Medium · Code Comprehension · RAG / Retrieval

Scenario

A support bot uses vector search to find the knowledge-base article most relevant to a question. Users complain it keeps surfacing an unrelated article as the top result, even when a clearly better article exists.

cosine_distance returns a distance, where 0 means identical and larger means less similar. search embeds the query, scores every document, and returns the top results. The embeddings here are tiny hand-made vectors so you can reason about them by eye.

Run the code as-is. The query is about refunds, and the first result is the shipping article.

Your task: find why the ranking is backwards and fix search. Do not change the documents, the embeddings, or cosine_distance.

Interviewers love this one because the code runs without errors and returns plausible-looking output. The only way to catch it is to reason about what the numbers mean.

Solve it in the browser on GenAIDojo