A production-ready pipeline for detecting hallucinations in LLM reasoning
Overview β’ Features β’ Quick Start β’ UI Guide β’ API β’ Training β’ Contributing
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.
The system works in three key stages:
- Analysis: Breaks down chains of reasoning into individual steps
- Classification: Applies transformer-based sequence classification to each step
- 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.
|
|
|
- Python 3.9+
- Docker and Docker Compose (recommended)
- CUDA-compatible GPU (optional, for accelerated training)
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:8000Option 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.appOption 3: PyPI Installation
# Install from PyPI
pip install llm-outputVerifier
# Start services
llm-outputVerifier startThe Streamlit UI provides an intuitive interface for analyzing reasoning chains:
- Open http://localhost:8501 in your browser
- Enter a mathematical question in the first text field
- Paste chain-of-thought reasoning (with each step on a new line) in the second text field
- Click "Analyze Reasoning"
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.
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 |
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']}")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}")llm-outputVerifier provides a flexible training pipeline that allows you to customize various aspects of the model training process.
# Train with default settings
python scripts/train.py# 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_modelTraining progress is tracked with Weights & Biases by default. To view training metrics:
- Create a W&B account at wandb.ai
- Set your API key:
export WANDB_API_KEY=your_api_key - Run training with
--use_wandbflag - View metrics at the W&B project dashboard
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_modelhallucination_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
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.
# 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/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
