A production-ready, graph-native long-term memory system for AI applications. Automatically extracts entities and relationships from conversations, maintains temporal context in Neo4j, and enables intelligent multi-hop reasoning for enhanced AI responses.
Traditional LLMs are stateless and forget everything after each conversation. This system provides:
- Persistent Memory: Store and recall information across sessions
- Relationship Intelligence: Understand connections between entities
- Temporal Tracking: Know when information was learned and updated
- Multi-hop Reasoning: Answer complex questions requiring multiple context pieces
- Audit Trail: Complete history of all stored conversations
- Personal AI assistants that remember user preferences and history
- Customer support systems that maintain context across interactions
- Research tools that build knowledge graphs from documents
- Team collaboration bots that track project relationships
- Any AI application requiring long-term contextual memory
CONVERSATION INGESTION PIPELINE
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Input: "Alice is a software engineer at Google working with Bob" β
ββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββ
β memory_manager.py β
β add_memory() β
βββββββββββ¬βββββββββββ
β
ββββΊ Generate conversation_id: conv_20241012_102030
β
βΌ
ββββββββββββββββββββββ
β entity_extractor.pyβ
β extract_from_text()β
βββββββββββ¬βββββββββββ
β
βΌ
ββββββββββββββββββββββ
β Gemini API Call β
β (LLM Processing) β
βββββββββββ¬βββββββββββ
β
βΌ
ββββββββββββββββ΄βββββββββββββββ
β β
βΌ βΌ
ββββββββββββββββββββ ββββββββββββββββββββ
β Extracted β β Extracted β
β Entities: β β Relations: β
β β β β
β - Alice:person β β - AliceβGoogle β
β - Google:org β β [WORKS_AT] β
β - Bob:person β β - AliceβBob β
β β β [WORKS_WITH] β
ββββββββββ¬ββββββββββ ββββββββββ¬ββββββββββ
β β
ββββββββββββββββ¬βββββββββββββββ
β
βΌ
ββββββββββββββββββββββ
β graph_db.py β
β Neo4j Operations β
βββββββββββ¬βββββββββββ
β
ββββββββββββββββΌβββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββββββββ ββββββββββββββ ββββββββββββββββ
β MERGE Entity β β MERGE β β CREATE β
β Nodes β β Relation β β JSON Log β
β β β Edges β β β
β SET props, β β SET props, β β conversationsβ
β observations, β β confidence,β β /conv_*.json β
β timestamps β β timestamps β β β
ββββββββββ¬ββββββββ ββββββββ¬ββββββ ββββββββ¬ββββββββ
β β β
ββββββββββββββββββΌββββββββββββββββ
β
βΌ
βββββββββββββββββββ
β Neo4j Graph β
β Database β
β β
β (Alice)βWORKS_ATβ(Google) β
β β β
β ββWORKS_WITHβ(Bob) β
βββββββββββββββββββ
QUERY & RETRIEVAL PIPELINE
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Query: "What does Alice do?" β
ββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββ
β memory_manager.py β
β chat_with_context()β
βββββββββββ¬βββββββββββ
β
βΌ
ββββββββββββββββββββββ
β graph_db.py β
β search_entities() β
βββββββββββ¬βββββββββββ
β
ββββΊ Extract terms: ["Alice"]
β
βΌ
ββββββββββββββββββββββ
β Cypher Query: β
β MATCH (e:Entity) β
β WHERE name β
β CONTAINS "Alice" β
βββββββββββ¬βββββββββββ
β
βΌ
ββββββββββββββββββββββ
β Found: Alice β
β Type: person β
βββββββββββ¬βββββββββββ
β
βΌ
ββββββββββββββββββββββ
β graph_db.py β
βget_entity_context()β
βββββββββββ¬βββββββββββ
β
βΌ
ββββββββββββββββββββββ
β Multi-hop Query: β
β MATCH path= β
β (e)-[*1..2]-(c) β
βββββββββββ¬βββββββββββ
β
βΌ
ββββββββββββββββ΄βββββββββββββββ
β β
βΌ βΌ
ββββββββββββββββββββ ββββββββββββββββββββ
β Entity Details: β β Connections: β
β β β β
β Name: Alice β β - Google (org) β
β Type: person β β via WORKS_AT β
β Facts: β β - Bob (person) β
β - software β β via WORKS_WITH β
β engineer β β β
ββββββββββ¬ββββββββββ ββββββββββ¬ββββββββββ
β β
ββββββββββββββββ¬βββββββββββββββ
β
βΌ
ββββββββββββββββββββββ
β Format Context: β
β β
β "Alice is a β
β software engineerβ
β connected to β
β Google and Bob" β
βββββββββββ¬βββββββββββ
β
βΌ
ββββββββββββββββββββββ
β llm_client.py β
βgenerate_response() β
βββββββββββ¬βββββββββββ
β
ββββΊ Send: Query + Context
β
βΌ
ββββββββββββββββββββββ
β Gemini API Call β
β (LLM Generation) β
βββββββββββ¬βββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Response: "Alice is a software engineer at Google who works with β
β Bob on various projects." β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Component | Technology | Purpose |
|---|---|---|
| Graph Database | Neo4j Desktop | Entity and relationship storage with native graph operations |
| LLM | Google Gemini API | Entity extraction and context-aware response generation |
| Backend | Python 3.10+ | Application logic and orchestration |
| Graph Driver | neo4j-driver | Python interface to Neo4j database |
| Storage | JSON files | Conversation audit logs |
NEO4J KNOWLEDGE GRAPH REPRESENTATION
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββ
β Dr. Smith β
β Type: person β
β βββββββββββββ β
β - leads AI div β
β - oversees proj β
ββββββββββββ¬βββββββββββ
β
β MANAGES
β
ββββββββββββββββββΌβββββββββββββββββ
β β β
β β β
βββββββββββΌβββββββββ β ββββββββββΌββββββββββ
β Alice β β β Bob β
β Type: person β β β Type: person β
β ββββββββββββββ β β β ββββββββββββββ β
β - software eng β β β - senior res β
β - specializes βββββββββΌβββββββ€ - collaborates β
β in NLP β WORKS_WITH β with Alice β
ββββββ¬βββββββββ¬βββββ β βββββββ¬βββββββββββββ
β β β β
WORKS_ATβ βWORKS_ON β βWORKS_ON
β β β β
ββββββΌβββββ β ββββββββββΌβββββββββββββΌβββββββββ
β Google β β β Neo4j Integration Project β
βType:org β β β Type: project β
βββββββββββ β β ββββββββββββββββββββββββββ β
β- tech coβ β β - builds scalable memory β
βββββββββββ β β - budget: 2M β
β β - status: active β
β βββββββββββββββββββββββββββββββββ
β
β
ββββββββββΌβββββββββ
β Knowledge β
β Graph System β
β Type: project β
β βββββββββββββ β
β - uses Neo4j β
β - for AI mem β
βββββββββββββββββββ
RELATIONSHIP TYPES:
βββββββββββββββββββ
WORKS_AT : Employment relationship
WORKS_WITH : Collaboration relationship
WORKS_ON : Project involvement
MANAGES : Management hierarchy
COLLABORATES_ON : Joint work on specific items
NODE PROPERTIES:
ββββββββββββββββ
name : Entity identifier
entity_type : Category (person, organization, project, etc.)
observations : Array of facts/attributes
created_at : Timestamp of first mention
last_updated : Timestamp of latest update
EDGE PROPERTIES:
ββββββββββββββββ
type : Relationship category
confidence : Score (0.0-1.0)
created_at : When relationship was established
valid_from : Temporal validity start
valid_to : Temporal validity end (null = current)
source_conv : Originating conversation ID
- Python 3.10 or higher
- Neo4j Desktop (free)
- Google Gemini API key (free tier available)
Download from https://neo4j.com/download/
- Create new Project
- Add Local DBMS
- Name:
ai-memory-graph - Password: Set secure password
- Version: Latest 5.x
- Name:
- Start database (verify green status)
cd ai-memory-graph
python -m venv venv
# Windows
venv\Scripts\activate
# Mac/Linux
source venv/bin/activate
pip install -r requirements.txt# Windows
copy .env.example .env
# Mac/Linux
cp .env.example .envEdit .env file:
GEMINI_API_KEY=your_gemini_api_key
NEO4J_PASSWORD=your_neo4j_password
NEO4J_URI=bolt://localhost:7687
NEO4J_USER=neo4j
NEO4J_DATABASE=neo4j
Get Gemini API key: https://ai.google.dev/
python main.pyStore information in the knowledge graph:
Select option: 1
> Alice is a software engineer at Google working on AI projects with Bob.
Processing...
Success!
Entities created: 4
Relations created: 3
Conversation ID: conv_20241012_102030
Find specific entities:
Select option: 2
> Alice
Searching...
1. Alice (person)
- software engineer
Ask questions that use stored memory:
Select option: 3
> What does Alice do?
Response:
Alice is a software engineer at Google who works on AI projects with Bob.
View system statistics:
Select option: 5
Memory Statistics:
Total Entities: 12
Total Relations: 8
ai-memory-graph/
βββ config/
β βββ settings.py # Configuration and environment management
βββ core/
β βββ llm_client.py # Gemini API client wrapper
β βββ graph_db.py # Neo4j database operations
β βββ entity_extractor.py # Entity and relation extraction
β βββ memory_manager.py # High-level orchestration layer
βββ utils/
β βββ helpers.py # Utility functions and logging
βββ data/
β βββ conversations/ # JSON conversation logs
βββ tests/
β βββ test_basic.py # Unit tests
βββ main.py # CLI interface
βββ requirements.txt # Python dependencies
βββ .env.example # Environment template
βββ README.md # Documentation
Orchestrates the entire flow: accepts input, coordinates extraction, manages storage, and handles retrieval.
Uses Gemini to parse natural language and extract structured entities and relationships with confidence scores.
Manages Neo4j operations: CRUD for entities/relations, search, multi-hop traversal, and temporal queries.
Wrapper for Gemini API with structured output parsing and error handling.
{
"name": "Alice",
"entity_type": "person",
"observations": ["software engineer", "works on AI"],
"created_at": "2024-10-12T10:30:00",
"last_updated": "2024-10-12T10:30:00"
}{
"type": "WORKS_WITH",
"confidence": 0.95,
"created_at": "2024-10-12T10:30:00",
"valid_from": "2024-10-12T10:30:00",
"valid_to": null,
"source_conversation": "conv_20241012_102030"
}Run unit tests:
pytest tests/Run with coverage:
pytest --cov=core tests/- Verify database is running (green status in Neo4j Desktop)
- Check password in
.envmatches database password - Ensure port 7687 is available
- Try restarting Neo4j Desktop
- Verify API key is valid at https://ai.google.dev/
- Check for extra spaces in
.envfile - Ensure API key has no usage limits exceeded
- Verify internet connection
- Activate virtual environment
- Reinstall dependencies:
pip install -r requirements.txt - Check Python version:
python --version(must be 3.10+)
- Entity searches use indexed lookups (O(log n))
- Relationship traversal limited to 2 hops by default
- Search results limited to 10 entities
- Conversation logs stored as separate JSON files
- Store
.envfile securely (never commit to version control) - Use strong Neo4j passwords
- Rotate Gemini API keys periodically
- Sanitize user input before storage
- Review conversation logs for sensitive data
Potential improvements for production deployment:
- Vector embeddings for semantic search
- Entity resolution and deduplication
- Importance scoring and memory pruning
- Multi-user support with authentication
- REST API for programmatic access
- Cloud deployment (GCP, AWS, Azure)
- Batch processing for document ingestion
- Export/import functionality
For production deployment on GCP:
- Replace Neo4j Desktop with Neo4j Aura (managed)
- Deploy application on Cloud Run
- Store logs in Cloud Storage
- Use Secret Manager for credentials
- Configure Cloud Logging and Monitoring
MIT License
Contributions are welcome. Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request with clear description
For issues and questions:
- Check this README and troubleshooting section
- Review application logs
- Verify all prerequisites are met
- Check Neo4j and Gemini API status