Skip to content

Reranking

rankify.models.reranking

HF_PRE_DEFIND_MODELS = {'upr': {'t5-small': 'google/t5-small-lm-adapt', 't5-base': 'google/t5-base-lm-adapt', 't5-large': 'google/t5-large-lm-adapt', 't0-3b': 'bigscience/T0_3B', 't0-11b': 'bigscience/T0', 'gpt-neo-2.7b': 'EleutherAI/gpt-neo-2.7B', 'gpt-j-6b': 'EleutherAI/gpt-j-6b', 'gpt2': 'openai-community/gpt2', 'gpt2-medium': 'openai-community/gpt2-medium', 'gpt2-large': 'openai-community/gpt2-large', 'gpt2-xl': 'openai-community/gpt2-xl', 'flan-t5-xl': 'google/flan-t5-xl'}, 'rankgpt-api': {'gpt-3.5': 'gpt-3.5', 'gpt-4': 'gpt-4o', 'gpt-4-mini': 'gpt-4o-mini', 'llamav3.1-8b': 'llamav3.1-8b', 'llamav3.1-70b': 'llamav3.1-70b', 'claude-3-5': 'claude-3-5'}, 'rankgpt': {'llamav3.1-8b': 'meta-llama/Meta-Llama-3.1-8B-Instruct', 'llamav3.1-70b': 'meta-llama/Llama-3.1-70B-Instruct', 'Llama-3.2-1B': 'meta-llama/Llama-3.2-1B-Instruct', 'Llama-3.2-3B': 'meta-llama/Llama-3.2-3B-Instruct', 'Qwen2.5-7B': 'Qwen/Qwen2.5-7B', 'Mistral-7B-Instruct-v0.2': 'mistralai/Mistral-7B-Instruct-v0.2', 'Mistral-7B-Instruct-v0.3': 'mistralai/Mistral-7B-Instruct-v0.3'}, 'flashrank': {'ms-marco-TinyBERT-L-2-v2': 'ms-marco-TinyBERT-L-2-v2', 'ms-marco-MiniLM-L-12-v2': 'ms-marco-MiniLM-L-12-v2', 'ms-marco-MultiBERT-L-12': 'ms-marco-MultiBERT-L-12', 'rank-T5-flan': 'rank-T5-flan', 'ce-esci-MiniLM-L12-v2': 'ce-esci-MiniLM-L12-v2', 'rank_zephyr_7b_v1_full': 'rank_zephyr_7b_v1_full', 'miniReranker_arabic_v1': 'miniReranker_arabic_v1'}, 'flashrank-model-file': {'ms-marco-TinyBERT-L-2-v2': 'flashrank-TinyBERT-L-2-v2.onnx', 'ms-marco-MiniLM-L-12-v2': 'flashrank-MiniLM-L-12-v2_Q.onnx', 'ms-marco-MultiBERT-L-12': 'flashrank-MultiBERT-L12_Q.onnx', 'rank-T5-flan': 'flashrank-rankt5_Q.onnx', 'ce-esci-MiniLM-L12-v2': 'flashrank-ce-esci-MiniLM-L12-v2_Q.onnx', 'rank_zephyr_7b_v1_full': 'rank_zephyr_7b_v1_full.Q4_K_M.gguf', 'miniReranker_arabic_v1': 'miniReranker_arabic_v1.onnx'}, 'monot5': {'monot5-base-msmarco': 'castorini/monot5-base-msmarco', 'monot5-base-msmarco-10k': 'castorini/monot5-base-msmarco-10k', 'monot5-large-msmarco': 'castorini/monot5-large-msmarco', 'monot5-large-msmarco-10k': 'castorini/monot5-large-msmarco-10k', 'monot5-base-med-msmarco': 'castorini/monot5-base-med-msmarco', 'monot5-3b-med-msmarco': 'castorini/monot5-3b-med-msmarco', 'monot5-3b-msmarco-10k': 'castorini/monot5-3b-msmarco-10k', 'mt5-base-en-msmarco': 'unicamp-dl/mt5-base-en-msmarco', 'ptt5-base-pt-msmarco-10k-v2': 'unicamp-dl/ptt5-base-pt-msmarco-10k-v2', 'ptt5-base-pt-msmarco-100k-v2': 'unicamp-dl/ptt5-base-pt-msmarco-100k-v2', 'ptt5-base-en-pt-msmarco-100k-v2': 'unicamp-dl/ptt5-base-en-pt-msmarco-100k-v2', 'mt5-base-en-pt-msmarco-v2': 'unicamp-dl/mt5-base-en-pt-msmarco-v2', 'mt5-base-mmarco-v2': 'unicamp-dl/mt5-base-mmarco-v2', 'mt5-base-en-pt-msmarco-v1': 'unicamp-dl/mt5-base-en-pt-msmarco-v1', 'mt5-base-mmarco-v1': 'unicamp-dl/mt5-base-mmarco-v1', 'ptt5-base-pt-msmarco-10k-v1': 'unicamp-dl/ptt5-base-pt-msmarco-10k-v1', 'ptt5-base-pt-msmarco-100k-v1': 'unicamp-dl/ptt5-base-pt-msmarco-100k-v1', 'ptt5-base-en-pt-msmarco-10k-v1': 'unicamp-dl/ptt5-base-en-pt-msmarco-10k-v1', 'mt5-3B-mmarco-en-pt': 'unicamp-dl/mt5-3B-mmarco-en-pt', 'mt5-13b-mmarco-100k': 'unicamp-dl/mt5-13b-mmarco-100k', 'monoptt5-small': 'unicamp-dl/monoptt5-small', 'monoptt5-base': 'unicamp-dl/monoptt5-base', 'monoptt5-large': 'unicamp-dl/monoptt5-large', 'monoptt5-3b': 'unicamp-dl/monoptt5-3b'}, 'rankt5': {'rankt5-base': 'Soyoung97/RankT5-base', 'rankt5-large': 'Soyoung97/RankT5-large', 'rankt5-3b': 'Soyoung97/RankT5-3b'}, 'listt5': {'listt5-base': 'Soyoung97/ListT5-base', 'listt5-3b': 'Soyoung97/ListT5-3b'}, 'inranker': {'inranker-small': 'unicamp-dl/InRanker-small', 'inranker-base': 'unicamp-dl/InRanker-base', 'inranker-3b': 'unicamp-dl/InRanker-3B'}, 'apiranker': {'cohere': 'cohere', 'jina': 'jina', 'voyage': 'voyage', 'mixedbread.ai': 'mixedbread.ai'}, 'transformer_ranker': {'mxbai-rerank-xsmall': 'mixedbread-ai/mxbai-rerank-xsmall-v1', 'mxbai-rerank-base': 'mixedbread-ai/mxbai-rerank-base-v1', 'mxbai-rerank-large': 'mixedbread-ai/mxbai-rerank-large-v1', 'bge-reranker-base': 'BAAI/bge-reranker-base', 'bge-reranker-large': 'BAAI/bge-reranker-large', 'bge-reranker-v2-m3': 'BAAI/bge-reranker-v2-m3', 'bce-reranker-base': 'maidalun1020/bce-reranker-base_v1', 'jina-reranker-tiny': 'jinaai/jina-reranker-v1-tiny-en', 'jina-reranker-turbo': 'jinaai/jina-reranker-v1-turbo-en', 'jina-reranker-base-multilingual': 'jinaai/jina-reranker-v2-base-multilingual', 'gte-multilingual-reranker-base': 'Alibaba-NLP/gte-multilingual-reranker-base', 'camembert-base-mmarcoFR': 'antoinelouis/crossencoder-camembert-base-mmarcoFR', 'camembert-large-mmarcoFR': 'antoinelouis/crossencoder-camembert-large-mmarcoFR', 'camemberta-base-mmarcoFR': 'antoinelouis/crossencoder-camemberta-base-mmarcoFR', 'distilcamembert-mmarcoFR': 'antoinelouis/crossencoder-distilcamembert-mmarcoFR', 'cross-encoder-mmarco-mMiniLMv2-L12-H384-v1': 'corrius/cross-encoder-mmarco-mMiniLMv2-L12-H384-v1', 'nli-deberta-v3-large': 'cross-encoder/nli-deberta-v3-large', 'ms-marco-MiniLM-L-12-v2': 'cross-encoder/ms-marco-MiniLM-L-12-v2', 'ms-marco-MiniLM-L-6-v2': 'cross-encoder/ms-marco-MiniLM-L-6-v2', 'ms-marco-MiniLM-L-4-v2': 'cross-encoder/ms-marco-MiniLM-L-4-v2', 'ms-marco-MiniLM-L-2-v2': 'cross-encoder/ms-marco-MiniLM-L-2-v2', 'ms-marco-TinyBERT-L-2-v2': 'cross-encoder/ms-marco-TinyBERT-L-2-v2', 'ms-marco-electra-base': 'cross-encoder/ms-marco-electra-base', 'ms-marco-TinyBERT-L-6': 'cross-encoder/ms-marco-TinyBERT-L-6', 'ms-marco-TinyBERT-L-4': 'cross-encoder/ms-marco-TinyBERT-L-4', 'ms-marco-TinyBERT-L-2': 'cross-encoder/ms-marco-TinyBERT-L-2', 'msmarco-MiniLM-L12-en-de-v1': 'cross-encoder/msmarco-MiniLM-L12-en-de-v1', 'msmarco-MiniLM-L6-en-de-v1': 'cross-encoder/msmarco-MiniLM-L6-en-de-v1'}, 'llm_layerwise_ranker': {'bge-multilingual-gemma2': 'BAAI/bge-multilingual-gemma2', 'bge-reranker-v2-gemma': 'BAAI/bge-reranker-v2-gemma', 'bge-reranker-v2-minicpm-layerwise': 'BAAI/bge-reranker-v2-minicpm-layerwise', 'bge-reranker-v2.5-gemma2-lightweight': 'BAAI/bge-reranker-v2.5-gemma2-lightweight'}, 'first_ranker': {'First-Model': 'rryisthebest/First_Model', 'Llama-3-8B': 'meta-llama/Meta-Llama-3-8B-Instruct'}, 'lit5dist': {'LiT5-Distill-base': 'castorini/LiT5-Distill-base', 'LiT5-Distill-large': 'castorini/LiT5-Distill-large', 'LiT5-Distill-xl': 'castorini/LiT5-Distill-xl', 'LiT5-Distill-base-v2': 'castorini/LiT5-Distill-base-v2', 'LiT5-Distill-large-v2': 'castorini/LiT5-Distill-large-v2', 'LiT5-Distill-xl-v2': 'castorini/LiT5-Distill-xl-v2'}, 'lit5score': {'LiT5-Score-base': 'castorini/LiT5-Score-base', 'LiT5-Score-large': 'castorini/LiT5-Score-large', 'LiT5-Score-xl': 'castorini/LiT5-Score-xl'}, 'vicuna_reranker': {'rank_vicuna_7b_v1': 'castorini/rank_vicuna_7b_v1', 'rank_vicuna_7b_v1_noda': 'castorini/rank_vicuna_7b_v1_noda', 'rank_vicuna_7b_v1_fp16': 'castorini/rank_vicuna_7b_v1_fp16', 'rank_vicuna_7b_v1_noda_fp16': 'castorini/rank_vicuna_7b_v1_noda_fp16'}, 'zephyr_reranker': {'rank_zephyr_7b_v1_full': 'castorini/rank_zephyr_7b_v1_full'}, 'blender_reranker': {'PairRM': 'llm-blender/PairRM'}, 'splade_reranker': {'splade-cocondenser': 'naver/splade-cocondenser-ensembledistil'}, 'sentence_transformer_reranker': {'all-MiniLM-L6-v2': 'all-MiniLM-L6-v2', 'gtr-t5-base': 'sentence-transformers/gtr-t5-base', 'gtr-t5-large': 'sentence-transformers/gtr-t5-large', 'gtr-t5-xl': 'sentence-transformers/gtr-t5-xl', 'gtr-t5-xxl': 'sentence-transformers/gtr-t5-xxl', 'sentence-t5-base': 'sentence-transformers/sentence-t5-base', 'sentence-t5-xl': 'sentence-transformers/sentence-t5-xl', 'sentence-t5-xxl': 'sentence-transformers/sentence-t5-xxl', 'sentence-t5-large': 'sentence-transformers/sentence-t5-large', 'distilbert-multilingual-nli-stsb-quora-ranking': 'sentence-transformers/distilbert-multilingual-nli-stsb-quora-ranking', 'msmarco-bert-co-condensor': 'sentence-transformers/msmarco-bert-co-condensor', 'msmarco-roberta-base-v2': 'sentence-transformers/msmarco-roberta-base-v2'}, 'colbert_ranker': {'colbertv2.0': 'colbert-ir/colbertv2.0', 'FranchColBERT': 'bclavie/FraColBERTv2', 'JapanColBERT': 'bclavie/JaColBERTv2', 'SpanishColBERT': 'AdrienB134/ColBERTv2.0-spanish-mmarcoES', 'jina-colbert-v1-en': 'jinaai/jina-colbert-v1-en', 'ArabicColBERT-250k': 'akhooli/arabic-colbertv2-250k-norm', 'ArabicColBERT-711k': 'akhooli/arabic-colbertv2-711k-norm', 'BengaliColBERT': 'turjo4nis/colbertv2.0-bn', 'mxbai-colbert-large-v1': 'mixedbread-ai/mxbai-colbert-large-v1'}, 'monobert': {'monobert-large': 'castorini/monobert-large-msmarco'}, 'llm2vec': {'Meta-Llama-31-8B': 'McGill-NLP/LLM2Vec-Meta-Llama-31-8B-Instruct-mntp', 'Meta-Llama-3-8B': 'McGill-NLP/LLM2Vec-Meta-Llama-3-8B-Instruct-mntp', 'Mistral-7B': 'McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp', 'Llama-2-7B': 'McGill-NLP/LLM2Vec-Llama-2-7b-chat-hf-mntp', 'Sheared-LLaMA': 'McGill-NLP/LLM2Vec-Sheared-LLaMA-mntp'}, 'twolar': {'twolar-xl': 'Dundalia/TWOLAR-xl', 'twolar-large': 'Dundalia/TWOLAR-large'}, 'duot5': {'duot5-base-msmarco': 'castorini/duot5-base-msmarco', 'duot5-3b-msmarco': 'castorini/duot5-3b-msmarco', 'duot5-large-msmarco': 'castorini/duot5-large-msmarco-10k'}, 'rankllama': {'rankllama-v1-7b-lora-passage': 'castorini/rankllama-v1-7b-lora-passage'}, 'dear_reranker': {'dear-3b-reranker-ce-v1': 'abdoelsayed/dear-3b-reranker-ce-v1', 'dear-3b-reranker-ranknet-v1': 'abdoelsayed/dear-3b-reranker-ranknet-v1', 'dear-3b-reranker-ce-lora-v1': 'abdoelsayed/dear-3b-reranker-ce-lora-v1', 'dear-8b-reranker-ce-v1': 'abdoelsayed/dear-8b-reranker-ce-v1'}, 'echorank': {'flan-t5-large': 'google/flan-t5-large', 'flan-t5-xl': 'google/flan-t5-xl'}, 'incontext_reranker': {'llamav3.1-8b': 'meta-llama/Meta-Llama-3.1-8B-Instruct', 'llamav3.1-70b': 'meta-llama/Llama-3.1-70B-Instruct', 'Mistral-7B-Instruct-v0.2': 'mistralai/Mistral-7B-Instruct-v0.2'}, 'tart': {'tart-full-flan-t5-xl': 'facebook/tart-full-flan-t5-xl', 'tart-dual-flan-t5-xl': 'facebook/tart-dual-flan-t5-xl'}, 'prp': {'llamav3.1-8b': 'meta-llama/Meta-Llama-3.1-8B-Instruct', 'llamav3.1-70b': 'meta-llama/Llama-3.1-70B-Instruct', 'Llama-3.2-1B': 'meta-llama/Llama-3.2-1B-Instruct', 'Llama-3.2-3B': 'meta-llama/Llama-3.2-3B-Instruct', 'Mistral-7B-Instruct-v0.3': 'mistralai/Mistral-7B-Instruct-v0.3'}, 'prp-api': {'gpt-3.5': 'gpt-3.5-turbo', 'gpt-4': 'gpt-4o', 'gpt-4-mini': 'gpt-4o-mini', 'llamav3.1-8b': 'llamav3.1-8b', 'llamav3.1-70b': 'llamav3.1-70b'}, 'rankgemma': {'gemma-2-2b': 'google/gemma-2-2b-it', 'gemma-2-9b': 'google/gemma-2-9b-it', 'gemma-2-27b': 'google/gemma-2-27b-it'}, 'rankmistral': {'mistral-7b': 'mistralai/Mistral-7B-Instruct-v0.3', 'mistral-7b-v0.2': 'mistralai/Mistral-7B-Instruct-v0.2', 'mixtral-8x7b': 'mistralai/Mixtral-8x7B-Instruct-v0.1'}} module-attribute

