Giving Models Memory (Embedding & Vector Math)

Cosine Similarity

Learning Outcome

4

Compare cosine similarity with simple keyword matching.

3

Recognize the steps for calculating cosine similarity and its role in RAG systems.

2

Identify  how vectors and angles relate to similarity scores.

1

Understand what Cosine Similarity is and how it measures similarity between vectors.

Let's Recall

Text Embeddings – AI converts text into numerical vectors that capture meaning, so similar ideas end up
close together in a mathematical space.

 


We know embeddings place similar meanings close together — but how does an AI actually measure and
calculate that "closeness" mathematically?
Let's understand Cosine Similarity.

Aarav and Riya are walking. When both walk North, they move in the same direction, so they are highly similar. If Aarav walks North and Riya walks East, their directions are different, so they are less similar.

Later, Aarav searches “Laptop” in an online store. The store has Notebook Computer, Portable Computer, and Ultrabook.

The AI converts these words into vectors (embeddings). Since their meanings are related, their vectors point in similar directions.

Cosine similarity compares these directions and gives the related words a high similarity score.

What is Cosine Similarity?

Cosine Similarity is a mathematical measure that calculates how similar two vectors are by measuring the cosine of  the angle between them. In Generative AI, it is commonly used to compare text embeddings and determine how  similar two pieces of text are in meaning.

What is a Vector?

A vector is simply a list of numbers. When a sentence is converted into an embedding, it  becomes a vector.

Example:

Sentence A: "I love reading books." → Vector A: [0.21, 0.45, -0.12, 0.78]

 

Sentence B: "I enjoy reading novels." → Vector B: [0.20, 0.47, -0.10, 0.80]

 

Code : cosine_similarity([0.21, 0.45, -0.12, 0.78], [0.20, 0.47, -0.10, 0.80]) → 0.998 

Understanding the Angle

Imagine two arrows starting from the same point.

 Case 1: Both arrows point in the same direction.

Angle = 0°.

Similarity = Maximum.

The smaller the angle between two vectors, the more similar their meanings.

 Case 2: The arrows point in very different directions. Angle = 90°.

Similarity = Low

Case 3: The arrows point in opposite directions.

Angle = 180°.

Similarity = Minimum.

Cosine Similarity Score

In most text embedding applications, similarity scores usually range between 0 and 1, since embeddings representing natural language are rarely exact opposites.

Code

from sklearn.metrics.pairwise import cosine_similarity

How Does Cosine Similarity Work?

In most AI applications, libraries calculate cosine similarity automatically — you don't need to compute it  manually.

Cosine Similarity in RAG Systems (Core Mechanism)

Documents → Embeddings → Query Embedding → Cosine Similarity → Similar Documents → LLM Answer

A patient asks, “I have chest pain and difficulty breathing.” Even without the exact phrase, relevant documents on cardiac symptoms and breathing difficulties can be found.

Identify the goal: Find relevant documents despite different wording.

Convert the query and documents into vectors.

2

Use cosine similarity to find the closest document vectors.
Retrieve the relevant documents and let the LLM generate the answer.

Step 1:

Step 2:

2

Step 3:

Without cosine similarity, RAG systems would struggle to identify and retrieve the most relevant  documents.

 Real-Life Examples

Semantic Search: “Healthy food” → Nutritious meals, Balanced diet

Chatbot: “Internet not working” → Wi-Fi problems

Recommendations: “Deep Learning” → AI, Neural Networks

Plagiarism: Different words → Similar meaning

Cosine Similarity vs Keyword Matching (Comparison) ​

  

An Easy Way to Remember ​

Think of a classroom. Students facing the same direction are likely following the same lesson. Students facing  different directions are focused elsewhere.

Cosine similarity simply measures how closely two vectors point in the same direction.

  

Daily Life Applications

Online Store: 

“Laptop” retrieves notebook computers and ultrabooks.

Streaming Service: 

Recommends shows with similar embeddings to those you watched.

Examples around us:

Support Chatbot:

Understands “Internet not working” as “Wi-Fi connection problems.”

Plagiarism Checker:

Detects similar ideas despite different wording.

Summary

4

Used in RAG, search, recommendations, chatbots, and plagiarism detection.

3

Converts text into vectors, compares their angle, and gives a similarity score.

2

Smaller angle = higher similarity; near 1 = similar, near 0/negative = different.

1

Measures similarity between two vectors using the angle between them.

Quiz

How can a hospital system retrieve relevant documents even when the exact query words don't match?

B. Cosine similarity of embedding vectors

C. Manual staff tagging

D. Random selection

A. Exact keyword matching

Quiz-Answer

B. Cosine similarity of embedding vectors

How can a hospital system retrieve relevant documents even when the exact query words don't match?

A. Exact keyword matching

C. Manual staff tagging

D. Random selection

GenAI - Cosine Similarity

By Content ITV

GenAI - Cosine Similarity

  • 137