Skip to content

muhkartal/llm-outputVerifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

llm-outputVerifier

screenshot

License: MIT Python 3.9+ Code style: black Docker Documentation PyPI version

A production-ready pipeline for detecting hallucinations in LLM reasoning

Overview β€’ Features β€’ Quick Start β€’ UI Guide β€’ API β€’ Training β€’ Contributing

Overview

llm-outputVerifier is an enterprise-grade system that analyzes chain-of-thought (CoT) reasoning from large language models, scrutinizing each reasoning step to classify it as either grounded or hallucinated. The pipeline leverages the GSM8K (Grade School Math 8K) dataset, augmenting it with carefully designed synthetic corruptions to create high-quality training data for the hallucination detection model.

Fallback image description

How It Works

The system works in three key stages:

  1. Analysis: Breaks down chains of reasoning into individual steps
  2. Classification: Applies transformer-based sequence classification to each step
  3. Visualization: Provides confidence-scored results with detailed explanations

Our models achieve 94.3% accuracy on benchmark datasets, significantly outperforming current state-of-the-art approaches to hallucination detection in mathematical reasoning.

Key Features

Robust Training Pipeline

  • State-of-the-art transformer-based sequence classification
  • Mixed-precision training and gradient checkpointing
  • Efficient vectorized data loading
  • Synthetic data augmentation techniques

Production Architecture

  • Clean, modular design with type annotations
  • Memory-efficient processing for large datasets
  • Scalable FastAPI backend with validation
  • Intuitive Streamlit frontend with visualizations

DevOps Integration

  • Fully dockerized with multi-stage builds
  • Comprehensive CI/CD with GitHub Actions
  • Rigorous testing with 95%+ coverage
  • Performance monitoring and health checks

Quick Start

Prerequisites

  • Python 3.9+
  • Docker and Docker Compose (recommended)
  • CUDA-compatible GPU (optional, for accelerated training)

Installation

Option 1: Docker Deployment (Recommended)
# Clone repository
git clone https://github.com/username/llm-outputVerifier.git
cd llm-outputVerifier

# Configure environment variables (optional)
cp .env.example .env
# Edit .env file with your configuration

# Build and run with Docker Compose
docker-compose up --build

# Access UI at http://localhost:8501
# Access API at http://localhost:8000
Option 2: Direct Installation
# Clone repository
git clone https://github.com/username/llm-outputVerifier.git
cd llm-outputVerifier

# Create and activate virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -e .

# Run tests to verify installation
pytest tests/

# Start the API and UI
python -m hallucination_hunter.api.main & python -m hallucination_hunter.ui.app
Option 3: PyPI Installation
# Install from PyPI
pip install llm-outputVerifier

# Start services
llm-outputVerifier start

Using the UI

The Streamlit UI provides an intuitive interface for analyzing reasoning chains:

Fallback image description
  1. Open http://localhost:8501 in your browser
  2. Enter a mathematical question in the first text field
  3. Paste chain-of-thought reasoning (with each step on a new line) in the second text field
  4. Click "Analyze Reasoning"

Understanding Results

The system processes the reasoning and displays results with color-coded confidence scoring:

  • 🟒 Green: Grounded reasoning (high confidence)
  • πŸ”΅ Blue: Likely grounded (lower confidence)
  • 🟠 Orange: Potential hallucination (lower confidence)
  • πŸ”΄ Red: Confirmed hallucination (high confidence)

The UI also provides summary metrics including the total hallucination rate and confidence distribution.

Using the API

The RESTful API is accessible at http://localhost:8000 and provides comprehensive endpoints for integration:

Endpoint Method Description
/ GET Welcome message and API information
/health GET Health check and status monitoring
/docs GET Interactive API documentation (Swagger UI)
/predict POST Hallucination detection endpoint
/batch_predict POST Batch processing for multiple chains
/models GET List available models
/metrics GET Performance and usage metrics

Example API Request

import requests

# Define the input data
data = {
    "question": "John has 5 apples. He buys 2 more. How many apples does he have now?",
    "reasoning": "John starts with 5 apples.\nThen he buys 2 more apples.\nSo in total, he has 5 + 2 = 7 apples."
}

# Send POST request to the API
response = requests.post(
    "http://localhost:8000/predict",
    json=data,
    headers={"Content-Type": "application/json"}
)