METHOD_MAP = {'upr': UPR, 'rankgpt-api': RankGPT, 'rankgpt': RankGPT, 'flashrank': FlashRanker, 'monot5': MonoT5, 'rankt5': RankT5, 'listt5': ListT5, 'inranker': InRanker, 'apiranker': APIRanker, 'transformer_ranker': TransformerRanker, 'llm_layerwise_ranker': LLMLayerWiseRanker, 'blender_reranker': BlenderReranker, 'splade_reranker': SpladeReranker, 'sentence_transformer_reranker': SentenceTransformerReranker, 'colbert_ranker': ColBERTReranker, 'monobert': MonoBERT, 'llm2vec': LLM2VecReranker, 'twolar': TWOLAR, 'echorank': EchoRankReranker, 'duot5': DuoT5, 'rankllama': RankLLaMAReranker, 'dear_reranker': DeARReranker, 'incontext_reranker': InContextReranker, 'prp': PRPReranker, 'prp-api': PRPReranker, 'tart': TARTReranker, 'rankgemma': RankGemmaReranker, 'rankmistral': RankMistralReranker} module-attribute

Document

Represents a document consisting of a question, answers, and contexts.

Attributes:

Name Type Description
question Question

The question associated with the document.

answers Answer

The answers to the question.

contexts list[Context]

