2025-06-13 12:06:57 -04:00
|
|
|
"""
|
|
|
|
|
Copyright 2024, Zep Software, Inc.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
from collections.abc import Coroutine
|
2025-06-13 12:15:33 -04:00
|
|
|
from typing import Any
|
2025-06-13 12:06:57 -04:00
|
|
|
|
2025-06-30 12:04:21 -07:00
|
|
|
from neo4j import AsyncGraphDatabase, EagerResult
|
2025-06-13 12:15:33 -04:00
|
|
|
from typing_extensions import LiteralString
|
2025-06-13 12:06:57 -04:00
|
|
|
|
|
|
|
|
from graphiti_core.driver.driver import GraphDriver, GraphDriverSession
|
|
|
|
|
from graphiti_core.helpers import DEFAULT_DATABASE
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Neo4jDriver(GraphDriver):
|
|
|
|
|
provider: str = 'neo4j'
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
uri: str,
|
2025-06-13 14:12:09 -04:00
|
|
|
user: str | None,
|
|
|
|
|
password: str | None,
|
2025-06-13 12:06:57 -04:00
|
|
|
):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.client = AsyncGraphDatabase.driver(
|
|
|
|
|
uri=uri,
|
2025-06-13 14:12:09 -04:00
|
|
|
auth=(user or '', password or ''),
|
2025-06-13 12:06:57 -04:00
|
|
|
)
|
|
|
|
|
|
2025-06-30 12:04:21 -07:00
|
|
|
async def execute_query(self, cypher_query_: LiteralString, **kwargs: Any) -> EagerResult:
|
2025-06-13 12:06:57 -04:00
|
|
|
params = kwargs.pop('params', None)
|
|
|
|
|
result = await self.client.execute_query(cypher_query_, parameters_=params, **kwargs)
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def session(self, database: str) -> GraphDriverSession:
|
|
|
|
|
return self.client.session(database=database) # type: ignore
|
|
|
|
|
|
|
|
|
|
async def close(self) -> None:
|
|
|
|
|
return await self.client.close()
|
|
|
|
|
|
2025-06-30 12:04:21 -07:00
|
|
|
def delete_all_indexes(
|
|
|
|
|
self, database_: str = DEFAULT_DATABASE
|
|
|
|
|
) -> Coroutine[Any, Any, EagerResult]:
|
2025-06-13 12:06:57 -04:00
|
|
|
return self.client.execute_query(
|
|
|
|
|
'CALL db.indexes() YIELD name DROP INDEX name',
|
|
|
|
|
database_=database_,
|
|
|
|
|
)
|