Skip to content

Commit

Permalink
Fix: do not return embeddings in HybridRetriever (neo4j#187)
Browse files Browse the repository at this point in the history
* Fix: do not return embedding property in HybridRetriever

* Update changelog

* Fix changelog
  • Loading branch information
stellasia authored Oct 21, 2024
1 parent ca86f26 commit 358fafd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Changed
- Vector and Hybrid retrievers used with `return_properties` now also return the node labels (`nodeLabels`) and the node's element ID (`id`).
- `HybridRetriever` now filters out the embedding property index in `self.vector_index_name` from the retriever result by default.


## 1.1.0

Expand Down
10 changes: 7 additions & 3 deletions src/neo4j_graphrag/retrievers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ def _verify_version(self) -> None:
if version_tuple < target_version:
raise Neo4jVersionError()

def _fetch_index_infos(self) -> None:
"""Fetch the node label and embedding property from the index definition"""
def _fetch_index_infos(self, vector_index_name: str) -> None:
"""Fetch the node label and embedding property from the index definition
Args:
vector_index_name (str): Name of the vector index
"""
query = (
"SHOW VECTOR INDEXES "
"YIELD name, labelsOrTypes, properties, options "
Expand All @@ -141,7 +145,7 @@ def _fetch_index_infos(self) -> None:
"options.indexConfig.`vector.dimensions` as dimensions"
)
query_result = self.driver.execute_query(
query, {"index_name": self.index_name}, database_=self.neo4j_database
query, {"index_name": vector_index_name}, database_=self.neo4j_database
)
try:
result = query_result.records[0]
Expand Down
4 changes: 4 additions & 0 deletions src/neo4j_graphrag/retrievers/hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ def __init__(
else None
)
self.result_formatter = validated_data.result_formatter
self._embedding_node_property = None
self._embedding_dimension = None
self._fetch_index_infos(self.vector_index_name)

def default_record_formatter(self, record: neo4j.Record) -> RetrieverResultItem:
"""
Expand Down Expand Up @@ -187,6 +190,7 @@ def get_search_results(
search_query, _ = get_search_query(
SearchType.HYBRID,
self.return_properties,
embedding_node_property=self._embedding_node_property,
neo4j_version_is_5_23_or_above=self.neo4j_version_is_5_23_or_above,
)

Expand Down
4 changes: 2 additions & 2 deletions src/neo4j_graphrag/retrievers/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def __init__(
self._node_label = None
self._embedding_node_property = None
self._embedding_dimension = None
self._fetch_index_infos()
self._fetch_index_infos(self.index_name)

def default_record_formatter(self, record: neo4j.Record) -> RetrieverResultItem:
"""
Expand Down Expand Up @@ -287,7 +287,7 @@ def __init__(
self._node_label = None
self._node_embedding_property = None
self._embedding_dimension = None
self._fetch_index_infos()
self._fetch_index_infos(self.index_name)

def get_search_results(
self,
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/test_hybrid_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_hybrid_retriever_search_text(
assert len(results.items) == 5
for result in results.items:
assert isinstance(result, RetrieverResultItem)
assert "'vectorProperty': None," in result.content


@pytest.mark.usefixtures("setup_neo4j_for_retrieval")
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/retrievers/test_hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ def test_hybrid_cypher_retriever_invalid_database_name(
assert "Input should be a valid string" in str(exc_info.value)


@patch("neo4j_graphrag.retrievers.HybridRetriever._fetch_index_infos")
@patch("neo4j_graphrag.retrievers.HybridRetriever._verify_version")
def test_hybrid_search_text_happy_path(
_verify_version_mock: MagicMock,
_fetch_index_infos_mock: MagicMock,
driver: MagicMock,
embedder: MagicMock,
neo4j_record: MagicMock,
Expand All @@ -176,13 +178,17 @@ def test_hybrid_search_text_happy_path(
driver, vector_index_name, fulltext_index_name, embedder
)
retriever.neo4j_version_is_5_23_or_above = True
retriever._embedding_node_property = (
"embedding" # variable normally filled by fetch_index_infos
)
retriever.driver.execute_query.return_value = [ # type: ignore
[neo4j_record],
None,
None,
]
search_query, _ = get_search_query(
SearchType.HYBRID,
embedding_node_property="embedding",
neo4j_version_is_5_23_or_above=retriever.neo4j_version_is_5_23_or_above,
)

Expand All @@ -208,9 +214,11 @@ def test_hybrid_search_text_happy_path(
)


@patch("neo4j_graphrag.retrievers.HybridRetriever._fetch_index_infos")
@patch("neo4j_graphrag.retrievers.HybridRetriever._verify_version")
def test_hybrid_search_favors_query_vector_over_embedding_vector(
_verify_version_mock: MagicMock,
_fetch_index_infos_mock: MagicMock,
driver: MagicMock,
embedder: MagicMock,
neo4j_record: MagicMock,
Expand Down Expand Up @@ -288,9 +296,11 @@ def test_hybrid_search_retriever_search_missing_embedder_for_text(
)


@patch("neo4j_graphrag.retrievers.HybridRetriever._fetch_index_infos")
@patch("neo4j_graphrag.retrievers.HybridRetriever._verify_version")
def test_hybrid_retriever_return_properties(
_verify_version_mock: MagicMock,
_fetch_index_infos_mock: MagicMock,
driver: MagicMock,
embedder: MagicMock,
neo4j_record: MagicMock,
Expand Down

0 comments on commit 358fafd

Please sign in to comment.