Overview
The paraphrasing technique is the foundation of PAS2’s hallucination detection approach. By generating semantically equivalent variations of a query, PAS2 can test whether an AI model provides consistent answers to questions that ask for the same information in different ways.How it works
PAS2 generates multiple paraphrases of the original query using the Mistral API. These paraphrases preserve the original meaning while varying the wording and structure.Original query input
The user provides an original query that will be tested for hallucination potential.
Paraphrase generation
The system generates N paraphrases (default: 3) using a language model with specific instructions to maintain semantic equivalence.
Implementation details
Thegenerate_paraphrases() method is responsible for creating semantic variations:
Method signature
Parameters:query(str): The original question or prompt to paraphrasen_paraphrases(int): Number of paraphrases to generate (default: 3)
List[str]: A list containing the original query plus N paraphrases
System prompt
The paraphrasing process uses a carefully crafted system prompt:The system uses JSON mode (
response_format={"type": "json_object"}) to ensure structured output that can be reliably parsed.Response handling
The implementation handles multiple JSON structures flexibly:JSON structure variations
JSON structure variations
The code handles three common response formats:
- Object with “paraphrases” key:
{"paraphrases": [...]} - Object with “results” key:
{"results": [...]} - Direct array:
["paraphrase1", "paraphrase2", ...]
Fallback mechanism
When paraphrase generation fails, PAS2 uses a fallback strategy to ensure the detection process can continue:The fallback mechanism generates simple template-based paraphrases. While less sophisticated than model-generated paraphrases, this ensures the system remains functional even during API failures.
Query collection structure
The method returns a list where:- Index 0: Original query (unchanged)
- Index 1-N: Generated paraphrases
Performance considerations
The paraphrasing step typically completes in 1-3 seconds, depending on:- API response time
- Number of paraphrases requested
- Complexity of the original query
All paraphrase generation is logged with timing information for monitoring and debugging purposes (pas2.py:116).