# Process the response
result = response.json()
print(f"Analyzed {result['num_steps']} steps")
print(f"Found {result['num_hallucinations']} hallucinations")

# Print the prediction for each step
for i, pred in enumerate(result["predictions"]):
    status = "HALLUCINATION" if pred["is_hallucination"] else "GROUNDED"
    confidence = pred["confidence"]
    print(f"Step {i+1}: {status} ({confidence:.2f})")
    print(f"  {pred['step']}")

Performance Considerations

The API supports both synchronous and asynchronous processing modes:

# Asynchronous batch processing for large workloads
response = requests.post(
    "http://localhost:8000/batch_predict?async=true",
    json={"items": batch_data}
)

# Get the job ID from the response
job_id = response.json()["job_id"]

# Check job status
status_response = requests.get(f"http://localhost:8000/job/{job_id}")

Training Custom Models

llm-outputVerifier provides a flexible training pipeline that allows you to customize various aspects of the model training process.

Basic Training

# Train with default settings
python scripts/train.py

Advanced Configuration

# Train with custom settings
python scripts/train.py \
    --config configs/custom_config.json \
    --model_name roberta-large \
    --batch_size 32 \
    --learning_rate 3e-5 \
    --num_epochs 5 \
    --corruption_rate 0.4 \
    --output_dir ./models/custom_model

Experiment Tracking

Training progress is tracked with Weights & Biases by default. To view training metrics:

  1. Create a W&B account at wandb.ai
  2. Set your API key: export WANDB_API_KEY=your_api_key
  3. Run training with --use_wandb flag
  4. View metrics at the W&B project dashboard
Fallback image description

Performance Optimization

For large models or datasets, use these optimization flags:

python scripts/train.py \
    --model_name roberta-large \
    --batch_size 8 \
    --gradient_accumulation_steps 4 \
    --mixed_precision fp16 \
    --gradient_checkpointing \
    --output_dir ./models/optimized_model

Project Structure

hallucination_hunter/
β”œβ”€β”€ data/                  # Dataset handling and synthetic corruption
β”‚   β”œβ”€β”€ augmentation.py    # Synthetic data corruption techniques
β”‚   β”œβ”€β”€ dataset.py         # Dataset class and dataloader creation
β”‚   └── preprocessing.py   # Data preparation and vectorization
β”œβ”€β”€ models/                # Model architecture definitions
β”‚   β”œβ”€β”€ classifier.py      # Hallucination classifier architecture
β”‚   └── encoder.py         # Transformer encoder utilities
β”œβ”€β”€ training/              # Training implementation
β”‚   β”œβ”€β”€ optimizer.py       # Optimizer and learning rate scheduling
β”‚   └── trainer.py         # Main training loop with metrics tracking
β”œβ”€β”€ evaluation/            # Evaluation metrics and analysis
β”‚   β”œβ”€β”€ metrics.py         # Classification metrics calculation
β”‚   └── visualizer.py      # Result visualization utilities
β”œβ”€β”€ inference/             # Model inference pipeline
β”‚   └── predictor.py       # Prediction logic for reasoning chains
β”œβ”€β”€ api/                   # FastAPI implementation
β”‚   β”œβ”€β”€ main.py            # API server and endpoints
β”‚   └── schemas.py         # Pydantic data validation schemas
└── ui/                    # Streamlit user interface
    └── app.py             # Interactive web application

Contributing

Contributions to llm-outputVerifier are welcome! Please see our CONTRIBUTING.md guide for details on how to submit pull requests, report issues, or request features.

Development Setup

# Clone repository
git clone https://github.com/username/llm-outputVerifier.git
cd llm-outputVerifier

# Set up development environment
python -m venv venv
source venv/bin/activate
pip install -e ".[dev]"

# Run code quality checks
black hallucination_hunter tests
isort hallucination_hunter tests
mypy hallucination_hunter
pytest tests/

License

This project is licensed under the MIT License - see the LICENSE file for details.

llm-outputVerifier - LLM-specific output checker

GitHub β€’ Docker Hub β€’ Developer Website

Developed by Muhammad Ibrahim Kartal | kartal.dev

About

framework for detecting hallucinations in LLM chain-of-thought reasoning. Features synthetic data corruption, transformer-based classifiers, Streamlit UI, and FastAPI backend.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors