From 3da5bac8c4743c59940e857d5d7a0552604d3fa9 Mon Sep 17 00:00:00 2001 From: "David S. Batista" Date: Fri, 6 Dec 2024 10:28:41 +0100 Subject: [PATCH] refactor: converting some DocumentJoiner methods to staticmethod (#8606) * converting some methods to static, since they change/depend on state of the object * adding release notes * removing tab --- haystack/components/joiners/document_joiner.py | 12 +++++++----- ...docjoiner-methods-to-static-4668f5d227667dd5.yaml | 4 ++++ 2 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 releasenotes/notes/converting-docjoiner-methods-to-static-4668f5d227667dd5.yaml diff --git a/haystack/components/joiners/document_joiner.py b/haystack/components/joiners/document_joiner.py index 103ab1c574..ec1aedb225 100644 --- a/haystack/components/joiners/document_joiner.py +++ b/haystack/components/joiners/document_joiner.py @@ -115,10 +115,10 @@ def __init__( if isinstance(join_mode, str): join_mode = JoinMode.from_str(join_mode) join_mode_functions = { - JoinMode.CONCATENATE: self._concatenate, + JoinMode.CONCATENATE: DocumentJoiner._concatenate, JoinMode.MERGE: self._merge, JoinMode.RECIPROCAL_RANK_FUSION: self._reciprocal_rank_fusion, - JoinMode.DISTRIBUTION_BASED_RANK_FUSION: self._distribution_based_rank_fusion, + JoinMode.DISTRIBUTION_BASED_RANK_FUSION: DocumentJoiner._distribution_based_rank_fusion, } self.join_mode_function = join_mode_functions[join_mode] self.join_mode = join_mode @@ -162,7 +162,8 @@ def run(self, documents: Variadic[List[Document]], top_k: Optional[int] = None): return {"documents": output_documents} - def _concatenate(self, document_lists: List[List[Document]]) -> List[Document]: + @staticmethod + def _concatenate(document_lists: List[List[Document]]) -> List[Document]: """ Concatenate multiple lists of Documents and return only the Document with the highest score for duplicates. """ @@ -230,7 +231,8 @@ def _reciprocal_rank_fusion(self, document_lists: List[List[Document]]) -> List[ return list(documents_map.values()) - def _distribution_based_rank_fusion(self, document_lists: List[List[Document]]) -> List[Document]: + @staticmethod + def _distribution_based_rank_fusion(document_lists: List[List[Document]]) -> List[Document]: """ Merge multiple lists of Documents and assign scores based on Distribution-Based Score Fusion. @@ -256,7 +258,7 @@ def _distribution_based_rank_fusion(self, document_lists: List[List[Document]]) doc.score = (doc.score - min_score) / delta_score if delta_score != 0.0 else 0.0 # if all docs have the same score delta_score is 0, the docs are uninformative for the query - output = self._concatenate(document_lists=document_lists) + output = DocumentJoiner._concatenate(document_lists=document_lists) return output diff --git a/releasenotes/notes/converting-docjoiner-methods-to-static-4668f5d227667dd5.yaml b/releasenotes/notes/converting-docjoiner-methods-to-static-4668f5d227667dd5.yaml new file mode 100644 index 0000000000..6893e0df53 --- /dev/null +++ b/releasenotes/notes/converting-docjoiner-methods-to-static-4668f5d227667dd5.yaml @@ -0,0 +1,4 @@ +--- +enhancements: + -| + DocumentJoiner methods `_concatenate()` and `_distribution_based_rank_fusion()` were converted to static methods.