Advanced AI Engineering

The Mathematical Revolution behind Semantic Search

If you ask a traditional database such as MySQL and PostgreSQL to search for documents containing the word car, the database searches for that exact combination of letters. If you search for vehicle, the database finds nothing, even though the meaning is virtually identical. After all, computers do not naturally understand language; they understand only numbers. The breakthrough that enables modern AI (and RAG systems) to understand the meaning and context of human language is based on two mathematical concepts: Vector Embeddings and Cosine Similarity.

What is Vector Embedding?

A vector embedding is a sequence of numbers (a vector of, for example, 1536 dimensions) that represents the semantic meaning of a piece of text (a word, a sentence, or an entire document). You send text to an embedding model (such as OpenAI’s text-embedding-3-small), and the model spits out a long list of numbers.

You can imagine this as a gigantic, multidimensional space (a semantic map). In this space, words with similar meanings lie close together. In this mathematical space, the vector for the word ‘king’ lies very close to ‘queen’ and ‘throne’, but is miles away from ‘tractor’ or ‘calculator’. In doing so, the model captures nuances in tone, context, and synonyms.

Cosine Similarity: Measuring the Distance in Vector Space

Once thousands of documents have been converted into vectors and stored in a Vector Database, you can start asking questions. How does the database know which document best fits the user’s query? This happens via Cosine Similarity.

When a user asks a question (“How do I resolve a network outage?”), this question is immediately converted into a vector. The system then calculates the angle between the question vector and all document vectors in the database. The cosine of that angle yields a score between -1 and 1:

  • A score of 1.0 means that the vectors point in exactly the same direction (the meaning is 100% identical).
  • A score of 0.0 means that they are orthogonal (no relationship at all).
  • A score of -1.0 means exactly the opposite.

The database sorts the results based on the highest score and selects the most relevant documents lightning fast.

Choosing the Right Vector Database

To perform these calculations on millions of vectors within milliseconds, a traditional database is insufficient. A complete market has emerged around specialized vector databases such as Qdrant, Milvus, Weaviate, and Pinecone, or extensions to existing systems such as pgvector for PostgreSQL. Understanding embeddings and similarity is the absolute foundation for any developer who wants to build advanced AI applications. Read more about AI development at Computable.

 

Next:
Knowledge base overview

Verified by MonsterInsights