Embedding
Embedding
A representation that converts text, images, and so on into high-dimensional vectors. Quantifies semantic similarity. A core element of RAG.
Embedding
A representation that converts text, images, and so on into high-dimensional vectors. Quantifies semantic similarity. A core element of RAG.
Embedding is a technique that converts data such as text, images, and audio into numerical vectors. In this vector space, semantically similar items are positioned close to one another. For example, the embedding vectors of "king" and "queen" are placed closer together than those of "apple" and "banana." This allows computers to process meaning and relationships mathematically.
The dimensionality of an embedding is directly tied to its expressive power. OpenAI's text-embedding-3-large uses 3,072 dimensions, and Cohere's embed-english-v3.0 uses 1,024 dimensions. Higher dimensionality can capture richer meaning, but storage space and computational cost increase. In practice, it is important to choose a model with an appropriate dimensionality for the use case.
Embedding is a core component of RAG (Retrieval-Augmented Generation). Documents are split into chunks, converted into embeddings, and stored in a vector database; the user's question is also converted into an embedding to search for similar documents. Providing the relevant documents retrieved this way to an LLM reduces hallucinations and enables answers that reflect up-to-date information.
It has evolved from word-level embeddings (Word2Vec, GloVe) to sentence embeddings (Sentence-BERT) and multimodal embeddings (CLIP). Recently, unified embedding models have also emerged that embed various formats, such as text, images, and code, into the same vector space with a single model.
An example of generating embeddings and calculating similarity using OpenAI and open-source models.
import numpy as np
from openai import OpenAI
from sentence_transformers import SentenceTransformer
# OpenAI 임베딩 생성
client = OpenAI()
def get_openai_embedding(text, model="text-embedding-3-small"):
response = client.embeddings.create(input=text, model=model)
return response.data[0].embedding
# 텍스트 임베딩
texts = [
"인공지능은 미래 기술입니다",
"AI가 세상을 바꾸고 있습니다",
"오늘 날씨가 좋습니다"
]
embeddings = [get_openai_embedding(t) for t in texts]
# 코사인 유사도 계산
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
print(f"문장 1-2 유사도: {cosine_similarity(embeddings[0], embeddings[1]):.4f}") # 높음
print(f"문장 1-3 유사도: {cosine_similarity(embeddings[0], embeddings[2]):.4f}") # 낮음
# 오픈소스 모델 (Sentence-Transformers)
model = SentenceTransformer('all-MiniLM-L6-v2') # 384차원, 빠름
embeddings_local = model.encode(texts)
print(f"로컬 모델 차원: {embeddings_local.shape}")
# 한국어 특화 모델
ko_model = SentenceTransformer('jhgan/ko-sroberta-multitask')
ko_embeddings = ko_model.encode(["안녕하세요", "반갑습니다"])
print(f"한국어 유사도: {cosine_similarity(ko_embeddings[0], ko_embeddings[1]):.4f}")
| Model | Dimensions | Price (1M tokens) | Features |
|---|---|---|---|
| text-embedding-3-large | 3,072 | $0.13 | Top performance, adjustable dimensions |
| text-embedding-3-small | 1,536 | $0.02 | Cost-efficient, general-purpose |
| Cohere embed-v3 | 1,024 | $0.10 | Multilingual, search-optimized |
| all-MiniLM-L6-v2 | 384 | Free (local) | Open-source, fast |
| bge-large-en-v1.5 | 1,024 | Free (local) | Best open-source performance |
"If we embed 100,000 documents and store them in Pinecone, we can search in real time for documents similar to the question and provide them to the LLM as context."
"If we switch to text-embedding-3-small, costs drop by 85%, and the performance difference on the MTEB benchmark is about 3%."
"Searching with embeddings alone is weak at keyword matching. Let's apply hybrid search that combines BM25 and embedding scores."
If too small, context is lost; if too large, noise increases. Typically 200-500 tokens is appropriate.
If you use different embedding models for storage and search, similarity calculations become meaningless. Always use the same model.
Embedding Korean with an English-only model causes quality to drop sharply. Choose a multilingual or Korean-specialized model.
Vector storage/search costs can be higher than embedding generation. Consider dimensionality reduction or quantization.