MixedbreadAI
Welcome to the mixedbread ai reranker guide! This guide will help you use mixedbread aiâs API to rerank search query results, ensuring you get the most relevant information, just like picking the freshest bread from the bakery.
To find out more about the latest features and updates, visit the mixedbread.ai.
Table of Contents
Section titled âTable of ContentsâFirst, you will need to install the llamaindex
package.
npm i llamaindex @llamaindex/openai @llamaindex/mixedbread
Next, sign up for an API key at mixedbread.ai. Once you have your API key, you can import the necessary modules and create a new instance of the MixedbreadAIReranker
class.
import { Document, VectorStoreIndex, Settings,} from "llamaindex";import { OpenAI } from "@llamaindex/openai";import { MixedbreadAIReranker } from "@llamaindex/mixedbread";
Usage with LlamaIndex
Section titled âUsage with LlamaIndexâThis section will guide you through integrating mixedbreadâs reranker with LlamaIndex.
Step 1: Load and Index Documents
Section titled âStep 1: Load and Index DocumentsâFor this example, we will use a single document. In a real-world scenario, you would have multiple documents to index, like a variety of breads in a bakery.
const document = new Document({ text: "This is a sample document.", id_: "sampleDoc",});
Settings.llm = new OpenAI({ model: "gpt-3.5-turbo", temperature: 0.1 });
const index = await VectorStoreIndex.fromDocuments([document]);
Step 2: Increase Similarity TopK
Section titled âStep 2: Increase Similarity TopKâThe default value for similarityTopK
is 2, which means only the most similar document will be returned. To get more results, like picking a variety of fresh breads, you can increase the value of similarityTopK
.
const retriever = index.asRetriever({ similarityTopK: 5,});
Step 3: Create a MixedbreadAIReranker Instance
Section titled âStep 3: Create a MixedbreadAIReranker InstanceâCreate a new instance of the MixedbreadAIReranker
class.
const nodePostprocessor = new MixedbreadAIReranker({ apiKey: "<MIXEDBREAD_API_KEY>", topN: 4,});
Step 4: Create a Query Engine
Section titled âStep 4: Create a Query EngineâCombine the retriever and node postprocessor to create a query engine. This setup ensures that your queries are processed and reranked to provide the best results, like arranging the bread in the order of freshness and quality.
const queryEngine = index.asQueryEngine({ retriever, nodePostprocessors: [nodePostprocessor],});
// Log the responseconst response = await queryEngine.query("Where did the author grow up?");console.log(response);
With mixedbreadâs Reranker, youâre all set to serve up the most relevant and well-ordered results, just like a skilled baker arranging their best breads for eager customers. Enjoy the perfect blend of technology and culinary delight!
Simple Reranking Guide
Section titled âSimple Reranking GuideâThis section will guide you through a simple reranking process using mixedbread ai.
Step 1: Create an Instance of MixedbreadAIReranker
Section titled âStep 1: Create an Instance of MixedbreadAIRerankerâCreate a new instance of the MixedbreadAIReranker
class, passing in your API key and the number of results you want to return. Itâs like setting up your bakery to offer a specific number of freshly baked items.
const reranker = new MixedbreadAIReranker({ apiKey: "<MIXEDBREAD_API_KEY>", topN: 4,});
Step 2: Define Nodes and Query
Section titled âStep 2: Define Nodes and QueryâDefine the nodes (documents) you want to rerank and the query.
const nodes = [ { node: new BaseNode("To bake bread you need flour") }, { node: new BaseNode("To bake bread you need yeast") },];const query = "What do you need to bake bread?";
Step 3: Perform Reranking
Section titled âStep 3: Perform RerankingâUse the postprocessNodes
method to rerank the nodes based on the query.
const result = await reranker.postprocessNodes(nodes, query);console.log(result); // Like pulling freshly baked nodes out of the oven.
Reranking with Objects
Section titled âReranking with ObjectsâThis section will guide you through reranking when working with objects.
Step 1: Create an Instance of MixedbreadAIReranker
Section titled âStep 1: Create an Instance of MixedbreadAIRerankerâCreate a new instance of the MixedbreadAIReranker
class, just like before.
const reranker = new MixedbreadAIReranker({ apiKey: "<MIXEDBREAD_API_KEY>", model: "mixedbread-ai/mxbai-rerank-large-v1", topK: 5, rankFields: ["title", "content"], returnInput: true, maxRetries: 5,});
Step 2: Define Documents and Query
Section titled âStep 2: Define Documents and QueryâDefine the documents (objects) you want to rerank and the query.
const documents = [ { title: "Bread Recipe", content: "To bake bread you need flour" }, { title: "Bread Recipe", content: "To bake bread you need yeast" },];const query = "What do you need to bake bread?";
Step 3: Perform Reranking
Section titled âStep 3: Perform RerankingâUse the rerank
method to reorder the documents based on the query.
const result = await reranker.rerank(documents, query);console.log(result); // Perfectly customized results, ready to serve.