Skip to content

Commit

Permalink
refactor: converting some DocumentJoiner methods to staticmethod (dee…
Browse files Browse the repository at this point in the history
…pset-ai#8606)

* converting some methods to static, since they change/depend on state of the object

* adding release notes

* removing tab
  • Loading branch information
davidsbatista authored Dec 6, 2024
1 parent e349a7f commit 3da5bac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 7 additions & 5 deletions haystack/components/joiners/document_joiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
enhancements:
-|
DocumentJoiner methods `_concatenate()` and `_distribution_based_rank_fusion()` were converted to static methods.

0 comments on commit 3da5bac

Please sign in to comment.