A Retrieval-Augmented Generation (RAG) system for intelligent document classification, analysis, and question-answering. Built with LangChain, this system enables users to interact with document corpora through natural language queries with support for multiple LLM providers and embedding models.
DocClassifier enables semantic search and conversational AI over large document collections. It processes documents, builds a vector knowledge base, and provides both CLI and web interfaces for querying information from your documents.
- Multi-format Document Support: PDF, Word, PowerPoint, Excel, Markdown, HTML, CSV, JSON, EPUB, and more
- Advanced Document Processing: Automatic chunking with configurable strategies and overlap
- Vector Database Integration: ChromaDB for efficient semantic search
- Multiple LLM Providers: Google Gemini, OpenAI, Ollama (local), and GPT4All
- Flexible Embedding Models: Google, HuggingFace, and OpenAI embeddings
- Document Classification: Keyword-based or LLM-based classification
- Streaming Responses: Real-time response generation for improved UX
- Session Management: Persistent chat sessions with history tracking
- Interactive CLI: Rich terminal interface with command history
- Web Interface: Streamlit-based UI with ChatGPT-style design
DocClassifier/
├── src/
│ ├── config/ # Configuration management
│ ├── database/ # Vector store implementations
│ ├── document/ # Document processing and chunking
│ ├── llm/ # LLM factories and chain builders
│ ├── evaluation/ # System evaluation tools
│ ├── system.py # Core RAG orchestrator
│ ├── session.py # Chat session management
│ └── models.py # Data models
├── app.py # Streamlit web interface
├── main.py # CLI entry point
├── parser.py # Command-line argument parser
└── environment.yml # Conda environment specification
- RAGSystem: Main orchestrator coordinating document processing, retrieval, and generation
- DocumentProcessor: Handles document loading, chunking, and classification
- VectorStoreManager: Manages vector database operations and similarity search
- ChainFactory: Creates LangChain chains for different query types
- SessionStore: Manages persistent chat sessions with title generation
- Python 3.13+
- Conda or Miniconda
- API keys for your chosen LLM provider (Google AI, OpenAI, etc.)
- Clone the repository:
cd DocClassifier- Create the conda environment:
conda env create -f environment.yml
conda activate kpmg- Configure environment variables:
cp .env.example .env
# Edit .env and add your API keysRequired environment variables:
# Google AI (default)
GOOGLE_API_KEY=your_google_api_key
# OpenAI (optional)
OPENAI_API_KEY=your_openai_api_keyThe CLI provides several subcommands for different operations:
Build or rebuild the knowledge base from your document corpus:
# Index documents from default corpus path
python main.py index
# Index with custom corpus path
python main.py index /path/to/documents
# Force rebuild existing index
python main.py index --overwrite
# Use multiprocessing for faster indexing
python main.py index --multiprocessAsk single questions and get direct answers:
python main.py query "What is the procurement policy?"Start a conversational session with history:
# Start new chat session
python main.py chat
# Resume existing session
python main.py chat --session <session_id>
# Enable streaming responses
python main.py chat --streamChat Commands:
/help- Show available commands/history- View conversation history/clear- Clear current session history/new- Start a new chat session/sessions- List all saved sessions/stream- Toggle streaming mode/quit- Exit chat
# List all sessions
python main.py session --list
# View session history
python main.py session --history <session_id>
# Clear specific session
python main.py session --clear <session_id># Count documents in database
python main.py db --count
# Preview first N documents
python main.py db --peek 5Launch the Streamlit web application:
streamlit run app.pyFeatures:
- ChatGPT-inspired dark theme UI
- Model selection dropdown
- Session management sidebar
- Source attribution with confidence scores
- File opening from sources
- Streaming responses
Document classification categories are defined in src/config/keywords.yml:
GENERAL:
- document
- policy
HR:
- employee
- recruitment
- salary
FINANCE:
- budget
- invoice
- payment- Google Gemini: gemini-2.0-flash-exp, gemini-1.5-pro, gemini-1.5-flash
- OpenAI: gpt-4o, gpt-4-turbo, gpt-3.5-turbo
- Ollama (local): llama3.2, phi3.5, qwen2.5
- GPT4All (local): Meta-Llama-3-8B-Instruct
- Google: text-embedding-004
- OpenAI: text-embedding-3-small, text-embedding-3-large
- HuggingFace: all-MiniLM-L6-v2, bge-small-en-v1.5, gte-small
src/config/- Configuration classes and keyword definitionssrc/database/- Vector store abstractions and implementationssrc/document/- Document loaders, chunkers, and classifierssrc/llm/- LLM provider factories and chain builderssrc/evaluation/- Testing and evaluation utilitiesmain.py- CLI entry point with async orchestrationapp.py- Streamlit web applicationparser.py- CLI argument parsing
# Run test suite with benchmark queries
python main.py testEnable profiling by setting PROFILE_ENABLE = True in main.py:
PROFILE_ENABLE = TrueProfile results are saved to profile.prof and can be visualized with:
snakeviz profile.prof- Multiprocessing: Use
--multiprocessflag for faster document indexing - Batch Processing: Configure batch sizes for embeddings and LLM inference
- Streaming: Enable streaming for real-time response generation
- Similarity Threshold: Adjust threshold based on embedding model quality
- Chunk Size: Balance between context size and retrieval precision
Logs are written to logs/ directory with rotating file handlers. Configure logging level via:
# In src/config/logging_config.py
level = logging.INFO # or DEBUG, WARNING, ERRORIssue: ChromaDB initialization fails
- Ensure
chroma_dbdirectory has write permissions - Clear and rebuild:
rm -rf chroma_db && python main.py index --overwrite
Issue: API rate limits
- Reduce batch sizes in configuration
- Add delays between requests
- Use local models via Ollama
Issue: Out of memory during indexing
- Reduce
chunk_sizeandbatch_size - Process documents in smaller batches
- Enable multiprocessing instead of async
Issue: Low quality responses
- Increase
similarity_thresholdfor stricter retrieval - Adjust
chunk_sizefor better context - Try different embedding models
- Increase
max_resultsfor broader context
Built with: