Skip to main content

概述

本指南将帮助您集成和使用 Mixedbread AI 重排序 API。重排序 API 允许您根据给定查询对文档列表进行重新排序,从而提高搜索结果或任何排名列表的相关性。

安装

首先,安装 @langchain/mixedbread-ai 包:
请参阅 此部分 以获取有关安装 LangChain 包的一般说明。
npm install @langchain/mixedbread-ai

身份验证

通过在 Mixedbread AI 注册获取您的 API 密钥。然后,您可以将 MXBAI_API_KEY 环境变量设置为您的 Mixedbread AI API 密钥,或在构造类时直接将其作为 apiKey 选项传递。

使用重排序

MixedbreadAIReranker 类提供对重排序 API 的访问。以下是如何使用它:
  1. 导入类:首先,从包中导入 MixedbreadAIReranker 类。
import { MixedbreadAIReranker } from "@langchain/mixedbread-ai";
  1. 实例化类:使用您的 API 密钥创建 MixedbreadAIReranker 的实例。
const reranker = new MixedbreadAIReranker({ apiKey: "your-api-key" });
  1. 重排序文档:使用 rerankDocuments 方法根据查询重新排序文档。
const documents = [
  { pageContent: "To bake bread you need flour" },
  { pageContent: "To bake bread you need yeast" },
  { pageContent: "To eat bread you need nothing but good taste" },
];
const query = "What do you need to bake bread?";
const result = await reranker.compressDocuments(documents, query);
console.log(result);

其他资源

有关更多信息,请参阅 重排序 API 文档