FastembedRanker
使用此组件,通过 FastEmbed 支持的交叉编码器模型,根据文档与查询的相似性对文档进行排序。
| pipeline 中的最常见位置 | 在查询管道中,在返回文档列表的组件(例如 Retriever)之后 |
| 强制运行变量 | “documents”:文档列表 “query”: 查询字符串 |
| 输出变量 | “documents”:文档列表 |
| API 参考 | FastEmbed |
| GitHub 链接 | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/fastembed |
概述
FastembedRanker 根据文档与查询的相似性对文档进行排序。它使用 FastEmbed 支持的交叉编码器模型。
基于 ONXX Runtime,FastEmbed 在标准 CPU 机器上提供了快速的体验。
FastembedRanker 在查询管道中最为有用,例如检索增强生成 (RAG) 管道或文档搜索管道,以确保检索到的文档按相关性排序。您可以在 Retriever(例如 InMemoryEmbeddingRetriever)之后使用它来改进搜索结果。使用FastembedRanker 与 Retriever 一起使用时,请考虑将 Retriever 的top_k 设置为较小的值。这样,Ranker 需要处理的文档就会更少,这有助于加快管道的处理速度。
默认情况下,此组件使用Xenova/ms-marco-MiniLM-L-6-v2 模型,但您可以通过调整 Ranker 初始化时的model 参数来切换到其他模型。有关不同初始化设置的详细信息,请参阅 API 参考页面。
兼容的模型
您可以在 FastEmbed 文档中找到兼容的模型。
安装
要开始使用此集成与 Haystack,请使用以下命令安装软件包:
pip install fastembed-haystack
参数
您可以设置模型存储在缓存目录中的路径。您还可以设置单个onnxruntime 会话可以使用的线程数。
cache_dir= "/your_cacheDirectory"
ranker = FastembedRanker(
model="Xenova/ms-marco-MiniLM-L-6-v2",
cache_dir=cache_dir,
threads=2
)
如果您想使用数据并行编码,可以设置参数parallel 和batch_size.
- 如果
parallel> 1,将使用数据并行编码。建议用于大型数据集的离线编码。 - 如果
parallel为 0,则使用所有可用核心。 - 如果为 None,则不使用数据并行处理;使用默认的
onnxruntime线程。
用法
单独使用
此示例使用FastembedRanker 对两个简单的文档进行排序。要运行 Ranker,请传递一个query,提供documents,并在top_k 参数中设置要返回的文档数量。
from haystack import Document
from haystack_integrations.components.rankers.fastembed import FastembedRanker
docs = [Document(content="Paris"), Document(content="Berlin")]
ranker = FastembedRanker()
ranker.warm_up()
ranker.run(query="City in France", documents=docs, top_k=1)
在 pipeline 中
以下是一个管道的示例,该管道使用InMemoryDocumentStore 基于关键字搜索(使用InMemoryBM25Retriever)检索文档。然后,它使用FastembedRanker 根据检索到的文档与查询的相似性对它们进行排序。该管道使用 Ranker 的默认设置。
from haystack import Document, Pipeline
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack_integrations.components.rankers.fastembed import FastembedRanker
docs = [
Document(content="Paris is in France"),
Document(content="Berlin is in Germany"),
Document(content="Lyon is in France"),
]
document_store = InMemoryDocumentStore()
document_store.write_documents(docs)
retriever = InMemoryBM25Retriever(document_store=document_store)
ranker = FastembedRanker()
document_ranker_pipeline = Pipeline()
document_ranker_pipeline.add_component(instance=retriever, name="retriever")
document_ranker_pipeline.add_component(instance=ranker, name="ranker")
document_ranker_pipeline.connect("retriever.documents", "ranker.documents")
query = "Cities in France"
res = document_ranker_pipeline.run(data={"retriever": {"query": query, "top_k": 3}, "ranker": {"query": query, "top_k": 2}})
更新于 11 个月前
