Embedding là biểu diễn vector số của dữ liệu (text, image, audio) sao cho khoảng cách trong không gian vector phản ánh độ tương đồng ngữ nghĩa. Ví dụ vector của "dog" và "puppy" rất gần nhau; "dog" và "bicycle" xa nhau.
Cách tạo text embedding:
1. Tokenize text thành token IDs.
2. Đưa qua encoder model (thường là Transformer encoder như BERT, hoặc LLM dùng làm encoder).
3. Pool hidden states của các token thành 1 vector duy nhất — các cách: CLS token (BERT), mean pooling (E5, BGE), last-token pooling (GPT-like).
4. (tuỳ chọn) Normalize L2 để cosine similarity = dot product.
Output: vector cố định kích thước (thường 384, 768, 1024, 1536, 3072 dim).
Training: model học embedding bằng contrastive learning — cho pair (anchor, positive) gần nhau và (anchor, negative) xa nhau trong không gian. Dataset: MS MARCO, NLI, tự sinh từ LLM. Loss phổ biến: InfoNCE / triplet loss.
Thuộc tính hữu ích:
- Cosine similarity giữa 2 vector đo độ liên quan ngữ nghĩa.
- Phép toán vector: king - man + woman ≈ queen (word2vec).
- Embedding đa ngôn ngữ (E5-multilingual, BGE-M3) — câu tiếng Anh và tiếng Việt cùng nghĩa sẽ gần nhau.
Model phổ biến 2025:
- OpenAI text-embedding-3-small (1536d, rẻ) / -large (3072d, chất lượng cao, hỗ trợ Matryoshka — cắt chiều).
- Cohere embed-v3 (multilingual, 1024d).
- BGE-M3 / BGE-large-en-v1.5 (open source, top MTEB).
- Voyage-3 (chuyên domain-specific).
An embedding is a numeric vector representation of data (text, image, audio) where distance in vector space reflects semantic similarity. E.g. vectors for "dog" and "puppy" are close; "dog" and "bicycle" are far.
How text embeddings are produced:
1. Tokenize text into token IDs.
2. Pass through an encoder model (usually a Transformer encoder like BERT, or an LLM repurposed as encoder).
3. Pool token hidden states into a single vector — options: CLS token (BERT), mean pooling (E5, BGE), last-token pooling (GPT-like).
4. (optional) L2 normalize so cosine similarity = dot product.
Output: fixed-dimension vector (typically 384, 768, 1024, 1536, 3072).
Training: models learn embeddings via contrastive learning — push (anchor, positive) pairs close and (anchor, negative) pairs apart. Datasets: MS MARCO, NLI, LLM-synthesized. Common losses: InfoNCE / triplet loss.
Useful properties:
- Cosine similarity between two vectors measures semantic relevance.
- Vector arithmetic: king - man + woman ≈ queen (word2vec).
- Multilingual embeddings (E5-multilingual, BGE-M3) — an English sentence and its Vietnamese counterpart are close.
Popular models in 2025:
- OpenAI text-embedding-3-small (1536d, cheap) / -large (3072d, high quality, supports Matryoshka — truncatable dims).
- Cohere embed-v3 (multilingual, 1024d).
- BGE-M3 / BGE-large-en-v1.5 (open source, top MTEB).
- Voyage-3 (domain-specific).