rerank-ts
rerank-ts is a lightweight TypeScript library for re-ranking search results from retreival systems.
Adding Re-Ranking almost always improves accuracy of retrieval pipelines. If you are building a RAG application, and using semantic search or full-text search using this library to re-rank the results will improve accuracy of the application in most cases. However, re-ranking usually adds some amount of latency. We have added knobs in the LLM ReRanker to control latency.
Installation
Install rerank using npm:
npm install rerank
Algorithms
- LLM Re-Rankers
- Reciprocal Rank Fusion
LLM Re-Rankers
Available Providers:
- Groq:
ProviderGroq
- OpenAI:
ProviderOpenAI
Model Providers are implemented with a clean interface, we welcome contributions to add support for other model providers from the community!
Example Usage:
import { LLMReranker, ProviderGroq } from "rerank";
const provider = new ProviderGroq("llama3-8b-8192", API_KEY);
const reranker = new LLMReranker(provider);
const list = [
{ key: "bc8fe338", value: "I hate vegetables" },
{ key: "236386f2", value: "I love mangoes" },
];
const query = "I love apples";
const result = await reranker.rerank(list, "key", "value", query);
Reciprocal Rank Fusion
Combine multiple rank lists by assigning scores based on reciprocal ranks, effectively prioritizing higher-ranked items across all lists.
Paper
Example Usage:
import { reciprocalRankFusion } from "rerank";
interface SearchResult {
url: string;
name: string;
}
const rankedLists: SearchResult[][] = await Promise.all([
searchIndex("exampleIndex1", "person riding skateboard"),
searchIndex("exampleIndex2", "person skating on the sidewalk"),
searchIndex("exampleIndex3", "skateboard trick"),
]);
const rankedURLs = reciprocalRankFusion(rankedLists, "url");
const resultsMap = new Map<string, SearchResult>();
rankedLists.flat().forEach((result) => {
resultsMap.set(result.url, result);
});
const sortedResults = Array.from(rankedURLs.keys()).map((url) => {
return resultsMap.get(url);
});