A list of related contexts.

reorder_contexts list[Context] or None

A reordered list of contexts based on relevance.

Source code in rankify/dataset/dataset.py
class Document:
    """
    Represents a document consisting of a question, answers, and contexts.

    Attributes:
        question (Question): The question associated with the document.
        answers (Answer): The answers to the question.
        contexts (list[Context]): A list of related contexts.
        reorder_contexts (list[Context] or None): A reordered list of contexts based on relevance.
    """
    def __init__(self, question: Question, answers: Answer, contexts: list = None , id: int = None) -> None:
        """
        Initializes a Document instance.

        Args:
            question (Question): The question associated with the document.
            answers (Answer): The answers to the question.
            contexts (list[Context], optional): A list of contexts related to the question.

        Example:
            ```python
            q = Question("What is the capital of France?")
            a = Answer(["Paris"])
            c1 = Context(score=0.9, has_answer=True, id=1, title="Paris", text="The capital of France is Paris.")
            c2 = Context(score=0.5, has_answer=False, id=2, title="Berlin", text="Berlin is the capital of Germany.")
            d = Document(question=q, answers=a, contexts=[c1, c2])
            print(d)
            ```
        """
        self.question: Question = question
        self.answers: Answer = answers
        self.contexts: List[Context] = contexts
        self.reorder_contexts: List[Context] = None
        self.id = str(id) 

    @classmethod
    def from_dict(cls, data: dict,n_docs:int=100) -> 'Document':
        """
        Creates a Document instance from a dictionary.

        Args:
            data (dict): A dictionary containing the question, answers, and contexts.
            n_docs (int, optional): The number of contexts to include. Defaults to 100.

        Returns:
            Document: A new Document instance.

        Example:
            ```python
            data = {
                "question": "What is the capital of France?",
                "answers": ["Paris"],
                "ctxs": [
                    {"score": 0.9, "has_answer": True, "id": 1, "title": "Paris", "text": "The capital of France is Paris."},
                    {"score": 0.5, "has_answer": False, "id": 2, "title": "Berlin", "text": "Berlin is the capital of Germany."}
                ]
            }
            d = Document.from_dict(data)
            print(d.question)
            ```
        """
        question = Question(data["question"])
        if "answers" in data:
            answers = Answer(data["answers"])
        else:
            answers =Answer('')

        if "query_id" in data:
            id = data["query_id"]
        else:
            id = None
        contexts = [Context(**ctx) for ctx in data["ctxs"][:n_docs]]
        return cls(question, answers, contexts, id=id)

    def to_dict(self) -> Dict[str, Optional[object]]:
        """
        Converts the document into a dictionary representation.

        Returns:
            dict: A dictionary containing the question, answers, and contexts.
        """
        return {
            "question": self.question.question,
            "answers": self.answers.answers,
            "contexts": [ctx.to_dict() for ctx in self.contexts]
        }
    def to_dict_reoreder(self) -> Dict[str,Optional[object]]:
        return {
            "question" : self.question.question,
            "answers" : self.answers.answers,
            "contexts" : [ctx.to_dict() for ctx in self.reorder_contexts]
        }
    def __str__(self) -> str:
        """
        Returns a string representation of the Document instance.

        Returns:
            str: The formatted document information.

        Example:
            ```python
            d = Document(Question("What is the capital of France?"), Answer(["Paris"]))
            print(d)
            ```
        """
        contexts_str = "\n\n".join([str(ctx) for ctx in self.contexts])
        reorder_contexts_str= ''
        if self.reorder_contexts is not None:
            reorder_contexts_str = "\n\n".join([str(ctx) for ctx in self.reorder_contexts])
        return f"{self.question}\n\n{self.answers}\n\nContext: \n\n{contexts_str}\nReorder contexts: \n\n{reorder_contexts_str}"

