Neo4j + AI: Building Intelligent Applications with Knowledge Graphs
Why Neo4j for AI
Neo4j is the leading graph database for a reason: it makes relationship-rich data feel natural. When you combine Neo4j with AI, you get a stack where the database handles structured knowledge and the LLM handles natural language.
Use Case 1: Graph-Backed RAG
Replace flat vector search with Cypher-powered retrieval:
// Find all papers and their authors related to a topic
MATCH (p:Paper)-[:ABOUT]->(t:Topic {name: "GraphRAG"})
MATCH (p)<-[:AUTHORED]-(a:Author)
RETURN p.title, a.name, p.abstract
Feed the structured results into your LLM prompt for grounded, relationship-aware answers.
Use Case 2: Natural Language to Cypher
Let users ask questions in plain English:
User: "Show me all authors who wrote about GraphRAG and Neo4j"
LLM generates:
MATCH (a:Author)-[:AUTHORED]->(p:Paper)
WHERE p.abstract CONTAINS "GraphRAG"
AND p.abstract CONTAINS "Neo4j"
RETURN a.name, p.title
Use GraphCypherQAChain from LangChain or write your own prompt template. Always validate generated Cypher before execution.
Use Case 3: Autonomous Graph Agents
Build agents that explore and enrich the graph autonomously:
- Query agent: Given a user question, traverses the graph to find relevant sub-graphs
- Extraction agent: Processes new documents, extracts entities, inserts into graph
- Maintenance agent: Detects stale or contradictory relationships, flags for review
Getting Started
docker run \
--name neo4j-ai \
--publish=7474:7474 --publish=7687:7687 \
-e NEO4J_AUTH=neo4j/password \
-e NEO4J_PLUGINS='["apoc"]' \
neo4j:5
Then connect from Python:
from neo4j import GraphDatabase
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))
with driver.session() as session:
result = session.run("MATCH (n) RETURN count(n) AS count")
print(result.single()["count"])
The Neo4j knowledge graph infrastructure documented in AGENTS.md connects directly to these patterns. The graph is already running — you just need to query it.