WeaviateBM25Retriever
WeaviateBM25Retriever
쿼리와 일치하는 문서를 Weaviate Document Store에서 가져오는 키워드 기반 Retriever예요. BM25 알고리즘으로 문서와 쿼리 사이의 단어 중첩을 계산해 유사도를 판단해요.
파이프라인에서 가장 흔한 위치: 1. RAG 파이프라인의 PromptBuilder 앞 / 2. 의미 검색 파이프라인의 마지막 컴포넌트 / 3. 추출형 QA 파이프라인의 TransformersExtractiveReader 앞
필수 init 변수: document_store — WeaviateDocumentStore 인스턴스
필수 run 변수: query — 문자열
출력 변수: documents — 쿼리와 일치하는 문서 리스트
API 레퍼런스: Weaviate
GitHub 링크: https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/weaviate
패키지 이름: weaviate-haystack
출처: 문서
본문
개요 (Overview)
WeaviateBM25Retriever는 WeaviateDocumentStore에서 쿼리와 일치하는 Document를 가져오는 키워드 기반 Retriever예요. 문서와 쿼리 사이의 유사도를 BM25 알고리즘으로 판단하는데, 이 알고리즘은 두 문자열 사이의 가중치 있는 단어 중첩을 계산해요.
WeaviateBM25Retriever는 단어 중첩 기준으로 문자열을 매칭하기 때문에, 사람 이름이나 제품 이름, ID, 잘 정의된 오류 메시지 같은 정확한 일치를 찾는 데 자주 사용돼요. BM25 알고리즘은 매우 가볍고 단순해요. 영역 밖(도메인 외) 데이터에서 더 복잡한 임베딩 기반 접근으로 이걸 이기기 어려울 때도 있어요.
쿼리와 문서 사이의 의미적(semantic) 매칭을 원한다면, 임베딩 모델로 만든 벡터로 관련 정보를 검색하는 WeaviateEmbeddingRetriever를 사용하세요.
파라미터 (Parameters)
query 외에도 WeaviateBM25Retriever는 top_k(가져올 최대 Document 수)와 filters(검색 범위를 좁히는 데 쓰임) 같은 선택적 파라미터를 받아요.
사용법 (Usage)
설치 (Installation)
Haystack에서 Weaviate를 쓰려면 패키지를 설치해요:
pip install weaviate-haystack
단독으로 사용하기 (On its own)
이 Retriever는 동작하려면 WeaviateDocumentStore 인스턴스와 인덱싱된 Document가 필요해요.
from haystack_integrations.document_stores.weaviate.document_store import (
WeaviateDocumentStore,
)
from haystack_integrations.components.retrievers.weaviate import WeaviateBM25Retriever
document_store = WeaviateDocumentStore(url="http://localhost:8080")
retriever = WeaviateBM25Retriever(document_store=document_store)
retriever.run(query="How to make a pizza", top_k=3)
파이프라인 안에서 사용하기 (In a Pipeline)
from haystack_integrations.document_stores.weaviate.document_store import (
WeaviateDocumentStore,
)
from haystack_integrations.components.retrievers.weaviate import (
WeaviateBM25Retriever,
)
from haystack import Document
from haystack import Pipeline
from haystack.components.builders.answer_builder import AnswerBuilder
from haystack.components.builders import ChatPromptBuilder
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.document_stores.types import DuplicatePolicy
# Create a RAG query pipeline
prompt_template = [
ChatMessage.from_user(
"""
Given these documents, answer the question.
Documents:
{% for doc in documents %}
{{ doc.content }}
{% endfor %}
\nQuestion: {{question}}
\nAnswer:
""",
),
]
document_store = WeaviateDocumentStore(url="http://localhost:8080")
# Add Documents
documents = [
Document(content="There are over 7,000 languages spoken around the world today."),
Document(
content="Elephants have been observed to behave in a way that indicates a high level of self-awareness, such as recognizing themselves in mirrors.",
),
Document(
content="In certain parts of the world, like the Maldives, Puerto Rico, and San Diego, you can witness the phenomenon of bioluminescent waves.",
),
]
# DuplicatePolicy.SKIP param is optional, but useful to run the script multiple times without throwing errors
document_store.write_documents(documents=documents, policy=DuplicatePolicy.SKIP)
rag_pipeline = Pipeline()
rag_pipeline.add_component(
name="retriever",
instance=WeaviateBM25Retriever(document_store=document_store),
)
rag_pipeline.add_component(
instance=ChatPromptBuilder(template=prompt_template, required_variables="*"),
name="prompt_builder",
)
rag_pipeline.add_component(instance=OpenAIChatGenerator(), name="llm")
rag_pipeline.add_component(instance=AnswerBuilder(), name="answer_builder")
rag_pipeline.connect("retriever", "prompt_builder.documents")
rag_pipeline.connect("prompt_builder.prompt", "llm.messages")
rag_pipeline.connect("llm.replies", "answer_builder.replies")
rag_pipeline.connect("retriever", "answer_builder.documents")
question = "How many languages are spoken around the world today?"
result = rag_pipeline.run(
{
"retriever": {"query": question},
"prompt_builder": {"question": question},
"answer_builder": {"query": question},
},
)
print(result["answer_builder"]["answers"][0])