FastembedSparseTextEmbedder
FastembedSparseTextEmbedder
간단한 문자열(예: 쿼리)을 희소 벡터로 임베딩하기 위해 사용하는 컴포넌트예요.
출처: 문서
본문
- 파이프라인에서의 일반적인 위치: 쿼리/RAG 파이프라인에서 희소 임베딩 리트리버 앞에 사용해요.
- 필수 실행 변수:
text(문자열) - 출력 변수:
sparse_embedding(SparseEmbedding객체)
문서 리스트를 임베딩할 때는 계산된 희소 임베딩으로 문서를 보강하는 FastembedSparseDocumentEmbedder를 사용하세요.
개요 (Overview)
FastembedSparseTextEmbedder는 FastEmbed가 지원하는 희소 임베딩 모델을 사용해 문자열을 희소 벡터로 변환해요.
희소 임베딩 검색을 수행할 때 먼저 이 컴포넌트로 쿼리를 희소 벡터로 변환하세요. 그런 다음 희소 임베딩 리트리버가 이 벡터를 사용해 유사하거나 관련 있는 문서를 검색해요.
호환 모델 (Compatible Models)
지원되는 모델은 FastEmbed 문서에서 찾을 수 있어요.
현재 지원되는 모델은 SPLADE를 기반으로 해요. SPLADE는 텍스트의 희소 표현을 만드는 기법으로, 임베딩의 각 0이 아닌 값이 BERT WordPiece 어휘에서 용어의 중요도 가중치를 나타내요. 자세한 내용은 희소 임베딩 기반 리트리버를 설명하는 문서를 참고하세요.
설치 (Installation)
이 통합을 Haystack에서 사용하려면 패키지를 설치하세요.
pip install fastembed-haystack
파라미터 (Parameters)
모델이 저장될 경로를 캐시 디렉터리로 설정할 수 있어요. 단일 onnxruntime 세션이 사용할 스레드 수를 설정할 수도 있어요.
cache_dir = "/your_cacheDirectory"
embedder = FastembedSparseTextEmbedder(
model="prithivida/Splade_PP_en_v1",
cache_dir=cache_dir,
threads=2,
)
데이터 병렬 인코딩을 사용하려면 parallel 파라미터를 설정할 수 있어요.
parallel> 1이면 데이터 병렬 인코딩을 사용해요. 대규모 데이터셋의 오프라인 인코딩에 권장돼요.parallel이 0이면 사용 가능한 모든 코어를 사용해요.- None이면 데이터 병렬 처리를 사용하지 않고 기본
onnxruntime스레딩을 사용해요.
tip
같은 모델 기반의 Sparse Text Embedder와 Sparse Document Embedder를 만들면, Haystack은 뒤에서 공유 리소스를 사용해 리소스를 아껴요.
사용법 (Usage)
단독 사용 (On its own)
from haystack_integrations.components.embedders.fastembed import (
FastembedSparseTextEmbedder,
)
text = """It clearly says online this will work on a Mac OS system.
The disk comes and it does not, only Windows.
Do Not order this if you have a Mac!!"""
text_embedder = FastembedSparseTextEmbedder(model="prithivida/Splade_PP_en_v1")
sparse_embedding = text_embedder.run(text)["sparse_embedding"]
파이프라인에서 사용 (In a pipeline)
현재 희소 임베딩 검색은 QdrantDocumentStore만 지원해요. 먼저 패키지를 설치하세요.
pip install qdrant-haystack
그다음 이 파이프라인을 사용해 보세요.
from haystack import Document, Pipeline
from haystack_integrations.document_stores.qdrant import QdrantDocumentStore
from haystack_integrations.components.retrievers.qdrant import (
QdrantSparseEmbeddingRetriever,
)
from haystack_integrations.components.embedders.fastembed import (
FastembedSparseTextEmbedder,
FastembedSparseDocumentEmbedder,
FastembedTextEmbedder,
)
document_store = QdrantDocumentStore(
":memory:",
recreate_index=True,
use_sparse_embeddings=True,
)
documents = [
Document(content="My name is Wolfgang and I live in Berlin"),
Document(content="I saw a black horse running"),
Document(content="Germany has many big cities"),
Document(content="fastembed is supported by and maintained by Qdrant."),
]
sparse_document_embedder = FastembedSparseDocumentEmbedder(
model="prithivida/Splade_PP_en_v1",
)
documents_with_sparse_embeddings = sparse_document_embedder.run(documents)["documents"]
document_store.write_documents(documents_with_sparse_embeddings)
query_pipeline = Pipeline()
query_pipeline.add_component("sparse_text_embedder", FastembedSparseTextEmbedder())
query_pipeline.add_component(
"sparse_retriever",
QdrantSparseEmbeddingRetriever(document_store=document_store),
)
query_pipeline.connect(
"sparse_text_embedder.sparse_embedding",
"sparse_retriever.query_sparse_embedding",
)
query = "Who supports fastembed?"
result = query_pipeline.run({"sparse_text_embedder": {"text": query}})
print(result["sparse_retriever"]["documents"][0]) # noqa: T201
# Document(id=...,
# content: 'fastembed is supported by and maintained by Qdrant.',
# score: 0.561..)
추가 자료 (Additional References)
🧑🍳 Cookbook: Sparse Embedding Retrieval with Qdrant and FastEmbed