__init__(question, answers, contexts=None, id=None)

Initializes a Document instance.

Parameters:

Name Type Description Default
question Question

The question associated with the document.

required
answers Answer

The answers to the question.

required
contexts list[Context]

A list of contexts related to the question.

None
Example
q = Question("What is the capital of France?")
a = Answer(["Paris"])
c1 = Context(score=0.9, has_answer=True, id=1, title="Paris", text="The capital of France is Paris.")
c2 = Context(score=0.5, has_answer=False, id=2, title="Berlin", text="Berlin is the capital of Germany.")
d = Document(question=q, answers=a, contexts=[c1, c2])
print(d)
Source code in rankify/dataset/dataset.py
def __init__(self, question: Question, answers: Answer, contexts: list = None , id: int = None) -> None:
    """
    Initializes a Document instance.

    Args:
        question (Question): The question associated with the document.
        answers (Answer): The answers to the question.
        contexts (list[Context], optional): A list of contexts related to the question.

    Example:
        ```python
        q = Question("What is the capital of France?")
        a = Answer(["Paris"])
        c1 = Context(score=0.9, has_answer=True, id=1, title="Paris", text="The capital of France is Paris.")
        c2 = Context(score=0.5, has_answer=False, id=2, title="Berlin", text="Berlin is the capital of Germany.")
        d = Document(question=q, answers=a, contexts=[c1, c2])
        print(d)
        ```
    """
    self.question: Question = question
    self.answers: Answer = answers
    self.contexts: List[Context] = contexts
    self.reorder_contexts: List[Context] = None
    self.id = str(id) 

