Intermediate level · Module 4: Retrieval-Augmented Generation · Lesson 1 of 3
Embeddings and Semantic Search
Represent text as vectors and find passages by meaning rather than exact words.
In this lesson you will
- Explain embeddings and cosine similarity
- Search documents by meaning
Key concepts
embeddingvectorcosine similaritysemantic search
The lesson
An embedding model turns a piece of text into a vector — a list of numbers — so that texts with similar meanings get similar vectors.
Cosine similarity measures how closely two vectors point in the same direction: near 1 for similar meanings, near 0 for unrelated ones. Searching means embedding the question and finding the passages with the highest similarity.
Semantic search finds 'papers needed for fee help' even when the document says 'documents required for school fee support', which keyword search would miss.
Step by step
- Embed five sentences with an embedding model.
- Compute similarities between them.
- Search with a question that uses different words.
- Compare with keyword search.
Semantic search in a few lines: cosine similarity
Rank documents by how close their meaning is to a question, even when they use different words.
from math import sqrt def cosine(a, b): dot = sum(x * y for x, y in zip(a, b)) return dot / (sqrt(sum(x * x for x in a)) * sqrt(sum(y * y for y in b))) # Tiny made-up "embeddings". Imagine the 4 numbers mean:# [about fees, about health, about documents, about opening hours]# (real embeddings have hundreds of numbers with no simple meanings)documents = { "How to register for the fee support scheme": [0.9, 0.1, 0.2, 0.0], "Clinic opening hours and vaccination days": [0.1, 0.8, 0.1, 0.5], "Documents needed to apply for fee support": [0.7, 0.1, 0.7, 0.1],}question = [0.7, 0.1, 0.7, 0.0] # "What papers do I need for school fee help?" ranked = sorted(documents.items(), key=lambda item: cosine(question, item[1]), reverse=True)for title, vector in ranked: print(f"{cosine(question, vector):.3f} {title}")
Output
0.995 Documents needed to apply for fee support
0.845 How to register for the fee support scheme
0.232 Clinic opening hours and vaccination daysHow it works
Cosine similarity divides the dot product of two vectors by the product of their lengths, giving a score near 1 when they point the same way. The question says 'papers', the best document says 'documents' — no shared keyword — yet their vectors are almost identical, so it ranks first. In a real retrieval system an embedding model produces the vectors, and the top-ranked passages are given to the language model to answer from. Vectors are invented for teaching; the output was verified with an equivalent JavaScript implementation because no Python interpreter was available when the example was written.
Key concepts
embedding vectorsdot productcosine similarityranking by meaning
Practice activity
Build a small semantic search over the FAQ documents and test it with ten questions.
Expected result: A search that returns relevant passages for most questions, with results recorded.
Check your understanding
-
What does an embedding model produce?
Show answer
A vector of numbers representing the meaning of text.
-
What does a cosine similarity near 1 mean?
Show answer
The two texts have very similar meanings.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.