Skip to content

Base KnowledgeStore

Base Knowledge Store

BaseKnowledgeStore

Bases: BaseModel, ABC

Base Knowledge Store Class.

Source code in src/fed_rag/base/knowledge_store.py
class BaseKnowledgeStore(BaseModel, ABC):
    """Base Knowledge Store Class."""

    model_config = ConfigDict(arbitrary_types_allowed=True)
    name: str = Field(
        description="Name of Knowledge Store used for caching and loading.",
        default=DEFAULT_KNOWLEDGE_STORE_NAME,
    )

    @abstractmethod
    def load_node(self, node: KnowledgeNode) -> None:
        """Load a KnowledgeNode into the KnowledgeStore."""

    @abstractmethod
    def load_nodes(self, nodes: list[KnowledgeNode]) -> None:
        """Load multiple KnowledgeNodes in batch."""

    @abstractmethod
    def retrieve(
        self, query_emb: list[float], top_k: int
    ) -> list[tuple[float, KnowledgeNode]]:
        """Retrieve top-k nodes from KnowledgeStore against a provided user query.

        Returns:
            A list of tuples where the first element represents the similarity score
            of the node to the query, and the second element is the node itself.
        """

    @abstractmethod
    def delete_node(self, node_id: str) -> bool:
        """Remove a node from the KnowledgeStore by ID, returning success status."""

    @abstractmethod
    def clear(self) -> None:
        """Clear all nodes from the KnowledgeStore."""

    @property
    @abstractmethod
    def count(self) -> int:
        """Return the number of nodes in the store."""

    @abstractmethod
    def persist(self) -> None:
        """Save the KnowledgeStore nodes to a permanent storage."""

    @abstractmethod
    def load(self) -> None:
        """
        Load the KnowledgeStore nodes from a permanent storage using `name`.

        Args:
            ks_id: The id of the knowledge store to load.
        """

count abstractmethod property

count

Return the number of nodes in the store.

load_node abstractmethod

load_node(node)

Load a KnowledgeNode into the KnowledgeStore.

Source code in src/fed_rag/base/knowledge_store.py
@abstractmethod
def load_node(self, node: KnowledgeNode) -> None:
    """Load a KnowledgeNode into the KnowledgeStore."""

load_nodes abstractmethod

load_nodes(nodes)

Load multiple KnowledgeNodes in batch.

Source code in src/fed_rag/base/knowledge_store.py
@abstractmethod
def load_nodes(self, nodes: list[KnowledgeNode]) -> None:
    """Load multiple KnowledgeNodes in batch."""

retrieve abstractmethod

retrieve(query_emb, top_k)

Retrieve top-k nodes from KnowledgeStore against a provided user query.

Returns:

Type Description
list[tuple[float, KnowledgeNode]]

A list of tuples where the first element represents the similarity score

list[tuple[float, KnowledgeNode]]

of the node to the query, and the second element is the node itself.

Source code in src/fed_rag/base/knowledge_store.py
@abstractmethod
def retrieve(
    self, query_emb: list[float], top_k: int
) -> list[tuple[float, KnowledgeNode]]:
    """Retrieve top-k nodes from KnowledgeStore against a provided user query.

    Returns:
        A list of tuples where the first element represents the similarity score
        of the node to the query, and the second element is the node itself.
    """

delete_node abstractmethod

delete_node(node_id)

Remove a node from the KnowledgeStore by ID, returning success status.

Source code in src/fed_rag/base/knowledge_store.py
@abstractmethod
def delete_node(self, node_id: str) -> bool:
    """Remove a node from the KnowledgeStore by ID, returning success status."""

clear abstractmethod

clear()

Clear all nodes from the KnowledgeStore.

Source code in src/fed_rag/base/knowledge_store.py
@abstractmethod
def clear(self) -> None:
    """Clear all nodes from the KnowledgeStore."""

persist abstractmethod

persist()

Save the KnowledgeStore nodes to a permanent storage.

Source code in src/fed_rag/base/knowledge_store.py
@abstractmethod
def persist(self) -> None:
    """Save the KnowledgeStore nodes to a permanent storage."""

load abstractmethod

load()

Load the KnowledgeStore nodes from a permanent storage using name.

Parameters:

Name Type Description Default
ks_id

The id of the knowledge store to load.

required
Source code in src/fed_rag/base/knowledge_store.py
@abstractmethod
def load(self) -> None:
    """
    Load the KnowledgeStore nodes from a permanent storage using `name`.

    Args:
        ks_id: The id of the knowledge store to load.
    """