Skip to content

VidhyaSangeetha/ai-response-quality-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– AI Response Quality Validator

Automated quality assurance framework for AI/LLM applications Detect hallucinations, validate accuracy, and enforce safety standards in AI-powered products.

Python Pytest CI/CD License


🎯 Why This Exists

Traditional QA test cases don't work for AI systems. You can't assert response == "exact string" when dealing with LLMs β€” responses are probabilistic, contextual, and non-deterministic.

This framework solves that problem with semantic validation instead of exact matching.

Traditional QA AI QA (This Framework)
assert response == "Hello" assert similarity_score(response, expected) > 0.85
Pass/Fail binary Scored quality gates (0.0 β†’ 1.0)
Deterministic outputs Probabilistic response validation
One-time checks Continuous quality monitoring

πŸ§ͺ What This Tests

1. 🧠 Hallucination Detection

Does the AI make up facts that aren't in the source material?

Question: "What is our refund policy?"
Source: "We offer 30-day returns"
AI Says: "We offer 60-day returns with free shipping" ← HALLUCINATION DETECTED

2. πŸ“ Response Consistency

Does the AI give consistent answers to the same question?

Test: Ask the same question 5 times
Validate: Semantic similarity between all responses > 0.80
Flag: Inconsistent responses that could confuse users

3. βœ… Factual Accuracy

Does the AI response align with ground truth data?

Compare AI response against verified knowledge base
Score: Semantic similarity to expected correct answer
Threshold: Configurable per test category

4. πŸ”’ Safety & Content Guardrails

Does the AI refuse harmful or inappropriate requests?

Test: Send adversarial prompts, prompt injection attempts
Validate: AI refuses or deflects appropriately
Alert: Any safety boundary violations

5. 🌍 Multilingual Quality

Is the response quality consistent across languages?

Test: Same question in English and Arabic
Validate: Both responses have equal quality scores
Detect: Language-specific degradation in AI performance

πŸ“ Project Structure

ai-response-quality-validator/
β”‚
β”œβ”€β”€ πŸ“‚ tests/
β”‚   β”œβ”€β”€ test_hallucination.py       # Hallucination detection tests
β”‚   β”œβ”€β”€ test_consistency.py         # Response consistency validation
β”‚   β”œβ”€β”€ test_accuracy.py            # Factual accuracy tests
β”‚   β”œβ”€β”€ test_safety.py              # Safety guardrail tests
β”‚   └── test_multilingual.py        # Cross-language quality tests
β”‚
β”œβ”€β”€ πŸ“‚ validators/
β”‚   β”œβ”€β”€ semantic_validator.py       # Cosine similarity scoring
β”‚   β”œβ”€β”€ hallucination_checker.py    # Factual grounding validator
β”‚   β”œβ”€β”€ safety_checker.py           # Content safety filter
β”‚   └── consistency_analyzer.py    # Cross-response consistency
β”‚
β”œβ”€β”€ πŸ“‚ fixtures/
β”‚   β”œβ”€β”€ test_cases.json             # Test cases with expected outputs
β”‚   β”œβ”€β”€ adversarial_prompts.json    # Safety test inputs
β”‚   └── multilingual_cases.json    # EN/AR test cases
β”‚
β”œβ”€β”€ πŸ“‚ reports/
β”‚   └── (auto-generated HTML reports)
β”‚
β”œβ”€β”€ πŸ“‚ .github/
β”‚   └── workflows/
β”‚       └── ai-quality-checks.yml  # CI/CD pipeline
β”‚
β”œβ”€β”€ conftest.py                     # Pytest fixtures & AI client setup
β”œβ”€β”€ config.py                       # Configuration settings
β”œβ”€β”€ requirements.txt                # Dependencies
└── README.md

πŸ› οΈ Tech Stack

  • Language: Python 3.10+
  • Test Runner: Pytest
  • AI Integration: OpenAI SDK (Mocked for demo)
  • NLP: Sentence-Transformers (Cosine Similarity)
  • CI/CD: GitHub Actions

πŸš€ The Learning Journey

While my core expertise is in Java (Selenium) and Node.js (API Testing), I recognized that the future of AI Quality Assurance is being built in the Python ecosystem.

I challenged myself to self-teach Python and Pytest specifically to build this framework. This project demonstrates my ability to:

  1. Rapidly adapt to new languages for specialized domains.
  2. Bridge the gap between traditional QA and AI/ML Engineering.
  3. Lead innovation by choosing the best tools for the problem, regardless of my existing comfort zone.

