Spaces:
Runtime error
Runtime error
File size: 7,236 Bytes
d3f7d23 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | import os
import logging
from typing import List, Dict, Any
from neo4j import GraphDatabase
logger = logging.getLogger("rag_backend")
class Neo4jGraphStore:
def __init__(self):
self.uri = os.getenv("NEO4J_URI", "bolt://localhost:7687")
self.username = os.getenv("NEO4J_USERNAME", "neo4j")
self.password = os.getenv("NEO4J_PASSWORD", "password")
self.driver = None
self.is_connected = False
if not os.getenv("NEO4J_URI") and not os.getenv("NEO4J_PASSWORD"):
logger.warning("NEO4J_URI and NEO4J_PASSWORD not found in environment variables. Graph features will be disabled or fallback to default bolt://localhost:7687.")
try:
logger.info(f"Connecting to Neo4j at {self.uri}...")
self.driver = GraphDatabase.driver(self.uri, auth=(self.username, self.password))
# Verify connection with a lightweight query
with self.driver.session() as session:
session.run("RETURN 1")
self.is_connected = True
logger.info("Connected to Neo4j successfully.")
self._ensure_constraints()
except Exception as e:
logger.error(f"Failed to connect to Neo4j: {e}. GraphRAG feature will run in fallback (degraded) mode.")
self.driver = None
self.is_connected = False
def _ensure_constraints(self):
"""Creates unique constraints to prevent duplicate entities and chunks."""
if not self.driver:
return
try:
with self.driver.session() as session:
session.run("CREATE CONSTRAINT unique_entity_name IF NOT EXISTS FOR (e:Entity) REQUIRE e.name IS UNIQUE")
session.run("CREATE CONSTRAINT unique_chunk_id IF NOT EXISTS FOR (c:Chunk) REQUIRE c.id IS UNIQUE")
logger.info("Neo4j database constraints verified/created.")
except Exception as e:
logger.error(f"Failed to create Neo4j constraints: {e}")
def add_graph_data(
self,
chunk_id: str,
chunk_text: str,
source: str,
entities: List[Dict[str, Any]],
relationships: List[Dict[str, Any]]
) -> bool:
"""
Inserts entities, relationships, and links them to the source chunk in Neo4j.
"""
if not self.driver:
logger.warning("Neo4j driver not initialized. Skipping graph ingestion.")
return False
query = """
// 1. Create Chunk
MERGE (c:Chunk {id: $chunk_id})
SET c.text = $chunk_text, c.source = $source
// 2. Create Entities & Link to Chunk
WITH c
UNWIND $entities AS ent
MERGE (e:Entity {name: ent.name})
ON CREATE SET e.type = ent.type, e.description = ent.description
MERGE (c)-[:MENTIONS]->(e)
// 3. Create Relationships between Entities
WITH c
UNWIND $relationships AS rel
MERGE (source:Entity {name: rel.source})
MERGE (target:Entity {name: rel.target})
MERGE (source)-[r:CONNECTS_TO {relationship_type: rel.type}]->(target)
ON CREATE SET r.description = rel.description
"""
try:
with self.driver.session() as session:
session.run(
query,
chunk_id=chunk_id,
chunk_text=chunk_text,
source=source,
entities=entities,
relationships=relationships
)
return True
except Exception as e:
logger.error(f"Error adding data to Neo4j: {e}")
return False
def query_subgraph(self, entity_names: List[str]) -> str:
"""
Retrieves 1-hop and 2-hop connected entities for context injection.
"""
if not self.driver:
return "Graph database connection is not available."
if not entity_names:
return "No entities provided for graph query."
# Case-insensitive or direct match? Since they are extracted, exact matches on normalized names are best.
query = """
MATCH (e:Entity) WHERE e.name IN $names
MATCH (e)-[r:CONNECTS_TO]-(neighbor:Entity)
RETURN e.name AS source, type(r) AS rel, r.relationship_type AS rel_type, r.description AS desc, neighbor.name AS target
LIMIT 30
"""
try:
with self.driver.session() as session:
result = session.run(query, names=entity_names)
records = list(result)
if not records:
return f"No graph relationships found for entities: {', '.join(entity_names)}"
lines = []
for record in records:
rel_label = record['rel_type'] or record['rel']
desc = f" ({record['desc']})" if record['desc'] else ""
lines.append(f"({record['source']}) -[{rel_label}{desc}]-> ({record['target']})")
return "\n".join(lines)
except Exception as e:
logger.error(f"Error querying Neo4j subgraph: {e}")
return f"Error querying graph database: {str(e)}"
def clear(self) -> bool:
"""Clears all nodes and relationships from the Neo4j database."""
if not self.driver:
return False
try:
with self.driver.session() as session:
session.run("MATCH (n) DETACH DELETE n")
logger.info("Neo4j database has been reset successfully.")
return True
except Exception as e:
logger.error(f"Error clearing Neo4j database: {e}")
return False
def get_status(self) -> Dict[str, Any]:
"""Returns connection status and basic database metrics."""
if not self.driver:
return {
"connection_type": "Disconnected",
"uri": self.uri,
"status": "offline",
"nodes_count": 0,
"relationships_count": 0
}
try:
with self.driver.session() as session:
nodes_res = session.run("MATCH (n) RETURN count(n) AS c")
nodes_count = nodes_res.single()["c"]
rels_res = session.run("MATCH ()-[r]->() RETURN count(r) AS c")
rels_count = rels_res.single()["c"]
return {
"connection_type": f"Neo4j ({self.uri})",
"status": "online",
"nodes_count": nodes_count,
"relationships_count": rels_count
}
except Exception as e:
logger.error(f"Error getting Neo4j status: {e}")
return {
"connection_type": f"Neo4j ({self.uri})",
"status": "error",
"error": str(e),
"nodes_count": 0,
"relationships_count": 0
}
def close(self):
"""Closes the Neo4j driver connection."""
if self.driver:
self.driver.close()
logger.info("Neo4j connection closed.")
|