
Relational Wisdom: Graph Databases in AI
Go beyond similarity. Learn how to use Neo4j and Knowledge Graphs to store complex associations and transitive relationships for your agents.
Graph Databases for Complex Relationships
Most AI agents use Vector Databases (Chroma, Pinecone). These are great for finding "Similar" things, but they are terrible at finding "Connected" things.
If you ask an agent: "Find the email address of the person who introduced me to the CEO of Google," a vector search will find 1,000 emails mentioning "CEO" or "Google." It won't find the specific Chain of Introduction. For this, you need a Knowledge Graph.
1. Vector vs. Graph Memory
- Vector (Semantic): "I found this document because it's about the same topic as your query."
- Graph (Relational): "I found this person because they know Person Y, and Person Y works for Company Z."
2. The Multi-Hop Retrieval Problem
Multi-hop questions are the "Final Boss" of RAG.
- Question: "What is the phone number of the office where the Q3 board meeting was held?"
- Reasoning Loop:
- Find "Q3 Board Meeting".
- Find the "Location" of that meeting.
- Find the "Contact Info" for that location.
In a Vector DB, these are 3 separate searches with a high risk of error. In a Graph DB (like Neo4j), it is a single Traversals query.
3. Implementing Graph Memory in Agents
We use a pattern called Schema-Based Ingestion.
- The Graph: Nodes for People, Documents, Companies, and Events.
- The Tool:
query_knowledge_graph(cypher_query: str). - The Workflow:
- The LLM translates the user's natural language into a Cypher query (the SQL of Graph databases).
- The tool executes the query and returns the results.
4. GraphRAG: Combining the Best of Both Worlds
In production, we often store the Summary of a node as a Vector.
- You search the Vector DB to find the "Entry Node" (e.g., "The Meeting").
- Once you are at the node, you use the Graph to "Spread out" to its neighbors (e.g., "The Attendees").
5. Use Case: Corporate Knowledge Management
Imagine an agent for an HR department.
- Graph Structure:
[Employee] -- (REPORT_TO) --> [Manager],[Manager] -- (MANAGES) --> [Budget]. - Query: "Who is currently overseeing the budget for the Project X team?"
- Agent Logic: The agent traverses from
Project X->Team Members->Manager->Dedicated Budget.
6. Real-World Constraint: Data Quality
A graph is only as good as its Ingestion Logic.
- If you just dump 1,000 PDFs into a graph, the connections will be messy and vague.
- Solution: Use a "Schema-Strict" Extraction agent (Module 11.2) to identify entities before they are saved to the graph.
Summary and Mental Model
Think of a Vector DB like a Global Library Index. You can find books by title or topic.
Think of a Graph DB like a Facebook Friends List. You can see who knows who, who works where, and how you are connected to anyone in the system.
For personal agents, the "Network" of the user's life is more important than the "Words" they say.
Exercise: Graph Modeling
- Traversals: You are building an agent for Crime Scene Investigators.
- Draft the Nodes and Edges for a "Suspect Knowledge Graph."
- (e.g., Node: [Person], Edge: (OWNED_VEHICLE)).
- Selection: Why is a Graph DB better for a "Recommendation Engine" ("Users like you also bought X") than a Vector DB?
- Querying: Write a pseudo-code "Cypher" query to find "All documents written by 'John' in the year 2024." Ready for the operational side? Next lesson: Memory Management Strategies.