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.
This commit is contained in:
Rafael Pereira 2024-07-17 17:37:47 +01:00 committed by GitHub
parent 2a3288b15d
commit cf28708e7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 6 deletions

View File

@ -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

View File

@ -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)