AzureAISearchBM25Retriever
AzureAISearchBM25Retriever
Azure AI Search Document Store에서 쿼리와 일치하는 Documents를 가져오는 키워드 기반 Retriever예요.
파이프라인에서 가장 흔한 위치:
- RAG 파이프라인에서 PromptBuilder 앞
- 의미 검색(semantic search) 파이프라인의 마지막 컴포넌트
- 추출형 QA 파이프라인에서 TransformersExtractiveReader 앞
필수 init 변수: document_store — AzureAISearchDocumentStore 인스턴스
필수 run 변수: query — 문자열
출력 변수: documents — 쿼리와 일치하는 문서 목록
API reference: Azure AI Search
GitHub link: https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/azure_ai_search
Package name: azure-ai-search-haystack
출처: 문서
본문
Overview
AzureAISearchBM25Retriever는 AzureAISearchDocumentStore에서 쿼리와 일치하는 문서를 가져오도록 설계된 키워드 기반 Retriever예요. BM25 알고리즘을 사용해 쿼리와 문서 사이의 가중치가 있는 단어 중복을 계산해 유사도를 결정합니다. Retriever는 텍스트 쿼리를 받지만, 불리언 연산자로 결합한 용어 조합도 제공할 수 있어요. 유효한 쿼리의 예로는 "pool", "pool spa", "pool spa +airport" 같은 것이 있습니다.
query 외에도 AzureAISearchBM25Retriever는 top_k(가져올 최대 문서 수)와 검색 공간을 좁히는 filters 등 선택 파라미터를 받아요.
검색 인덱스에 의미 구성(semantic configuration)이 포함되어 있다면, 의미 순위 매김을 활성화해 Retriever의 결과에 적용할 수 있어요. 자세한 내용은 Azure AI 문서를 참고하세요.
BM25와 벡터 검색의 조합을 원한다면 AzureAISearchHybridRetriever를 쓰세요. 이것은 벡터 검색과 BM25 검색을 모두 사용해 문서와 쿼리를 매칭합니다.
Usage
Installation
이 통합을 쓰려면 배포된 Azure AI Search 서비스가 있는 활성 Azure 구독이 있어야 해요.
Haystack에서 Azure AI 검색을 쓰려면 패키지를 설치하세요:
pip install azure-ai-search-haystack
On its own
이 Retriever는 실행하려면 AzureAISearchDocumentStore와 인덱싱된 문서가 필요해요.
from haystack import Document
from haystack_integrations.components.retrievers.azure_ai_search import (
AzureAISearchBM25Retriever,
)
from haystack_integrations.document_stores.azure_ai_search import (
AzureAISearchDocumentStore,
)
document_store = AzureAISearchDocumentStore(index_name="haystack_docs")
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.",
),
]
document_store.write_documents(documents=documents)
retriever = AzureAISearchBM25Retriever(document_store=document_store)
retriever.run(query="How many languages are spoken around the world today?")
In a RAG pipeline
아래 예시는 AzureAISearchBM25Retriever를 RAG 파이프라인에서 사용하는 방법을 보여줘요. OPENAI_API_KEY를 환경 변수로 설정하고 다음 코드를 실행하세요:
from haystack_integrations.components.retrievers.azure_ai_search import (
AzureAISearchBM25Retriever,
)
from haystack_integrations.document_stores.azure_ai_search import (
AzureAISearchDocumentStore,
)
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
import os
api_key = os.environ["OPENAI_API_KEY"]
# Create a RAG query pipeline
prompt_template = [
ChatMessage.from_user(
"""
Given these documents, answer the question.\nDocuments:
{% for doc in documents %}
{{ doc.content }}
{% endfor %}
\nQuestion: {{question}}
\nAnswer:
""",
),
]
document_store = AzureAISearchDocumentStore(index_name="haystack-docs")
# 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.",
),
]
# policy param is optional, as AzureAISearchDocumentStore has a default policy of DuplicatePolicy.OVERWRITE
document_store.write_documents(documents=documents, policy=DuplicatePolicy.OVERWRITE)
retriever = AzureAISearchBM25Retriever(document_store=document_store)
rag_pipeline = Pipeline()
rag_pipeline.add_component(name="retriever", instance=retriever)
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")
# ... (파이프라인 연결 계속, 원문 참조)
더 알아보기 (Learn more)
- AzureAISearchDocumentStore
- AzureAISearchHybridRetriever — 벡터+BM25 혼합 검색