-
Notifications
You must be signed in to change notification settings - Fork 721
feat: add hallucination bench #838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # Hallucination Detection Benchmark | ||
|
|
||
| E2E evaluation of hallucination detection through the semantic router. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```bash | ||
| # 1. Start vLLM (if not running) | ||
| docker run -d --gpus all -p 8083:8000 vllm/vllm-openai:latest \ | ||
| vllm serve --model Qwen/Qwen2.5-14B-Instruct-AWQ | ||
|
|
||
| # 2. Start semantic router with hallucination config | ||
| cd /path/to/semantic-router | ||
| export LD_LIBRARY_PATH=$PWD/candle-binding/target/release | ||
| ./bin/router -config=bench/hallucination_bench/config.yaml | ||
|
|
||
| # 3. Start Envoy | ||
| func-e run --config-path config/envoy.yaml | ||
|
|
||
| # 4. Run benchmark | ||
| python -m bench.hallucination_bench.evaluate \ | ||
| --endpoint http://localhost:8801 \ | ||
| --dataset halueval \ | ||
| --max-samples 50 | ||
| ``` | ||
|
|
||
| ## Using the Large Model | ||
|
|
||
| The large model (`lettucedect-large-modernbert-en-v1`, 395M params) provides better detection accuracy than the base model. | ||
|
|
||
| ### Step 1: Download the Large Model | ||
|
|
||
| ```bash | ||
| cd /path/to/semantic-router | ||
|
|
||
| # Download from HuggingFace | ||
| hf download KRLabsOrg/lettucedect-large-modernbert-en-v1 \ | ||
| --local-dir models/lettucedect-large-modernbert-en-v1 | ||
| ``` | ||
|
|
||
| ### Step 2: Update Config | ||
|
|
||
| Edit `bench/hallucination_bench/config.yaml`: | ||
|
|
||
| ```yaml | ||
| hallucination_mitigation: | ||
| enabled: true | ||
|
|
||
| hallucination_model: | ||
| model_id: "models/lettucedect-large-modernbert-en-v1" # Use large model | ||
| threshold: 0.5 | ||
| use_cpu: true # Set to false for GPU | ||
| ``` | ||
|
|
||
| ### Step 3: Restart Router | ||
|
|
||
| ```bash | ||
| # Kill existing router | ||
| pkill -f "router.*config.yaml" | ||
|
|
||
| # Start with updated config | ||
| export LD_LIBRARY_PATH=$PWD/candle-binding/target/release | ||
| ./bin/router -config=bench/hallucination_bench/config.yaml | ||
| ``` | ||
|
|
||
| ## Supported Models | ||
|
|
||
| | Model | Params | HuggingFace ID | | ||
| |-------|--------|----------------| | ||
| | Base | 149M | `KRLabsOrg/lettucedect-base-modernbert-en-v1` | | ||
| | Large | 395M | `KRLabsOrg/lettucedect-large-modernbert-en-v1` | | ||
|
|
||
| Both use `ModernBertForTokenClassification` architecture supported by candle-binding. | ||
|
|
||
| ## Config Reference | ||
|
|
||
| Key settings in `config.yaml`: | ||
|
|
||
| ```yaml | ||
| # vLLM endpoint | ||
| vllm_endpoints: | ||
| - name: "vllm-qwen" | ||
| address: "127.0.0.1" | ||
| port: 8083 | ||
|
|
||
| # Hallucination detection | ||
| hallucination_mitigation: | ||
| enabled: true | ||
| hallucination_model: | ||
| model_id: "models/lettucedect-large-modernbert-en-v1" | ||
| threshold: 0.5 | ||
| use_cpu: true | ||
| on_hallucination_detected: "warn" # or "block" | ||
| ``` | ||
|
|
||
| ## Datasets | ||
|
|
||
| | Dataset | Command | | ||
| |---------|---------| | ||
| | HaluEval | `--dataset halueval` | | ||
| | Custom | `--dataset /path/to/data.jsonl` | | ||
|
|
||
| ## Output | ||
|
|
||
| Results saved to `bench/hallucination_bench/results/` with: | ||
|
|
||
| - Precision, Recall, F1 (when ground truth available) | ||
| - Latency metrics (avg, p50, p99) | ||
| - Per-sample detection results | ||
|
|
||
| ### Two-Stage Pipeline Efficiency Metrics | ||
|
|
||
| The benchmark tracks the computational savings from the two-stage detection pipeline: | ||
|
|
||
| ``` | ||
| ⚡ Two-Stage Pipeline Efficiency: | ||
| ---------------------------------------- | ||
| Fact-check needed: 65/100 queries | ||
| Detection skipped: 35/100 queries | ||
| Avg context length: 4500 chars | ||
| Estimated detect time: 6500.00 ms (if all ran) | ||
| Actual detect time: 4225.00 ms | ||
| Time saved: 2275.00 ms | ||
| Efficiency gain: 35.0% | ||
|
|
||
| 💡 Pre-filtering skipped 35.0% of requests, | ||
| saving 2275ms of detection compute. | ||
| ``` | ||
|
|
||
| This demonstrates the value of the HaluGate Sentinel pre-classifier: | ||
|
|
||
| - **O(1) filtering** before **O(n) detection** (n = context length) | ||
| - Non-factual queries (creative, opinion, brainstorming) skip expensive token classification | ||
| - Critical for RAG applications with large contexts (8K+ tokens) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| """Hallucination Detection Benchmark for Semantic Router. | ||
|
|
||
| This package provides end-to-end evaluation of the hallucination detection pipeline | ||
| through the router + Envoy stack. | ||
| """ | ||
|
|
||
| from .evaluate import HallucinationBenchmark | ||
| from .datasets import HaluEvalDataset, CustomDataset, get_dataset | ||
|
|
||
| __all__ = ["HallucinationBenchmark", "HaluEvalDataset", "CustomDataset", "get_dataset"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| # Configuration for hallucination detection benchmark | ||
| # Connects to real vLLM server at port 8083 | ||
|
|
||
| bert_model: | ||
| model_id: models/all-MiniLM-L12-v2 | ||
| threshold: 0.6 | ||
| use_cpu: true | ||
|
|
||
| semantic_cache: | ||
| enabled: false # Disable cache for benchmarking | ||
|
|
||
| # Classifier configuration | ||
| classifier: | ||
| category_model: | ||
| model_id: "models/category_classifier_modernbert-base_model" | ||
| use_modernbert: true | ||
| threshold: 0.6 | ||
| use_cpu: true | ||
| category_mapping_path: "models/category_classifier_modernbert-base_model/category_mapping.json" | ||
| pii_model: | ||
| model_id: "models/pii_classifier_modernbert-base_presidio_token_model" | ||
| use_modernbert: true | ||
| threshold: 0.7 | ||
| use_cpu: true | ||
| pii_mapping_path: "models/pii_classifier_modernbert-base_presidio_token_model/pii_type_mapping.json" | ||
|
|
||
| # Hallucination mitigation configuration | ||
| hallucination_mitigation: | ||
| enabled: true | ||
|
|
||
| # Fact-check classifier: determines if a prompt needs fact verification | ||
| fact_check_model: | ||
| model_id: "models/halugate-sentinel" | ||
| threshold: 0.6 | ||
| use_cpu: true | ||
| mapping_path: "models/halugate-sentinel/fact_check_mapping.json" | ||
|
|
||
| # Hallucination detector: verifies if LLM response is grounded in context | ||
| # Using large model (395M params) for better accuracy | ||
| hallucination_model: | ||
| model_id: "models/lettucedect-large-modernbert-en-v1" | ||
| threshold: 0.5 | ||
| use_cpu: true | ||
|
|
||
| # NLI model: provides explanations for hallucinated spans | ||
| nli_model: | ||
| model_id: "models/ModernBERT-base-nli" | ||
| threshold: 0.7 | ||
| use_cpu: true | ||
|
|
||
| # Action when hallucination detected: "warn" adds headers, "block" returns error | ||
| on_hallucination_detected: "warn" | ||
|
|
||
| # Fact-check rules for signal classification | ||
| # The classifier outputs one of these signals that can be referenced in decision conditions | ||
| fact_check_rules: | ||
| - name: needs_fact_check | ||
| description: "Query contains factual claims that should be verified against context" | ||
| - name: no_fact_check_needed | ||
| description: "Query is creative, code-related, or opinion-based - no fact verification needed" | ||
|
|
||
| # Prompt guard | ||
| prompt_guard: | ||
| enabled: true | ||
| use_modernbert: true | ||
| model_id: "models/jailbreak_classifier_modernbert-base_model" | ||
| threshold: 0.7 | ||
| use_cpu: true | ||
| jailbreak_mapping_path: "models/jailbreak_classifier_modernbert-base_model/jailbreak_type_mapping.json" | ||
|
|
||
| # vLLM endpoint - real vLLM server | ||
| vllm_endpoints: | ||
| - name: "vllm-general" | ||
| address: "127.0.0.1" | ||
| port: 8083 | ||
| weight: 1 | ||
| health_check_path: "/health" | ||
|
|
||
| # Model configuration - use the actual model from vLLM | ||
| model_config: | ||
| "Qwen/Qwen2.5-14B-Instruct-AWQ": | ||
| reasoning_family: "qwen3" | ||
| preferred_endpoints: ["vllm-general"] | ||
|
|
||
| # Categories for routing | ||
| categories: | ||
| - name: general | ||
| description: "General questions" | ||
| mmlu_categories: ["other"] | ||
| - name: math | ||
| description: "Mathematics and quantitative reasoning" | ||
| mmlu_categories: ["math"] | ||
| - name: science | ||
| description: "Science questions" | ||
| mmlu_categories: ["physics", "chemistry", "biology"] | ||
|
|
||
| strategy: "priority" | ||
|
|
||
| decisions: | ||
| - name: "math_decision" | ||
| description: "Mathematics and quantitative reasoning" | ||
| priority: 100 | ||
| rules: | ||
| operator: "AND" | ||
| conditions: | ||
| - type: "domain" | ||
| name: "math" | ||
| modelRefs: | ||
| - model: "Qwen/Qwen2.5-14B-Instruct-AWQ" | ||
| use_reasoning: true | ||
| plugins: | ||
| - type: "pii" | ||
| configuration: | ||
| enabled: true | ||
| pii_types_allowed: [] | ||
| - type: "hallucination" | ||
| configuration: | ||
| enabled: true | ||
| use_nli: true | ||
| hallucination_action: "header" | ||
| unverified_factual_action: "header" | ||
| include_hallucination_details: false | ||
|
|
||
| - name: "science_decision" | ||
| description: "Science questions" | ||
| priority: 100 | ||
| rules: | ||
| operator: "AND" | ||
| conditions: | ||
| - type: "domain" | ||
| name: "science" | ||
| modelRefs: | ||
| - model: "Qwen/Qwen2.5-14B-Instruct-AWQ" | ||
| use_reasoning: true | ||
| plugins: | ||
| - type: "pii" | ||
| configuration: | ||
| enabled: true | ||
| pii_types_allowed: [] | ||
| - type: "hallucination" | ||
| configuration: | ||
| enabled: true | ||
| use_nli: true | ||
| hallucination_action: "header" | ||
| unverified_factual_action: "header" | ||
| include_hallucination_details: false | ||
|
|
||
| - name: "general_decision" | ||
| description: "General questions" | ||
| priority: 50 | ||
| rules: | ||
| operator: "AND" | ||
| conditions: | ||
| - type: "domain" | ||
| name: "general" | ||
| modelRefs: | ||
| - model: "Qwen/Qwen2.5-14B-Instruct-AWQ" | ||
| use_reasoning: false | ||
| plugins: | ||
| - type: "pii" | ||
| configuration: | ||
| enabled: true | ||
| pii_types_allowed: [] | ||
| - type: "hallucination" | ||
| configuration: | ||
| enabled: true | ||
| use_nli: true | ||
| hallucination_action: "header" | ||
| unverified_factual_action: "header" | ||
| include_hallucination_details: false | ||
|
|
||
| default_model: "Qwen/Qwen2.5-14B-Instruct-AWQ" | ||
|
|
||
| # API Configuration | ||
| api: | ||
| batch_classification: | ||
| metrics: | ||
| enabled: true | ||
| detailed_goroutine_tracking: true | ||
| high_resolution_timing: false | ||
| sample_rate: 1.0 | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be bench/hallucination/config.yaml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, it is fixed now