A RAG (Retrieval-Augmented Generation) agent that answers questions in the style and wisdom of Charlie Munger.
The Munger RAG Agent is a specialized AI assistant that combines retrieval-augmented generation (RAG) with Charlie Munger's distinctive thinking patterns and communication style. Drawing from Munger's vast knowledge across multiple disciplines, this agent provides insights using his characteristic mental models, analogies, and practical wisdom.
- Charlie Munger's Thinking Style: Answers questions using Munger's mental models and multidisciplinary approach
- RAG-Powered Knowledge: Retrieval-augmented generation from Munger's talks, writings, and wisdom
- Mental Models Integration: Applies psychology, economics, business, and investing principles
- AI Integration: OpenAI GPT-4 Turbo for advanced language processing
- Modern Tech Stack: FastAPI backend, React frontend with TypeScript
- Vector Storage: FAISS vector database for efficient knowledge retrieval
- AI Frameworks: LangChain, LangGraph, DSPy for multi-agent orchestration
- Python 3.11+
- Node.js 18+
- Git
-
Clone the repository
git clone https://github.com/yourusername/the-munger-talks cd the-munger-talks -
Install Python dependencies
pip install -r requirements.txt
-
Install Node.js dependencies
cd frontend npm install cd ..
-
Configure environment
cp .env.example .env # Edit .env and add your OpenAI API key -
Start the services
# Backend (FastAPI) python start_backend.py # Frontend (React + Vite) - in a new terminal ./start_frontend.sh
the-munger-talks/
├── backend/ # Python backend
│ ├── agent/ # Multi-agent system
│ │ ├── dspy_modules/ # DSPy integration modules
│ │ │ ├── dspy_config.py # DSPy configuration & LLM adapter
│ │ │ └── simple_modules.py # DSPy planners, synthesizers, verifiers
│ │ ├── graph/ # LangGraph workflow orchestration
│ │ │ ├── memory.py # Conversation memory management
│ │ │ ├── nodes/ # Specialized agent nodes
│ │ │ │ ├── mental_model_analyzer.py # Mental model detection & analysis
│ │ │ │ ├── planner.py # Query planning & strategy determination
│ │ │ │ ├── retriever.py # Document retrieval & reranking
│ │ │ │ ├── synthesizer.py # Munger-style response generation
│ │ │ │ └── verifier.py # Response quality validation
│ │ │ ├── state.py # Comprehensive state management
│ │ │ └── workflow.py # LangGraph workflow definitions
│ │ └── llm/ # LLM client & configuration
│ │ ├── client.py # OpenAI client wrapper
│ │ └── settings.py # Model configuration
│ ├── app/ # FastAPI application
│ │ ├── config/ # Application configuration
│ │ │ └── config.py # Settings & environment config
│ │ ├── dependencies.py # Dependency injection & managers
│ │ ├── routes.py # API endpoints
│ │ └── schemas.py # Pydantic data models
│ ├── indexing_and_retrieval/ # Document processing pipeline
│ │ ├── indexing.py # Document ingestion & chunking
│ │ ├── retrieval/ # Retrieval system
│ │ │ ├── retrieval.py # FAISS vector search
│ │ │ ├── rerank.py # Cross-encoder reranking
│ │ │ └── storage.py # Vector database management
│ │ ├── storage/ # Data persistence
│ │ │ ├── chunks.json # Processed document chunks
│ │ │ ├── conversation_memory/ # Session memory storage
│ │ │ └── faiss_db/ # FAISS vector database
│ │ └── kg/ # Knowledge graph (future)
│ ├── tests/ # Test suites
│ │ ├── unit/ # Unit tests
│ │ ├── integration/ # Integration tests
│ │ └── golden/ # Golden test cases
│ └── main.py # Application entry point
├── frontend/ # React frontend
│ ├── src/
│ │ ├── components/ # React components
│ │ │ ├── ChatInterface.tsx # Main chat interface
│ │ │ ├── ChatMessage.tsx # Individual message component
│ │ │ └── ThinkingIndicator.tsx # Loading state indicator
│ │ ├── hooks/ # Custom React hooks
│ │ ├── pages/ # Page components
│ │ ├── types/ # TypeScript type definitions
│ │ │ └── chat.ts # Chat-related types
│ │ ├── utils/ # Frontend utilities
│ │ │ └── api.ts # API client functions
│ │ ├── App.tsx # Main app component
│ │ └── main.tsx # Application entry point
│ ├── public/ # Static assets
│ │ ├── Charlie_Munger.jpg # Charlie Munger portrait
│ │ └── charlie-munger-avatar.svg # Avatar icon
│ ├── package.json # Node.js dependencies
│ ├── vite.config.ts # Vite build configuration
│ └── tsconfig.json # TypeScript configuration
├── docs/ # Documentation & source material
│ └── PoorCharliesAlmanack_book.pdf # Primary knowledge source
├── pyproject.toml # Python project configuration
├── requirements.txt # Python dependencies
├── Makefile # Build automation
├── start_backend.py # Backend startup script
└── start_frontend.sh # Frontend startup script
- FastAPI: Modern, fast web framework
- LangChain: AI application framework
- LangGraph: Multi-agent workflow orchestration
- FAISS: Vector database for semantic search
- OpenAI: GPT-4 Turbo for language processing
- DSPy: Programming with language models
- HuggingFace: Cross-encoder models for reranking
- React: User interface library
- TypeScript: Type-safe JavaScript
- Vite: Fast build tool
- Chakra UI: Component library
The Munger RAG Agent backend is built on a sophisticated multi-agent architecture that combines retrieval-augmented generation with Charlie Munger's distinctive thinking patterns. Here's how it works:
- Primary Model: GPT-4 Turbo (configurable via
backend/agent/llm/settings.py) - Client: Custom
LLMClientwrapper around OpenAI API - Configuration: Temperature, max tokens, and model selection via environment variables
- Streaming Support: Real-time response streaming for web interface
The system processes Charlie Munger's knowledge through a multi-stage pipeline:
- Document Loading: PDF documents loaded using
PyPDFLoaderfrom LangChain - Text Extraction: Full text extracted and combined from all pages
- Intelligent Chunking:
- Uses
RecursiveCharacterTextSplitterwith 4000 token chunks - 200 token overlap for context preservation
- Smart separators:
["\nChapter ", "\n\n", "\n", " ", ""]
- Uses
- Metadata Enrichment: Each chunk tagged with source type, document ID, and token count
- Storage: Chunks stored in JSON format for persistence and FAISS vector database
The retrieval system uses a sophisticated two-stage approach:
-
Initial Retrieval:
- FAISS vector search using
sentence-transformers/all-MiniLM-L6-v2embeddings - Strategy-aware retrieval counts (10-18 documents based on query type)
- Source preference filtering (speeches, interviews, books, essays)
- FAISS vector search using
-
Reranking:
- Cross-encoder reranking using
cross-encoder/ms-marco-MiniLM-L-6-v2 - Query-document relevance scoring
- Top-K selection based on query complexity
- Metadata extraction for quality assessment
- Cross-encoder reranking using
The system leverages DSPy for advanced language model programming:
- DSPy Planner: Analyzes queries and determines optimal strategies
- DSPy Synthesizer: Generates context-aware responses in Munger's voice
- DSPy Verifier: Validates response quality and factual accuracy
- DSPy Retrieval Decider: Determines when retrieval is necessary
- Fallback System: Graceful degradation to rule-based approaches when DSPy unavailable
The agent uses LangGraph to orchestrate a sophisticated workflow with multiple specialized nodes:
- Conversational Workflow: Maintains memory and context across interactions
- Standard Workflow: Single-query processing without memory
- Simple Workflow: Minimal nodes for testing and development
- Memory Context Node: Manages conversation history and context
- Planner Node: Analyzes queries and determines retrieval strategy
- Retriever Node: Coordinates document retrieval and reranking
- Mental Model Analyzer: Identifies and explains relevant mental models
- Synthesizer Node: Generates Munger-style responses
- Verifier Node: Validates response quality and determines retry logic
- Memory Update Node: Updates conversation memory
- Retrieval Decision: Determines if document retrieval is needed
- Mental Model Analysis: Decides when to analyze mental models
- Retry Logic: Handles failures and retry attempts
- Conversation Flow: Manages multi-turn conversations
- Query Classification: Identifies query types (quote, explanation, mental_model, story, decision_framework)
- Complexity Analysis: Assesses query complexity (simple, moderate, complex)
- Mental Model Detection: Identifies relevant mental models using keyword matching
- Strategy Determination: Creates retrieval strategies based on query analysis
- DSPy Enhancement: Uses DSPy for advanced query understanding when available
- Strategy-Aware Retrieval: Adapts retrieval parameters based on query type
- Multi-Stage Processing: Initial retrieval → Reranking → Source filtering
- Metadata Extraction: Tracks retrieval performance and document statistics
- Context Sufficiency: Validates retrieved context adequacy
- Caching: Implements retrieval caching for repeated queries
- Model Identification: Detects applicable mental models from retrieved content
- Explanation Generation: Provides clear explanations of mental models
- Application Notes: Generates guidance on how to apply models
- Integration: Seamlessly integrates with synthesizer for response generation
- Style Guidelines: Enforces Munger's communication characteristics:
- Directness and straightforwardness
- Practical wisdom focus
- Clear analogies and examples
- Intellectual humility
- Multidisciplinary connections
- Simple explanations of complex ideas
- Brutal honesty about limitations
- Context Integration: Combines retrieved documents with mental models
- DSPy Enhancement: Uses DSPy for context-aware response generation
- Quality Metrics: Tracks response confidence and style consistency
- Multi-Criteria Evaluation: Assesses responses across multiple dimensions:
- Factual accuracy (30% weight)
- Style consistency (25% weight)
- Completeness (20% weight)
- Practical value (15% weight)
- Mental model application (10% weight)
- LLM-Based Validation: Uses GPT-4 for sophisticated quality assessment
- Retry Logic: Determines when responses need regeneration
- Quality Scoring: Provides quantitative quality metrics
The system uses a comprehensive MungerState TypedDict that tracks:
- Input & Context: User queries, conversation history, session management
- Planning & Analysis: Query types, complexity, mental models, intent
- Retrieval Pipeline: Documents, metadata, strategies, decisions
- Mental Model Analysis: Applicable models, explanations, application notes
- Response Generation: Generated content, confidence, sources, citations
- Control Flow: Current step, retry counts, error handling
- Munger-Specific Features: Style requirements, voice activation, analogies
- Quality Metrics: Response quality, factual accuracy, style consistency
- Debugging: Execution traces, performance metrics, debug information
This architecture enables the system to provide authentic, high-quality responses that capture Charlie Munger's distinctive thinking patterns while maintaining robust error handling and quality assurance throughout the process.
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ User Query │───▶│ Memory Context │───▶│ Planner │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│
▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Verifier │◀───│ Synthesizer │◀───│ Retriever │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
▼ │ ▼
┌─────────────────┐ │ ┌─────────────────┐
│ Memory Update │ │ │ Mental Model │
└─────────────────┘ │ │ Analyzer │
│ │ └─────────────────┘
▼ │ │
┌─────────────────┐ │ │
│ END/CONTINUE │ │ │
└─────────────────┘ │ │
│ │
▼ │
┌─────────────────┐ │
│ DSPy Modules │ │
│ - Planner │ │
│ - Synthesizer │ │
│ - Verifier │ │
│ - Retrieval │ │
└─────────────────┘ │
│
┌───────────────────────┘
│
▼
┌─────────────────┐
│ FAISS Vector │
│ Database │
│ │
│ - Embeddings │
│ - Chunks │
│ - Metadata │
└─────────────────┘
- Input Processing: User query → Memory context analysis → Query planning
- Retrieval Pipeline: Strategy determination → Document retrieval → Reranking → Mental model analysis
- Response Generation: Context synthesis → Munger-style response generation → Quality verification
- Output & Memory: Response validation → Memory update → End or continue conversation
- DSPy Enhancement: All major nodes enhanced with DSPy modules for advanced reasoning
- Vector Storage: FAISS database provides semantic search and retrieval capabilities
Once the backend is running, visit:
- FastAPI docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- Streamlit: http://localhost:8501
# Run all tests
pytest
# Run with coverage
pytest --cov=backend
# Run specific test categories
pytest -m unit
pytest -m integrationCreate a .env file with:
# Required API Keys
OPENAI_API_KEY=your_openai_key_here
# Optional HuggingFace (for cross-encoder reranking)
HUGGINGFACE_API_TOKEN=your_hf_token_here
# Development Settings
DEBUG=true
FRONTEND_URL=http://localhost:3000
BACKEND_URL=http://localhost:8000Inspired by Charlie Munger's multidisciplinary approach to learning and problem-solving.
"The best thing a human being can do is to help another human being know more." - Charlie Munger