A production-grade RAG-powered context layer for inbox agents with hybrid retrieval, local LLM integration, and precedent-aware learning.
Full-stack system demonstrating advanced retrieval techniques and AI agent learning from human feedback.
- โ Intelligent Agent - LLM-powered reasoning with deep context understanding
- โ Intent Analysis - Automatic message intent, urgency, and topic extraction
- โ Multi-Modal Retrieval - Vector (semantic) + Keyword (BM25) + Graph (precedent)
- โ Local LLM Integration - Ollama/Llama3 for reasoning and draft generation
- โ Cross-Encoder Reranking - Improve retrieval quality with re-scoring
- โ Vector Database - ChromaDB for persistent embeddings
- โ Local Embeddings - sentence-transformers (no API required)
- โ Context Graph - PostgreSQL graph schema for decision relationships
- โ Precedent Learning - Agent learns from human overrides and patterns
- โ Graph Visualization - React Flow interactive display
- โ Production RAG - Chunking, metadata filtering, hybrid fusion
# Start all services
docker-compose up
# Initialize database (first time only)
docker-compose exec backend python init_db.py
# Open http://localhost:5173See QUICKSTART.md for detailed instructions.
# 1. Create PostgreSQL database
createdb inbox_context_graph
# 2. Setup backend
cd backend
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
echo "DATABASE_URL=postgresql://postgres:postgres@localhost:5432/inbox_context_graph" > .env
python init_db.py
uvicorn main:app --reload
# 3. Setup frontend (new terminal)
cd frontend
npm install
npm run dev
# 4. Open http://localhost:5173- QUICKSTART.md - Step-by-step setup guide
- DEMO_GUIDE.md - How to demonstrate the before/after learning
- ARCHITECTURE.md - Deep dive into system design
โโโโโโโโโโโโโโโ
โ React UI โ โ Inbox, Graph Viz, Decision History
โโโโโโโโฌโโโโโโโ
โ REST API
โโโโโโโโผโโโโโโโ
โ FastAPI โ โ Agent Engine, Hybrid Retrieval
โโโโโโโโฌโโโโโโโ
โ SQL
โโโโโโโโผโโโโโโโ
โ PostgreSQL โ โ Messages, Decisions, Context Graph
โโโโโโโโโโโโโโโ
Tech Stack:
- Frontend: React + Tailwind CSS + React Flow
- Backend: Python FastAPI
- Database: PostgreSQL (graph-style schema)
- Embeddings: OpenAI API (optional, works without)
User clicks message
โ
1๏ธโฃ DEEP ANALYSIS (LLM)
โ Extract intent, topics, urgency
โ Identify if action required
โ
2๏ธโฃ HYBRID RETRIEVAL
โ Semantic search (embeddings)
โ Keyword search (BM25)
โ Graph traversal (precedents)
โ Cross-encoder reranking
โ
3๏ธโฃ LLM REASONING
โ Analyze message + context + precedents
โ Make sophisticated decision
โ Explain reasoning with references
โ
4๏ธโฃ DRAFT GENERATION
โ Generate contextual email draft
โ Match tone and style
โ
5๏ธโฃ PRESENT TO USER
โ Show action, tone, reasoning
โ Display draft response
โ User accepts or overrides
โ
6๏ธโฃ LEARNING
โ Capture decision trace
โ Update context graph
โ Store in vector DB
๐ง Message from Investor: "Can we sync about Q4 metrics?"
โ
๐ Analysis: urgent_request, topics: [Q4, metrics, sync]
โ
๐ค Agent Decision: reply_now + neutral
Reasoning: "Urgent investment inquiry requires immediate attention"
Draft: "Happy to sync! I'm available Tuesday or Wednesday..."
โ
โ๏ธ User Override: reply_now + warm
โ
โ
Decision captured as precedent
๐ง Another Investor Message: "Following up on our last chat..."
โ
๐ Analysis: follow_up, topics: [follow_up, previous discussion]
โ
๐ Retrieval: Finds 5 similar investor interactions
โ User always chose "warm" tone
โ Always replied within same day
โ
๐ค Agent Decision: reply_now + warm
Reasoning: "Based on 5 past investor messages, you consistently
reply warmly and promptly. The follow-up nature suggests this
is part of an ongoing conversation requiring timely response."
Draft: "Thanks for following up! Here's where we are..."
โ
โ
User accepts (validates learned pattern!)
Every decision becomes nodes + edges:
Message โ Decision โ Action
โ Tone
โ SenderType
โ PrecedentDecisions
Every decision is captured with full context:
{
"decision_id": "uuid",
"message_id": "msg_123",
"agent_suggestion": { "action": "reply_now", "tone": "neutral" },
"human_action": { "action": "reply_now", "tone": "warm" },
"context_used": {
"sender_type": "investor",
"similar_decisions": ["dec_12", "dec_19"]
},
"why": "Based on 2 prior investor messages, you usually replied warmly",
"timestamp": "2024-01-15T10:30:00Z"
}Combines two strategies:
- Semantic Similarity - Find messages with similar content (embeddings)
- Structured Filtering - Filter by sender_type (investor/sales/support)
Result: Context-aware precedent that actually makes sense.
Interactive graph showing:
- Message nodes (purple)
- Decision nodes (blue)
- Action/Tone/SenderType nodes (yellow/orange/green)
- Precedent edges (red, animated)
Click any decision node to see full trace.
# Run API tests
cd backend
python test_api.py
# Should see:
# โ
ALL TESTS PASSED!- Reset - Click "Reset Demo" to start fresh
- Make 2-3 decisions - Notice generic reasoning ("No precedent found")
- Make 2-3 more - Notice precedent appearing ("Based on 3 prior messages...")
- View Graph - See decisions forming a network
- Celebrate - The agent learned from you! ๐
This pattern works for:
- Email management - Learn reply patterns per sender type
- Customer support - Learn escalation patterns per issue type
- Code review - Learn approval patterns per code smell
- Content moderation - Learn moderation decisions per violation type
Any domain where:
- AI makes suggestions
- Humans make final decisions
- Patterns emerge over time
cd backend
source venv/bin/activate
uvicorn main:app --reload # Auto-reload on changescd frontend
npm run dev # Hot module replacement# Reset decisions only (keep messages)
curl -X POST http://localhost:8000/reset
# Full reset (including messages)
cd backend
python init_db.py# Required
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/inbox_context_graph
# Optional (system works without it using mock embeddings)
OPENAI_API_KEY=sk-your-key-hereDemo-quality, not production-ready:
- Real working code, not mockups
- Visible behavior change (the key demo requirement)
- Simple but correct implementation
- Prioritize clarity over optimization
NOT included (intentionally):
- Authentication/authorization
- Rate limiting
- Error recovery
- Production deployment config
- Extensive test coverage
- Performance optimization
This is a working prototype to demonstrate the concept. It shows the core idea clearly.
โโโ backend/
โ โโโ main.py # FastAPI app
โ โโโ models.py # SQLAlchemy models
โ โโโ agent.py # Agent suggestion engine
โ โโโ embeddings.py # Semantic similarity
โ โโโ init_db.py # Database initialization
โ โโโ requirements.txt
โโโ frontend/
โ โโโ src/
โ โ โโโ App.jsx # Main app component
โ โ โโโ components/
โ โ โ โโโ MessageList.jsx
โ โ โ โโโ MessageDetail.jsx
โ โ โ โโโ GraphViewer.jsx
โ โ โ โโโ DecisionHistory.jsx
โ โ โโโ api.js # API client
โ โโโ package.json
โโโ QUICKSTART.md # Setup instructions
โโโ DEMO_GUIDE.md # Demo walkthrough
โโโ ARCHITECTURE.md # Technical deep dive
โโโ docker-compose.yml # Docker setup
This is a demonstration project. Feel free to fork and extend!
Ideas for extensions:
- Multi-user support
- Real Gmail/Slack integration
- Temporal patterns ("you reply faster on Mondays")
- Active learning ("I'm uncertain, want to guide me?")
- Better graph layout algorithms
MIT - Use freely for demos, learning, or as a starting point for your own projects.
Built to demonstrate how context graphs can make AI agents genuinely smarter over time. ๐