Skip to content

Reranker

Modules concerning the rerankers used in Gen AI applications.

FlagEmbeddingReranker(model_path, use_fp16=True)

Bases: BaseReranker

Reranks a list of chunks using a flag embedding.

Requires the FlagEmbedding package to be installed.

Attributes:

Name Type Description
reranker FlagReranker

The flag embedding reranker model.

Initializes a new instance of the FlagEmbeddingReranker class.

Parameters:

Name Type Description Default
model_path str

A path containing the reranker model.

required
use_fp16 bool

Whether to reduce the model size to FP16 or not. Defaults to True.

True

rerank(chunks, query=None) async

Rerank the list of chunks using the flag embedding reranker.

This method calculates the score for each chunk based on the given query and reranks the chunks based on the score in descending order.

Parameters:

Name Type Description Default
chunks list[Chunk]

The list of chunks to be reranked.

required
query str | None

The query to be used for reranking. Defaults to None.

None

Returns:

Type Description
list[Chunk]

list[Chunk]: A list of reranked chunks.

SimilarityBasedReranker(embeddings, similarity_func=lambda x, y: 1 - cosine_distance(x, y))

Bases: BaseReranker

A class to rerank chunks based on their similarity to a given query.

Attributes:

Name Type Description
embeddings BaseEMInvoker

An instance of the BaseEMInvoker class to embed text.

similarity_func Callable[[list[float], list[float]], float]

A callback function to calculate the similarity.

Initializes the SimilarityBasedReranker class with a similarity function.

This constructor method initializes an instance of the SimilarityBasedReranker class, setting up the similarity function that will be used to rerank chunks based on their similarity to a query.

Parameters:

Name Type Description Default
embeddings BaseEMInvoker

An instance of the BaseEMInvoker class that will be used to calculate the embeddings of the query and the chunks.

required
similarity_func Callable[[list[float], list[float]], float]

A callback function that takes two parameters (the query embeddings and chunk embeddings), and returns a similarity score as a float. Defaults to cosine similarity.

lambda x, y: 1 - cosine(x, y)

Returns:

Type Description
None

None

rerank(chunks, query=None) async

Reranks a list of chunks based on similarity to a given query.

This function takes a list of chunks and a query, then reranks the chunks based on their score in descending order (higher score means higher similarity). The similarity score is calculated using a predefined similarity function.

Parameters:

Name Type Description Default
chunks list[Chunk]

A list of Chunk objects to be reranked.

required
query str | None

The query to be used for reranking. Defaults to None.

None

Returns:

Type Description
list[Chunk]

list[Chunk]: A list of Chunk objects reranked based on similarity to the query.

TEIReranker(url, username=None, password=None, api_key=None, timeout=10, fallback_to_original=False)

Bases: BaseReranker

A class to rerank chunks by utilizing a reranker model hosted on Text Embedding Inference (TEI).

Attributes:

Name Type Description
url str

The URL of the TEI endpoint that hosts the reranker model.

auth_kwargs dict[str, Any]

Additional authentication arguments for the TEI reranker model.

timeout int

The timeout for the TEI reranker model in seconds.

fallback_to_original bool

Whether to fallback to the original chunks if the reranking fails.

Initializes a new instance of the TEIReranker class.

Parameters:

Name Type Description Default
url str

The URL of the TEI endpoint that hosts the reranker model.

required
username str | None

The username for the TEI reranker model. Defaults to None.

None
password str | None

The password for the TEI reranker model. Defaults to None.

None
api_key str | None

The API key for the TEI reranker model. Defaults to None.

None
timeout int

The timeout for the TEI reranker model in seconds. Defaults to 10.

10
fallback_to_original bool

Whether to fallback to the original chunks if the reranking fails. Defaults to False.

False

rerank(chunks, query=None) async

Reranks a list of chunks by using a reranker model hosted on Text Embedding Inference (TEI).

This function takes a list of chunks and a query, makes a request to the TEI reranker model, then reranks the chunks based on the ordering returned by the model.

Parameters:

Name Type Description Default
chunks list[Chunk]

A list of Chunk objects to be reranked.

required
query str | None

The query to be used for reranking. Defaults to None.

None

Returns:

Type Description
list[Chunk]

list[Chunk]: A list of Chunk objects reranked based on similarity to the query.

Raises:

Type Description
ValueError

If the query is empty.

RuntimeError

If the reranking request to the TEI endpoint fails.