[docs]classRetrieversIntegrationTests(BaseStandardTests):""" Base class for retrievers integration tests. """@property@abstractmethoddefretriever_constructor(self)->Type[BaseRetriever]:""" A BaseRetriever subclass to be tested. """...@propertydefretriever_constructor_params(self)->dict:""" Returns a dictionary of parameters to pass to the retriever constructor. """return{}@property@abstractmethoddefretriever_query_example(self)->str:""" Returns a str representing the "query" of an example retriever call. """...@pytest.fixturedefretriever(self)->BaseRetriever:""" :private: """returnself.retriever_constructor(**self.retriever_constructor_params)
[docs]deftest_k_constructor_param(self)->None:""" Test that the retriever constructor accepts a k parameter, representing the number of documents to return. .. dropdown:: Troubleshooting If this test fails, either the retriever constructor does not accept a k parameter, or the retriever does not return the correct number of documents (`k`) when it is set. For example, a retriever like .. code-block:: python MyRetriever(k=3).invoke("query") should return 3 documents when invoked with a query. """params={k:vfork,vinself.retriever_constructor_params.items()ifk!="k"}params_3={**params,"k":3}retriever_3=self.retriever_constructor(**params_3)result_3=retriever_3.invoke(self.retriever_query_example)assertlen(result_3)==3assertall(isinstance(doc,Document)fordocinresult_3)params_1={**params,"k":1}retriever_1=self.retriever_constructor(**params_1)result_1=retriever_1.invoke(self.retriever_query_example)assertlen(result_1)==1assertall(isinstance(doc,Document)fordocinresult_1)
[docs]deftest_invoke_with_k_kwarg(self,retriever:BaseRetriever)->None:""" Test that the invoke method accepts a k parameter, representing the number of documents to return. .. dropdown:: Troubleshooting If this test fails, the retriever's invoke method does not accept a k parameter, or the retriever does not return the correct number of documents (`k`) when it is set. For example, a retriever like .. code-block:: python MyRetriever().invoke("query", k=3) should return 3 documents when invoked with a query. """result_1=retriever.invoke(self.retriever_query_example,k=1)assertlen(result_1)==1assertall(isinstance(doc,Document)fordocinresult_1)result_3=retriever.invoke(self.retriever_query_example,k=3)assertlen(result_3)==3assertall(isinstance(doc,Document)fordocinresult_3)
[docs]deftest_invoke_returns_documents(self,retriever:BaseRetriever)->None:""" If invoked with the example params, the retriever should return a list of Documents. .. dropdown:: Troubleshooting If this test fails, the retriever's invoke method does not return a list of `langchain_core.document.Document` objects. Please confirm that your `_get_relevant_documents` method returns a list of `Document` objects. """result=retriever.invoke(self.retriever_query_example)assertisinstance(result,list)assertall(isinstance(doc,Document)fordocinresult)
[docs]asyncdeftest_ainvoke_returns_documents(self,retriever:BaseRetriever)->None:""" If ainvoked with the example params, the retriever should return a list of Documents. See :meth:`test_invoke_returns_documents` for more information on troubleshooting. """result=awaitretriever.ainvoke(self.retriever_query_example)assertisinstance(result,list)assertall(isinstance(doc,Document)fordocinresult)