Quick Stats
Completed
0
Time Spent
0m
Streak
0
User
Embeddings and Semantic Search
How meaning becomes math.
Turning text into vectors
The heart of RAG is finding text that's relevant to a question, even when it shares no words with it. Keyword search can't do that — "reset my password" won't match a doc titled "recovering account access." Embeddings solve it by converting a piece of text into a list of numbers (a vector) that captures its meaning. Texts about similar ideas land near each other in this vector space; unrelated texts land far apart.
Searching by meaning
Once everything is a vector, "relevance" becomes "closeness." To find the right chunks, we embed the user's question into the same space and look for the nearest stored vectors, usually with cosine similarity (a measure of the angle between vectors). The closest chunks are the most semantically related — which is why this is called semantic search.
from openai import OpenAI
client = OpenAI()
v = client.embeddings.create(model="text-embedding-3-small", input="reset my password")
print(len(v.data[0].embedding)) # e.g. 1536 numbersEach piece of text becomes a fixed-length vector like this. Embed your whole knowledge base once, store the vectors, and every future question is just a nearest-neighbor lookup.
What makes embeddings good
- The model matters. Better embedding models place related meanings closer and separate subtle differences. Retrieval-tuned models (from OpenAI, Cohere, Voyage, and open options) beat generic ones.
- Same model for both sides. Always embed your documents and your queries with the same model — vectors from different models aren't comparable.
- Domain fit. A model that understands legal or medical language will retrieve better in those domains.
An embedding is meaning turned into geometry. Once text is a point in space, "find me something relevant" becomes "find me something nearby" — a problem computers solve in milliseconds.
Try this: Picture three phrases — "happy dog," "joyful puppy," and "tax return." The first two would sit almost on top of each other in embedding space; the third, far away. That spatial intuition is exactly what powers semantic search.