Skip to content

ReAct RAG

rankify.generator.rag_methods.react_rag

BaseRAGModel

Bases: ABC

Base RAG Model for Retrieval-Augmented Generation (RAG).

This is an abstract base class for implementing LLM endpoints in rankify. It defines the interface for generating responses and optional embedding generation.

Methods:

Name Description
generate

str, **kwargs) -> str: Abstract method to generate a response based on the given prompt.

embed

str, **kwargs) -> List[float]: Optional method to generate embeddings for the given text.

Notes
  • This class serves as a blueprint for RAG models like OpenAIModel and HuggingFaceModel.
  • The embed method is optional and can be implemented if needed.
  • This class needs to be extended to include new LLM endpoints in Rankify.
Source code in rankify/generator/models/base_rag_model.py
class BaseRAGModel(ABC):
    """
    **Base RAG Model** for Retrieval-Augmented Generation (RAG).

    This is an abstract base class for implementing LLM endpoints in rankify. 
    It defines the interface for generating responses and optional embedding generation.

    Methods:
        generate(prompt: str, **kwargs) -> str:
            Abstract method to generate a response based on the given prompt.
        embed(text: str, **kwargs) -> List[float]:
            Optional method to generate embeddings for the given text.

    Notes:
        - This class serves as a blueprint for RAG models like `OpenAIModel` and `HuggingFaceModel`.
        - The `embed` method is optional and can be implemented if needed.
        - This class needs to be extended to include new LLM endpoints in Rankify.
    """

    @abstractmethod
    def generate(self, prompt: str, **kwargs) -> str:
        """Generate a response based on the given prompt."""
        pass

    def embed(self, text: str, **kwargs) -> List[float]:
        """Optional: Generate embeddings for the given text."""
        raise NotImplementedError("Embedding is not required for this implementation.")

generate(prompt, **kwargs) abstractmethod

Generate a response based on the given prompt.

Source code in rankify/generator/models/base_rag_model.py
@abstractmethod
def generate(self, prompt: str, **kwargs) -> str:
    """Generate a response based on the given prompt."""
    pass

embed(text, **kwargs)

Optional: Generate embeddings for the given text.

Source code in rankify/generator/models/base_rag_model.py
def embed(self, text: str, **kwargs) -> List[float]:
    """Optional: Generate embeddings for the given text."""
    raise NotImplementedError("Embedding is not required for this implementation.")

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}"

BaseRAGMethod

Bases: ABC

Base RAG Method for Retrieval-Augmented Generation (RAG) techniques.

This abstract base class defines the blueprint for implementing RAG methods in Rankify. Each RAG method (e.g., zero-shot, chain-of-thought, Fusion-in-Decoder) should inherit from this class and implement the logic for answering questions using a provided RAG model.

Attributes:

Name Type Description
model BaseRAGModel

The RAG model instance used for generation.

Methods:

Name Description
answer_questions

List[Document], custom_prompt=None, **kwargs) -> List[str]: Abstract method to answer questions based on a list of documents and optional custom prompt.

Notes
  • Extend this class to implement new RAG techniques or strategies.
  • The answer_questions method must be implemented by all subclasses.
  • This class enables modularity and extensibility for different retrieval-augmented generation approaches.
Source code in rankify/generator/rag_methods/base_rag_method.py
class BaseRAGMethod(ABC):
    """
    **Base RAG Method** for Retrieval-Augmented Generation (RAG) techniques.

    This abstract base class defines the blueprint for implementing RAG methods in Rankify.
    Each RAG method (e.g., zero-shot, chain-of-thought, Fusion-in-Decoder) should inherit from this class
    and implement the logic for answering questions using a provided RAG model.

    Attributes:
        model (BaseRAGModel): The RAG model instance used for generation.

    Methods:
        answer_questions(documents: List[Document], custom_prompt=None, **kwargs) -> List[str]:
            Abstract method to answer questions based on a list of documents and optional custom prompt.

    Notes:
        - Extend this class to implement new RAG techniques or strategies.
        - The `answer_questions` method must be implemented by all subclasses.
        - This class enables modularity and extensibility for different retrieval-augmented generation approaches.
    """
    def __init__(self, model: BaseRAGModel, **kwargs):
        """
        Initialize the BaseRAGMethod.

        Args:
            model (BaseRAGModel): The RAG model instance used for generation.
            **kwargs: Additional configuration parameters for the RAG method.
        """
        self.model = model

    @abstractmethod
    def answer_questions(self, documents: List[Document], custom_prompt=None, **kwargs) -> List[str]:
        """
        Abstract method to answer questions based on a list of documents.

        Args:
            documents (List[Document]): List of Document objects containing questions and contexts.
            custom_prompt (str, optional): Custom prompt to override default prompt generation.
            **kwargs: Additional parameters for the answering logic.

        Returns:
            List[str]: List of generated answers, one per document.

        Notes:
            - Must be implemented by subclasses to define the RAG technique's answering logic.
            - Enables flexible integration of different prompting or generation strategies.
        """
        pass

