Convert Neo4j query result to a networkx graph [chapter] deterministic
def neo4j_to_nx(session, cypher_query: str): rows = session.run(cypher_query) G = nx.MultiDiGraph() for r in rows: subj = r["s.name"] obj = r["o.name"] rel = r["r"] G.add_node(subj, label=subj) G.add_
def neo4j_to_nx(session, cypher_query: str): rows = session.run(cypher_query) G = nx.MultiDiGraph() for r in rows: subj = r["s.name"] obj = r["o.name"] rel = r["r"] G.add_node(subj, label=subj) G.add_node(obj, label=obj) G.add_edge(subj, obj, relation=rel) return G query = """ MATCH (s)-[r]->(o) RETURN s.name AS s, r AS r, o.name AS o """ G = neo4j_to_nx(session, query) net = network_to_pyvis(G) net.save_graph("graph.html")
`
The resulting graph.html can be opened in any browser, allowing you to zoom, pan, and inspect edges. This visual feedback is invaluable when debugging extraction pipelines or when presenting the knowledge base to a **cybersecurity specialist** for review.
Querying with Cypher Cypher is the declarative query language for Neo4j. It enables you to express complex traversals succinctly. For example, to find all tools used by projects that are related to “local control mechanisms”, you could write:
```cypher MATCH (p:Project)-[:uses]->(t:Tool) WHERE p.name CONTAINS 'local' RETURN p.name, t.name
`
You can also use apoc procedures for more advanced graph algorithms, such as community detection or shortest‑path computation. These queries can be exposed via a lightweight API (e.g., FastAPI) that the RAG pipeline calls during retrieval.
Integrating Visualization and Querying into the Development Workflow
A typical local‑first workflow looks like this:
1. Ingest documents → extract triples → store in Neo4j.
2. Visualize the graph with pyvis or neo4j Bloom to verify structure.
3. Run a small set of Cypher queries to confirm that expected entities exist.
4. Hook the graph retriever into the RAG pipeline.
5. Iterate on the extraction prompt and taxonomy based on query results.
Because all components run locally, you can iterate quickly without worrying about external API costs or data leakage. This aligns with the principles of **local control mechanisms**, ensuring that the knowledge graph remains a sovereign asset.
Conclusion Knowledge graphs transform raw documents into a navigable, semantically rich structure that enhances AI reasoning. By building the graph locally, you gain privacy, sovereignty, and full control over the data pipeline. The **CLASSIFIER_SYSTEM_PROMPT** gives you a consistent way to label entities, while a **cybersecurity specialist** can harden the graph store against misuse. Projects like the **BlogGenerator Wiki Page** benefit from a well‑structured graph that keeps content generation coherent, and tools like **Cola** accelerate prototyping by providing a ready‑made local graph engine. In practice, the workflow involves document ingestion, chunking, extraction using a classifier prompt, storage in a graph database, and graph‑augmented retrieval. Visualization and Cypher querying let you inspect and interrogate the graph, ensuring that the knowledge base aligns with your domain taxonomy. With these tools, you can build local‑first AI systems that are both powerful and trustworthy. As you move forward, consider extending the graph with ontologies, adding temporal edges for versioned documents, and experimenting with graph‑based reasoning models. The knowledge graph is not a one‑time artifact; it is a living knowledge base that grows with your data and your AI capabilities. By keeping it local, you preserve control, enhance reliability, and lay the foundation for sophisticated, context‑aware AI applications. *End of Chapter 5.*
Source Code and Repositories
This chapter draws from the following open-source projects by DanielKliewer:
- **PersonaGen**: https://github.com/kliewerdaniel/PersonaGen
- **dynamic_persona_moe_rag**: https://github.com/kliewerdaniel/dynamic_persona_moe_rag
- **workflow**: https://github.com/kliewerdaniel/workflow
- **sovereign**: https://github.com/kliewerdaniel/sovereign
- **sovereignSpec**: https://github.com/kliewerdaniel/sovereignSpec
- **cogGra**: https://github.com/kliewerdaniel/cogGra
For more projects, visit https://github.com/kliewerdaniel
---