from_dict(data, n_docs=100) classmethod

Creates a Document instance from a dictionary.

Parameters:

Name Type Description Default
data dict

A dictionary containing the question, answers, and contexts.

required
n_docs int

The number of contexts to include. Defaults to 100.

100

Returns:

Name Type Description
Document Document

A new Document instance.

Example
data = {
    "question": "What is the capital of France?",
    "answers": ["Paris"],
    "ctxs": [
        {"score": 0.9, "has_answer": True, "id": 1, "title": "Paris", "text": "The capital of France is Paris."},
        {"score": 0.5, "has_answer": False, "id": 2, "title": "Berlin", "text": "Berlin is the capital of Germany."}
    ]
}
d = Document.from_dict(data)
print(d.question)
Source code in rankify/dataset/dataset.py
@classmethod
def from_dict(cls, data: dict,n_docs:int=100) -> 'Document':
    """
    Creates a Document instance from a dictionary.

    Args:
        data (dict): A dictionary containing the question, answers, and contexts.
        n_docs (int, optional): The number of contexts to include. Defaults to 100.

    Returns:
        Document: A new Document instance.

    Example:
        ```python
        data = {
            "question": "What is the capital of France?",
            "answers": ["Paris"],
            "ctxs": [
                {"score": 0.9, "has_answer": True, "id": 1, "title": "Paris", "text": "The capital of France is Paris."},
                {"score": 0.5, "has_answer": False, "id": 2, "title": "Berlin", "text": "Berlin is the capital of Germany."}
            ]
        }
        d = Document.from_dict(data)
        print(d.question)
        ```
    """
    question = Question(data["question"])
    if "answers" in data:
        answers = Answer(data["answers"])
    else:
        answers =Answer('')

    if "query_id" in data:
        id = data["query_id"]
    else:
        id = None
    contexts = [Context(**ctx) for ctx in data["ctxs"][:n_docs]]
    return cls(question, answers, contexts, id=id)

