Search code examples
vectorvectorizationlangchainlarge-language-modelpy-langchain

Using Langchain with custom retrievers


I have this requirement, where i want to create a knowledge retriver which will call the API to get the closest matching information, I know that we have these integrations in langchain with multiple vector stores, but we have requirement were we have to call the API to find the closest matching document how can we create our custom retriver in langchain which will call this API to get the nearest matching informtaion

I'm trying to build the custom retriver in langchain but still not able figure it out


Solution

  • custom Retriever:

    class URRetrival(BaseRetriever):
    
    def __int__(self):
        pass
    
    def _get_relevant_documents(
            self, query: str, *, run_manager: CallbackManagerForRetrieverRun
    ) -> List[Document]:
        # response = URAPI(request)
        # convert response (json or xml) in to langchain Document like  doc = Document(page_content="response docs")
        # dump all those result in array of docs and return below
        return result_docs
    
    async def _aget_relevant_documents(
            self,
            query: str,
            *,
            run_manager: AsyncCallbackManagerForRetrieverRun,
            **kwargs: Any,
    ) -> List[Document]:
        raise NotImplementedError()
        
            
    

    URRetrival : will be your Retrival name _get_relevant_documents : will call when your chain run and its looking for relevant docs

    now you can add any kind of implementation inside _get_relevant_documents method and return whatever relevant for you