FastEmbed와 Qdrant로 시맨틱 검색하기
FastEmbed와 Qdrant로 시맨틱 검색하기 (fastembed-fastembed-semantic-search)
FastEmbed는 Qdrant에 내장된 가벼운 임베딩 통합 기능이에요. 이 페이지에서는 Qdrant Client의 인메모리 모드와 FastEmbed를 조합해, 별도의 임베딩 서비스 없이 시맨틱 검색을 바로 실행하는 과정을 따라 해볼게요.
출처: Qdrant 공식문서
먼저 FastEmbed가 포함된 Qdrant Client를 설치합니다.
pip install "qdrant-client[fastembed]>=1.14.2"
클라이언트 초기화
Qdrant Client는 간단한 인메모리 모드를 제공해서 로컬에서 바로 시맨틱 검색을 시도해 볼 수 있어요.
from qdrant_client import QdrantClient , models client = QdrantClient ( ":memory:" ) # Qdrant is running from RAM.
데이터 추가
이제 샘플 문서 두 개와 각각의 메타데이터, 포인트 id를 추가할 수 있어요.
docs = [ "Qdrant has a LangChain integration for chatbots." , "Qdrant has a LlamaIndex integration for agents." , ] metadata = [ { "source" : "langchain-docs" }, { "source" : "llamaindex-docs" }, ] ids = [ 42 , 2 ]
컬렉션 생성
Qdrant는 벡터와 관련 메타데이터를 컬렉션에 저장합니다. 컬렉션은 생성 시 벡터 파라미터를 설정해야 해요. 이 튜토리얼에서는 BAAI/bge-small-en으로 임베딩을 계산할 거예요.
model_name = "BAAI/bge-small-en" client . create_collection ( collection_name = "test_collection" , vectors_config = models . VectorParams ( size = client . get_embedding_size ( model_name ), distance = models . Distance . COSINE ), # size and distance are model dependent )
컬렉션에 문서 업서트
Qdrant 클라이언트는 FastEmbed 통합을 통해 클라이언트 메서드 안에서 암묵적으로 추론할 수 있어요. 데이터를 models.Document(이미지라면 models.Image) 같은 모델로 감싸야 합니다.
metadata_with_docs = [ { "document" : doc , "source" : meta [ "source" ]} for doc , meta in zip ( docs , metadata ) ] client . upload_collection ( collection_name = "test_collection" , vectors = [ models . Document ( text = doc , model = model_name ) for doc in docs ], payload = metadata_with_docs , ids = ids , )
벡터 검색 실행
여기서는 시맨틱하게 관련된 결과를 얻을 수 있는 테스트 질문을 던져볼게요.
search_result = client . query_points ( collection_name = "test_collection" , query = models . Document ( text = "Which integration is best for agents?" , model = model_name ) ) . points print ( search_result )
시맨틱 검색 엔진은 관련성 순서대로 가장 유사한 결과를 가져와요. 이 경우 두 번째 문장(LlamaIndex 관련)이 더 관련성이 높습니다.
[ ScoredPoint ( id = 2 , score = 0.87491801319731 , payload = { "document" : "Qdrant has a LlamaIndex integration for agents." , "source" : "llamaindex-docs" , }, ... ), ScoredPoint ( id = 42 , score = 0.8351846627714035 , payload = { "document" : "Qdrant has a LangChain integration for chatbots." , "source" : "langchain-docs" , }, ... ), ]