FastembedRanker
FastembedRanker
FastEmbed가 지원하는 크로스 인코더 모델을 사용해 쿼리와의 유사도에 따라 문서 순위를 매기는 컴포넌트예요.
출처: 문서
본문
- 파이프라인에서의 일반적인 위치: 쿼리 파이프라인에서 Retriever처럼 문서 리스트를 반환하는 컴포넌트 뒤에 사용해요.
- 필수 실행 변수:
documents(문서 리스트),query(쿼리 문자열) - 출력 변수:
documents(문서 리스트)
개요 (Overview)
FastembedRanker는 문서가 쿼리와 얼마나 유사한지에 따라 순위를 매겨요. FastEmbed가 지원하는 크로스 인코더 모델을 사용하죠. ONNX Runtime을 기반으로 하므로 FastEmbed는 표준 CPU 머신에서도 빠른 경험을 제공해요.
FastembedRanker는 RAG(검색 증강 생성) 파이프라인이나 문서 검색 파이프라인 같은 쿼리 파이프라인에서 검색된 문서가 관련성 순서로 정렬되도록 하는 데 가장 유용해요. InMemoryEmbeddingRetriever 같은 Retriever 뒤에 사용해 검색 결과를 개선할 수 있어요. FastembedRanker를 Retriever와 함께 사용할 때는 Retriever의 top_k를 작은 수로 설정하는 것을 고려하세요. 그러면 Ranker가 처리할 문서가 줄어들어 파이프라인이 더 빨라질 수 있어요.
기본적으로 이 컴포넌트는 Xenova/ms-marco-MiniLM-L-6-v2 모델을 사용하지만, Ranker 초기화 시 model_name 파라미터를 조정해 다른 모델로 바꿀 수 있어요. 다양한 초기화 설정에 대한 자세한 내용은 API reference 페이지를 참고하세요.
호환 모델 (Compatible Models)
호환되는 모델은 FastEmbed 문서에서 찾을 수 있어요.
설치 (Installation)
이 통합을 Haystack에서 사용하려면 패키지를 설치하세요.
pip install fastembed-haystack
파라미터 (Parameters)
모델이 저장될 경로를 캐시 디렉터리로 설정할 수 있어요. 단일 onnxruntime 세션이 사용할 스레드 수를 설정할 수도 있어요.
cache_dir = "/your_cacheDirectory"
ranker = FastembedRanker(
model_name="Xenova/ms-marco-MiniLM-L-6-v2",
cache_dir=cache_dir,
threads=2,
)
데이터 병렬 인코딩을 사용하려면 parallel과 batch_size 파라미터를 설정할 수 있어요.
parallel> 1이면 데이터 병렬 인코딩을 사용해요. 대규모 데이터셋의 오프라인 인코딩에 권장돼요.parallel이 0이면 사용 가능한 모든 코어를 사용해요.- None이면 데이터 병렬 처리를 사용하지 않고 기본
onnxruntime스레딩을 사용해요.
사용법 (Usage)
단독 사용 (On its own)
FastembedRanker로 두 개의 간단한 문서 순위를 매기는 예시예요. Ranker를 실행하려면 query를 전달하고 documents를 제공하며 top_k 파라미터로 반환할 문서 수를 설정하세요.
from haystack import Document
from haystack_integrations.components.rankers.fastembed import FastembedRanker
docs = [Document(content="Paris"), Document(content="Berlin")]
ranker = FastembedRanker()
ranker.run(query="City in France", documents=docs, top_k=1)
파이프라인에서 사용 (In a pipeline)
InMemoryBM25Retriever로 키워드 검색을 사용해 InMemoryDocumentStore에서 문서를 검색하는 파이프라인 예시예요. 그다음 FastembedRanker로 쿼리와의 유사도에 따라 검색된 문서 순위를 매겨요. 파이프라인은 Ranker의 기본 설정을 사용해요.
from haystack import Document, Pipeline
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack_integrations.components.rankers.fastembed import FastembedRanker
docs = [
Document(content="Paris is in France"),
Document(content="Berlin is in Germany"),
Document(content="Lyon is in France"),
]
document_store = InMemoryDocumentStore()
document_store.write_documents(docs)
retriever = InMemoryBM25Retriever(document_store=document_store)
ranker = FastembedRanker()
document_ranker_pipeline = Pipeline()
document_ranker_pipeline.add_component(instance=retriever, name="retriever")
document_ranker_pipeline.add_component(instance=ranker, name="ranker")
document_ranker_pipeline.connect("retriever.documents", "ranker.documents")
query = "Cities in France"
res = document_ranker_pipeline.run(
data={
"retriever": {"query": query, "top_k": 3},
"ranker": {"query": query, "top_k": 2},
},
)