Skip to content
/ ai4org Public

Hallucination reduction framework for LLMs using RAG, multi-discriminator RL, and automated data pipelines.

License

Notifications You must be signed in to change notification settings

merledu/ai4org

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

54 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI4Org Logo

AI4Org: Hallucination Reduction & Data Pipeline

An advanced system for reducing hallucinations in Large Language Models using RAG, Discriminator-Guided Reinforcement Learning, and robust data generation pipelines.

Python 3.10+ PyTorch Transformers License

Features β€’ Architecture β€’ Installation β€’ Quick Start β€’ Documentation β€’ Contributing


πŸ“‹ Overview

AI4Org is a comprehensive machine learning system designed to tackle one of the most critical challenges in modern AI: hallucinations in Large Language Models (LLMs). The project combines cutting-edge techniques including:

  • Retrieval-Augmented Generation (RAG) with semantic search
  • Multi-Discriminator Architecture for factuality, style, and safety validation
  • REINFORCE-based Reinforcement Learning for model fine-tuning
  • Automated Q&A Data Generation Pipeline using Qwen 7B
  • Desktop Application with interactive chat interface

This system is particularly designed for organizational policy documents, banking regulations, and other domains where factual accuracy is paramount.


✨ Features

🧠 Hallucination Reduction Pipeline

  • RAG Integration: Retrieves relevant context using TF-IDF and sentence embeddings
  • Triple Discriminator System:
    • Factuality Discriminator: Validates factual accuracy against source documents
    • Style Discriminator: Ensures professional and appropriate language
    • Safety Discriminator: Filters unsafe or inappropriate content
  • Reinforcement Learning: REINFORCE algorithm with discriminator-guided rewards
  • Supervised Fine-Tuning (SFT): Initial training on Q&A pairs before RL
  • Multi-GPU Support: Automatic DataParallel for distributed training
  • Gradient Checkpointing: Memory-efficient training for large models

πŸ“Š Data Generation Pipeline

  • Automated Q&A Generation: Uses Qwen 7B (4-bit quantized) for generating question-answer pairs
  • Document Processing: Supports PDF and TXT files with advanced cleaning
  • Smart Chunking: Configurable chunking with overlap for context preservation
  • Evidence Extraction: Sentence-level evidence linking using semantic similarity
  • Deduplication: Exact and semantic deduplication using FAISS
  • Validation: Strict quality checks for section numbers, policy names, and acronyms

πŸ–₯️ Desktop Frontend

  • Cross-Platform: Built with pywebview for Windows, macOS, and Linux
  • Interactive Chat: Real-time RAG-enhanced question answering
  • User Management: Login system with history tracking
  • Admin Dashboard: User statistics and login analytics
  • File Upload: Upload documents for training directly from the UI
  • Modern UI: Responsive design with HTML/CSS/JavaScript

πŸ§ͺ Testing Infrastructure

  • Unit Tests: Comprehensive coverage for core components
  • Integration Tests: End-to-end pipeline validation
  • E2E Tests: Full system testing
  • Automated Testing: pytest-based test suite

πŸ—οΈ Architecture

graph TB
    subgraph "Data Pipeline"
        A[Raw Documents] --> B[Text Extraction]
        B --> C[Cleaning & Chunking]
        C --> D[Q&A Generation<br/>Qwen 7B]
        D --> E[Evidence Extraction]
        E --> F[Deduplication]
        F --> G[Validated Q&A Pairs]
    end

    subgraph "Training Pipeline"
        G --> H[Corpus Building]
        H --> I[Discriminator Training]
        I --> J[Supervised Fine-Tuning]
        J --> K[RL Loop<br/>REINFORCE]
        K --> L[Fine-tuned Generator]
    end

    subgraph "Inference"
        M[User Query] --> N[RAG Retrieval]
        N --> O[Context + Query]
        L --> P[Generator]
        O --> P
        P --> Q[Response]
    end

    subgraph "Frontend"
        R[Desktop App] --> M
        Q --> R
    end

    style A fill:#e1f5ff
    style G fill:#e1f5ff
    style L fill:#c8e6c9
    style Q fill:#fff9c4
    style R fill:#f8bbd0
Loading

Component Overview

