Retrieval Score Threshold
A RAG service receives ranked hits as [document_id, score] pairs. Implement filter_retrieval_hits(hits, min_score, max_hits). Return document IDs with score greater than or equal to min_score, preserving input order, up to max_hits results. Ignore later duplicates of the same ID, even if the first occurrence failed the threshold. Return [] when max_hits <= 0. IDs are strings and scores are numbers.
Explore the underlying concept with illustrative inputs. This experiment is separate from the problem’s test cases.
Retrieval pipeline
Embed a query, rank documents and select context using a small vector model.
- Query ready
- Vectors compared
- Candidates ranked
- Context selected
Query: How does KV caching work? · vector [1, 0]
Manually assigned two-dimensional vectors illustrate ranking. No embedding service or language model runs in this demo.
Change the experiment
q = embed(query)
ranked = sort_by_cosine(q, documents)
context = ranked[:top_k]
# Pass context to a separate generatorTry it yourself
Switch the query and compare top-k values. Explain why adding more documents can introduce irrelevant context.
Query ready. Advance to inspect the next stage.
Vector similarity ranks candidates, but it does not prove relevance or factual correctness. Evaluate retrieval on your own queries.
Playback pauses in hidden tabs and offscreen. With reduced motion enabled, use the timeline or Next step.Examples
A guided view of the published example, not a trace of your code.
[[["a",0.9],["b",0.4],["a",0.99],["c",0.8]],0.5,2]["a","c"]Start with the input values shown in this example.
Input: hits = [["a",0.9],["b",0.4],["a",0.99],["c",0.8]], min_score = 0.5, max_hits = 2
Output: ["a","c"]
Explanation: ranked and deduplicated
Input: hits = [["a",0.4],["a",0.9],["b",0.5]], min_score = 0.5, max_hits = 3
Output: ["b"]
Explanation: first occurrence wins
Hint 1
A document ID is considered seen even when its first score is too low.
Hint 2
The score threshold is inclusive.