Source code for ibm_watsonx_ai.foundation_models.extensions.rag.retriever.base_retriever
# -----------------------------------------------------------------------------------------# (C) Copyright IBM Corp. 2024.# https://opensource.org/licenses/BSD-3-Clause# -----------------------------------------------------------------------------------------fromabcimportABC,abstractmethodfromtypingimportAnyfromibm_watsonx_ai.foundation_models.extensions.rag.vector_stores.base_vector_storeimport(BaseVectorStore,)fromibm_watsonx_ai.wml_client_errorimportMissingExtensiontry:fromlangchain_core.documentsimportDocumentexceptImportError:raiseMissingExtension("langchain")
[docs]classBaseRetriever(ABC):"""Abstract class for all retriever handlers for the chosen vector store. Returns some document chunks in a RAG pipeline using a concrete ``retrieve`` implementation. :param vector_store: vector store used in document retrieval :type vector_store: BaseVectorStore """def__init__(self,vector_store:BaseVectorStore)->None:super().__init__()self.vector_store:BaseVectorStore=vector_store
[docs]@abstractmethoddefretrieve(self,query:str,**kwargs:Any)->list[Document]:"""Retrieve elements from the vector store using the provided `query`. :param query: text query to be used for searching :type query: str :return: list of retrieved LangChain documents :rtype: list[langchain_core.documents.Document] """raiseNotImplementedError
[docs]defto_dict(self)->dict[str,Any]:"""Serializes the ``init_parameters`` retriever so it can be reconstructed by the ``from_vector_store`` class method. :return: serialized ``init_parameters`` :rtype: dict """raiseNotImplementedError
[docs]@classmethod@abstractmethoddeffrom_vector_store(cls,vector_store:BaseVectorStore,init_parameters:dict[str,Any]|None=None,)->"BaseRetriever":"""Deserializes the ``init_parameters`` retriever into a concrete one using arguments. :param vector_store: vector store used to create the retriever :type vector_store: BaseVectorStore :param init_parameters: parameters to initialize the retriever with :type init_parameters: dict[str, Any] :return: concrete Retriever or None if data is incorrect :rtype: BaseRetriever | None """raiseNotImplementedError