Reranking is the process of scoring and sorting documents based on their relevance to a query.

Reranking is often used in conjunction with a search engine, where the search engine retrieves documents that might be relevant to a user’s query using relatively computationally inexpensive information retrieval techniques like BM25 and dense embeddings, and the more computationally expensive but also more accurate reranker then resorts those documents based on their relevance to the query, before the top dozen results are presented to the user (or, when used as part of a retrieval-augmented generation (RAG) pipeline, to a generative model).

Isaacus’ legal universal classification models currently dual as rerankers and are accessible through our dedicated reranking API endpoint.

Usage

Our reranking endpoint takes a query and an array of texts as input and returns a list of results scored and sorted based on their relevance to the query.

As an example, the code snippet below shows how you could use our reranking endpoint to rank a list of legal texts based on their relevance to a query asking what the essential elements of a negligence claim are (please consult our quickstart guide first if you haven’t set up your Isaacus account and API client).

from isaacus import Isaacus

# Create an Isaacus API client.
client = Isaacus(api_key="PASTE_YOUR_API_KEY_HERE")

# Define a query and a list of texts to rerank.
query = "What are the essential elements required to establish a negligence claim?"
texts = [
    "To form a contract, there must be an offer, acceptance, consideration, "
    "and mutual intent to be bound.",
    
    "Criminal cases involve a completely different standard, requiring proof "
    "beyond a reasonable doubt.",

    "In a negligence claim, the plaintiff must prove duty, breach, causation, "
    "and damages.",

    "Negligence in tort law requires establishing a duty of care that the "
    "defendant owed to the plaintiff.",

    "The concept of negligence is central to tort law, with courts assessing "
    "whether a breach of duty caused harm."
]

# Rerank the texts.
reranking = client.rerankings.create(
    model="kanon-universal-classifier",
    query=query,
    texts=texts,
)

# Unpack the results.
results = [{
    "index": result.index,
    "text": texts[result.index],
    "score": result.score,
} for result in reranking.results]

# Print the reranked results.
for result in results:
    print(
        '-' * 10,
        f'index = {result["index"]} | score = {result["score"]:.2f}',
        '-' * 10,
        '\n',
        result["text"],
        end='\n\n',
    )