Skip to content

Commit cf28708

Browse files
authored
Neo4j: Update with non-deprecated cypher methods, and new method to associate relationship embeddings (#23725)
**Description:** At the moment neo4j wrapper is using setVectorProperty, which is deprecated ([link](https://neo4j.com/docs/operations-manual/5/reference/procedures/#procedure_db_create_setVectorProperty)). I replaced with the non-deprecated version. Neo4j recently introduced a new cypher method to associate embeddings into relations using "setRelationshipVectorProperty" method. In this PR I also implemented a new method to perform this association maintaining the same format used in the "add_embeddings" method which is used to associate embeddings into Nodes. I also included a test case for this new method.
1 parent 2a3288b commit cf28708

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

libs/community/langchain_community/vectorstores/neo4j_vector.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -854,11 +854,11 @@ def add_embeddings(
854854
"CALL { WITH row "
855855
f"MERGE (c:`{self.node_label}` {{id: row.id}}) "
856856
"WITH c, row "
857-
f"CALL db.create.setVectorProperty(c, "
857+
f"CALL db.create.setNodeVectorProperty(c, "
858858
f"'{self.embedding_node_property}', row.embedding) "
859-
"YIELD node "
860859
f"SET c.`{self.text_node_property}` = row.text "
861-
"SET c += row.metadata } IN TRANSACTIONS OF 1000 ROWS"
860+
"SET c += row.metadata "
861+
"} IN TRANSACTIONS OF 1000 ROWS "
862862
)
863863

864864
parameters = {
@@ -1462,9 +1462,9 @@ def from_existing_graph(
14621462
"UNWIND $data AS row "
14631463
f"MATCH (n:`{node_label}`) "
14641464
"WHERE elementId(n) = row.id "
1465-
f"CALL db.create.setVectorProperty(n, "
1465+
f"CALL db.create.setNodeVectorProperty(n, "
14661466
f"'{embedding_node_property}', row.embedding) "
1467-
"YIELD node RETURN count(*)",
1467+
"RETURN count(*)",
14681468
params=params,
14691469
)
14701470
# If embedding calculation should be stopped

libs/community/tests/integration_tests/vectorstores/test_neo4jvector.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ def test_neo4jvector_with_metadatas_with_scores() -> None:
199199
password=password,
200200
pre_delete_collection=True,
201201
)
202-
output = docsearch.similarity_search_with_score("foo", k=1)
202+
output = [
203+
(doc, round(score, 1))
204+
for doc, score in docsearch.similarity_search_with_score("foo", k=1)
205+
]
203206
assert output == [(Document(page_content="foo", metadata={"page": "0"}), 1.0)]
204207

205208
drop_vector_indexes(docsearch)

0 commit comments

Comments
 (0)