From 822cc55b027630f31d5409e48abc355097b1f917 Mon Sep 17 00:00:00 2001 From: Matea Pesic <80577904+matea16@users.noreply.github.com> Date: Fri, 23 May 2025 08:55:55 +0200 Subject: [PATCH] Update Memgraph integration (#850) ## Description Update Memgraph integration with the missing abstract methods `get_neighbors`, `get_node`, `get_nodes` ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin. --------- Co-authored-by: github-actions[bot] --- .env.template | 4 ++-- CONTRIBUTORS.md | 12 +++++++++++ .../graph/memgraph/memgraph_adapter.py | 21 ++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 CONTRIBUTORS.md diff --git a/.env.template b/.env.template index cfcb62bb4..4cc11d96e 100644 --- a/.env.template +++ b/.env.template @@ -27,9 +27,9 @@ EMBEDDING_API_VERSION="" EMBEDDING_DIMENSIONS=3072 EMBEDDING_MAX_TOKENS=8191 -# "neo4j", "networkx" or "kuzu" +# "neo4j", "networkx", "kuzu" or "memgraph" GRAPH_DATABASE_PROVIDER="networkx" -# Only needed if using neo4j +# Only needed if using neo4j or memgraph GRAPH_DATABASE_URL= GRAPH_DATABASE_USERNAME= GRAPH_DATABASE_PASSWORD= diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md new file mode 100644 index 000000000..523695a95 --- /dev/null +++ b/CONTRIBUTORS.md @@ -0,0 +1,12 @@ +## 💫 Contributors + +Thanks to our amazing contributors! 💖 + + + + + +## 🏆 Top Contributors + +| Rank | Contributor | Contributions | +|------|------------|---------------| diff --git a/cognee/infrastructure/databases/graph/memgraph/memgraph_adapter.py b/cognee/infrastructure/databases/graph/memgraph/memgraph_adapter.py index 5ef438077..f74143971 100644 --- a/cognee/infrastructure/databases/graph/memgraph/memgraph_adapter.py +++ b/cognee/infrastructure/databases/graph/memgraph/memgraph_adapter.py @@ -358,13 +358,32 @@ class MemgraphAdapter(GraphDBInterface): return [result["successor"] for result in results] - async def get_neighbours(self, node_id: str) -> List[Dict[str, Any]]: + async def get_neighbors(self, node_id: str) -> List[Dict[str, Any]]: predecessors, successors = await asyncio.gather( self.get_predecessors(node_id), self.get_successors(node_id) ) return predecessors + successors + async def get_node(self, node_id: str) -> Optional[Dict[str, Any]]: + """Get a single node by ID.""" + query = """ + MATCH (node {id: $node_id}) + RETURN node + """ + results = await self.query(query, {"node_id": node_id}) + return results[0]["node"] if results else None + + async def get_nodes(self, node_ids: List[str]) -> List[Dict[str, Any]]: + """Get multiple nodes by their IDs.""" + query = """ + UNWIND $node_ids AS id + MATCH (node {id: id}) + RETURN node + """ + results = await self.query(query, {"node_ids": node_ids}) + return [result["node"] for result in results] + async def get_connections(self, node_id: UUID) -> list: predecessors_query = """ MATCH (node)<-[relation]-(neighbour)