文档API 参考📓 教程🧑‍🍳 食谱🤝 集成💜 Discord🎨 Studio
文档

FilterRetriever

使用此 Retriever 结合任何 Document Store,以获取匹配特定过滤条件的 Documents。

pipeline 中的最常见位置在 Pipeline 的开头
必需的初始化变量"document_store": Document Store 的一个实例
强制运行变量“filters”: 一个字典,包含与 Document Store 支持的语法相同的过滤条件
输出变量“documents”: 所有匹配这些过滤条件的文档
API 参考Retrievers (检索器)
GitHub 链接https://github.com/deepset-ai/haystack/blob/main/haystack/components/retrievers/filter_retriever.py

概述

FilterRetriever 检索与提供的过滤条件匹配的 Documents。

它是一种特殊的 Retriever——它可以与所有 Document Store 一起工作,而不是专门为只与一个 Document Store 工作而设计。

然而,与其他 Retriever 一样,它在初始化时需要一个 Document Store,并且它只会对该实例的内容进行过滤。

因此,它可以在 Pipeline 中像其他 Retriever 一样使用。

使用时请注意FilterRetriever 处理包含大量 Documents 的 Document Store 时,因为FilterRetriever 将返回所有匹配过滤条件的文档。在 Pipeline 中run 命令(没有过滤条件时)很容易导致其他组件(例如 Generators)不堪重负。

filter_retriever.run({})

另请注意,FilterRetriever 不会对您的 Documents 进行评分或以任何方式对其进行排名。如果您需要根据与查询的相似度对 Documents 进行排名,请考虑使用 Ranker 组件。

用法

单独使用

from haystack import Document
from haystack.components.retrievers import FilterRetriever
from haystack.document_stores.in_memory import InMemoryDocumentStore

docs = [
	Document(content="Python is a popular programming language", meta={"lang": "en"}),
	Document(content="python ist eine beliebte Programmiersprache", meta={"lang": "de"}),
]

doc_store = InMemoryDocumentStore()
doc_store.write_documents(docs)
retriever = FilterRetriever(doc_store)
result = retriever.run(filters={"field": "lang", "operator": "==", "value": "en"})

assert "documents" in result
assert len(result["documents"]) == 1
assert result["documents"][0].content == "Python is a popular programming language"

在 RAG 管道中

将您的OPENAI_API_KEY 设置为环境变量,然后运行以下代码

from haystack.components.retrievers.filter_retriever import FilterRetriever
from haystack.document_stores.in_memory import InMemoryDocumentStore

from haystack import Document, 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

import os
api_key = os.environ['OPENAI_API_KEY']

document_store = InMemoryDocumentStore()
documents = [
		Document(content="Mark lives in Berlin.", meta={"year": 2018}),
		Document(content="Mark lives in Paris.", meta={"year": 2021}),
		Document(content="Mark is Danish.", meta={"year": 2021}),
		Document(content="Mark lives in New York.", meta={"year": 2023}),
]
document_store.write_documents(documents=documents)

# Create a RAG query pipeline
prompt_template = """
    Given these documents, answer the question.\nDocuments:
    {% for doc in documents %}
        {{ doc.content }}
    {% endfor %}

    \nQuestion: {{question}}
    \nAnswer:
    """

rag_pipeline = Pipeline()
rag_pipeline.add_component(name="retriever", instance=FilterRetriever(document_store=document_store))
rag_pipeline.add_component(instance=PromptBuilder(template=prompt_template), name="prompt_builder")
rag_pipeline.add_component(instance=OpenAIGenerator(api_key=api_key), name="llm")
rag_pipeline.connect("retriever", "prompt_builder.documents")
rag_pipeline.connect("prompt_builder", "llm")

result = rag_pipeline.run(
  {
    "retriever": {"filters": {"field": "year", "operator": "==", "value": 2021}},
    "prompt_builder": {"question": "Where does Mark live?"},
  }
)
print(result['answer_builder']['answers'][0])`

这是您可能会看到的示例输出

According to the provided documents, Mark lives in Paris.

相关链接

在我们的 API 参考中查看参数详情