VibeKoding / Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat / Principles of Embeddings and Vector RetrievalPrinciples of Embeddings and Vector Retrieval
VK

Principles of Embeddings and Vector RetrievalPrinciples of Embeddings and Vector Retrieval

๐Ÿ“š Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat ๐ŸŒ Dual Bahasa (ID / EN) โšก VibeKoding Native

Ensiklopedia VibeKoding: Principles of Embeddings and Vector Retrieval.Ensiklopedia VibeKoding: Principles of Embeddings and Vector Retrieval.

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

How does a computer understand that "cats and dogs are similar, but neither is similar to a car"? To humans this is common sense, but to a computer, "cat", "dog", and "car" are just three unrelated strings. Embedding technology is the key to solving this problem โ€” it turns text into numerical vectors, allowing computers to understand semantic "closeness and distance."How does a computer understand that "cats and dogs are similar, but neither is similar to a car"? To humans this is common sense, but to a computer, "cat", "dog", and "car" are just three unrelated strings. Embedding technology is the key to solving this problem โ€” it turns text into numerical vectors, allowing computers to understand semantic "closeness and distance."

What will you learn in this article?What will you learn in this article?

After completing this chapter, you will gain:After completing this chapter, you will gain:

ChapterContentCore Concepts
Chapter 1Embedding ConceptsSemantic space, vector representation
Chapter 2Similarity CalculationCosine similarity, Euclidean distance
Chapter 3Vector IndexingBrute-force search vs. ANN
Chapter 4Vector DatabasesPinecone, Milvus, Chroma
Chapter 5End-to-End PipelineText โ†’ Vector โ†’ Storage โ†’ Query

------

0. Panorama: The Bridge from Text to Numbers0. Panorama: The Bridge from Text to Numbers

In the world of natural language processing, there is a fundamental challenge: computers only understand numbers, not text.In the world of natural language processing, there is a fundamental challenge: computers only understand numbers, not text.

Early approaches assigned each word a numeric ID (One-Hot encoding), for example "cat"=001, "dog"=010, "car"=100. But this had a fatal flaw: all words are equally distant from each other. The distance from "cat" to "dog" is exactly the same as the distance from "cat" to "car" โ€” which clearly defies our intuition.Early approaches assigned each word a numeric ID (One-Hot encoding), for example "cat"=001, "dog"=010, "car"=100. But this had a fatal flaw: all words are equally distant from each other. The distance from "cat" to "dog" is exactly the same as the distance from "cat" to "car" โ€” which clearly defies our intuition.

The revolutionary insight of Embedding is this: it maps each word into a dense, low-dimensional vector space, where semantically similar words naturally cluster together. In this space, "cat" and "dog" are close neighbors, while "car" sits far away โ€” the computer can finally "understand" semantics.The revolutionary insight of Embedding is this: it maps each word into a dense, low-dimensional vector space, where semantically similar words naturally cluster together. In this space, "cat" and "dog" are close neighbors, while "car" sits far away โ€” the computer can finally "understand" semantics.

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

- One-Hot: Dimension = vocabulary size (potentially tens of thousands), each vector has only a single 1 and all other entries are 0 โ€” sparse and devoid of semantics - Embedding: Dimensions typically 768โ€“1536, every number carries meaning โ€” dense and rich in semantic information - Key breakthrough: Word2Vec (2013) proved that "a word's meaning can be defined by its context," ushering in the Embedding era- One-Hot: Dimension = vocabulary size (potentially tens of thousands), each vector has only a single 1 and all other entries are 0 โ€” sparse and devoid of semantics - Embedding: Dimensions typically 768โ€“1536, every number carries meaning โ€” dense and rich in semantic information - Key breakthrough: Word2Vec (2013) proved that "a word's meaning can be defined by its context," ushering in the Embedding era

------

1. Embedding Concepts: Turning Text into Coordinates1. Embedding Concepts: Turning Text into Coordinates

The core idea of Embedding can be summarized in one sentence: use a set of numbers (a vector) to represent the meaning of a word or sentence.The core idea of Embedding can be summarized in one sentence: use a set of numbers (a vector) to represent the meaning of a word or sentence.

Imagine a two-dimensional coordinate system. Place "cat" at coordinates (0.2, 0.7), "dog" at (0.3, 0.6), and "car" at (0.9, 0.1). You'll notice "cat" and "dog" have similar coordinates, while "car" is far away from both. This is the intuition behind Embedding โ€” semantic similarity becomes spatial distance.Imagine a two-dimensional coordinate system. Place "cat" at coordinates (0.2, 0.7), "dog" at (0.3, 0.6), and "car" at (0.9, 0.1). You'll notice "cat" and "dog" have similar coordinates, while "car" is far away from both. This is the intuition behind Embedding โ€” semantic similarity becomes spatial distance.

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

