Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Generative AI & Large Language Models — Alpha School

Lesson 6: Embeddings and Semantic Search

In Lesson 4 you saw that words can be placed as points in a space, where similar words sit near each other. The same idea works for whole sentences, paragraphs and documents: each one can be turned into a list of numbers — an embedding — that captures what it is about. Once text becomes numbers, a remarkable thing becomes possible: you can search by meaning, not just by words. That is semantic search, and it is the engine behind much of what you will build later in this course.

Ordinary search matches words. If you search a help centre for 'how do I change my password', a word-matching search looks for those exact words and misses a page titled 'resetting your login credentials', even though it answers your question. Semantic search instead turns your question and every page into embeddings, and finds the pages whose embeddings sit closest to your question's embedding. It understands that 'change my password' and 'resetting your login credentials' mean nearly the same thing, because their embeddings are near each other in the space.

How close is close?

Closeness in the embedding space is usually measured with cosine similarity, a number that says how much two lists of numbers point in the same direction. A score near 1 means the two texts are very similar in meaning; near 0 means they are unrelated; negative means they point in opposite directions. The exact number matters less than the ranking: when you search, the system simply returns the texts with the highest similarity to your question. It is the same idea as a recommendation system — 'people who liked this also liked that' — except the similarity is between meanings, not between people's tastes.

Embeddings are also used to organise information. A company can turn all its documents into embeddings and cluster them, discovering that its support tickets fall into natural groups — billing, delivery, returns — without anyone labelling them. A researcher can find the papers most similar to one they already like. A student can find the lesson most relevant to a question. Wherever you need to find, group or compare text by what it means, embeddings are the tool.

When embeddings fail

Embeddings are powerful, but they have blind spots that are worth knowing before you rely on them. Negation is one: 'I love this phone' and 'I do not love this phone' share almost all their words, so their embeddings can sit close together even though they mean opposite things. Sarcasm is another: the words say one thing and the meaning is the reverse, and no embedding can see the tone. Very long documents are a third problem, as you saw in Lesson 6: squashing a whole book into one list of numbers loses the detail. And embeddings inherit the biases of their training text, so a search for a profession might return results skewed towards one gender, not because anyone programmed that in, but because the text itself was skewed. None of this makes embeddings useless — they are still the best tool for searching by meaning — but it means a semantic search system should be tested on the awkward cases, not only on the easy ones, and its results should be shown to a human rather than acted on blindly.

There are limits, and they are the same ones you met in Lesson 4. Embeddings capture how text is used, not whether it is true. Two texts can be very close in meaning while one is correct and the other is wrong. An embedding also flattens a whole document into one list of numbers, so a long document about many things may sit in a blurry middle, similar to everything and close to nothing. Good systems work around this by splitting long documents into short chunks before embedding them — an idea that returns in Lesson 11, when you build retrieval-augmented generation.

Meaning as distance: near neighbours are similar

dog puppy cat king queen prince river bank Similar meanings sit close together Real embeddings have hundreds of directions; this map shows just two.

Animals cluster on the left, royalty on the right. 'Bank' sits between the two groups because it belongs to both — a word with two meanings.

Key Terms

Embedding
A list of numbers representing a piece of text — a word, sentence or document — capturing what it is about.
Semantic search
Searching by meaning: finding the texts whose embeddings are closest to the question's embedding, even when they share no words with it.
Cosine similarity
A number measuring how much two embeddings point in the same direction; near 1 means very similar in meaning.
Vector database
A store designed to hold many embeddings and find the closest ones to a question quickly.
Chunk
A short piece cut from a long document before embedding, so each embedding captures one clear topic.
Keyword search
Searching by matching exact words, which misses pages that answer the question using different words.

Discussion Questions

  1. You search a help centre for 'change my password' and the answer page says 'resetting your login credentials'. Why does word-matching search fail here, and how does semantic search succeed?
  2. Two documents can be close in meaning while one is true and the other false. What does that mean for a system that relies on embeddings alone?
  3. Why split a long document into chunks before embedding it, instead of embedding the whole thing at once?

Practice Questions

  1. What is semantic search?
  2. How is semantic search different from keyword search?
  3. What does a cosine similarity near 1 mean?
  4. What is a chunk, and why is it used?
  5. Give one use of embeddings besides search.
Show answers
  1. Searching by meaning: finding texts whose embeddings are closest to the question's embedding. It finds pages that answer the question even when they use different words.
  2. Keyword search matches exact words; semantic search matches meaning. A keyword search misses a page titled 'resetting your login credentials' when you ask about changing a password.
  3. The two texts are very similar in meaning. Near 0 means unrelated, and negative means they point in opposite directions.
  4. A short piece cut from a long document before embedding, so each embedding captures one clear topic. A whole long document about many things would sit in a blurry middle, similar to everything and close to nothing.
  5. Grouping documents into clusters, such as support tickets falling into billing, delivery and returns. Embeddings can also find the papers or lessons most similar to one you already like.
Keep reading to complete this lesson