跳到主要内容
BM25,也称为Okapi BM25,是信息检索系统中用于评估文档与给定搜索查询相关性的排序函数。 您可以将其作为检索管道的一部分,在从其他来源检索到初始文档集后,作为后处理步骤对文档进行重新排序。

设置

BM25Retriever@langchain/community 导出。你需要这样安装它
npm install @langchain/community @langchain/core
此检索器使用来自 此实现 的 Okapi BM25 代码。

用法

您现在可以使用之前检索到的文档创建一个新的检索器
import { BM25Retriever } from "@langchain/community/retrievers/bm25";

const retriever = BM25Retriever.fromDocuments([
  { pageContent: "Buildings are made out of brick", metadata: {} },
  { pageContent: "Buildings are made out of wood", metadata: {} },
  { pageContent: "Buildings are made out of stone", metadata: {} },
  { pageContent: "Cars are made out of metal", metadata: {} },
  { pageContent: "Cars are made out of plastic", metadata: {} },
  { pageContent: "mitochondria is the powerhouse of the cell", metadata: {} },
  { pageContent: "mitochondria is made of lipids", metadata: {} },
], { k: 4 });

// Will return the 4 documents reranked by the BM25 algorithm
await retriever.invoke("mitochondria");
[
  { pageContent: 'mitochondria is made of lipids', metadata: {} },
  {
    pageContent: 'mitochondria is the powerhouse of the cell',
    metadata: {}
  },
  { pageContent: 'Buildings are made out of brick', metadata: {} },
  { pageContent: 'Buildings are made out of wood', metadata: {} }
]

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.