1. Semantic clustering: Words with similar meanings automatically group together (animals in one cluster, food in another, technology in another) 2. Analogical relationships: Vector arithmetic can express semantic relationships โ€” the classic example: king โˆ’ man + woman โ‰ˆ queen 3. Dimensional meaning: Each dimension implicitly encodes some semantic feature (e.g., "is it an animal", "size", "sentiment", etc.)1. Semantic clustering: Words with similar meanings automatically group together (animals in one cluster, food in another, technology in another) 2. Analogical relationships: Vector arithmetic can express semantic relationships โ€” the classic example: king โˆ’ man + woman โ‰ˆ queen 3. Dimensional meaning: Each dimension implicitly encodes some semantic feature (e.g., "is it an animal", "size", "sentiment", etc.)

Encoding MethodDimensionsSemantic InformationTypical Application
One-HotVocabulary size (~50,000)NoneTraditional NLP
Word2Vec100โ€“300Word-level semanticsWord similarity, analogical reasoning
BERT Embedding768Contextual semanticsSentence understanding, QA
OpenAI text-embedding-31536โ€“3072Deep semanticsRAG, semantic search

------

2. Similarity Calculation: Method for "Close" Are Two Vectors2. Similarity Calculation: Method for "Close" Are Two Vectors

With vector representations in hand, the next question is naturally: how do we measure how similar two vectors are? It's like measuring how close two cities are on a map โ€” you can measure the straight-line distance, or you can check whether they point in the same direction.With vector representations in hand, the next question is naturally: how do we measure how similar two vectors are? It's like measuring how close two cities are on a map โ€” you can measure the straight-line distance, or you can check whether they point in the same direction.

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

- Cosine Similarity: Measures whether two vectors point in the same direction. Range: [-1, 1]. 1 means identical direction, 0 means orthogonal (unrelated), -1 means completely opposite. The go-to choice for text semantic comparison because it's unaffected by vector magnitude. - Euclidean Distance: Measures the straight-line distance between the endpoints of two vectors. Range: [0, โˆž). 0 means they coincide exactly; the larger the value, the less similar. Suitable for scenarios where "absolute magnitude" matters.- Cosine Similarity: Measures whether two vectors point in the same direction. Range: [-1, 1]. 1 means identical direction, 0 means orthogonal (unrelated), -1 means completely opposite. The go-to choice for text semantic comparison because it's unaffected by vector magnitude. - Euclidean Distance: Measures the straight-line distance between the endpoints of two vectors. Range: [0, โˆž). 0 means they coincide exactly; the larger the value, the less similar. Suitable for scenarios where "absolute magnitude" matters.

MetricFormula IntuitionRangeUse Cases
Cosine SimilarityCompares direction, ignores magnitude[-1, 1]Text semantic search, recommendation systems
Euclidean DistanceStraight-line distance between endpoints[0, โˆž)Image features, cluster analysis
Dot ProductDirection ร— Magnitude(-โˆž, +โˆž)Fast computation with normalized vectors
Manhattan DistanceDistance traveled along coordinate axes[0, โˆž)High-dimensional sparse vectors

------

3. Vector Indexing: Approach to searching Millions of Vectors in Milliseconds3. Vector Indexing: Approach to searching Millions of Vectors in Milliseconds

Suppose you have 1 million documents, each converted into a 1536-dimensional vector. A user asks a question, and you need to find the 10 most similar ones. The most direct approach is to compute similarity one by one โ€” but that means performing 1 million 1536-dimensional vector operations, which is far too slow.Suppose you have 1 million documents, each converted into a 1536-dimensional vector. A user asks a question, and you need to find the 10 most similar ones. The most direct approach is to compute similarity one by one โ€” but that means performing 1 million 1536-dimensional vector operations, which is far too slow.

This is the problem that vector indexing solves: trade space for time by building index structures through preprocessing, reducing retrieval speed from O(n) to approximately O(log n).This is the problem that vector indexing solves: trade space for time by building index structures through preprocessing, reducing retrieval speed from O(n) to approximately O(log n).

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

