@@ -104,8 +104,7 @@ def __init__(
104104 metadata fields. If False, it will skip retrieving the context for documents that are missing
105105 the required metadata fields, but will still include the original document in the results.
106106 """
107- if window_size < 1 :
108- raise ValueError ("The window_size parameter must be greater than 0." )
107+ self ._validate_window_size (window_size )
109108
110109 self .window_size = window_size
111110 self .document_store = document_store
@@ -187,7 +186,8 @@ def run(self, retrieved_documents: list[Document], window_size: int | None = Non
187186
188187 :param retrieved_documents: List of retrieved documents from the previous retriever.
189188 :param window_size: The number of documents to retrieve before and after the relevant one. This will overwrite
190- the `window_size` parameter set in the constructor.
189+ the `window_size` parameter set in the constructor. It must be greater than 0; values of
190+ 0 and negative values are rejected.
191191 :returns:
192192 A dictionary with the following keys:
193193 - `context_windows`: A list of strings, where each string represents the concatenated text from the
@@ -197,8 +197,8 @@ def run(self, retrieved_documents: list[Document], window_size: int | None = Non
197197 meta field.
198198
199199 """
200- window_size = window_size or self . window_size
201- SentenceWindowRetriever . _raise_if_windows_size_is_negative (window_size )
200+ window_size = self . window_size if window_size is None else window_size
201+ self . _validate_window_size (window_size )
202202 self ._raise_if_documents_do_not_have_expected_metadata (retrieved_documents )
203203
204204 context_text = []
@@ -220,7 +220,8 @@ async def run_async(self, retrieved_documents: list[Document], window_size: int
220220
221221 :param retrieved_documents: List of retrieved documents from the previous retriever.
222222 :param window_size: The number of documents to retrieve before and after the relevant one. This will overwrite
223- the `window_size` parameter set in the constructor.
223+ the `window_size` parameter set in the constructor. It must be greater than 0; values of
224+ 0 and negative values are rejected.
224225 :returns:
225226 A dictionary with the following keys:
226227 - `context_windows`: A list of strings, where each string represents the concatenated text from the
@@ -230,8 +231,8 @@ async def run_async(self, retrieved_documents: list[Document], window_size: int
230231 meta field.
231232
232233 """
233- window_size = window_size or self . window_size
234- SentenceWindowRetriever . _raise_if_windows_size_is_negative (window_size )
234+ window_size = self . window_size if window_size is None else window_size
235+ self . _validate_window_size (window_size )
235236 self ._raise_if_documents_do_not_have_expected_metadata (retrieved_documents )
236237
237238 context_text = []
@@ -244,7 +245,7 @@ async def run_async(self, retrieved_documents: list[Document], window_size: int
244245 return {"context_windows" : context_text , "context_documents" : context_documents }
245246
246247 @staticmethod
247- def _raise_if_windows_size_is_negative (window_size : int ) -> None :
248+ def _validate_window_size (window_size : int ) -> None :
248249 if window_size < 1 :
249250 raise ValueError ("The window_size parameter must be greater than 0." )
250251
0 commit comments