From cf28708e7b70862db216fccb7825bc8553aae6ca Mon Sep 17 00:00:00 2001 From: Rafael Pereira <82586689+RafaelXokito@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:37:47 +0100 Subject: [PATCH] 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. --- .../langchain_community/vectorstores/neo4j_vector.py | 10 +++++----- .../integration_tests/vectorstores/test_neo4jvector.py | 5 ++++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/libs/community/langchain_community/vectorstores/neo4j_vector.py b/libs/community/langchain_community/vectorstores/neo4j_vector.py index caefda20420..e083f3ba3e0 100644 --- a/libs/community/langchain_community/vectorstores/neo4j_vector.py +++ b/libs/community/langchain_community/vectorstores/neo4j_vector.py @@ -854,11 +854,11 @@ class Neo4jVector(VectorStore): "CALL { WITH row " f"MERGE (c:`{self.node_label}` {{id: row.id}}) " "WITH c, row " - f"CALL db.create.setVectorProperty(c, " + f"CALL db.create.setNodeVectorProperty(c, " f"'{self.embedding_node_property}', row.embedding) " - "YIELD node " f"SET c.`{self.text_node_property}` = row.text " - "SET c += row.metadata } IN TRANSACTIONS OF 1000 ROWS" + "SET c += row.metadata " + "} IN TRANSACTIONS OF 1000 ROWS " ) parameters = { @@ -1462,9 +1462,9 @@ class Neo4jVector(VectorStore): "UNWIND $data AS row " f"MATCH (n:`{node_label}`) " "WHERE elementId(n) = row.id " - f"CALL db.create.setVectorProperty(n, " + f"CALL db.create.setNodeVectorProperty(n, " f"'{embedding_node_property}', row.embedding) " - "YIELD node RETURN count(*)", + "RETURN count(*)", params=params, ) # If embedding calculation should be stopped diff --git a/libs/community/tests/integration_tests/vectorstores/test_neo4jvector.py b/libs/community/tests/integration_tests/vectorstores/test_neo4jvector.py index 50869b985f2..761450f4a91 100644 --- a/libs/community/tests/integration_tests/vectorstores/test_neo4jvector.py +++ b/libs/community/tests/integration_tests/vectorstores/test_neo4jvector.py @@ -199,7 +199,10 @@ def test_neo4jvector_with_metadatas_with_scores() -> None: password=password, pre_delete_collection=True, ) - output = docsearch.similarity_search_with_score("foo", k=1) + output = [ + (doc, round(score, 1)) + for doc, score in docsearch.similarity_search_with_score("foo", k=1) + ] assert output == [(Document(page_content="foo", metadata={"page": "0"}), 1.0)] drop_vector_indexes(docsearch)