Skip to content

Commit 2c3702b

Browse files
committed
Resolve merge conflicts and fix BuildGremlinExampleIndex
- Fix merge conflicts in build_gremlin_example_index.py - Maintain empty examples handling while using new async parallel embeddings - Update tests to work with new directory structure and utility functions - Add proper mocking for new dependencies
1 parent dbcad5f commit 2c3702b

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

hugegraph-llm/src/hugegraph_llm/operators/index_op/build_gremlin_example_index.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def __init__(self, embedding: BaseEmbedding, examples: List[Dict[str, str]]):
3636
self.filename_prefix = get_filename_prefix(llm_settings.embedding_type, getattr(embedding, "model_name", None))
3737

3838
def run(self, context: Dict[str, Any]) -> Dict[str, Any]:
39-
examples_embedding = []
4039
embed_dim = 0
4140

4241
if len(self.examples) > 0:

hugegraph-llm/src/tests/operators/index_op/test_build_gremlin_example_index.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,23 @@ def setUp(self):
4747
)
4848
self.patcher1.start()
4949

50+
# Mock the new utility functions
51+
self.patcher2 = patch("hugegraph_llm.operators.index_op.build_gremlin_example_index.get_index_folder_name")
52+
self.mock_get_index_folder_name = self.patcher2.start()
53+
self.mock_get_index_folder_name.return_value = "hugegraph"
54+
55+
self.patcher3 = patch("hugegraph_llm.operators.index_op.build_gremlin_example_index.get_filename_prefix")
56+
self.mock_get_filename_prefix = self.patcher3.start()
57+
self.mock_get_filename_prefix.return_value = "test_prefix"
58+
59+
self.patcher4 = patch("hugegraph_llm.operators.index_op.build_gremlin_example_index.get_embeddings_parallel")
60+
self.mock_get_embeddings_parallel = self.patcher4.start()
61+
self.mock_get_embeddings_parallel.return_value = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]
62+
5063
# Mock VectorIndex
5164
self.mock_vector_index = MagicMock(spec=VectorIndex)
52-
self.patcher2 = patch("hugegraph_llm.operators.index_op.build_gremlin_example_index.VectorIndex")
53-
self.mock_vector_index_class = self.patcher2.start()
65+
self.patcher5 = patch("hugegraph_llm.operators.index_op.build_gremlin_example_index.VectorIndex")
66+
self.mock_vector_index_class = self.patcher5.start()
5467
self.mock_vector_index_class.return_value = self.mock_vector_index
5568

5669
def tearDown(self):
@@ -60,6 +73,9 @@ def tearDown(self):
6073
# Stop the patchers
6174
self.patcher1.stop()
6275
self.patcher2.stop()
76+
self.patcher3.stop()
77+
self.patcher4.stop()
78+
self.patcher5.stop()
6379

6480
def test_init(self):
6581
# Test initialization
@@ -71,8 +87,8 @@ def test_init(self):
7187
# Check if the examples are set correctly
7288
self.assertEqual(builder.examples, self.examples)
7389

74-
# Check if the index_dir is set correctly
75-
expected_index_dir = os.path.join(self.temp_dir, "gremlin_examples")
90+
# Check if the index_dir is set correctly (now includes folder structure)
91+
expected_index_dir = os.path.join(self.temp_dir, "hugegraph", "gremlin_examples")
7692
self.assertEqual(builder.index_dir, expected_index_dir)
7793

7894
def test_run_with_examples(self):
@@ -85,21 +101,19 @@ def test_run_with_examples(self):
85101
# Run the builder
86102
result = builder.run(context)
87103

88-
# Check if get_text_embedding was called for each example
89-
self.assertEqual(self.mock_embedding.get_text_embedding.call_count, 2)
90-
self.mock_embedding.get_text_embedding.assert_any_call("g.V().hasLabel('person')")
91-
self.mock_embedding.get_text_embedding.assert_any_call("g.V().hasLabel('movie')")
104+
# Check if get_embeddings_parallel was called
105+
self.mock_get_embeddings_parallel.assert_called_once()
92106

93107
# Check if VectorIndex was initialized with the correct dimension
94108
self.mock_vector_index_class.assert_called_once_with(3) # dimension of [0.1, 0.2, 0.3]
95109

96110
# Check if add was called with the correct arguments
97-
expected_embeddings = [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]
111+
expected_embeddings = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]] # from mock return value
98112
self.mock_vector_index.add.assert_called_once_with(expected_embeddings, self.examples)
99113

100-
# Check if to_index_file was called with the correct path
101-
expected_index_dir = os.path.join(self.temp_dir, "gremlin_examples")
102-
self.mock_vector_index.to_index_file.assert_called_once_with(expected_index_dir)
114+
# Check if to_index_file was called with the correct path and prefix
115+
expected_index_dir = os.path.join(self.temp_dir, "hugegraph", "gremlin_examples")
116+
self.mock_vector_index.to_index_file.assert_called_once_with(expected_index_dir, "test_prefix")
103117

104118
# Check if the context is updated correctly
105119
expected_context = {"embed_dim": 3}
@@ -110,11 +124,14 @@ def test_run_with_empty_examples(self):
110124
builder = BuildGremlinExampleIndex(self.mock_embedding, [])
111125

112126
# Create a context
113-
context = {}
127+
context = {"test": "value"}
114128

115-
# Run the builder
116-
with self.assertRaises(IndexError):
117-
builder.run(context)
129+
# The run method should handle empty examples gracefully
130+
result = builder.run(context)
131+
132+
# Should return embed_dim as 0 for empty examples
133+
self.assertEqual(result["embed_dim"], 0)
134+
self.assertEqual(result["test"], "value") # Original context should be preserved
118135

119136
# Check if VectorIndex was not initialized
120137
self.mock_vector_index_class.assert_not_called()

0 commit comments

Comments
 (0)