- Brute-force (Flat): Compare one by one. 100% accurate but slow. Suitable for small datasets (< 100k). - IVF (Inverted File Index): Partition the vector space into regions (clusters) first, then search only the nearest few regions during querying. Like organizing a library by subject โ€” you only go to the relevant sections when looking for a book. - HNSW (Hierarchical Navigable Small World): Build a multi-layer graph structure, navigating from coarse to fine granularity layer by layer. Like first looking at a world map to locate a country, then a provincial map, and finally a street map. - PQ (Product Quantization): Compress high-dimensional vectors into short codes, sacrificing a small amount of precision for significant memory savings. Suitable for ultra-large-scale datasets.- Brute-force (Flat): Compare one by one. 100% accurate but slow. Suitable for small datasets (< 100k). - IVF (Inverted File Index): Partition the vector space into regions (clusters) first, then search only the nearest few regions during querying. Like organizing a library by subject โ€” you only go to the relevant sections when looking for a book. - HNSW (Hierarchical Navigable Small World): Build a multi-layer graph structure, navigating from coarse to fine granularity layer by layer. Like first looking at a world map to locate a country, then a provincial map, and finally a street map. - PQ (Product Quantization): Compress high-dimensional vectors into short codes, sacrificing a small amount of precision for significant memory savings. Suitable for ultra-large-scale datasets.

Index TypeBuild SpeedQuery SpeedRecallMemory UsageSuitable Scale
Flat (Brute-force)No build neededSlow100%High< 100k
IVFModerateFast95%+Medium100kโ€“10M
HNSWSlowVery fast99%+High100kโ€“10M
PQModerateFast90%+Very low> 10M
IVF-PQModerateFast92%+Low> 100M

------

4. Vector Databases: Storage Engines Purpose-Built for Vectors4. Vector Databases: Storage Engines Purpose-Built for Vectors

With vectors and indexing algorithms in hand, you need a place to store and manage them. Traditional databases (MySQL, PostgreSQL) excel at structured data but struggle with high-dimensional vector similarity search. Vector databases are designed specifically for this scenario.With vectors and indexing algorithms in hand, you need a place to store and manage them. Traditional databases (MySQL, PostgreSQL) excel at structured data but struggle with high-dimensional vector similarity search. Vector databases are designed specifically for this scenario.

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

1. Efficient storage: Storage formats optimized for high-dimensional floating-point vectors 2. ANN retrieval: Built-in support for multiple approximate nearest neighbor indexing algorithms (HNSW, IVF, etc.) 3. Metadata filtering: Support for filtering by tags, timestamps, and other criteria alongside vector search 4. Real-time updates: Support for dynamic insertion, deletion, and modification of vectors without rebuilding the entire index 5. Horizontal scaling: Distributed architecture supporting billion-scale vector collections1. Efficient storage: Storage formats optimized for high-dimensional floating-point vectors 2. ANN retrieval: Built-in support for multiple approximate nearest neighbor indexing algorithms (HNSW, IVF, etc.) 3. Metadata filtering: Support for filtering by tags, timestamps, and other criteria alongside vector search 4. Real-time updates: Support for dynamic insertion, deletion, and modification of vectors without rebuilding the entire index 5. Horizontal scaling: Distributed architecture supporting billion-scale vector collections

DatabaseTypeCharacteristicsUse Cases
PineconeFully-managed cloud serviceZero ops, works out of the boxRapid prototyping, small-to-medium production
MilvusOpen-source distributedHigh performance, scalableLarge-scale production environments
ChromaOpen-source lightweightEmbeddable, clean APILocal development, small projects
WeaviateOpen-source cloud-nativeBuilt-in vectorization, GraphQLScenarios requiring automatic vectorization
QdrantOpen-source high-performanceRust implementation, strong filteringScenarios requiring complex filtering
pgvectorPostgreSQL extensionReuses existing PG infrastructureTeams already using PostgreSQL

------

5. End-to-End Pipeline: The Complete Flow from Text to Retrieval5. End-to-End Pipeline: The Complete Flow from Text to Retrieval

Now that we understand each component, let's wire them together and see how a complete vector retrieval system works.Now that we understand each component, let's wire them together and see how a complete vector retrieval system works.

The entire flow splits into two tracks: offline ingestion (turning documents into vectors and storing them) and online querying (turning questions into vectors and searching).The entire flow splits into two tracks: offline ingestion (turning documents into vectors and storing them) and online querying (turning questions into vectors and searching).

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

