Basic

Ranking the chunks

Embed every chunk once, embed the question, score every pair, and take the top few. That is the whole search, and the tuning is in how many you take and what you do with the scores.

What you will see

['ship', 'hours'] - top k with k=2. Note refund is out, which is correct for this question.

About 1 min of reading

Try It

Rank toy vectors.

1

Set up

q = [1, 0] docs = [('ship', [0.9, 0.2]), ('refund', [0.1, 0.95]), ('hours', [0.8, 0.1])] scored = sorted(((t, q[0]*v[0] + q[1]*v[1]) for t, v in docs), key=lambda x: -x[1]) print(scored)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
ship 0.9, hours 0.8, refund 0.05 - the ranking the search would use.
2

Take two

print([t for t, _ in scored[:2]])

Before you run it, say out loud what it will print. Then open the answer.

What it prints
['ship', 'hours'] - top k with k=2. Note refund is out, which is correct for this question.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Every chunk is scored and sorted.
1 Embed the question
↓
2 Score against every stored vector
↓
3 Sort by score
↓
4 Take the top k
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

q = [1, 0] sets nothing on screen
docs = [('ship', [0.9, 0.2]), ('refund', [0.1, 0.95]), ('hours', [0.8, 0.1])] sets nothing on screen
scored = sorted(((t, q[0]*v[0] + q[1]*v[1]) for t, v in docs), key=lambda x: -x[1]) sets nothing on screen
print(scored) ship 0.9, hours 0.8, refund 0.05 - the ranking the search would use.
Why this works
Look at the scores

Print the top five with their scores. A big drop between the second and third tells you the question had two good answers; a flat list tells you the question matched nothing well.

Embed the documents once

Embedding the same text every query is slow and wasteful. Do it once at build time, store the vectors, and embed only the question at search time.

Examples

Top k, not all

Take the best few, not everything above a threshold. A fixed count keeps the prompt size predictable, which matters because context costs and a growing list eventually overflows.

The score is not a probability

A score of 0.62 does not mean 62 percent likely correct. Comparing scores to each other is valid; reading one as a confidence is not, which is why the threshold is tuned on your own data.

Sort before you cut

Rank every candidate, then take the top k. Cutting first and sorting after gives you an arbitrary subset, and the right chunk may never have been in it.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← What an embedding is
Previous Lesson
What an embedding is
Next Lesson
Keyword search beside it
Keyword search beside it →