FastEmbed 퀵스타트

FastEmbed 퀵스타트 (fastembed-fastembed-quickstart)

FastEmbed로 텍스트 임베딩을 만들어 보는 가장 짧은 길이에요. 큰 라이브러리 구성 없이, 몇 줄로 기본 모델을 불러와 문서 벡터를 뽑아내는 흐름을 그대로 따라 해볼게요.

먼저 fastembed를 설치하고, 데모용으로 쓰일 예제 데이터를 다루기 위해 List와 NumPy를 준비해요.

pip install fastembed
from typing import List
import numpy as np

기본 모델 불러오기 (Load default model)

이 예제에서는 기본 텍스트 임베딩 모델인 BAAI/bge-small-en-v1.5를 사용해요. 모델을 가져올 때는 TextEmbedding을 import 하면 돼요.

from fastembed import TextEmbedding

예제 데이터 추가하기 (Add sample data)

이제 두 개의 샘플 문서를 추가해요. 문서는 반드시 리스트 형태여야 하고, 각 문서는 문자열이어야 해요.

documents: List[str] = [
    "FastEmbed is lighter than Transformers & Sentence-Transformers.",
    "FastEmbed is supported by and maintained by Qdrant.",
]

모델을 다운로드하고 초기화해요. 과정이 잘 마무리됐는지 확인하는 메시지도 출력해볼게요.

embedding_model = TextEmbedding()
print("The model BAAI/bge-small-en-v1.5 is ready to use.")

임베딩 생성하기 (Generate embeddings)

두 문서 각각의 임베딩을 생성해요. embed는 제너레이터를 돌려주기 때문에 list()로 감싸서 실제 벡터 목록을 만들어요.

embeddings_generator = embedding_model.embed(documents)
embeddings_list = list(embeddings_generator)
len(embeddings_list[0])

기본 모델은 384차원의 벡터를 만들어요. 출력을 보면 각 문서가 numpy.ndarray 타입, (384,) 형태의 벡터로 변환된 걸 확인할 수 있어요.

Document: This is built to be faster and lighter than other embedding libraries e.g. Transformers, Sentence-Transformers, etc.
Vector of type: <class 'numpy.ndarray'> with shape: (384,)
Document: fastembed is supported by and maintained by Qdrant.
Vector of type: <class 'numpy.ndarray'> with shape: (384,)

임베딩 값을 직접 출력해 보면 숫자 배열이라 크게 의미는 없어 보이지만, 형태를 눈으로 확인하는 용도로는 좋아요.

print("Embeddings:\n", embeddings_list)
Embeddings:
 [[-0.11154681  0.00976555  0.00524559  0.01951888 -0.01934952  0.02943449
  -0.10519084 -0.00890122  0.01831438  0.01486796 -0.05642502  0.02561352
  -0.00120165  0.00637456  0.02633459  0.0089221   0.05313658  0.03955453
  -0.04400245 -0.02929407  0.04691846 -0.02515868  0.00778646 -0.05410657
...
  -0.00243012 -0.01820582  0.02938612  0.02108984 -0.02178085  0.02971899
  -0.00790564  0.03561783  0.0652488  -0.04371546 -0.05550042  0.02651665
  -0.01116153 -0.01682246 -0.05976734 -0.03143916  0.06522726  0.01801389
  -0.02611006  0.01627177 -0.0368538   0.03968835  0.027597    0.03305927]]

이제 문서가 벡터로 변환됐으니, 이 벡터를 Qdrant 컬렉션에 넣고 검색에 활용하는 다음 단계로 넘어가면 돼요.

출처: Qdrant 공식문서

더 알아보기 (Learn more)