Spaces:
Runtime error
Runtime error
Download graph_store.py from its-seraj/rag-backend-api: direct link, hf CLI and curl.
- Browser
- Download file 7.24 kB
-
https://huggingface.co/spaces/its-seraj/rag-backend-api/resolve/main/graph_store.py
- Command line
-
hf download hf://spaces/its-seraj/rag-backend-api/graph_store.py
-
curl -L -o graph_store.py https://huggingface.co/spaces/its-seraj/rag-backend-api/resolve/main/graph_store.py
7.24 kB
| 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.") | |