My account

Embeddings

An embedding model turns a piece of text into a list of numbers (a vector). Texts with a close meaning get vectors that point in a close direction, so you can compare meaning with simple math like cosine similarity. Use the same model for documents and questions, save which model made each vector, and embed again when you change the model. Embeddings are great for meaning and weak for exact things like order numbers.

Not reviewedWritten with AI helpReading time: 14 minShop help assistant exampleC# and .NET 10 code

Author: bezzad

Contents

The problem: “money back” finds nothing

Our shop has about 300 help articles. The search box uses SQL LIKE on the words. A customer types “how do I get my money back”. The article is called “Refunds and returns”. No word matches, so the search shows nothing, and the customer opens a support ticket.

Adding synonyms by hand does not scale: “money back”, “refund”, “return the item”, “cancel and pay back”, and the same in Persian. We need search by meaning, not by letters.

What is an embedding?

An embedding model reads a text and returns a fixed list of numbers, for example 1,536 numbers. This list is a vector: a point (or an arrow) in a space with 1,536 directions.

The model was trained so that texts with a close meaning get close vectors. “Refund my order” and “I want my money back” land near each other. “Track my parcel” lands in another area.

Getting money backShippingRefund my orderI want my money backReturn an itemQuestion: money back?Where is my parcel?Track my orderDelivery timeClose points = close meaning. Words do not need to match.
A real embedding has hundreds of numbers. This picture squeezes it to two, only to show the idea: texts about getting money back are in one group, texts about shipping in another.
  1. One text in, one vector out. A sentence, a paragraph or a whole short article gives one vector.
  2. The numbers alone mean nothing. Number 712 is not “the money number”. Only the distance between vectors has a meaning.
  3. It is not a language model answer. An embedding model does not write text. It is smaller, faster and much cheaper per token.

Comparing two vectors

The usual way to compare is cosine similarity: how much two arrows point the same way.

near 1close meaningnear 0no relationnear minus 1opposite (rare in practice)
A small angle means a close meaning (similarity near 1). A right angle means no relation (near 0). Length does not matter, only direction.
// Packages: Microsoft.Extensions.AI and System.Numerics.Tensors
IEmbeddingGenerator<string, Embedding<float>> embedder = /* from DI */;

ReadOnlyMemory<float> a = await embedder.GenerateVectorAsync("Refund my order");
ReadOnlyMemory<float> b = await embedder.GenerateVectorAsync("I want my money back");
ReadOnlyMemory<float> c = await embedder.GenerateVectorAsync("Where is my parcel?");

float ab = TensorPrimitives.CosineSimilarity(a.Span, b.Span); // high
float ac = TensorPrimitives.CosineSimilarity(a.Span, c.Span); // lower
  1. Cosine similarity goes from −1 to 1. Bigger means closer.
  2. Cosine distance is 1 minus the similarity. Smaller means closer. Databases often sort by distance.
  3. Normalized vectors have length 1. Many providers return them like this. Then cosine similarity and the dot product give the same order, and the dot product is a little faster.
  4. The scores are relative. One model may give 0.8 for close texts, another 0.5. Do not copy a “good score” from a blog. Measure it on your own data.

Picking an embedding model

Question Why it matters
Does it know my languages? Our customers write Persian and English. A model trained mostly on English puts Persian texts in poor places.
How many dimensions? More numbers can hold more detail but need more storage and slower search. Some models let you ask for fewer.
How long can the input be? Text longer than the limit is cut or rejected. Long articles must be split into pieces (see the RAG lesson).
Hosted or local? Hosted is easy. Local keeps data inside your servers and has no cost per call.
Price per million tokens? You embed every document once, and every question every time.

The best check is not a public ranking. Take 30 to 50 real customer questions, write which article answers each one, and see which model puts the right article near the top. This is a small eval, and it takes an afternoon.

The golden rule: one model for both sides

Documents and questions must be embedded with the same model, the same version and the same settings (like the number of dimensions). Vectors from two different models live in two different spaces. Comparing them gives numbers, but the numbers mean nothing.

The danger: two models can have the same length, like 1,536. The database does not complain. The search just returns random-looking results.

So save the model name with every vector:

public sealed class ArticleChunk
{
    public int Id { get; set; }
    public int ArticleId { get; set; }
    public string Text { get; set; } = "";
    public string EmbeddingModel { get; set; } = "";   // for example "text-embedding-3-small"
    public float[] Embedding { get; set; } = [];
}

When you change the model:

  1. Embed all documents again with the new model, into a new column or table.
  2. Switch the question side to the new model at the same moment.
  3. Compare both on your small eval before you delete the old vectors.

Cost and speed tips

  1. Send many texts in one call. The GenerateAsync method takes a list. One call with 100 pieces is much faster than 100 calls.
  2. Embed a document only when it changes. Keep a hash of the text, and skip it when the hash is the same.
  3. Clean the text first. Remove menus, cookie banners and HTML tags. Noise moves the vector away from the real meaning.
  4. Persian needs more tokens. Many tokenizers cut Persian into more pieces than English, so the same article costs more.

What embeddings are bad at

  1. Exact codes. “Order 48213” and “order 48231” look almost the same to an embedding model. For ids, SKUs and error codes, use normal keyword search or a database filter.
  2. Negation. “Items you can return” and “items you cannot return” can get very close vectors.
  3. Numbers and dates. “Orders over 50 dollars” is not understood as a math rule.
  4. Fresh words. A new product name that did not exist in the training data may land in a strange place.

This is why real systems often mix vector search with keyword search. The advanced RAG lesson shows how.

Common mistakes

Mistake Result Right way
Questions and documents use different models Random results, no error. Same model and version on both sides; save the model name.
Using a “good score” number from the internet Wrong cut-off for your model and data. Measure scores on your own examples.
One embedding per call in a loop Slow and costly indexing. Batch many texts per call.
Embedding raw HTML pages Menus and banners blur the meaning. Clean the text first.
Using embeddings to find an order id Near-miss ids come back. Keyword search or a database filter for exact values.

Summary in five lines

  1. An embedding model turns text into a vector; close meaning gives close vectors.
  2. Compare vectors with cosine similarity; only relative scores on your data mean something.
  3. Pick the model with your languages, your length and a small eval of real questions.
  4. Use the same model for documents and questions, save the model name, and re-embed on change.
  5. Embeddings are weak at exact codes, negation and numbers; mix in keyword search.

Done reading this lesson?Mark it as finished so it shows in your lessons list and report card.