to_dict()

Converts the document into a dictionary representation.

Returns:

Name Type Description
dict Dict[str, Optional[object]]

A dictionary containing the question, answers, and contexts.

Source code in rankify/dataset/dataset.py
def to_dict(self) -> Dict[str, Optional[object]]:
    """
    Converts the document into a dictionary representation.

    Returns:
        dict: A dictionary containing the question, answers, and contexts.
    """
    return {
        "question": self.question.question,
        "answers": self.answers.answers,
        "contexts": [ctx.to_dict() for ctx in self.contexts]
    }

__str__()

Returns a string representation of the Document instance.

Returns:

Name Type Description
str str

The formatted document information.

Example
d = Document(Question("What is the capital of France?"), Answer(["Paris"]))
print(d)
Source code in rankify/dataset/dataset.py
def __str__(self) -> str:
    """
    Returns a string representation of the Document instance.

    Returns:
        str: The formatted document information.

    Example:
        ```python
        d = Document(Question("What is the capital of France?"), Answer(["Paris"]))
        print(d)
        ```
    """
    contexts_str = "\n\n".join([str(ctx) for ctx in self.contexts])
    reorder_contexts_str= ''
    if self.reorder_contexts is not None:
        reorder_contexts_str = "\n\n".join([str(ctx) for ctx in self.reorder_contexts])
    return f"{self.question}\n\n{self.answers}\n\nContext: \n\n{contexts_str}\nReorder contexts: \n\n{reorder_contexts_str}"

