A hallucination detection model fine-tuned on RAGTruth dataset with Data2txt augmentation using extended 32K context ModernBERT. Specifically designed for long documents that exceed 8K tokens.
Evaluated on
llm-semantic-router/longcontext-haldetect (337 test samples, avg 17,550 tokens):
This model detects hallucinations in LLM-generated text by classifying each token as either Supported (grounded in context) or Hallucinated (not supported by context).
1from transformers import AutoModelForTokenClassification, AutoTokenizer
2import torch
3
4model_name = "llm-semantic-router/modernbert-base-32k-haldetect"
5tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
6model = AutoModelForTokenClassification.from_pretrained(model_name, trust_remote_code=True)
7
8# Format: context + question + answer
9text = """Context: The Eiffel Tower is located in Paris, France. It was completed in 1889.
10Question: Where is the Eiffel Tower and when was it built?
11Answer: The Eiffel Tower is located in London, England and was completed in 1920."""
12
13inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=24000)
14with torch.no_grad():
15 outputs = model(**inputs)
16predictions = outputs.logits.argmax(dim=-1)
17
18# 0 = Supported, 1 = Hallucinated
19# Tokens for "London, England" and "1920" will be marked as hallucinated
1from lettucedetect.models.inference import HallucinationDetector
2
3detector = HallucinationDetector(
4 method="transformer",
5 model_path="llm-semantic-router/modernbert-base-32k-haldetect",
6 max_length=24000 # Use extended context
7)
8
9context = "The Eiffel Tower is located in Paris, France. It was completed in 1889."
10question = "Where is the Eiffel Tower?"
11answer = "The Eiffel Tower is located in London, England."
12
13spans = detector.predict(context, question, answer)
14# Returns: [{"text": "London, England", "start": 35, "end": 50, "confidence": 0.95}]
The DART and E2E datasets were synthetically generated using Qwen2.5-72B-Instruct to create both faithful and intentionally hallucinated responses from structured data, then LLM-annotated for span-level hallucinations.
1base_model: llm-semantic-router/modernbert-base-32k
2max_length: 8192
3batch_size: 32
4learning_rate: 1e-5
5epochs: 6
6loss: CrossEntropyLoss (weighted)
7scheduler: None (constant LR)
8early_stopping_patience: 4
1@misc{modernbert-32k-haldetect,
2 title={ModernBERT-32K Hallucination Detector with Data2txt Augmentation},
3 author={LLM Semantic Router Team},
4 year={2026},
5 url={https://huggingface.co/llm-semantic-router/modernbert-base-32k-haldetect}
6}