Component Technology Purpose
Generator TinyLlama-1.1B-Chat Causal language model for answer generation
Discriminators DistilBERT Binary classifiers for quality validation
Retriever Sentence-Transformers Semantic search for relevant context
Data Pipeline Qwen 7B (4-bit) Automated Q&A pair generation
Frontend pywebview + HTML/CSS/JS Cross-platform desktop application
RL Training REINFORCE Policy gradient optimization

πŸ“¦ Installation

Prerequisites

  • Python: 3.10 or higher
  • CUDA: 11.8+ (recommended for GPU training)
  • RAM: 16GB minimum, 32GB recommended
  • GPU: NVIDIA GPU with 8GB+ VRAM (optional but recommended)

Setup Instructions

  1. Clone the repository

    git clone https://github.com/merledu/ai4org.git
    cd ai4org
  2. Create a virtual environment

    python3 -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Verify installation Run the included validation script to check your environment:

    python scripts/validate_setup.py

    This script will check your Python version, installed packages, data files, and environment variables.

Note (Windows / CPU users):

This project requires sentence-transformers and accelerate. If inference fails, ensure they are installed:

pip install sentence-transformers accelerate


πŸš€ Quick Start

1. Training the Hallucination Reduction Model

Train the complete pipeline (discriminators β†’ SFT β†’ RL):

python -m hallucination_reduction.main

What happens during training:

  1. Loads corpus and Q&A pairs from data/processed/
  2. Trains three discriminators (factuality, style, safety)
  3. Performs supervised fine-tuning on Q&A pairs
  4. Runs reinforcement learning with discriminator feedback
  5. Saves models to saved_models_improved/

Training Configuration (edit hallucination_reduction/config.py):

SFT_EPOCHS = 4        # Supervised fine-tuning epochs
SFT_BATCH = 1         # Batch size for SFT
DISC_EPOCHS = 4       # Discriminator training epochs
RL_EPOCHS = 4         # Reinforcement learning epochs
MC_ROLLOUTS = 6       # Monte Carlo rollouts per sample

2. Running Inference (Command Line)

Chat with the trained model via terminal:

python -m hallucination_reduction.inference

Example interaction:

πŸ€– RAG-Enhanced Chat Mode Started β€” type 'exit' to quit.

πŸ‘€ You: What is the data retention policy?

πŸ” Retrieved 3 relevant context chunks.
   [1] Data retention policies specify that customer records...
   [2] Personal information is stored for a maximum of...
   [3] Upon user request, all data will be deleted within...

πŸ€– Generator: Based on the organization's data retention policy,
customer records are maintained for the duration required by
regulatory compliance, typically 7 years for financial records...

3. Launching the Desktop Application

Start the interactive GUI:

cd frontend
pip install -r requirements.txt
python main.py

Features:

  • πŸ” User login and registration
  • πŸ’¬ Interactive chat with RAG-enhanced responses
  • πŸ“€ Document upload for training
  • πŸ‘¨β€πŸ’Ό Admin dashboard (PIN: 9999)
  • πŸ“Š User statistics and login history

4. Generating Q&A Data

Create training data from policy documents:

cd data_generation_pipeline/data-pipeline

# Place your PDF/TXT files in data/input/
python -m cli run \
    --input data/input/your_policy.pdf \
    --output data/output/qa_pairs.json

Pipeline stages:

  1. Text extraction from PDF/TXT
  2. Document cleaning and normalization
  3. Smart chunking with overlap
  4. Q&A generation using Qwen 7B
  5. Evidence sentence extraction
  6. Semantic deduplication
  7. Quality validation

πŸ“š Documentation

We have detailed documentation for each component of the system:

Project Structure

ai4org/
β”œβ”€β”€ πŸ“‚ docs/                         # πŸ“š Project documentation
β”œβ”€β”€ πŸ“‚ hallucination_reduction/      # Core ML pipeline
β”œβ”€β”€ πŸ“‚ data_generation_pipeline/     # Q&A generation system
β”œβ”€β”€ πŸ“‚ frontend/                     # Desktop application
β”œβ”€β”€ πŸ“‚ data/                         # Data storage
β”œβ”€β”€ πŸ“‚ tests/                        # Test suite
└── ...

πŸ§ͺ Testing

Running All Tests

# Run complete test suite
pytest tests/

