| """ |
| Natural Language Inference detector – checks if generated response is consistent with input. |
| """ |
| import logging |
| from typing import Optional |
| import torch |
| from transformers import pipeline |
|
|
| logger = logging.getLogger(__name__) |
|
|
| class NLIDetector: |
| """Uses an NLI model to detect contradictions/hallucinations.""" |
| def __init__(self, model_name: str = "typeform/distilroberta-base-mnli"): |
| try: |
| self.pipeline = pipeline( |
| "text-classification", |
| model=model_name, |
| device=0 if torch.cuda.is_available() else -1 |
| ) |
| logger.info(f"NLI model {model_name} loaded.") |
| except Exception as e: |
| logger.error(f"Failed to load NLI model: {e}") |
| self.pipeline = None |
|
|
| def check(self, premise: str, hypothesis: str) -> Optional[float]: |
| """ |
| Returns probability of entailment (higher means more consistent). |
| """ |
| if self.pipeline is None: |
| return None |
| try: |
| result = self.pipeline(f"{premise} </s></s> {hypothesis}")[0] |
| |
| if result['label'] == 'ENTAILMENT': |
| return result['score'] |
| else: |
| |
| |
| |
| |
| |
| |
| return 0.0 |
| except Exception as e: |
| logger.error(f"NLI error: {e}") |
| return None |