Overview
Thedetect_hallucination method is the primary interface for hallucination detection in PAS2. It orchestrates the complete detection pipeline: generating paraphrases, fetching responses, and judging for hallucinations.
Method signature
Parameters
The user query to analyze for potential hallucinations.
Number of paraphrased versions to generate for comparison.
Return value
Dictionary containing complete detection results with the following keys:
The input query exactly as provided.
The model’s response to the original query.
List of paraphrased versions of the query.
List of model responses to the paraphrased queries.
Whether hallucinations were detected across the responses.
Confidence score between 0 and 1 for the detection result.
List of conflicting facts identified by the judge model.
Detailed explanation of the judgment from the judge model.
Concise summary of the hallucination analysis.
Detection pipeline
The method executes three main steps:Step 1: Generate paraphrases
Callsgenerate_paraphrases to create semantically equivalent versions of the query.
Step 2: Fetch responses
Gets responses from the model for each query (original + paraphrases).Step 3: Judge for hallucinations
Analyzes all responses using the judge model to detect inconsistencies.Example usage
Example with custom paraphrases
Progress tracking
If aprogress_callback is registered, the method reports progress through multiple stages:
Progress stages
The method emits the following progress events:starting: Detection process initiatedgenerating_paraphrases: Creating paraphrased queriesparaphrases_complete: Paraphrases generated successfullygetting_responses: Fetching model responsesresponses_progress: Individual response receivedresponses_complete: All responses collectedjudging: Analyzing responses for hallucinationscomplete: Detection finished
The method processes responses sequentially to provide fine-grained progress updates. For parallel response fetching, use the
get_responses method directly.Error handling
The method is designed to be robust:- Paraphrase generation failures trigger fallback paraphrases
- Individual response errors return error messages for those specific queries
- Judge model errors return a fallback judgment with
confidence_score=0.0
Performance considerations
- Total time: ~10-20 seconds for 3 paraphrases
- Time breakdown:
- Paraphrase generation: 1-3 seconds
- Response fetching: 5-10 seconds (sequential)
- Judgment: 2-5 seconds
- Increasing
n_paraphrasesproportionally increases response fetching time