ЁЯПл The SchoolтА║ЁЯЧ║я╕П Vector DatabasesтА║ЁЯУР рдзрдбрд╛ 03 тАФ Similarity: рджреЛрди рдЕрд░реНрде "рдХрд┐рддреА рдЬрд╡рд│" рдмрд╕рддрд╛рдд рддреЗ рдореЛрдЬрдгреЗ
ЁЯЦ╝я╕П See the drawing + lab ЁЯПа Course home ЁЯМ┐ Branch on GitHub тЬПя╕П View source
ЁЯЦ╝я╕П рдЖрдХреГрддреА рдЖрдгрд┐ labThe drawing + lab рдкреВрд░реНрдг рдкрд╛рдирд╛рд╡рд░ рдЙрдШрдбрд╛ тЖЧOpen full page тЖЧ

ЁЯУР рдзрдбрд╛ 03 тАФ Similarity: рджреЛрди рдЕрд░реНрде "рдХрд┐рддреА рдЬрд╡рд│" рдмрд╕рддрд╛рдд рддреЗ рдореЛрдЬрдгреЗ

ЁЯУН рддреБрдореНрд╣реА рдЗрдереЗ рдЖрд╣рд╛рдд: 8 рдкреИрдХреА рдзрдбрд╛ 03 ┬╖ рдорд╛рдЧреЗ: lesson-02-text-to-vectors ┬╖ рдкреБрдвреЗ: lesson-04-nearest-neighbors


ЁЯУж рдпрд╛ рдмреНрд░рдБрдЪрдордзреНрдпреЗ рдХрд╛рдп рдЖрд╣реЗ

рдзрдбреЗ 01тАУ02, рдЖрдгрд┐ рдкреНрд░рддреНрдпреЗрдХ vector database рдЪреЗ рддреАрди рдУрд│реАрдВрдЪреЗ рд╣реГрджрдп: similarity function тАФ рдЖрдгрд┐ рдЖрдкрд▓реЗ рдлрдХреНрдд рдПрдХрд╛ рдУрд│реАрдЪреЗ dot product рдХрд╛ рдЖрд╣реЗ.

ЁЯзТ 5 рд╡рд░реНрд╖рд╛рдВрдЪреНрдпрд╛ рдореБрд▓рд╛рд▓рд╛ рд╕рдордЬрд╛рд╡рд▓реНрдпрд╛рд╕рд╛рд░рдЦреЗ

Hall рдордзреНрдпреЗ рджреЛрди рдореБрд▓реЗ рдЙрднреА рдЖрд╣реЗрдд. рддреА рдХрд┐рддреА "рд╕рд╛рд░рдЦреА" рдЖрд╣реЗрдд? рджреЛрди рдкреНрд░рд╛рдорд╛рдгрд┐рдХ рдорд╛рдкреЗ:

Text рд╕рд╛рдареА рджрд┐рд╢рд╛ рдЬрд┐рдВрдХрддреЗ: robots рдмрджреНрджрд▓рдЪрд╛ 10 рдкрд╛рдирд╛рдВрдЪрд╛ рдирд┐рдмрдВрдз рдЖрдгрд┐ robots рдмрджреНрджрд▓рдЪреА рдПрдХрд╛ рдУрд│реАрдЪреА рдЯреАрдк рдПрдХрд╛рдЪ рджрд┐рд╢реЗрд▓рд╛ рдмреЛрдЯ рджрд╛рдЦрд╡рддрд╛рдд тАФ рдирд┐рдмрдВрдз рдлрдХреНрдд рдЬрд╛рд╕реНрдд рдЬреЛрд░рд╛рдиреЗ рджрд╛рдЦрд╡рддреЛ (рд▓рд╛рдВрдм vector). Distance рддреНрдпрд╛рдВрдирд╛ рд╡реЗрдЧрд│реЗ рдореНрд╣рдгреЗрд▓ (рдирд┐рдмрдВрдз рджреВрд░ рдЙрднрд╛ рдЕрд╕рддреЛ); cosine рддреНрдпрд╛рдВрдирд╛ рднрд╛рд╡рдВрдбреЗ рдореНрд╣рдгрддреЛ. рдЕрд░реНрде рдореНрд╣рдгрдЬреЗ рджрд┐рд╢рд╛; рд▓рд╛рдВрдмреА рдореНрд╣рдгрдЬреЗ рдлрдХреНрдд рдЖрд╡рд╛рдЬ. ЁЯУв

