The most common use case is detecting hallucinations by comparing responses to paraphrased queries:
import osfrom pas2 import PAS2# Initialize PAS2 with API keyspas2 = PAS2( mistral_api_key=os.environ.get("MISTRAL_API_KEY"), openai_api_key=os.environ.get("OPENAI_API_KEY"))# Detect hallucinations for a queryquery = "Who was the first person to land on the moon?"results = pas2.detect_hallucination(query, n_paraphrases=3)# Check the resultsif results["hallucination_detected"]: print(f"Hallucination detected with {results['confidence_score']:.2f} confidence") print(f"Reasoning: {results['reasoning']}")else: print("No hallucination detected")
PAS2 automatically loads API keys from environment variables:
import osfrom pas2 import PAS2# API keys are loaded from HF_MISTRAL_API_KEY and HF_OPENAI_API_KEY# or MISTRAL_API_KEY and OPENAI_API_KEYpas2 = PAS2()results = pas2.detect_hallucination( "What is the capital of France?", n_paraphrases=3)
Expected output:
{ "original_query": "What is the capital of France?", "original_response": "The capital of France is Paris.", "hallucination_detected": False, "confidence_score": 0.95, "summary": "All responses consistently identify Paris as the capital of France."}
Generate paraphrased versions of a query to test consistency:
from pas2 import PAS2import ospas2 = PAS2( mistral_api_key=os.environ.get("MISTRAL_API_KEY"), openai_api_key=os.environ.get("OPENAI_API_KEY"))query = "How many planets are in our solar system?"all_queries = pas2.generate_paraphrases(query, n_paraphrases=3)print("Original query:")print(all_queries[0])print("\nParaphrased queries:")for i, paraphrase in enumerate(all_queries[1:], 1): print(f"{i}. {paraphrase}")
Expected output:
Original query:How many planets are in our solar system?Paraphrased queries:1. What is the total number of planets in our solar system?2. Can you tell me how many planets exist in the solar system?3. How many planets does our solar system contain?
Retrieve responses from the LLM for multiple queries:
from pas2 import PAS2import ospas2 = PAS2( mistral_api_key=os.environ.get("MISTRAL_API_KEY"), openai_api_key=os.environ.get("OPENAI_API_KEY"))queries = [ "What is the speed of light?", "Can you tell me the speed at which light travels?", "How fast does light move?"]responses = pas2.get_responses(queries)for query, response in zip(queries, responses): print(f"Q: {query}") print(f"A: {response[:100]}...\n")
Expected output:
Q: What is the speed of light?A: The speed of light in a vacuum is approximately 299,792,458 meters per second (m/s), or about...Q: Can you tell me the speed at which light travels?A: Light travels at approximately 299,792,458 meters per second in a vacuum, which is often rou...Q: How fast does light move?A: Light moves at a constant speed of about 299,792 kilometers per second (or approximately 186,...