__init__(model, **kwargs)

Initialize the BaseRAGMethod.

Parameters:

Name Type Description Default
model BaseRAGModel

The RAG model instance used for generation.

required
**kwargs

Additional configuration parameters for the RAG method.

{}
Source code in rankify/generator/rag_methods/base_rag_method.py
def __init__(self, model: BaseRAGModel, **kwargs):
    """
    Initialize the BaseRAGMethod.

    Args:
        model (BaseRAGModel): The RAG model instance used for generation.
        **kwargs: Additional configuration parameters for the RAG method.
    """
    self.model = model

answer_questions(documents, custom_prompt=None, **kwargs) abstractmethod

Abstract method to answer questions based on a list of documents.

Parameters:

Name Type Description Default
documents List[Document]

List of Document objects containing questions and contexts.

required
custom_prompt str

Custom prompt to override default prompt generation.

None
**kwargs

Additional parameters for the answering logic.

{}

Returns:

Type Description
List[str]

List[str]: List of generated answers, one per document.

Notes
  • Must be implemented by subclasses to define the RAG technique's answering logic.
  • Enables flexible integration of different prompting or generation strategies.
Source code in rankify/generator/rag_methods/base_rag_method.py
@abstractmethod
def answer_questions(self, documents: List[Document], custom_prompt=None, **kwargs) -> List[str]:
    """
    Abstract method to answer questions based on a list of documents.

    Args:
        documents (List[Document]): List of Document objects containing questions and contexts.
        custom_prompt (str, optional): Custom prompt to override default prompt generation.
        **kwargs: Additional parameters for the answering logic.

    Returns:
        List[str]: List of generated answers, one per document.

    Notes:
        - Must be implemented by subclasses to define the RAG technique's answering logic.
        - Enables flexible integration of different prompting or generation strategies.
    """
    pass

ReActRAG

Bases: BaseRAGMethod

**ReActRAG (Reason+Act RAG) Method** for Retrieval-Augmented Generation.

Implements the ReAct technique, combining reasoning and action steps for open-domain question answering.
The model alternates between generating reasoning steps and issuing search actions to a retriever, iteratively building up context and history until a final answer is produced.

Attributes:
    model (BaseRAGModel): The RAG model instance used for generation.
    retriever: An external retriever instance for fetching new contexts.
    max_steps (int): Maximum number of reasoning/search steps per question (default: 20).
    max_contexts_per_search (int): Maximum number of contexts to add per search action (default: 3).
    use_internal_knowledge (bool): Whether to fallback to internal knowledge if no answer is found (default: True).

Methods:
    __init__(model, retriever, max_steps=20, max_contexts_per_search=3, use_internal_knowledge=True, **kwargs):
        Initializes the ReActRAG method with the provided model and retriever.
    answer_questions(documents: List[Document], custom_prompt=None, **kwargs) -> List[str]:
        Answers questions for a list of documents using iterative reasoning and retrieval.

Notes:
    - The method parses model outputs for "Search[query]" actions and "Final Answer:" statements.
    - Retrieved contexts are appended to the prompt and history for subsequent steps.
    - If no final answer is found, optionally falls back to internal knowledge or last output.
    - Suitable for complex questions requiring multi-step reasoning and dynamic retrieval.

Example:
    ```python
    import torch
    from rankify.dataset.dataset import Document, Question, Answer, Context
    from rankify.generator.generator import Generator
    from rankify.n_retreivers.retriever import Retriever
    question = Question("Who won the FIFA World Cup after Germany in 2014?")
    answers = Answer("")

    contexts = [
        Context(id=1, title="2014 FIFA World Cup", text="Germany won the FIFA World Cup in 2014, held in Brazil.", score=0.9),
        Context(id=2, title="FIFA World Cup History", text="The FIFA World Cup is held every four years, with different countries winning each time.", score=0.8),
        Context(id=3, title="World Cup Winners", text="The winners of the FIFA World Cup are celebrated globally.", score=0.7),
    ]

    docs = [Document(question=question, answers=answers, contexts=contexts)]

    # Initialize retriever (example: BM25, can be any retriever compatible with your pipeline)
    retriever = Retriever(method='bm25', n_docs=5, index_type='wiki')
    retrieved_documents = retriever.retrieve(docs)
    for i, doc in enumerate(retrieved_documents):
         print(f"

Document {i+1}:") print(doc)

    # Initialize Generator for ReAct RAG
    generator = Generator(
        method="react-rag",
        model_name='meta-llama/Meta-Llama-3.1-8B-Instruct',
        backend="huggingface",
        torch_dtype=torch.float16,
        retriever=retriever,
        stop_at_period=True
    )

    # Generate answer
    generated_answers = generator.generate(test_docs)
    print(generated_answers)  # Expected output: ["France"]
    ```