1. Document loading: Read raw text from various sources (PDFs, web pages, databases) 2. Text preprocessing: Clean, denoise, and normalize (strip HTML tags, special characters, etc.) 3. Text chunking: Split long text into appropriately sized chunks according to a strategy (200โ€“500 tokens) 4. Vectorization: Call an embedding model (e.g., OpenAI text-embedding-3-small) to convert each chunk into a vector 5. Store in vector database: Write the vectors along with the original text and metadata into the database1. Document loading: Read raw text from various sources (PDFs, web pages, databases) 2. Text preprocessing: Clean, denoise, and normalize (strip HTML tags, special characters, etc.) 3. Text chunking: Split long text into appropriately sized chunks according to a strategy (200โ€“500 tokens) 4. Vectorization: Call an embedding model (e.g., OpenAI text-embedding-3-small) to convert each chunk into a vector 5. Store in vector database: Write the vectors along with the original text and metadata into the database

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

1. Receive query: User inputs a natural language question 2. Query vectorization: Convert the question into a vector using the same embedding model 3. Similarity retrieval: Search the vector database for the Top-K most similar document chunks 4. Post-processing: Rerank, deduplicate, filter by metadata 5. Return results: Return the most relevant document chunks to the caller (or pass them to an LLM to generate an answer)1. Receive query: User inputs a natural language question 2. Query vectorization: Convert the question into a vector using the same embedding model 3. Similarity retrieval: Search the vector database for the Top-K most similar document chunks 4. Post-processing: Rerank, deduplicate, filter by metadata 5. Return results: Return the most relevant document chunks to the caller (or pass them to an LLM to generate an answer)

StageKey DecisionRecommended Approach
Embedding ModelAccuracy vs. cost vs. speedOpenAI text-embedding-3-small (best value)
Chunking StrategyGranularity vs. semantic integrityRecursive chunking, 200โ€“500 tokens
Vector DatabaseScale vs. operational costChroma for small projects, Pinecone/Milvus for production
Similarity MetricSemantic vs. exactCosine similarity (go-to for text scenarios)
Top-K ValueRecall vs. noiseRetrieve 20 first, rerank and take Top 5

------

SummarySummary

Embedding and vector retrieval form the bridge connecting "human language" and "machine understanding." They are also the foundational infrastructure for AI applications such as RAG, semantic search, and recommendation systems.Embedding and vector retrieval form the bridge connecting "human language" and "machine understanding." They are also the foundational infrastructure for AI applications such as RAG, semantic search, and recommendation systems.

Key takeaways from this chapter:Key takeaways from this chapter:

  1. The essence of Embedding: Mapping text into a high-dimensional vector space, turning semantic similarity into spatial distanceThe essence of Embedding: Mapping text into a high-dimensional vector space, turning semantic similarity into spatial distance
  2. Similarity metrics: Cosine similarity focuses on direction (ideal for text), Euclidean distance focuses on absolute distanceSimilarity metrics: Cosine similarity focuses on direction (ideal for text), Euclidean distance focuses on absolute distance
  3. Indexing is the performance key: HNSW and IVF reduce retrieval across millions of vectors to millisecondsIndexing is the performance key: HNSW and IVF reduce retrieval across millions of vectors to milliseconds
  4. Vector database selection: Chroma/pgvector for small projects, Pinecone/Milvus for productionVector database selection: Chroma/pgvector for small projects, Pinecone/Milvus for production
  5. End-to-end thinking: From document loading to final retrieval, every stage's choices affect the final outcomeEnd-to-end thinking: From document loading to final retrieval, every stage's choices affect the final outcome
  6. Further ReadingFurther Reading

    • [OpenAI Embeddings Documentation](https://platform.openai.com/docs/guides/embeddings) - Official embedding model usage guide[OpenAI Embeddings Documentation](https://platform.openai.com/docs/guides/embeddings) - Official embedding model usage guide
    • [Pinecone Learning Center](https://www.pinecone.io/learn/) - Systematic tutorials on vector databases and retrieval[Pinecone Learning Center](https://www.pinecone.io/learn/) - Systematic tutorials on vector databases and retrieval
    • [FAISS Wiki](https://github.com/facebookresearch/faiss/wiki) - Documentation for Facebook's open-source vector retrieval library[FAISS Wiki](https://github.com/facebookresearch/faiss/wiki) - Documentation for Facebook's open-source vector retrieval library
    • [Word2Vec Original Paper](https://arxiv.org/abs/1301.3781) - The seminal work that launched the Embedding era[Word2Vec Original Paper](https://arxiv.org/abs/1301.3781) - The seminal work that launched the Embedding era
    • [MTEB Leaderboard](https://huggingface.co/spaces/mteb/leaderboard) - Embedding model performance comparison leaderboard[MTEB Leaderboard](https://huggingface.co/spaces/mteb/leaderboard) - Embedding model performance comparison leaderboard