рдЖрдгрд┐ рдЖрдкрд▓рд╛ toy рд╡рд╛рдкрд░рддреЛ рддреА рд╡реНрдпрд╛рд╡рд╕рд╛рдпрд┐рдХ рдпреБрдХреНрддреА (vectordb.py): рд▓рд┐рд╣рд┐рддрд╛рдирд╛рдЪ рдкреНрд░рддреНрдпреЗрдХ vector рдЪреА рд▓рд╛рдВрдмреА 1 рдХрд░рд╛ (normalize) (рдзрдбрд╛ 02) тАФ рдордЧ cosine рдореНрд╣рдгрдЬреЗ рд╕рд╛рдзрд╛ dot product рдЙрд░рддреЛ: рдЬреБрд│рдгрд╛рд░реЗ рдирд┐рд░реНрджреЗрд╢рд╛рдВрдХ рдЧреБрдгрд╛, рдмреЗрд░реАрдЬ рдХрд░рд╛. рдПрдХ рдУрд│, рдкреНрд░рдЪрдВрдб рд╡реЗрдЧрд╡рд╛рди, hardware рд▓рд╛ рд╕реЛрдпреАрдЪреА. рдореНрд╣рдгреВрдирдЪ рдЖрдкрд▓реНрдпрд╛ code рдордзрд▓реЗ cosine() рдлрдХреНрдд sum(x*y тАж) рдЖрд╣реЗ тАФ рд╣реБрд╢рд╛рд░реАрдЪрд╛ рднрд╛рдЧ рджрд╛рд░рд╛рддрдЪ рдЭрд╛рд▓реЗрд▓рд╛ рдЕрд╕рддреЛ.

ЁЯЧ║я╕П рдЖрдХреГрддреА

flowchart TB
    subgraph hall["ЁЯЧ║я╕П the hall"]
        essay["ЁЯУЪ 10-page robot essay<br/>long vector, points NE"]
        note["ЁЯУЭ one-line robot note<br/>short vector, points NE"]
        lunch["ЁЯНЫ lunch menu<br/>points SW"]
    end
    cos["ЁЯзн cosine: compare DIRECTIONS<br/>essay┬╖note = 0.95 тЬЕ siblings<br/>essay┬╖lunch = 0.02 тАФ strangers"]
    dist["ЁЯУП distance: compare POSITIONS<br/>essay is 'far' from note тЭМ<br/>fooled by length/volume"]
    trick["тЪб the trick: normalize at write time<br/>тЖТ cosine becomes a plain dot product<br/>(one line, hardware-fast)"]
    hall --> cos
    hall --> dist
    cos --> trick

тЭУ рдХрд╛рдп

ЁЯдФ рдХрд╛

рдкреНрд░рддреНрдпреЗрдХ search рдирд┐рдХрд╛рд▓рд╛рдЪрд╛ рдХреНрд░рдо, рдкреНрд░рддреНрдпреЗрдХ "рд╕рд╛рд░рдЦреЗ tickets" ranking, рдкреНрд░рддреНрдпреЗрдХ RAG retrieval тАФ рдпрд╛рдЪ рдПрдХрд╛ рдЖрдХрдбреНрдпрд╛рдиреЗ рд▓рд╛рд╡рд▓реЗрд▓реЗ рдЕрд╕рддреЗ. Retrieval рдЪреБрдХреАрдЪреЗ рд╡рд╛рдЯрд▓реЗ рддрд░ рдкрд╣рд┐рд▓реА рдкрд╛рдпрд░реА рдиреЗрд╣рдореА: рдХреЛрдгрддрд╛ metric, рд╕рдЧрд│реЗ normalize рдЭрд╛рд▓реЗ рд╣реЛрддреЗ рдХрд╛, рджреЛрдиреНрд╣реА рдмрд╛рдЬреВрдВрдирд╛ рддреЛрдЪ embedder? (рддреАрди рдкреНрд░рд╢реНрди, рдирд╡реНрд╡рдж рдЯрдХреНрдХреЗ bugs.)

ЁЯзк рдХрд░реВрди рдкрд╛рд╣рд╛

python3 -c "
import sys; sys.path.insert(0,'vectordb')
from vectordb import embed, cosine
essay = 'the robot reads books about robots and robot learning ' * 10
note  = 'a robot book'
lunch = 'dal and rice for lunch on tuesday'
print('essay vs note :', round(cosine(embed(essay), embed(note)), 2), 'тЖР same direction, wildly different lengths')
print('essay vs lunch:', round(cosine(embed(essay), embed(lunch)), 2))
"