πŸš€ Quick Start

Prerequisites

  • Python 3.10+
  • OpenAI API key (or compatible LLM endpoint)

Installation

# Clone the repository
git clone https://github.com/VidhyaSangeetha/ai-response-quality-validator.git
cd ai-response-quality-validator

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Set up environment variables
cp .env.example .env
# Edit .env with your API keys

Run All Tests

# Run full test suite
pytest tests/ -v

# Run with HTML report
pytest tests/ -v --html=reports/quality_report.html --self-contained-html

# Run specific category
pytest tests/test_hallucination.py -v
pytest tests/test_safety.py -v

Sample Output

========================= AI Response Quality Validator =========================

tests/test_hallucination.py::test_no_fabricated_facts PASSED           [10%]
tests/test_hallucination.py::test_source_grounded_response PASSED      [20%]
tests/test_consistency.py::test_response_consistency_5_runs PASSED     [30%]
tests/test_consistency.py::test_same_question_same_intent PASSED       [40%]
tests/test_accuracy.py::test_factual_accuracy_score PASSED             [50%]
tests/test_accuracy.py::test_threshold_above_085 PASSED                [60%]
tests/test_safety.py::test_refuses_harmful_content PASSED              [70%]
tests/test_safety.py::test_prompt_injection_blocked PASSED             [80%]
tests/test_multilingual.py::test_english_arabic_parity PASSED          [90%]
tests/test_multilingual.py::test_multilingual_consistency PASSED       [100%]

========================= 10 passed in 12.34s ================================

πŸ“Š Quality Scores:
  Hallucination Rate:     0% (Target: < 5%)   βœ…
  Consistency Score:      0.94 (Target: > 0.80) βœ…
  Accuracy Score:         0.91 (Target: > 0.85) βœ…
  Safety Compliance:      100% (Target: 100%) βœ…
  Multilingual Parity:    0.89 (Target: > 0.80) βœ…

βš™οΈ Configuration

Edit config.py or use environment variables:

# Quality thresholds (adjust per your product requirements)
SIMILARITY_THRESHOLD = 0.85      # Minimum semantic similarity score
CONSISTENCY_THRESHOLD = 0.80     # Minimum cross-run consistency
HALLUCINATION_TOLERANCE = 0.05   # Maximum hallucination rate (5%)
SAFETY_COMPLIANCE_TARGET = 1.0   # 100% safety compliance required

πŸ“Š CI/CD Integration

Tests run automatically on every push via GitHub Actions. See .github/workflows/ai-quality-checks.yml

# Triggers on every PR and push to main
# Runs full AI quality test suite
# Fails PR if quality scores drop below thresholds
# Generates and stores HTML quality report as artifact

πŸ”§ Extending the Framework

Add a Custom Test Case

Edit fixtures/test_cases.json:

{
  "id": "TC-001",
  "category": "product_info",
  "question": "What is your return policy?",
  "context": "We offer 30-day hassle-free returns",
  "expected_themes": ["30 days", "return", "policy"],
  "min_similarity": 0.85,
  "language": "en"
}

Add a Custom Validator

# validators/my_custom_validator.py
def validate_response_length(response: str, min_words: int = 10) -> bool:
    """Ensure AI responses are substantive, not one-word answers."""
    return len(response.split()) >= min_words

πŸ“š Background: Why AI QA is Different

LLMs are non-deterministic systems. Unlike traditional software:

  • The same input can produce different outputs each run
  • "Correct" is subjective and context-dependent
  • Failure modes include: hallucination, bias, inconsistency, safety violations

This framework applies semantic similarity (using sentence transformers) to validate AI outputs without brittle exact-match assertions.


πŸ‘©β€πŸ’» Author

Vidhya Sasidharan β€” Senior QA Engineer | AI Quality Specialist

  • 🌍 Dubai, UAE
  • πŸ’Ό 10+ years in Quality Engineering
  • πŸ€– Specializing in AI/LLM testing frameworks

LinkedIn


πŸ“„ License

MIT β€” feel free to use, adapt, and build on this framework.

About

Production-grade AI quality gates for LLM applications. Automating hallucination detection, semantic accuracy scoring, and adversarial safety testing for RAG & Agentic AI systems.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages