Skip to content

Retrievers

Base Retriever.

BaseRetriever

Bases: BaseModel, ABC

Base Retriever Class.

This abstract class provides the interface for creating Retriever objects that encode strings into numerical vector representations.

Source code in src/fed_rag/base/retriever.py
class BaseRetriever(BaseModel, ABC):
    """Base Retriever Class.

    This abstract class provides the interface for creating Retriever objects that
    encode strings into numerical vector representations.
    """

    model_config = ConfigDict(arbitrary_types_allowed=True)

    @abstractmethod
    def encode_query(
        self, query: str | list[str], **kwargs: Any
    ) -> torch.Tensor:
        """Encode a string query into a torch.Tensor.

        Args:
            query (str | list[str]): The query or list of queries to encode.

        Returns:
            torch.Tensor: The vector representation(s) of the encoded query/queries.
        """

    @abstractmethod
    def encode_context(
        self, context: str | list[str], **kwargs: Any
    ) -> torch.Tensor:
        """Encode a string context into a torch.Tensor.

        Args:
            context (str | list[str]): The context or list of contexts to encode.

        Returns:
            torch.Tensor: The vector representation(s) of the encoded context(s).
        """

    @property
    @abstractmethod
    def encoder(self) -> torch.nn.Module | None:
        """PyTorch model associated with the encoder associated with retriever."""

    @property
    @abstractmethod
    def query_encoder(self) -> torch.nn.Module | None:
        """PyTorch model associated with the query encoder associated with retriever."""

    @property
    @abstractmethod
    def context_encoder(self) -> torch.nn.Module | None:
        """PyTorch model associated with the context encoder associated with retriever."""

encoder abstractmethod property

encoder

PyTorch model associated with the encoder associated with retriever.

query_encoder abstractmethod property

query_encoder

PyTorch model associated with the query encoder associated with retriever.

context_encoder abstractmethod property

context_encoder

PyTorch model associated with the context encoder associated with retriever.

encode_query abstractmethod

encode_query(query, **kwargs)

Encode a string query into a torch.Tensor.

Parameters:

Name Type Description Default
query str | list[str]

The query or list of queries to encode.

required

Returns:

Type Description
Tensor

torch.Tensor: The vector representation(s) of the encoded query/queries.

Source code in src/fed_rag/base/retriever.py
@abstractmethod
def encode_query(
    self, query: str | list[str], **kwargs: Any
) -> torch.Tensor:
    """Encode a string query into a torch.Tensor.

    Args:
        query (str | list[str]): The query or list of queries to encode.

    Returns:
        torch.Tensor: The vector representation(s) of the encoded query/queries.
    """

encode_context abstractmethod

encode_context(context, **kwargs)

Encode a string context into a torch.Tensor.

Parameters:

Name Type Description Default
context str | list[str]

The context or list of contexts to encode.

required

Returns:

Type Description
Tensor

torch.Tensor: The vector representation(s) of the encoded context(s).

Source code in src/fed_rag/base/retriever.py
@abstractmethod
def encode_context(
    self, context: str | list[str], **kwargs: Any
) -> torch.Tensor:
    """Encode a string context into a torch.Tensor.

    Args:
        context (str | list[str]): The context or list of contexts to encode.

    Returns:
        torch.Tensor: The vector representation(s) of the encoded context(s).
    """