Start learning free

LESSON 60 OF 129 · AI ENGINEERING: RETRIEVAL (RAG)

Similarity in Python

Practice

An embedding turns a text into a vector, a list of numbers. To search the shop's documents, your program needs one number that says how close two vectors are. That number is the cosine similarity.

It starts with the dot product: multiply the two vectors position by position and add the products. A product is large only where both vectors have a large number, so the sum grows when the numbers sit in the same places.

[2, 0, 1] and [1, 3, 2]  →  2×1 + 0×3 + 1×2  =  4

The dot product alone has a flaw: a vector with bigger numbers gets a bigger sum, whatever it means. So you divide by the two vectors' lengths. The length of a vector is the square root of its dot product with itself: for [3, 4] that is the square root of 9 + 16, which is 5. After the division the score is 1 when two vectors point the same way, 0 when they have nothing in common, and below 0 when they point in opposite ways.

Your program scores the question "Can I get my money back?" against the chunks of the returns policy and the shipping rules. This is what must print (the last four lines come from the check, which calls your similarity on small vectors):

0.783 returns.md Returns and refunds: You can return any item within 14 days ...
0.651 returns.md Returns and refunds: To start a return, write to help@corner...
0.64 returns.md Returns and refunds: Refunds go back to the card or PayPal a...
0.825 returns.md Returns and refunds: Gift cards and items bought on sale are...
0.324 returns.md Returns and refunds: If an item arrives damaged, tell us wit...
0.0 shipping.md Shipping: Standard shipping is free on orders over $50. Belo...
0.0 shipping.md Shipping: Standard delivery takes 3 to 5 business days. Expr...
0.094 shipping.md Shipping: We ship to the United States and Canada. Shipping ...
0.096 shipping.md Shipping: Orders placed before 2 pm ship the same day. You g...
similarity([3, 4], [6, 8]) = 1.0
similarity([1, 0], [0, 1]) = 0.0
similarity([1, 2], [2, 1]) = 0.8
similarity([1, 2], [-1, -2]) = -1.0

Your task: write dot(a, b) and similarity(a, b) in plain Python. Then embed the question, embed each chunk of returns.md and shipping.md, and print one row per chunk: the score rounded to 3 decimals, the source, and the first 60 characters of the text followed by three dots.


Assignment
Follow the Coding Tutorial to write the dot product and the cosine similarity, then score the question against the chunks of two documents.


Hint
The length of a vector is the square root of its dot product with itself, and math is already imported. embed takes a list and returns a list, so pass a list with one text and take the one vector out with [0].


STEP 1 OF 10
    Ctrl + Enter to check

    Ai Similarity in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free