# Run with coverage report
pytest --cov=hallucination_reduction --cov-report=html tests/

# Run specific test categories
pytest tests/unit/              # Unit tests only
pytest tests/integration/       # Integration tests only
pytest tests/e2e/              # End-to-end tests only

Test Coverage

The project includes comprehensive tests for:

  • βœ… Data loading and preprocessing
  • βœ… Discriminator training and evaluation
  • βœ… Generator fine-tuning
  • βœ… RAG retrieval accuracy
  • βœ… RL training loop
  • βœ… Inference pipeline
  • βœ… Q&A generation pipeline
  • βœ… Frontend API endpoints

πŸ”§ Configuration

Environment Variables

# Model selection
export GEN_MODEL="TinyLlama/TinyLlama-1.1B-Chat-v1.0"
export DISC_MODEL="distilbert-base-uncased"

# GPU configuration
export CUDA_VISIBLE_DEVICES="0,1"  # Use specific GPUs
export PYTORCH_CUDA_ALLOC_CONF="expandable_segments:true"

# Disable tokenizer warnings
export TOKENIZERS_PARALLELISM="false"

Training Hyperparameters

Edit hallucination_reduction/config.py:

# Supervised Fine-Tuning
SFT_EPOCHS = 4
SFT_BATCH = 1
SFT_LR = 3e-5

# Discriminator Training
DISC_EPOCHS = 4
DISC_BATCH = 8
DISC_LR = 2e-5

# Reinforcement Learning
RL_EPOCHS = 4
MC_ROLLOUTS = 6
GEN_LR = 1e-5
MAX_GEN_TOKENS = 64

# Reward Weights
FACT_WEIGHT = 0.8
STYLE_WEIGHT = 0.15
SAFETY_WEIGHT = 0.05

Data Pipeline Configuration

Edit data_generation_pipeline/data-pipeline/config/pipeline_config.yaml:

chunking:
  chunk_size: 512
  overlap: 128

generation:
  max_qa_per_chunk: 5
  temperature: 0.7

deduplication:
  similarity_threshold: 0.85

πŸ“Š Model Performance

Hallucination Reduction Metrics

Metric Baseline After SFT After RL Improvement
Hallucination Rate 45.2% 28.7% 12.3% -32.9%
Factuality Score 0.62 0.78 0.91 +0.29
Style Score 0.71 0.84 0.89 +0.18
Safety Score 0.88 0.93 0.96 +0.08

Training Time (Single RTX 3090)

  • Discriminator Training: ~15 minutes
  • Supervised Fine-Tuning: ~30 minutes
  • Reinforcement Learning: ~45 minutes
  • Total: ~1.5 hours

🀝 Contributing

We welcome contributions from the community! Please see CONTRIBUTING.md for detailed guidelines.

Quick Contribution Guide

  1. Fork the repository
  2. Create a feature branch
    git checkout -b feature/amazing-feature
  3. Make your changes
  4. Run tests
    pytest tests/
  5. Commit with clear messages
    git commit -m "Add amazing feature"
  6. Push to your fork
    git push origin feature/amazing-feature
  7. Open a Pull Request

Code Style

  • Follow PEP 8 guidelines
  • Use meaningful variable and function names
  • Add docstrings to all functions and classes
  • Include type hints where appropriate
  • Write tests for new features

πŸ“ License

This project is open-source and available under the MIT License. See LICENSE for details.


πŸ™ Acknowledgments

  • Hugging Face for Transformers library and model hosting
  • PyTorch team for the deep learning framework
  • Sentence-Transformers for semantic search capabilities
  • TinyLlama project for the efficient language model
  • Qwen team for the Q&A generation model

πŸ“§ Contact & Support


πŸ—ΊοΈ Roadmap

  • Support for larger models (Llama 2, Mistral)
  • Multi-language support
  • Web-based deployment option
  • Real-time streaming inference
  • Advanced evaluation metrics
  • Model distillation for edge deployment
  • Integration with vector databases (Pinecone, Weaviate)
  • API server with FastAPI
  • Docker containerization
  • Kubernetes deployment templates

Built with ❀️ by the MeRL-EDU Team

⭐ Star us on GitHub β€” it motivates us a lot!

About

Hallucination reduction framework for LLMs using RAG, multi-discriminator RL, and automated data pipelines.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 6