|
import json
|
|
import os
|
|
import datetime
|
|
from qa_loader import load_qa_and_create_vectorstore
|
|
from rag_chain import generate_response
|
|
from rapidfuzz import fuzz
|
|
|
|
|
|
os.makedirs("logs", exist_ok=True)
|
|
|
|
|
|
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
|
log_file = f"logs/auto_test_results_{timestamp}.txt"
|
|
|
|
|
|
retriever = load_qa_and_create_vectorstore()
|
|
|
|
|
|
with open("Q&A_cleaned.json", "r", encoding="utf-8") as f:
|
|
qa_data = json.load(f)
|
|
|
|
|
|
total_questions = len(qa_data)
|
|
correct_answers = 0
|
|
incorrect_answers = 0
|
|
|
|
|
|
SIMILARITY_THRESHOLD = 60
|
|
|
|
|
|
with open(log_file, "w", encoding="utf-8") as log:
|
|
log.write(f"Auto Test Run - {timestamp}\n")
|
|
log.write("=" * 80 + "\n")
|
|
|
|
for idx, item in enumerate(qa_data, start=1):
|
|
question = item['QUESTION']
|
|
expected_answer = item['ANSWER']
|
|
|
|
print(f"{idx}/{total_questions} Asking: {question}")
|
|
ai_response = generate_response(retriever, question)
|
|
|
|
|
|
similarity_score = fuzz.ratio(expected_answer.lower(), ai_response.lower())
|
|
|
|
if similarity_score >= SIMILARITY_THRESHOLD:
|
|
result = f"✅ Correct (Similarity: {similarity_score:.2f}%)"
|
|
correct_answers += 1
|
|
else:
|
|
result = f"❌ Incorrect (Similarity: {similarity_score:.2f}%)"
|
|
incorrect_answers += 1
|
|
|
|
|
|
log.write(f"Question {idx}/{total_questions}:\n")
|
|
log.write(f"Q: {question}\n")
|
|
log.write(f"Expected Answer: {expected_answer}\n")
|
|
log.write(f"AI Response: {ai_response}\n")
|
|
log.write(f"Similarity: {similarity_score:.2f}%\n")
|
|
log.write(f"Result: {result}\n")
|
|
log.write("-" * 80 + "\n")
|
|
|
|
print(f"🔎 {result} - Logged")
|
|
|
|
|
|
accuracy = (correct_answers / total_questions) * 100
|
|
|
|
log.write("\nTEST SUMMARY\n")
|
|
log.write("=" * 80 + "\n")
|
|
log.write(f"Total Questions: {total_questions}\n")
|
|
log.write(f"Correct Answers: {correct_answers}\n")
|
|
log.write(f"Incorrect Answers: {incorrect_answers}\n")
|
|
log.write(f"Accuracy: {accuracy:.2f}%\n")
|
|
log.write("=" * 80 + "\n")
|
|
|
|
|
|
print("\n🔔 TEST COMPLETED")
|
|
print(f"✅ Correct: {correct_answers}")
|
|
print(f"❌ Incorrect: {incorrect_answers}")
|
|
print(f"📊 Accuracy: {accuracy:.2f}%")
|
|
print(f"📂 Detailed log saved to: {log_file}")
|
|
|