Reranking

A class for reranking documents using different ranking models.

Attributes:

Name Type Description
method str

The name of the reranking method to be used.

model str

The name of the model used for reranking.

api_key str

An optional API key for accessing remote models or services.

ranker BaseRanking

An instance of a ranking model used for reranking.

Source code in rankify/models/reranking.py
class Reranking:
    """
    A class for reranking documents using different ranking models.

    Attributes:
        method (str): The name of the reranking method to be used.
        model (str): The name of the model used for reranking.
        api_key (str, optional): An optional API key for accessing remote models or services.
        ranker (BaseRanking): An instance of a ranking model used for reranking.
    """
    def __init__(self, method: str= None, model_name: str= None, api_key: str = None , **kwargs):
        """
        Initializes a Reranking instance.

        Args:
            method (str, optional): The name of the reranking method to be used. Defaults to None.
            model_name (str, optional): The name of the model to be used for reranking. Defaults to None.
            api_key (str, optional): An optional API key for accessing remote models or services. Defaults to None.
            **kwargs: Additional arguments passed to the reranking model.

        Example:
            ```python
            model = Reranking(method="listt5", model_name="listt5-base")
            ```
        """
        self.method = method
        self.model = model_name
        self.api_key= api_key
        self.kwargs = kwargs
        self.ranker = self.initialize()

    def initialize(self):
        """
        Initializes the ranking model based on the specified method and model name.

        Returns:
            BaseRanking: An instance of a ranking model.

        Raises:
            ValueError: If the specified ranking method is not supported.

        Example:
            ```python
            model = Reranking(method="listt5", model_name="listt5-base")
            ranker = model.initialize()
            ```
        """

        if self.model not in HF_PRE_DEFIND_MODELS[self.method]:
            model_name= self.model
        else:
            model_name= HF_PRE_DEFIND_MODELS[self.method][self.model]

        if self.method in METHOD_MAP:
            return METHOD_MAP[self.method](method=self.method,model_name=model_name, api_key=self.api_key, **self.kwargs)
        else:
            raise ValueError(f"Ranking method {self.method} is not supported or installed. please install the full version of Rankify")

    def rank(self, documents: list[Document]):
        """
        Reranks a list of documents using the specified ranking model.

        Args:
            documents (list[Document]): A list of Document instances that need to be reranked.

        Returns:
            list[Document]: The reranked list of Document instances.

        Example:
            ```python
            question = Question("When did Thomas Edison invent the light bulb?")
            answers = Answer(["1879"])
            contexts = [
                Context(text="Lightning strike at Seoul National University", id=1),
                Context(text="Thomas Edison invented the light bulb in 1879", id=5)
            ]
            document = Document(question=question, answers=answers, contexts=contexts)
            model = Reranking(method="listt5", model_name="listt5-base")
            model.rank([document])
            ```
        """
        return self.ranker.rank(documents=documents)

