File size: 5,286 Bytes
3998131 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
"""
Thorough Testing Script for Module A
Runs comprehensive real-life scenarios and generates a report.
"""
import os
import logging
import json
from datetime import datetime
from dotenv import load_dotenv
# Load env vars
load_dotenv()
from module_a.rag_chain import LegalRAGChain
from module_a.config import LOG_LEVEL
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def main():
print("=" * 80)
print("Running Thorough Tests for Nepal Justice Weaver")
print("=" * 80)
# Check API key
if not os.getenv("MISTRAL_API_KEY"):
print("Error: MISTRAL_API_KEY not found.")
return
# Initialize chain
try:
rag = LegalRAGChain()
except Exception as e:
print(f"Failed to initialize RAG chain: {e}")
return
# Test Scenarios
scenarios = [
{
"category": "Citizenship",
"query": "I am a single mother. Can I get citizenship for my child in my name? The father is not in the picture.",
"expected_intent": "Citizenship by descent (Article 11)"
},
{
"category": "Inheritance",
"query": "My parents want to give all property to my brother. As a daughter, do I have a claim?",
"expected_intent": "Equal inheritance rights (Article 18, Civil Code)"
},
{
"category": "Divorce",
"query": "My husband wants a divorce but refuses to share any property. What are my rights?",
"expected_intent": "Property partition upon divorce"
},
{
"category": "Marriage",
"query": "We want to do a court marriage. What is the process and what documents do we need?",
"expected_intent": "Marriage registration process"
},
{
"category": "Fundamental Rights",
"query": "Can the police arrest me for posting my opinion on social media?",
"expected_intent": "Freedom of opinion and speech (Article 17)"
},
{
"category": "Cyber Law",
"query": "Someone is harassing me online and threatening me. What can I do?",
"expected_intent": "Cyber crime / harassment laws (might be general guidance if specific act not in DB)"
},
{
"category": "Contracts",
"query": "I lent money to a friend verbally. Is this agreement valid in court?",
"expected_intent": "Contract validity / evidence"
}
]
results = []
print(f"\nRunning {len(scenarios)} scenarios...\n")
for i, scenario in enumerate(scenarios, 1):
print(f"Scenario {i}/{len(scenarios)}: {scenario['category']}")
print(f"Query: {scenario['query']}")
try:
# Run RAG
response = rag.run(scenario['query'])
# Store result
result_entry = {
"scenario": scenario,
"response": response,
"timestamp": datetime.now().isoformat()
}
results.append(result_entry)
print("β Success\n")
except Exception as e:
print(f"β Failed: {e}\n")
results.append({
"scenario": scenario,
"error": str(e),
"timestamp": datetime.now().isoformat()
})
# Generate Report
generate_report(results)
print("=" * 80)
print(f"Testing Complete. Report generated: module_a/test_report.md")
print("=" * 80)
def generate_report(results):
"""Generate a Markdown report from test results"""
report_path = "module_a/test_report.md"
with open(report_path, "w", encoding="utf-8") as f:
f.write("# Module A: Thorough Test Report\n\n")
f.write(f"**Date**: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"**Total Scenarios**: {len(results)}\n\n")
f.write("## Summary of Results\n\n")
for i, res in enumerate(results, 1):
category = res['scenario']['category']
status = "β
Success" if "error" not in res else "β Failed"
f.write(f"- **{i}. {category}**: {status}\n")
f.write("\n---\n\n")
for i, res in enumerate(results, 1):
scenario = res['scenario']
f.write(f"## Scenario {i}: {scenario['category']}\n\n")
f.write(f"**Query**: \"{scenario['query']}\"\n\n")
f.write(f"**Expected Intent**: {scenario['expected_intent']}\n\n")
if "error" in res:
f.write(f"> β **Error**: {res['error']}\n\n")
continue
response = res['response']
f.write("### Generated Response\n\n")
f.write(f"{response['explanation']}\n\n")
f.write("### Retrieved Sources\n\n")
for source in response['sources']:
f.write(f"- **{source['section']}** ({source['file']}) - Score: {source['relevance_score']:.4f}\n")
f.write("\n---\n\n")
if __name__ == "__main__":
main()
|