Skip to content

Commit

Permalink
Fix: #1042 add insert for empty GPTTreeIndex (#1175)
Browse files Browse the repository at this point in the history
Fix: add insert for empty GPTTreeIndex(#1042)

Co-authored-by: yueyang <zhouyueyang.zyy@antgroup.com>
  • Loading branch information
hxbs-zyy and hxbszyy authored Apr 14, 2023
1 parent 49aff68 commit ba84c26
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions gpt_index/data_structs/data_structs_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def insert_under_parent(
new_index = new_index or self.size
if parent_node is None:
self.root_nodes[new_index] = node.get_doc_id()
self.node_id_to_children_ids[node.get_doc_id()] = []
else:
if parent_node.doc_id not in self.node_id_to_children_ids:
self.node_id_to_children_ids[parent_node.get_doc_id()] = []
Expand Down
20 changes: 20 additions & 0 deletions tests/indices/tree/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,26 @@ def test_insert(
assert nodes[0].ref_doc_id == "new_doc_test"


@patch_common
def test_twice_insert_empty(
_mock_init: Any,
_mock_predict: Any,
_mock_total_tokens_used: Any,
_mock_split_text_overlap: Any,
_mock_split_text: Any,
) -> None:
"""# test twice insert from empty (with_id)"""
tree = GPTTreeIndex.from_documents([])

# test first insert
new_doc = Document("This is a new doc.", doc_id="new_doc")
tree.insert(new_doc)
# test second insert
new_doc_second = Document("This is a new doc2.", doc_id="new_doc_2")
tree.insert(new_doc_second)
assert len(tree.index_struct.all_nodes) == 2


def _mock_tokenizer(text: str) -> int:
"""Mock tokenizer that splits by spaces."""
return len(text.split(" "))
Expand Down

0 comments on commit ba84c26

Please sign in to comment.