Fast Diversification for Search & Retrieval
Low maintenance activity. Last release 10 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install pyversity

Fast Diversification for Search & Retrieval
Pyversity is a fast, lightweight library for diversifying retrieval results. Retrieval systems often return highly similar items. Pyversity efficiently re-ranks these results to encourage diversity, surfacing items that remain relevant but less redundant.
It implements several popular diversification strategies such as MMR, MSD, DPP, SSD, and Cover with a clear, unified API and benchmarks for each strategy. More information about the supported strategies can be found in the supported strategies section. The only dependency is NumPy, making the package very lightweight.
Quickstart
Install pyversity with:
pip install pyversity
Diversify retrieval results:
import numpy as np
from pyversity import diversify, Strategy
# Define embeddings and scores (e.g. cosine similarities of a query result)
embeddings = np.random.randn(100, 256)
scores = np.random.rand(100)
# Diversify the result
diversified_result = diversify(
embeddings=embeddings,
scores=scores,
k=10, # Number of items to select
strategy=Strategy.DPP, # Diversification strategy to use
diversity=0.5 # Diversity parameter (higher values prioritize diversity)
)
# Get the indices of the diversified result
diversified_indices = diversified_result.indices