|
| 1 | +# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from haystack import Document, Pipeline |
| 8 | +from haystack.components.preprocessors import HierarchicalDocumentSplitter |
| 9 | +from haystack.components.retrievers import InMemoryBM25Retriever |
| 10 | +from haystack.components.retrievers.auto_merging_retriever import AutoMergingRetriever |
| 11 | +from haystack.document_stores.in_memory import InMemoryDocumentStore |
| 12 | + |
| 13 | + |
| 14 | +class TestAutoMergingRetrieverAsync: |
| 15 | + @pytest.mark.asyncio |
| 16 | + async def test_run_missing_parent_id(self): |
| 17 | + docs = [Document(content="test", meta={"__level": 1, "__block_size": 10})] |
| 18 | + retriever = AutoMergingRetriever(InMemoryDocumentStore()) |
| 19 | + with pytest.raises( |
| 20 | + ValueError, match="The matched leaf documents do not have the required meta field '__parent_id'" |
| 21 | + ): |
| 22 | + await retriever.run_async(documents=docs) |
| 23 | + |
| 24 | + @pytest.mark.asyncio |
| 25 | + async def test_run_missing_level(self): |
| 26 | + docs = [Document(content="test", meta={"__parent_id": "parent1", "__block_size": 10})] |
| 27 | + |
| 28 | + retriever = AutoMergingRetriever(InMemoryDocumentStore()) |
| 29 | + with pytest.raises( |
| 30 | + ValueError, match="The matched leaf documents do not have the required meta field '__level'" |
| 31 | + ): |
| 32 | + await retriever.run_async(documents=docs) |
| 33 | + |
| 34 | + @pytest.mark.asyncio |
| 35 | + async def test_run_missing_block_size(self): |
| 36 | + docs = [Document(content="test", meta={"__parent_id": "parent1", "__level": 1})] |
| 37 | + |
| 38 | + retriever = AutoMergingRetriever(InMemoryDocumentStore()) |
| 39 | + with pytest.raises( |
| 40 | + ValueError, match="The matched leaf documents do not have the required meta field '__block_size'" |
| 41 | + ): |
| 42 | + await retriever.run_async(documents=docs) |
| 43 | + |
| 44 | + @pytest.mark.asyncio |
| 45 | + async def test_run_mixed_valid_and_invalid_documents(self): |
| 46 | + docs = [ |
| 47 | + Document(content="valid", meta={"__parent_id": "parent1", "__level": 1, "__block_size": 10}), |
| 48 | + Document(content="invalid", meta={"__level": 1, "__block_size": 10}), |
| 49 | + ] |
| 50 | + retriever = AutoMergingRetriever(InMemoryDocumentStore()) |
| 51 | + with pytest.raises( |
| 52 | + ValueError, match="The matched leaf documents do not have the required meta field '__parent_id'" |
| 53 | + ): |
| 54 | + await retriever.run_async(documents=docs) |
| 55 | + |
| 56 | + @pytest.mark.asyncio |
| 57 | + async def test_run_parent_not_found(self): |
| 58 | + doc_store = InMemoryDocumentStore() |
| 59 | + retriever = AutoMergingRetriever(doc_store, threshold=0.5) |
| 60 | + |
| 61 | + # a leaf document with a non-existent parent_id |
| 62 | + leaf_doc = Document( |
| 63 | + content="test", meta={"__parent_id": "non_existent_parent", "__level": 1, "__block_size": 10} |
| 64 | + ) |
| 65 | + |
| 66 | + with pytest.raises(ValueError, match="Expected 1 parent document with id non_existent_parent, found 0"): |
| 67 | + await retriever.run_async([leaf_doc]) |
| 68 | + |
| 69 | + @pytest.mark.asyncio |
| 70 | + async def test_run_parent_without_children_metadata(self): |
| 71 | + """Test case where a parent document exists but doesn't have the __children_ids metadata field""" |
| 72 | + doc_store = InMemoryDocumentStore() |
| 73 | + |
| 74 | + # Create and store a parent document without __children_ids metadata |
| 75 | + parent_doc = Document( |
| 76 | + content="parent content", |
| 77 | + id="parent1", |
| 78 | + meta={ |
| 79 | + "__level": 1, # Add other required metadata |
| 80 | + "__block_size": 10, |
| 81 | + }, |
| 82 | + ) |
| 83 | + doc_store.write_documents([parent_doc]) |
| 84 | + |
| 85 | + retriever = AutoMergingRetriever(doc_store, threshold=0.5) |
| 86 | + |
| 87 | + # Create a leaf document that points to this parent |
| 88 | + leaf_doc = Document(content="leaf content", meta={"__parent_id": "parent1", "__level": 2, "__block_size": 5}) |
| 89 | + |
| 90 | + with pytest.raises(ValueError, match="Parent document with id parent1 does not have any children"): |
| 91 | + await retriever.run_async([leaf_doc]) |
| 92 | + |
| 93 | + @pytest.mark.asyncio |
| 94 | + async def test_run_empty_documents(self): |
| 95 | + retriever = AutoMergingRetriever(InMemoryDocumentStore()) |
| 96 | + assert await retriever.run_async([]) == {"documents": []} |
| 97 | + |
| 98 | + @pytest.mark.asyncio |
| 99 | + async def test_run_return_parent_document(self): |
| 100 | + text = "The sun rose early in the morning. It cast a warm glow over the trees. Birds began to sing." |
| 101 | + |
| 102 | + docs = [Document(content=text)] |
| 103 | + builder = HierarchicalDocumentSplitter(block_sizes={10, 3}, split_overlap=0, split_by="word") |
| 104 | + docs = builder.run(docs) |
| 105 | + |
| 106 | + # store all non-leaf documents |
| 107 | + doc_store_parents = InMemoryDocumentStore() |
| 108 | + for doc in docs["documents"]: |
| 109 | + if doc.meta["__children_ids"]: |
| 110 | + doc_store_parents.write_documents([doc]) |
| 111 | + retriever = AutoMergingRetriever(doc_store_parents, threshold=0.5) |
| 112 | + |
| 113 | + # assume we retrieved 2 leaf docs from the same parent, the parent document should be returned, |
| 114 | + # since it has 3 children and the threshold=0.5, and we retrieved 2 children (2/3 > 0.66(6)) |
| 115 | + leaf_docs = [doc for doc in docs["documents"] if not doc.meta["__children_ids"]] |
| 116 | + docs = await retriever.run_async(leaf_docs[4:6]) |
| 117 | + assert len(docs["documents"]) == 1 |
| 118 | + assert docs["documents"][0].content == "warm glow over the trees. Birds began to sing." |
| 119 | + assert len(docs["documents"][0].meta["__children_ids"]) == 3 |
| 120 | + |
| 121 | + @pytest.mark.asyncio |
| 122 | + async def test_run_return_leafs_document(self): |
| 123 | + docs = [Document(content="The monarch of the wild blue yonder rises from the eastern side of the horizon.")] |
| 124 | + builder = HierarchicalDocumentSplitter(block_sizes={10, 3}, split_overlap=0, split_by="word") |
| 125 | + docs = builder.run(docs) |
| 126 | + |
| 127 | + doc_store_parents = InMemoryDocumentStore() |
| 128 | + for doc in docs["documents"]: |
| 129 | + if doc.meta["__level"] == 1: |
| 130 | + doc_store_parents.write_documents([doc]) |
| 131 | + |
| 132 | + leaf_docs = [doc for doc in docs["documents"] if not doc.meta["__children_ids"]] |
| 133 | + retriever = AutoMergingRetriever(doc_store_parents, threshold=0.6) |
| 134 | + result = await retriever.run_async([leaf_docs[4]]) |
| 135 | + |
| 136 | + assert len(result["documents"]) == 1 |
| 137 | + assert result["documents"][0].content == "eastern side of " |
| 138 | + assert result["documents"][0].meta["__parent_id"] == docs["documents"][2].id |
| 139 | + |
| 140 | + @pytest.mark.asyncio |
| 141 | + async def test_run_return_leafs_document_different_parents(self): |
| 142 | + docs = [Document(content="The monarch of the wild blue yonder rises from the eastern side of the horizon.")] |
| 143 | + builder = HierarchicalDocumentSplitter(block_sizes={10, 3}, split_overlap=0, split_by="word") |
| 144 | + docs = builder.run(docs) |
| 145 | + |
| 146 | + doc_store_parents = InMemoryDocumentStore() |
| 147 | + for doc in docs["documents"]: |
| 148 | + if doc.meta["__level"] == 1: |
| 149 | + doc_store_parents.write_documents([doc]) |
| 150 | + |
| 151 | + leaf_docs = [doc for doc in docs["documents"] if not doc.meta["__children_ids"]] |
| 152 | + retriever = AutoMergingRetriever(doc_store_parents, threshold=0.6) |
| 153 | + result = await retriever.run_async([leaf_docs[4], leaf_docs[3]]) |
| 154 | + |
| 155 | + assert len(result["documents"]) == 2 |
| 156 | + assert result["documents"][0].meta["__parent_id"] != result["documents"][1].meta["__parent_id"] |
| 157 | + |
| 158 | + @pytest.mark.asyncio |
| 159 | + async def test_run_go_up_hierarchy_multiple_levels(self): |
| 160 | + """ |
| 161 | + Test if the retriever can go up the hierarchy multiple levels to find the parent document. |
| 162 | +
|
| 163 | + Simulate a scenario where we have 4 leaf-documents that matched some initial query. The leaf-documents |
| 164 | + are continuously merged up the hierarchy until the threshold is no longer met. |
| 165 | + In this case it goes from the 4th level in the hierarchy up the 1st level. |
| 166 | + """ |
| 167 | + text = "The sun rose early in the morning. It cast a warm glow over the trees. Birds began to sing." |
| 168 | + |
| 169 | + docs = [Document(content=text)] |
| 170 | + builder = HierarchicalDocumentSplitter(block_sizes={6, 4, 2, 1}, split_overlap=0, split_by="word") |
| 171 | + docs = builder.run(docs) |
| 172 | + |
| 173 | + # store all non-leaf documents |
| 174 | + doc_store_parents = InMemoryDocumentStore() |
| 175 | + for doc in docs["documents"]: |
| 176 | + if doc.meta["__children_ids"]: |
| 177 | + doc_store_parents.write_documents([doc]) |
| 178 | + retriever = AutoMergingRetriever(doc_store_parents, threshold=0.4) |
| 179 | + |
| 180 | + # simulate a scenario where we have 4 leaf-documents that matched some initial query |
| 181 | + retrieved_leaf_docs = [d for d in docs["documents"] if d.content in {"The ", "sun ", "rose ", "early "}] |
| 182 | + |
| 183 | + result = await retriever.run_async(retrieved_leaf_docs) |
| 184 | + |
| 185 | + assert len(result["documents"]) == 1 |
| 186 | + assert result["documents"][0].content == "The sun rose early in the " |
| 187 | + |
| 188 | + @pytest.mark.asyncio |
| 189 | + async def test_run_go_up_hierarchy_multiple_levels_hit_root_document(self): |
| 190 | + """ |
| 191 | + Test case where we go up hierarchy until the root document, so the root document is returned. |
| 192 | +
|
| 193 | + It's the only document in the hierarchy which has no parent. |
| 194 | + """ |
| 195 | + text = "The sun rose early in the morning. It cast a warm glow over the trees. Birds began to sing." |
| 196 | + |
| 197 | + docs = [Document(content=text)] |
| 198 | + builder = HierarchicalDocumentSplitter(block_sizes={6, 4}, split_overlap=0, split_by="word") |
| 199 | + docs = builder.run(docs) |
| 200 | + |
| 201 | + # store all non-leaf documents |
| 202 | + doc_store_parents = InMemoryDocumentStore() |
| 203 | + for doc in docs["documents"]: |
| 204 | + if doc.meta["__children_ids"]: |
| 205 | + doc_store_parents.write_documents([doc]) |
| 206 | + retriever = AutoMergingRetriever(doc_store_parents, threshold=0.1) # set a low threshold to hit root document |
| 207 | + |
| 208 | + # simulate a scenario where we have 4 leaf-documents that matched some initial query |
| 209 | + retrieved_leaf_docs = [ |
| 210 | + d |
| 211 | + for d in docs["documents"] |
| 212 | + if d.content in {"The sun rose early ", "in the ", "morning. It cast a ", "over the trees. Birds "} |
| 213 | + ] |
| 214 | + |
| 215 | + result = await retriever.run_async(retrieved_leaf_docs) |
| 216 | + |
| 217 | + assert len(result["documents"]) == 1 |
| 218 | + assert result["documents"][0].meta["__level"] == 0 # hit root document |
0 commit comments