Skip to content

SamarthPyati/doc-classifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DocClassifier

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.

Overview

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.

Key Features

  • 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

Architecture

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

Core Components

  • 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

Installation

Prerequisites

  • Python 3.13+
  • Conda or Miniconda
  • API keys for your chosen LLM provider (Google AI, OpenAI, etc.)

Setup

  1. Clone the repository:
cd DocClassifier
  1. Create the conda environment:
conda env create -f environment.yml
conda activate kpmg
  1. Configure environment variables:
cp .env.example .env
# Edit .env and add your API keys

Required environment variables:

# Google AI (default)
GOOGLE_API_KEY=your_google_api_key

# OpenAI (optional)
OPENAI_API_KEY=your_openai_api_key

Usage

Command Line Interface

The CLI provides several subcommands for different operations:

Index Documents

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 --multiprocess

Query Mode

Ask single questions and get direct answers:

python main.py query "What is the procurement policy?"

Interactive Chat

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 --stream

Chat 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

Session Management

# 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>

Database Operations

# Count documents in database
python main.py db --count

# Preview first N documents
python main.py db --peek 5

Web Interface

Launch the Streamlit web application:

streamlit run app.py

Features:

  • ChatGPT-inspired dark theme UI
  • Model selection dropdown
  • Session management sidebar
  • Source attribution with confidence scores
  • File opening from sources
  • Streaming responses

Classification Keywords

Document classification categories are defined in src/config/keywords.yml:

GENERAL:
  - document
  - policy
HR:
  - employee
  - recruitment
  - salary
FINANCE:
  - budget
  - invoice
  - payment

Supported Models

LLM Models

  • 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

Embedding Models

  • 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

Development

Project Structure

  • src/config/ - Configuration classes and keyword definitions
  • src/database/ - Vector store abstractions and implementations
  • src/document/ - Document loaders, chunkers, and classifiers
  • src/llm/ - LLM provider factories and chain builders
  • src/evaluation/ - Testing and evaluation utilities
  • main.py - CLI entry point with async orchestration
  • app.py - Streamlit web application
  • parser.py - CLI argument parsing

Running Tests

# Run test suite with benchmark queries
python main.py test

Profiling

Enable profiling by setting PROFILE_ENABLE = True in main.py:

PROFILE_ENABLE = True

Profile results are saved to profile.prof and can be visualized with:

snakeviz profile.prof

Performance Optimization

  • Multiprocessing: Use --multiprocess flag 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

Logging

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, ERROR

Troubleshooting

Common Issues

Issue: ChromaDB initialization fails

  • Ensure chroma_db directory 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_size and batch_size
  • Process documents in smaller batches
  • Enable multiprocessing instead of async

Issue: Low quality responses

  • Increase similarity_threshold for stricter retrieval
  • Adjust chunk_size for better context
  • Try different embedding models
  • Increase max_results for broader context

Acknowledgments

Built with:

About

Multimodal Agentic RAG application for Doc classification and Querying with a interactive ChatGPT like chat interface..

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages