Skip to content

Commit

Permalink
keeping tail recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsbatista committed Jan 6, 2025
1 parent 136c3c9 commit a4c7950
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ def run(self, matched_leaf_documents: List[Document]):

AutoMergingRetriever._check_valid_documents(matched_leaf_documents)

def try_merge_level(documents: List[Document]) -> List[Document]:
def try_merge_level(documents: List[Document], docs_to_return: List[Document]) -> List[Document]:
if not documents:
return []

docs_to_return = []
parent_documents: Dict[str, List[Document]] = defaultdict(list) # to group the documents by their parent

for doc in documents:
Expand Down Expand Up @@ -167,8 +166,9 @@ def try_merge_level(documents: List[Document]) -> List[Document]:
return merged_docs

# Recursively try to merge the next level
return docs_to_return + try_merge_level(merged_docs)
return try_merge_level(merged_docs, docs_to_return)

# start the recursive merging process
final_docs = try_merge_level(matched_leaf_documents)
docs_to_return = []
final_docs = try_merge_level(matched_leaf_documents, docs_to_return)
return {"documents": final_docs}

0 comments on commit a4c7950

Please sign in to comment.