InferenceAI

Enter at least two characters to search.

Start free

Precision at K

easyEvaluation10 min10 XP

Return precision@k for ranked retrieved IDs against a collection of relevant IDs. Return 0.0 for k <= 0.

Explore the underlying concept with illustrative inputs. This experiment is separate from the problem’s test cases.

INTERACTIVE ENGINEERING · TEACHING MODEL

Retrieval pipeline

Embed a query, rank documents and select context using a small vector model.

  1. Query ready
  2. Vectors compared
  3. Candidates ranked
  4. Context selected

Query: How does KV caching work? · vector [1, 0]

A · Serving and KV memoryVector [1, 0]
B · Cache eviction policiesVector [0.8, 0.2]
C · Retrieval evaluationVector [0.2, 0.8]
D · Ranking with embeddingsVector [0, 1]

Manually assigned two-dimensional vectors illustrate ranking. No embedding service or language model runs in this demo.

Change the experiment

Algorithm sketch
q = embed(query)
ranked = sort_by_cosine(q, documents)
context = ranked[:top_k]
# Pass context to a separate generator
Try 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.

Open the full explainer and practice links

Examples

EXAMPLE WALKTHROUGHone relevant

A guided view of the published example, not a trace of your code.

INPUT[[1,3],[1,2,3],2]
TASKprecision_at_k()
EXPECTED OUTPUT0.5

Start with the input values shown in this example.

Example 1 of 2

Input: relevant = [1,3], retrieved = [1,2,3], k = 2

Output: 0.5

Explanation: one relevant

Input: relevant = [1,2], retrieved = [1,2], k = 2

Output: 1

Explanation: all relevant

Hint 1

Convert relevant IDs to a set.

Topics

EvaluationRetrievalPython