WeaviateBM25Retriever
这是一个基于关键字的 Retriever,用于从 Weaviate 文档存储中检索与查询匹配的文档。
| pipeline 中的最常见位置 | 1. 在 RAG 管道中的 PromptBuilder 之前 2. 语义搜索管道中的最后一个组件 3. 在提取式 QA 管道中的 ExtractiveReader 之前 |
| 必需的初始化变量 | "document_store": WeaviateDocumentStore 的一个实例 |
| 强制运行变量 | “query”: 一个字符串 |
| 输出变量 | "documents": 文档列表(与查询匹配) |
| API 参考 | Weaviate |
| GitHub 链接 | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/weaviate |
概述
WeaviateBM25Retriever 是一个基于关键字的 Retriever,用于从 WeaviateDocumentStore 中检索与查询匹配的文档。它基于 BM25 算法计算文档与查询之间的相似度,该算法通过计算两个字符串之间的加权词重叠来确定相似度。
两个字符串。
由于WeaviateBM25Retriever 基于词语重叠来匹配字符串,因此它常用于查找人名、产品名、ID 或明确定义的错误消息的精确匹配。BM25 算法非常轻量且简单。在处理域外数据时,很难用更复杂的基于嵌入的方法来超越它。
如果您希望查询和文档之间进行语义匹配,请使用 WeaviateEmbeddingRetriever,它使用嵌入模型创建的向量来检索相关信息。
参数
除了query,WeaviateBM25Retriever 接受其他可选参数,包括top_k(要检索的文档的最大数量)和filters(用于缩小搜索范围)。
用法
安装
要开始使用 Haystack 的 Weaviate,请使用以下命令安装该包:
pip install weaviate-haystack
单独使用
此 Retriever 需要一个 WeaviateDocumentStore 实例和已索引的文档才能运行。WeaviateDocumentStore 并已索引的文档才能运行。
from haystack_integrations.document_stores.weaviate.document_store import WeaviateDocumentStore
from haystack_integrations.components.retrievers.weaviate import WeaviateBM25Retriever
document_store = WeaviateDocumentStore(url="https://:8080")
retriever = WeaviateBM25Retriever(document_store=document_store)
retriever.run(query="How to make a pizza", top_k=3)
在 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.prompt_builder import PromptBuilder
from haystack.components.generators import OpenAIGenerator
from haystack.document_stores.types import DuplicatePolicy
# Create a RAG query pipeline
prompt_template = """
Given these documents, answer the question.\nDocuments:
{% for doc in documents %}
{{ doc.content }}
{% endfor %}
\nQuestion: {{question}}
\nAnswer:
"""
document_store = WeaviateDocumentStore(url="https://: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=PromptBuilder(template=prompt_template), name="prompt_builder"
)
rag_pipeline.add_component(instance=OpenAIGenerator(), name="llm")
rag_pipeline.add_component(instance=AnswerBuilder(), name="answer_builder")
rag_pipeline.connect("retriever", "prompt_builder.documents")
rag_pipeline.connect("prompt_builder", "llm")
rag_pipeline.connect("llm.replies", "answer_builder.replies")
rag_pipeline.connect("llm.metadata", "answer_builder.metadata")
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])
更新于 11 个月前
