Embedding
Embedding
Converting data into high-dimensional vectors. Represents semantic similarity. Central to RAG and search.
Embedding
Converting data into high-dimensional vectors. Represents semantic similarity. Central to RAG and search.
Embedding is a technology that converts unstructured data such as text, images, and audio into arrays of numbers in a high-dimensional vector space. Semantic relationships are expressed through vector operations, as in "king - man + woman = queen", enabling computers to understand and compare meaning.
Word2Vec (2013) was the beginning of word embeddings, and BERT (2018) advanced them into embeddings that understand context. Today, commercial embedding APIs such as OpenAI text-embedding-3 and Cohere embed-v3, as well as open-source tools such as sentence-transformers, are widely used.
The core of embedding is that data with similar meanings are located close to each other in the vector space. When the distance between two vectors is measured with cosine similarity, "puppy" and "dog" score 0.95 or higher, while "puppy" and "car" score 0.2 or lower. The dimensionality is usually between 384 and 3072.
It is a foundational technology for nearly all AI applications, including RAG (Retrieval-Augmented Generation), semantic search, recommendation systems, and duplicate detection. In particular, when combined with vector DBs (Pinecone, Weaviate, Qdrant), it can find similar documents among hundreds of millions of documents in milliseconds.
# OpenAI 임베딩 API 사용
from openai import OpenAI
import numpy as np
client = OpenAI()
def get_embedding(text: str, model="text-embedding-3-small") -> list[float]:
"""텍스트를 벡터로 변환 (1536 차원)"""
response = client.embeddings.create(input=text, model=model)
return response.data[0].embedding
def cosine_similarity(a: list, b: list) -> float:
"""두 벡터의 코사인 유사도 계산"""
a, b = np.array(a), np.array(b)
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
# 예시: 의미적 유사성 측정
texts = ["인공지능이 세상을 바꾸고 있다", "AI가 혁신을 이끈다", "오늘 날씨가 좋다"]
embeddings = [get_embedding(t) for t in texts]
print(f"문장1-문장2 유사도: {cosine_similarity(embeddings[0], embeddings[1]):.3f}") # ~0.85
print(f"문장1-문장3 유사도: {cosine_similarity(embeddings[0], embeddings[2]):.3f}") # ~0.15
# 비용: text-embedding-3-small = $0.02 / 1M tokens
# 비용: text-embedding-3-large = $0.13 / 1M tokens
This is a comparison of prices and performance of OpenAI embedding models as of January 2025.
| Model | Price (1M tokens) | Dimensions | MTEB Score |
|---|---|---|---|
| text-embedding-3-small | $0.02 | 1536 | 62.3% |
| text-embedding-3-large | $0.13 | 3072 | 64.6% |
| text-embedding-ada-002 | $0.10 | 1536 | 61.0% |
Recommended: Do not use ada-002, as it is 5 times more expensive and has lower performance. 3-small offers the best value for money, and using the Batch API gives a 50% discount. 3-large outperforms ada-002 even when reduced to 256 dimensions.
"Chunk the documents, embed them with text-embedding-3-small, and store them in Pinecone. Retrieve the top-k 5 chunks per query, and if you only put those with a cosine similarity of 0.7 or higher into the context, hallucinations are greatly reduced."
"Embeddings represent semantic similarity as vector distance. As in the Word2Vec example of 'king - man + woman = queen', semantic relationships are preserved through vector operations. In an actual project, we used OpenAI embeddings to build an automatic customer inquiry classification system and achieved 92% accuracy."
"If you need Korean-specific support, multilingual-e5-large is good, and for general purposes, text-embedding-3-small offers the best value for money. Looking at the MTEB benchmark, small performs better than ada-002 while being 5 times cheaper."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Embeddings from text-embedding-ada-002 and text-embedding-3-small are not compatible. If the model changes, all data must be re-embedded.
There is an 8192-token limit, and the meaning of long text becomes diluted. Chunk it into units of 500–1000 characters and embed each chunk individually.
Use the same embedding model consistently, and chunk documents by semantic units. Text preprocessing (noise removal, normalization) before embedding is also important.