__init__(method=None, model_name=None, api_key=None, **kwargs)

Initializes a Reranking instance.

Parameters:

Name Type Description Default
method str

The name of the reranking method to be used. Defaults to None.

None
model_name str

The name of the model to be used for reranking. Defaults to None.

None
api_key str

An optional API key for accessing remote models or services. Defaults to None.

None
**kwargs

Additional arguments passed to the reranking model.

{}
Example
model = Reranking(method="listt5", model_name="listt5-base")
Source code in rankify/models/reranking.py
def __init__(self, method: str= None, model_name: str= None, api_key: str = None , **kwargs):
    """
    Initializes a Reranking instance.

    Args:
        method (str, optional): The name of the reranking method to be used. Defaults to None.
        model_name (str, optional): The name of the model to be used for reranking. Defaults to None.
        api_key (str, optional): An optional API key for accessing remote models or services. Defaults to None.
        **kwargs: Additional arguments passed to the reranking model.

    Example:
        ```python
        model = Reranking(method="listt5", model_name="listt5-base")
        ```
    """
    self.method = method
    self.model = model_name
    self.api_key= api_key
    self.kwargs = kwargs
    self.ranker = self.initialize()

initialize()

Initializes the ranking model based on the specified method and model name.

Returns:

Name Type Description
BaseRanking

An instance of a ranking model.

Raises:

Type Description
ValueError

If the specified ranking method is not supported.

Example
model = Reranking(method="listt5", model_name="listt5-base")
ranker = model.initialize()
Source code in rankify/models/reranking.py
def initialize(self):
    """
    Initializes the ranking model based on the specified method and model name.

    Returns:
        BaseRanking: An instance of a ranking model.

    Raises:
        ValueError: If the specified ranking method is not supported.

    Example:
        ```python
        model = Reranking(method="listt5", model_name="listt5-base")
        ranker = model.initialize()
        ```
    """

    if self.model not in HF_PRE_DEFIND_MODELS[self.method]:
        model_name= self.model
    else:
        model_name= HF_PRE_DEFIND_MODELS[self.method][self.model]

    if self.method in METHOD_MAP:
        return METHOD_MAP[self.method](method=self.method,model_name=model_name, api_key=self.api_key, **self.kwargs)
    else:
        raise ValueError(f"Ranking method {self.method} is not supported or installed. please install the full version of Rankify")

rank(documents)

Reranks a list of documents using the specified ranking model.

Parameters:

Name Type Description Default
documents list[Document]

A list of Document instances that need to be reranked.

required

Returns:

Type Description

list[Document]: The reranked list of Document instances.

Example
question = Question("When did Thomas Edison invent the light bulb?")
answers = Answer(["1879"])
contexts = [
    Context(text="Lightning strike at Seoul National University", id=1),
    Context(text="Thomas Edison invented the light bulb in 1879", id=5)
]
document = Document(question=question, answers=answers, contexts=contexts)
model = Reranking(method="listt5", model_name="listt5-base")
model.rank([document])
Source code in rankify/models/reranking.py
def rank(self, documents: list[Document]):
    """
    Reranks a list of documents using the specified ranking model.

    Args:
        documents (list[Document]): A list of Document instances that need to be reranked.

    Returns:
        list[Document]: The reranked list of Document instances.

    Example:
        ```python
        question = Question("When did Thomas Edison invent the light bulb?")
        answers = Answer(["1879"])
        contexts = [
            Context(text="Lightning strike at Seoul National University", id=1),
            Context(text="Thomas Edison invented the light bulb in 1879", id=5)
        ]
        document = Document(question=question, answers=answers, contexts=contexts)
        model = Reranking(method="listt5", model_name="listt5-base")
        model.rank([document])
        ```
    """
    return self.ranker.rank(documents=documents)