Text embeddings explained
How AI turns text into vectors, compares their meaning and retrieves useful passages. A worked example shows what a similarity score can and cannot tell you.
A text embedding is a learned numerical representation of a piece of text. An embedding model turns a question, sentence or passage into a list of numbers called a vector. Software can compare those vectors to look for related meanings, even when the texts use different words.
A question such as ‘How do returns work?’ could be relevant to a passage headed ‘Sending items back’. Embeddings give a search system a way to connect them. The connection still needs checking. A passage can concern the right topic and give the wrong answer for the customer’s situation.
What is inside a vector?
A vector is an ordered list of numbers. Each position in that list is a dimension. An embedding model learns how to represent text numerically during training, so related inputs can produce representations that work well for its intended comparison task.
The numbers are useful because software can calculate relationships between whole vectors. A person does not need to read each coordinate to understand the original passage. The source text should remain available so that a search result can return something the reader can inspect.
A small worked example
This worked example uses two-dimensional vectors so the maths can be drawn on a page. Real text embedding models use many more dimensions. These coordinates are invented for teaching, not outputs measured from an embedding model.
- Question vector A = [1.0, 0.0].
- Passage vector B = [0.8, 0.6].
Cosine similarity compares the directions of two vectors. Draw both as arrows from the same starting point. A smaller angle between them gives a higher cosine similarity. To calculate it, multiply matching coordinates and add the products, then divide by the product of the vectors’ lengths.
- Multiply and add the coordinates. (1.0 × 0.8) + (0.0 × 0.6) = 0.8. This is the dot product.
- Find each vector’s length. A has length 1. B also has length 1 because the square root of (0.8² + 0.6²) is 1.
- Divide the dot product by the two lengths multiplied together. 0.8 ÷ (1 × 1) = 0.80.
Cosine similarity ranges from -1 to 1 for non-zero vectors. A score of 1 means the same direction, 0 means perpendicular directions, and -1 means opposite directions. Those are geometric relationships. How useful they are for finding text depends on what the embedding model learned.
How vectors become search results
A search system can encode its document collection ahead of time. When someone asks a question, it encodes that question and compares it with the stored vectors. The best-scoring matches point back to the original passages.
- Prepare passages that make sense on their own. Preserve enough surrounding detail to identify what each passage describes.
- Create and store an embedding for each passage, linked to its source text.
- Encode the new question into a compatible vector space.
- Compare the question vector with the stored vectors and rank the matches.
- Return the corresponding passages and check whether they answer the question.
The encoders need to produce compatible representations. Some models use different instructions or processing paths for questions and documents. Follow the model’s intended setup. Two arrays having the same length does not, by itself, make them compatible.
Where search needs more than similarity
Splitting documents into short passages can remove essential surrounding details. A line saying ‘the limit is thirty days’ is hard to use if the surrounding section explained which product, policy version or customer group it applies to. Keep those details with the passage.
Exact identifiers also need care. A query for a particular part number needs the right identifier, even if several nearby products have very similar descriptions. Hybrid search combines meaning-based retrieval with keyword matching. It is one way to retain both signals.
A reranker can provide another check. It reads the question and each candidate passage together, then scores their relevance. This adds processing, so it is commonly applied to the shortlist returned by the first search. It can improve the order without making every result correct.
How this fits into an AI answer
Retrieval-augmented generation, usually shortened to RAG, retrieves relevant material and gives it to a generative model alongside the question. Embedding-based search can supply that material. The generative model then writes an answer using what it has been given.
For a business assistant, the practical check is whether it finds the right evidence for real questions. That is part of preparing the information used by the services described on our AI employees page.
We would test retrieval before judging how polished the final answer sounds. Choose questions people actually ask, identify the passages that should answer them, then inspect what the system returns. Include questions the collection cannot answer. Record the misses and fix the source material or retrieval setup before relying on the generated replies.