npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rerank

v1.1.4

Published

rerank library for easy reranking of results

Downloads

125

Readme

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);

// Replace with your own list of objects to rerank.
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);
// ["236386f2", "bc8fe338"]

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";

// Example structure of a search result for this usage example
interface SearchResult {
  url: string; // we will use this as our key identifier
  name: string;
}

// Assume you are searching with 3 different queries and fetching results
// searchIndex is just for demonstration purposes and should be replaced with your actual search implementation
const rankedLists: SearchResult[][] = await Promise.all([
  searchIndex("exampleIndex1", "person riding skateboard"),
  searchIndex("exampleIndex2", "person skating on the sidewalk"),
  searchIndex("exampleIndex3", "skateboard trick"),
]);

// Perform Reciprocal Rank Fusion (RRF) on all of the results
// The RRF function takes the ranked lists and a key identifier, in this case "url"
// It returns a map of all URLs now ranked with an RRF score
const rankedURLs = reciprocalRankFusion(rankedLists, "url");

// Build a map of results keyed by URL for easy access
const resultsMap = new Map<string, SearchResult>();
rankedLists.flat().forEach((result) => {
  resultsMap.set(result.url, result);
});

// Get a single sorted list of results based on the RRF ranking
const sortedResults = Array.from(rankedURLs.keys()).map((url) => {
  return resultsMap.get(url);
});