Overview
ThePAS2 class is the main implementation of the Paraphrase-based Approach for Scrutinizing Systems. It uses a model-as-judge methodology to detect hallucinations by comparing responses to paraphrased versions of the same query.
Constructor
Mistral API key. If not provided, reads from
HF_MISTRAL_API_KEY or MISTRAL_API_KEY environment variables.OpenAI API key. If not provided, reads from
HF_OPENAI_API_KEY or OPENAI_API_KEY environment variables.Optional callback function to receive progress updates during detection.
Class attributes
Initialized Mistral API client instance.
Initialized OpenAI API client instance.
Model identifier for Mistral API (default:
"mistral-large-latest").Model identifier for OpenAI API (default:
"o3-mini").Registered callback function for progress updates.
Methods
generate_paraphrases
Generate paraphrased versions of a query. See generate_paraphrases for details.detect_hallucination
Detect hallucinations by analyzing responses to paraphrased queries. See detect_hallucination for details.judge_hallucination
Use a judge model to evaluate responses for hallucinations. See judge_hallucination for details.get_responses
List of query strings to send to the model.
List of response strings, one for each query in the same order.
This method uses
ThreadPoolExecutor with a maximum of 5 parallel workers to fetch responses efficiently.Example usage
Example with progress callback
Environment variable priority
The class checks for API keys in the following order:- Constructor parameters (
mistral_api_key,openai_api_key) - Hugging Face Spaces secrets (
HF_MISTRAL_API_KEY,HF_OPENAI_API_KEY) - Standard environment variables (
MISTRAL_API_KEY,OPENAI_API_KEY)