Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update subqueries building import query based on neo4j version #10

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Next

### Updated
- Fixed bugs in the Neo4jVector and GraphCypherQAChain classes preventing these classes from working with versions < 5.23 of Neo4j

## 0.1.0

### Added
Expand Down
60 changes: 44 additions & 16 deletions libs/neo4j/langchain_neo4j/vectorstores/neo4j_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,17 +594,29 @@ def __init__(
if pre_delete_collection:
from neo4j.exceptions import DatabaseError

self.query(
f"MATCH (n:`{self.node_label}`) "
"CALL (n) { DETACH DELETE n } "
"IN TRANSACTIONS OF 10000 ROWS;"
)
delete_query = self._build_delete_query()
self.query(delete_query)
# Delete index
try:
self.query(f"DROP INDEX {self.index_name}")
except DatabaseError: # Index didn't exist yet
pass

def _build_delete_query(self) -> str:
if self.neo4j_version_is_5_23_or_above:
query = (
f"MATCH (n:`{self.node_label}`) "
"CALL (n) { DETACH DELETE n } "
"IN TRANSACTIONS OF 10000 ROWS;"
)
else:
query = (
f"MATCH (n:`{self.node_label}`) "
"CALL { WITH n DETACH DELETE n } "
"IN TRANSACTIONS OF 10000 ROWS;"
)
return query

def query(
self,
query: str,
Expand Down Expand Up @@ -903,17 +915,7 @@ def add_embeddings(
if not metadatas:
metadatas = [{} for _ in texts]

import_query = (
"UNWIND $data AS row "
"CALL (row) { WITH row "
f"MERGE (c:`{self.node_label}` {{id: row.id}}) "
"WITH c, row "
f"CALL db.create.setNodeVectorProperty(c, "
f"'{self.embedding_node_property}', row.embedding) "
f"SET c.`{self.text_node_property}` = row.text "
"SET c += row.metadata "
"} IN TRANSACTIONS OF 1000 ROWS "
)
import_query = self._build_import_query()

parameters = {
"data": [
Expand All @@ -928,6 +930,32 @@ def add_embeddings(

return ids

def _build_import_query(self) -> str:
"""
Build the Cypher import query string based on the Neo4j version.

Returns:
str: The constructed Cypher query string.
"""
if self.neo4j_version_is_5_23_or_above:
oskarhane marked this conversation as resolved.
Show resolved Hide resolved
call_prefix = "CALL (row) { "
else:
call_prefix = "CALL { WITH row "

import_query = (
"UNWIND $data AS row "
f"{call_prefix}"
f"MERGE (c:`{self.node_label}` {{id: row.id}}) "
"WITH c, row "
f"CALL db.create.setNodeVectorProperty(c, "
f"'{self.embedding_node_property}', row.embedding) "
f"SET c.`{self.text_node_property}` = row.text "
"SET c += row.metadata "
"} IN TRANSACTIONS OF 1000 ROWS "
)

return import_query

def add_texts(
self,
texts: Iterable[str],
Expand Down
Loading
Loading