рд▓рд╛рдВрдмреА ├Ч10 тАФ similarity рдЬрд╢реАрдЪреНрдпрд╛ рддрд╢реА. рджрд┐рд╢рд╛ рдореНрд╣рдгрдЬреЗ рдЕрд░реНрде; normalization рдиреЗ рдЖрд╡рд╛рдЬ рдЖрдзреАрдЪ рдЦрд╛рдКрди рдЯрд╛рдХрд▓рд╛. ЁЯзн

тПня╕П рдкреБрдвреЗ

рдПрдХ query рд╡рд┐рд░реБрджреНрдз рджрд╣рд╛ рд▓рд╛рдЦ рдЬрд╛рдЧрд╛ тАФ рд╕рдЧрд│реНрдпрд╛рдВрдирд╛ рд╡рд┐рдЪрд╛рд░рдгреЗ рдЦреВрдк рд╣рд│реВ рдЖрд╣реЗ. Nearest neighbors: brute force, scaling рдЪреА рднрд┐рдВрдд, рдЖрдгрд┐ shortcut рдирдХрд╛рд╢реЗ (ANN).

git checkout lesson-04-nearest-neighbors

ЁЯУР Lesson 03 тАФ Similarity: measuring "how close" two meanings sit

ЁЯУН You are here: Lesson 03 of 8 ┬╖ Previous: lesson-02-text-to-vectors ┬╖ Next: lesson-04-nearest-neighbors


ЁЯУж What's in this branch

Lessons 01тАУ02, plus the three-line heart of every vector database: the similarity function тАФ and why ours is one line of dot product.

ЁЯзТ Explain like I'm 5

Two kids stand in the hall. How "similar" are they? Two honest measures:

For text, direction wins: a 10-page essay about robots and a one-line note about robots point the SAME way тАФ the essay just points harder (longer vector). Distance would call them different (the essay stands far away); cosine calls them siblings. Meaning is a direction; length is just volume. ЁЯУв

And the professional trick our toy uses (vectordb.py): normalize every vector to length 1 at write time (lesson 02) тАФ then cosine collapses into a plain dot product: multiply matching coordinates, add up. One line, massively fast, hardware-friendly. That's why cosine() in our code is just sum(x*y тАж) тАФ the clever part already happened at the door.

ЁЯЧ║я╕П Diagram

flowchart TB
    subgraph hall["ЁЯЧ║я╕П the hall"]
        essay["ЁЯУЪ 10-page robot essay<br/>long vector, points NE"]
        note["ЁЯУЭ one-line robot note<br/>short vector, points NE"]
        lunch["ЁЯНЫ lunch menu<br/>points SW"]
    end
    cos["ЁЯзн cosine: compare DIRECTIONS<br/>essay┬╖note = 0.95 тЬЕ siblings<br/>essay┬╖lunch = 0.02 тАФ strangers"]
    dist["ЁЯУП distance: compare POSITIONS<br/>essay is 'far' from note тЭМ<br/>fooled by length/volume"]
    trick["тЪб the trick: normalize at write time<br/>тЖТ cosine becomes a plain dot product<br/>(one line, hardware-fast)"]
    hall --> cos
    hall --> dist
    cos --> trick

тЭУ What

ЁЯдФ Why

Every search result ordering, every "similar tickets" ranking, every RAG retrieval тАФ sorted by this one number. When retrieval feels wrong, step one is always: what metric, was everything normalized, same embedder both sides? (Three questions, ninety percent of bugs.)

ЁЯзк Try it

python3 -c "
import sys; sys.path.insert(0,'vectordb')
from vectordb import embed, cosine
essay = 'the robot reads books about robots and robot learning ' * 10
note  = 'a robot book'
lunch = 'dal and rice for lunch on tuesday'
print('essay vs note :', round(cosine(embed(essay), embed(note)), 2), 'тЖР same direction, wildly different lengths')
print('essay vs lunch:', round(cosine(embed(essay), embed(lunch)), 2))
"

Length ├Ч10 тАФ similarity unmoved. Direction is meaning; normalization already ate the volume. ЁЯзн

тПня╕П Next

One query vs a MILLION seats тАФ asking everyone is too slow. Nearest neighbors: brute force, the scaling wall, and the shortcut maps (ANN).

git checkout lesson-04-nearest-neighbors
тЖР Previoustext to vectorsNext тЖТnearest neighbors

This page is the lesson's README from the lesson-03-similarity branch, shown here so the whole School stays on one site. Code files open on GitHub at the same branch.