Show HN: LLM Hallucination Detector – Works with GPT, Claude, and Local Models
3 days ago
5
A comprehensive, framework-agnostic toolkit for detecting potential hallucinations in Large Language Model (LLM) responses. Works with any LLM API including OpenAI GPT, Anthropic Claude, local models, and more.
fromhallucination_detectorimportHallucinationDetector, quick_hallucination_check# Quick boolean checkresponse="The Eiffel Tower was definitely built in 1887..."is_suspicious=quick_hallucination_check(response, threshold=0.7)
# Detailed analysisdetector=HallucinationDetector()
result=detector.analyze_response(response)
print(f"Hallucination probability: {result.hallucination_probability:.2f}")
# Provide context for better accuracycontext="The user asked about the Eiffel Tower's construction date."response="The Eiffel Tower was built in 1889 for the World's Fair."result=detector.analyze_response(response, context=context)
fromhallucination_detectorimport (
quick_hallucination_check,
get_hallucination_score,
analyze_with_recommendations
)
# Quick boolean checkis_hallucinating=quick_hallucination_check(response, threshold=0.7)
# Get just the probability scorescore=get_hallucination_score(response)
# Full analysis with recommendationsanalysis=analyze_with_recommendations(response, context="...")
1. Confidence Pattern Analysis
Analyzes language patterns that indicate uncertainty or overconfidence:
Uncertainty Indicators:
"I think", "might be", "possibly", "perhaps"
"I'm not sure", "unclear", "uncertain"
Overconfidence Indicators:
"definitely", "absolutely", "without doubt"
"always", "never", "100%", "guaranteed"
2. Factual Density Scoring
Identifies responses with high concentrations of specific factual claims:
Years and dates (1989, 2023)
Monetary amounts ($1.2M, €500K)
Percentages (75%, 23.4%)
Large numbers (5 million, 2.3 billion)
Evaluates logical flow and structural consistency:
Sentence length variance
Topic continuity
Logical progression
Compares response content against provided context:
Word overlap analysis
Semantic alignment
Contextual relevance scoring
Identifies excessive repetition patterns:
Repeated sentences
Redundant information
Circular reasoning
6. Contradiction Detection
Finds conflicting statements within the same response:
response="""The Eiffel Tower was definitely built in 1887 and is exactly 324 meters tall. It was designed by Gustave Eiffel and cost exactly $1.2 million to construct. Without doubt, it receives 7 million visitors every year."""result=detector.analyze_response(response)
# Output: High hallucination probability due to overconfident language
Example 2: Contradictory Response
response="""Python is always the best programming language for data science. However, Python is never suitable for machine learning projects. It's impossible to use Python for AI development."""result=detector.analyze_response(response)
# Output: High contradiction score detected
Example 3: Uncertain but Honest Response
response="""I believe the Eiffel Tower was built sometime in the late 1800s, possibly around 1889, but I'm not completely certain about the exact date. It seems to be approximately 300 meters tall, though I'd recommend checking official sources for precise measurements."""result=detector.analyze_response(response)
# Output: Lower hallucination probability due to appropriate uncertainty
We welcome contributions! Here's how you can help:
Additional detection methods
Domain-specific adaptations
Performance optimizations
Test case contributions
Documentation improvements
git clone https://github.com/yourusername/llm-hallucination-detector.git
cd llm-hallucination-detector
# Run tests
python -m pytest tests/
# Run examples
python hallucination_detector.py
Fork the repository
Create a feature branch (git checkout -b feature/amazing-feature)
Commit your changes (git commit -m 'Add amazing feature')
Push to the branch (git push origin feature/amazing-feature)
Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Inspired by research in LLM reliability and hallucination detection