References:
    - Yao et al. *ReAct: Synergizing Reasoning and Acting in Language Models*  
      [Paper](https://arxiv.org/abs/2210.03629)
Source code in rankify/generator/rag_methods/react_rag.py
class ReActRAG(BaseRAGMethod):
    """
    **ReActRAG (Reason+Act RAG) Method** for Retrieval-Augmented Generation.

    Implements the ReAct technique, combining reasoning and action steps for open-domain question answering.
    The model alternates between generating reasoning steps and issuing search actions to a retriever, iteratively building up context and history until a final answer is produced.

    Attributes:
        model (BaseRAGModel): The RAG model instance used for generation.
        retriever: An external retriever instance for fetching new contexts.
        max_steps (int): Maximum number of reasoning/search steps per question (default: 20).
        max_contexts_per_search (int): Maximum number of contexts to add per search action (default: 3).
        use_internal_knowledge (bool): Whether to fallback to internal knowledge if no answer is found (default: True).

    Methods:
        __init__(model, retriever, max_steps=20, max_contexts_per_search=3, use_internal_knowledge=True, **kwargs):
            Initializes the ReActRAG method with the provided model and retriever.
        answer_questions(documents: List[Document], custom_prompt=None, **kwargs) -> List[str]:
            Answers questions for a list of documents using iterative reasoning and retrieval.

    Notes:
        - The method parses model outputs for "Search[query]" actions and "Final Answer:" statements.
        - Retrieved contexts are appended to the prompt and history for subsequent steps.
        - If no final answer is found, optionally falls back to internal knowledge or last output.
        - Suitable for complex questions requiring multi-step reasoning and dynamic retrieval.

    Example:
        ```python
        import torch
        from rankify.dataset.dataset import Document, Question, Answer, Context
        from rankify.generator.generator import Generator
        from rankify.n_retreivers.retriever import Retriever
        question = Question("Who won the FIFA World Cup after Germany in 2014?")
        answers = Answer("")

        contexts = [
            Context(id=1, title="2014 FIFA World Cup", text="Germany won the FIFA World Cup in 2014, held in Brazil.", score=0.9),
            Context(id=2, title="FIFA World Cup History", text="The FIFA World Cup is held every four years, with different countries winning each time.", score=0.8),
            Context(id=3, title="World Cup Winners", text="The winners of the FIFA World Cup are celebrated globally.", score=0.7),
        ]

        docs = [Document(question=question, answers=answers, contexts=contexts)]

        # Initialize retriever (example: BM25, can be any retriever compatible with your pipeline)
        retriever = Retriever(method='bm25', n_docs=5, index_type='wiki')
        retrieved_documents = retriever.retrieve(docs)
        for i, doc in enumerate(retrieved_documents):
             print(f"\nDocument {i+1}:")
             print(doc)

        # Initialize Generator for ReAct RAG
        generator = Generator(
            method="react-rag",
            model_name='meta-llama/Meta-Llama-3.1-8B-Instruct',
            backend="huggingface",
            torch_dtype=torch.float16,
            retriever=retriever,
            stop_at_period=True
        )

        # Generate answer
        generated_answers = generator.generate(test_docs)
        print(generated_answers)  # Expected output: ["France"]
        ```

    References:
        - Yao et al. *ReAct: Synergizing Reasoning and Acting in Language Models*  
          [Paper](https://arxiv.org/abs/2210.03629)
    """

    def __init__(self, model: BaseRAGModel, retriever, max_steps: int = 20, max_contexts_per_search: int = 3, use_internal_knowledge: bool = True, **kwargs):
        """
        Initialize the ReActRAG method.

        Args:
            model (BaseRAGModel): The RAG model instance used for generation.
            retriever: An external retriever instance for fetching new contexts.
            max_steps (int, optional): Maximum number of reasoning/search steps per question (default: 20).
            max_contexts_per_search (int, optional): Maximum number of contexts to add per search action (default: 3).
            use_internal_knowledge (bool, optional): Whether to fallback to internal knowledge if no answer is found (default: True).
        """
        super().__init__(model=model)
        self.retriever = retriever  # Pass a retriever instance
        self.max_steps = max_steps
        self.max_contexts_per_search = max_contexts_per_search
        self.use_internal_knowledge = use_internal_knowledge

    def answer_questions(self, documents: List[Document], custom_prompt: Optional[str] = None, **kwargs) -> List[str]:
        """
        Answer questions for a list of documents using the ReAct reasoning and retrieval loop.

        Args:
            documents (List[Document]): A list of Document objects containing questions and contexts.
            custom_prompt (str, optional): Custom prompt to override default prompt generation.
            **kwargs: Additional parameters for the model's generate method.

        Returns:
            List[str]: Answers generated for each document, using iterative reasoning and retrieval.

        Notes:
            - Iteratively builds up history and context by parsing model outputs for reasoning and search actions.
            - Stops when a "Final Answer:" is obtained or max_steps is reached.
            - If no final answer is found, optionally falls back to internal knowledge (if self.use_internal_knowledge=True) or last output.
        """
        answers = []
        for document in documents:
            question = document.question.question
            print("answering question: " + document.question.question)
            contexts = [context.text for context in document.contexts]
            history = []
            final_answer = None
            for step in range(self.max_steps):
                prompt = self.model.prompt_generator.generate_user_prompt(
                    question, contexts, custom_prompt=custom_prompt
                )
                # Add history to the prompt
                if history:
                    prompt += "\n" + "\n".join(history)
                output = self.model.generate(prompt=prompt, **kwargs)
                # Check for Final Answer
                final_match = re.search(r"Final Answer:\s*(.*)", output)
                if final_match:
                    final_answer = final_match.group(1).strip()
                    break
                # Parse for Search action
                search_match = re.search(r"Search\[(.*?)\]", output)
                if search_match:
                    query = search_match.group(1)
                    # Use retriever to get new contexts (limit by max_contexts_per_search)
                    retrieved_docs = self.retriever.retrieve([Document(question=document.question, answers=document.answers, contexts=[])])
                    if retrieved_docs and retrieved_docs[0].contexts:
                        obs_texts = []
                        for ctx in retrieved_docs[0].contexts[:self.max_contexts_per_search]:
                            obs_texts.append(ctx.text)
                            contexts.append(ctx.text)
                        obs = "Observation: " + " ".join(obs_texts)
                        history.append(f"Search[{query}]\n{obs}")
                else:
                    # If neither Search nor Final Answer, add reasoning to history
                    history.append(output)
            # If final answer found, use it
            if final_answer is not None:
                answers.append(final_answer)
            else:
                # Fallback to internal knowledge if flag is set
                if self.use_internal_knowledge:
                    internal_prompt = (
                        "You are a knowledgeable assistant. Answer the following question using only your internal knowledge.\n"
                        f"Question: {question}\n"
                        "Answer:"
                    )
                    internal_output = self.model.generate(prompt=internal_prompt, **kwargs)
                    answers.append(internal_output)
                else:
                    # Else answer with last output
                    answers.append(output)
        return answers

__init__(model, retriever, max_steps=20, max_contexts_per_search=3, use_internal_knowledge=True, **kwargs)

Initialize the ReActRAG method.

Parameters:

Name Type Description Default
model BaseRAGModel

The RAG model instance used for generation.

required
retriever

An external retriever instance for fetching new contexts.

required
max_steps int

Maximum number of reasoning/search steps per question (default: 20).

20
max_contexts_per_search int

Maximum number of contexts to add per search action (default: 3).

3
use_internal_knowledge bool

Whether to fallback to internal knowledge if no answer is found (default: True).

True
Source code in rankify/generator/rag_methods/react_rag.py
def __init__(self, model: BaseRAGModel, retriever, max_steps: int = 20, max_contexts_per_search: int = 3, use_internal_knowledge: bool = True, **kwargs):
    """
    Initialize the ReActRAG method.

    Args:
        model (BaseRAGModel): The RAG model instance used for generation.
        retriever: An external retriever instance for fetching new contexts.
        max_steps (int, optional): Maximum number of reasoning/search steps per question (default: 20).
        max_contexts_per_search (int, optional): Maximum number of contexts to add per search action (default: 3).
        use_internal_knowledge (bool, optional): Whether to fallback to internal knowledge if no answer is found (default: True).
    """
    super().__init__(model=model)
    self.retriever = retriever  # Pass a retriever instance
    self.max_steps = max_steps
    self.max_contexts_per_search = max_contexts_per_search
    self.use_internal_knowledge = use_internal_knowledge

answer_questions(documents, custom_prompt=None, **kwargs)

Answer questions for a list of documents using the ReAct reasoning and retrieval loop.

Parameters:

Name Type Description Default
documents List[Document]

A list of Document objects containing questions and contexts.

required
custom_prompt str

Custom prompt to override default prompt generation.

None
**kwargs

Additional parameters for the model's generate method.

{}

Returns:

Type Description
List[str]

List[str]: Answers generated for each document, using iterative reasoning and retrieval.

Notes
  • Iteratively builds up history and context by parsing model outputs for reasoning and search actions.
  • Stops when a "Final Answer:" is obtained or max_steps is reached.
  • If no final answer is found, optionally falls back to internal knowledge (if self.use_internal_knowledge=True) or last output.
Source code in rankify/generator/rag_methods/react_rag.py
def answer_questions(self, documents: List[Document], custom_prompt: Optional[str] = None, **kwargs) -> List[str]:
    """
    Answer questions for a list of documents using the ReAct reasoning and retrieval loop.

    Args:
        documents (List[Document]): A list of Document objects containing questions and contexts.
        custom_prompt (str, optional): Custom prompt to override default prompt generation.
        **kwargs: Additional parameters for the model's generate method.

    Returns:
        List[str]: Answers generated for each document, using iterative reasoning and retrieval.

    Notes:
        - Iteratively builds up history and context by parsing model outputs for reasoning and search actions.
        - Stops when a "Final Answer:" is obtained or max_steps is reached.
        - If no final answer is found, optionally falls back to internal knowledge (if self.use_internal_knowledge=True) or last output.
    """
    answers = []
    for document in documents:
        question = document.question.question
        print("answering question: " + document.question.question)
        contexts = [context.text for context in document.contexts]
        history = []
        final_answer = None
        for step in range(self.max_steps):
            prompt = self.model.prompt_generator.generate_user_prompt(
                question, contexts, custom_prompt=custom_prompt
            )
            # Add history to the prompt
            if history:
                prompt += "\n" + "\n".join(history)
            output = self.model.generate(prompt=prompt, **kwargs)
            # Check for Final Answer
            final_match = re.search(r"Final Answer:\s*(.*)", output)
            if final_match:
                final_answer = final_match.group(1).strip()
                break
            # Parse for Search action
            search_match = re.search(r"Search\[(.*?)\]", output)
            if search_match:
                query = search_match.group(1)
                # Use retriever to get new contexts (limit by max_contexts_per_search)
                retrieved_docs = self.retriever.retrieve([Document(question=document.question, answers=document.answers, contexts=[])])
                if retrieved_docs and retrieved_docs[0].contexts:
                    obs_texts = []
                    for ctx in retrieved_docs[0].contexts[:self.max_contexts_per_search]:
                        obs_texts.append(ctx.text)
                        contexts.append(ctx.text)
                    obs = "Observation: " + " ".join(obs_texts)
                    history.append(f"Search[{query}]\n{obs}")
            else:
                # If neither Search nor Final Answer, add reasoning to history
                history.append(output)
        # If final answer found, use it
        if final_answer is not None:
            answers.append(final_answer)
        else:
            # Fallback to internal knowledge if flag is set
            if self.use_internal_knowledge:
                internal_prompt = (
                    "You are a knowledgeable assistant. Answer the following question using only your internal knowledge.\n"
                    f"Question: {question}\n"
                    "Answer:"
                )
                internal_output = self.model.generate(prompt=internal_prompt, **kwargs)
                answers.append(internal_output)
            else:
                # Else answer with last output
                answers.append(output)
    return answers