Reranker

gllm-retrieval | Involves EM | Tutorial: Reranker | API Reference

What's a Reranker?

A reranker is a component that reorders retrieved chunks based on their relevance to a query. After initial retrieval returns a set of candidate chunks, the reranker scores and sorts them to ensure the most relevant content appears first. This improves the quality of context provided to language models in RAG pipelines.

Rerankers are particularly useful when:

  1. Initial retrieval returns many candidates that need prioritization

  2. You want to combine results from multiple retrieval sources

  3. The retrieval method does not perfectly capture semantic relevance

Prerequisites

This example specifically requires completion of all setup steps listed on the Prerequisites page.

Installation

pip install --extra-index-url https://oauth2accesstoken:$(gcloud auth print-access-token)@glsdk.gdplabs.id/gen-ai-internal/simple/ "gllm-retrieval"

Quickstart

Let's start with a basic example using SimilarityBasedReranker, which uses embedding similarity to rerank chunks:

Expected Output

The chunks are reordered with the most relevant content first:

Available Rerankers

The SDK provides multiple reranker implementations for different use cases:

Reranker
Description
Best For

SimilarityBasedReranker

Uses embedding similarity scores

General-purpose semantic reranking

TEIReranker

Uses Text Embedding Inference endpoint

High-performance, self-hosted deployments

FlagEmbeddingReranker

Uses FlagEmbedding models

Multilingual and specialized domains

CohereBedrockReranker

Uses AWS Bedrock Cohere service

Cloud-based, managed reranking

Similarity-Based Reranking

The SimilarityBasedReranker calculates embedding similarity between the query and each chunk, then sorts by score:

Custom Similarity Functions: You can provide a custom similarity function that takes two embedding vectors and returns a float score. Higher scores indicate greater similarity.

TEI Reranking

The TEIReranker uses a reranker model hosted on Text Embedding Inference (TEI):

Authentication: TEIReranker supports both basic auth (username/password) and bearer token (api_key) authentication methods.

FlagEmbedding Reranking

The FlagEmbeddingReranker uses FlagEmbedding models for reranking:

Cohere Bedrock Reranking

The CohereBedrockReranker uses Cohere's reranker models hosted on AWS Bedrock:

AWS Credentials: Ensure your AWS credentials have Bedrock permissions. See AWS Bedrock documentation for supported models.

Using Rerankers in Pipelines

Rerankers integrate seamlessly with the SDK's pipeline system:

API Reference

For detailed API documentation, see the Reranker API Reference.

Last updated