@@ -24,23 +24,49 @@ def retriever_constructor_params(self) -> dict:
24
24
@property
25
25
@abstractmethod
26
26
def retriever_query_example (self ) -> str :
27
- """Returns a str representing the "query" of an example retriever call."""
28
- ...
27
+ """Returns a str representing the ``query`` of an example retriever call."""
28
+
29
+ @property
30
+ def num_results_arg_name (self ) -> str :
31
+ """Returns the name of the parameter for the number of results returned.
32
+
33
+ Usually something like ``k`` or ``top_k``."""
34
+ return "k"
29
35
30
36
@pytest .fixture
31
37
def retriever (self ) -> BaseRetriever :
32
38
""":private:"""
33
39
return self .retriever_constructor (** self .retriever_constructor_params )
34
40
35
41
def test_k_constructor_param (self ) -> None :
36
- """Test that the retriever constructor accepts a k parameter, representing
42
+ """Test the number of results constructor parameter.
43
+
44
+ Test that the retriever constructor accepts a parameter representing
37
45
the number of documents to return.
38
46
47
+ By default, the parameter tested is named ``k``, but it can be overridden by
48
+ setting the ``num_results_arg_name`` property.
49
+
50
+ .. note::
51
+ If the retriever doesn't support configuring the number of results returned
52
+ via the constructor, this test can be skipped using a pytest ``xfail`` on
53
+ the test class:
54
+
55
+ .. code-block:: python
56
+
57
+ @pytest.mark.xfail(
58
+ reason="This retriever doesn't support setting "
59
+ "the number of results via the constructor."
60
+ )
61
+ def test_k_constructor_param(self) -> None:
62
+ raise NotImplementedError
63
+
39
64
.. dropdown:: Troubleshooting
40
65
41
- If this test fails, either the retriever constructor does not accept a k
42
- parameter, or the retriever does not return the correct number of documents
43
- (`k`) when it is set.
66
+ If this test fails, the retriever constructor does not accept a number
67
+ of results parameter, or the retriever does not return the correct number
68
+ of documents ( of the one set in ``num_results_arg_name``) when it is
69
+ set.
44
70
45
71
For example, a retriever like
46
72
@@ -52,29 +78,51 @@ def test_k_constructor_param(self) -> None:
52
78
53
79
"""
54
80
params = {
55
- k : v for k , v in self .retriever_constructor_params .items () if k != "k"
81
+ k : v
82
+ for k , v in self .retriever_constructor_params .items ()
83
+ if k != self .num_results_arg_name
56
84
}
57
- params_3 = {** params , "k" : 3 }
85
+ params_3 = {** params , self . num_results_arg_name : 3 }
58
86
retriever_3 = self .retriever_constructor (** params_3 )
59
87
result_3 = retriever_3 .invoke (self .retriever_query_example )
60
88
assert len (result_3 ) == 3
61
89
assert all (isinstance (doc , Document ) for doc in result_3 )
62
90
63
- params_1 = {** params , "k" : 1 }
91
+ params_1 = {** params , self . num_results_arg_name : 1 }
64
92
retriever_1 = self .retriever_constructor (** params_1 )
65
93
result_1 = retriever_1 .invoke (self .retriever_query_example )
66
94
assert len (result_1 ) == 1
67
95
assert all (isinstance (doc , Document ) for doc in result_1 )
68
96
69
97
def test_invoke_with_k_kwarg (self , retriever : BaseRetriever ) -> None :
70
- """Test that the invoke method accepts a k parameter, representing the number of
71
- documents to return.
98
+ """Test the number of results parameter in ``invoke()``.
99
+
100
+ Test that the invoke method accepts a parameter representing
101
+ the number of documents to return.
102
+
103
+ By default, the parameter is named ``, but it can be overridden by
104
+ setting the ``num_results_arg_name`` property.
105
+
106
+ .. note::
107
+ If the retriever doesn't support configuring the number of results returned
108
+ via the invoke method, this test can be skipped using a pytest ``xfail`` on
109
+ the test class:
110
+
111
+ .. code-block:: python
112
+
113
+ @pytest.mark.xfail(
114
+ reason="This retriever doesn't support setting "
115
+ "the number of results in the invoke method."
116
+ )
117
+ def test_invoke_with_k_kwarg(self) -> None:
118
+ raise NotImplementedError
72
119
73
120
.. dropdown:: Troubleshooting
74
121
75
- If this test fails, the retriever's invoke method does not accept a k
76
- parameter, or the retriever does not return the correct number of documents
77
- (`k`) when it is set.
122
+ If this test fails, the retriever's invoke method does not accept a number
123
+ of results parameter, or the retriever does not return the correct number
124
+ of documents (``k`` of the one set in ``num_results_arg_name``) when it is
125
+ set.
78
126
79
127
For example, a retriever like
80
128
@@ -85,11 +133,15 @@ def test_invoke_with_k_kwarg(self, retriever: BaseRetriever) -> None:
85
133
should return 3 documents when invoked with a query.
86
134
87
135
"""
88
- result_1 = retriever .invoke (self .retriever_query_example , k = 1 )
136
+ result_1 = retriever .invoke (
137
+ self .retriever_query_example , None , ** {self .num_results_arg_name : 1 }
138
+ )
89
139
assert len (result_1 ) == 1
90
140
assert all (isinstance (doc , Document ) for doc in result_1 )
91
141
92
- result_3 = retriever .invoke (self .retriever_query_example , k = 3 )
142
+ result_3 = retriever .invoke (
143
+ self .retriever_query_example , None , ** {self .num_results_arg_name : 3 }
144
+ )
93
145
assert len (result_3 ) == 3
94
146
assert all (isinstance (doc , Document ) for doc in result_3 )
95
147
@@ -100,8 +152,8 @@ def test_invoke_returns_documents(self, retriever: BaseRetriever) -> None:
100
152
.. dropdown:: Troubleshooting
101
153
102
154
If this test fails, the retriever's invoke method does not return a list of
103
- `langchain_core.document.Document` objects. Please confirm that your
104
- `_get_relevant_documents` method returns a list of `Document` objects.
155
+ `` langchain_core.document.Document` ` objects. Please confirm that your
156
+ `` _get_relevant_documents`` method returns a list of `` Document` ` objects.
105
157
"""
106
158
result = retriever .invoke (self .retriever_query_example )
107
159
0 commit comments