File size: 11,726 Bytes
b6f9fa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""
FR-22: src/evaluate.py β€” MediRAG Evaluation Orchestrator
=========================================================
Top-level entry point for the evaluation pipeline.

Runs all 4 evaluation modules + RAGAS + aggregator for a given
(question, answer, context_docs) triple, returning a fully structured
composite EvalResult.

Usage as a module:
    from src.evaluate import run_evaluation
    result = run_evaluation(question, answer, context_docs)
    print(f"Score: {result.score:.3f} ({result.details['confidence_level']})")

Usage from CLI:
    python -m src.evaluate \\
        --question "What is the recommended dosage of Metformin for Type 2 Diabetes?" \\
        --answer "Metformin is typically started at 500mg twice daily..." \\
        --context-file data/processed/chunks.jsonl \\
        --top-k 5

SRS reference: FR-22, Section 7 (Evaluation Pipeline Overview)
"""
from __future__ import annotations

import argparse
import json
import logging
import sys
import time
from pathlib import Path
from typing import Optional

from src.modules.base import EvalResult
from src.modules.faithfulness import score_faithfulness
from src.modules.entity_verifier import verify_entities
from src.modules.source_credibility import score_source_credibility
from src.modules.contradiction import score_contradiction
from src.evaluation.ragas_eval import score_ragas
from src.evaluation.aggregator import aggregate

logger = logging.getLogger(__name__)


# ---------------------------------------------------------------------------
# Main evaluation function
# ---------------------------------------------------------------------------

def run_evaluation(
    question: str,
    answer: str,
    context_chunks: list[dict],
    rxnorm_cache_path: str = "data/rxnorm_cache.csv",
    run_ragas: bool = True,
    weights: Optional[dict[str, float]] = None,
    config: Optional[dict] = None,
) -> EvalResult:
    """
    Run the full MediRAG evaluation pipeline for a single QA pair.

    Args:
        question          : Original user question.
        answer            : LLM-generated answer to evaluate.
        context_chunks    : List of retrieved chunk dicts (from retriever.retrieve()).
                           Each chunk must have at minimum {'text': str}.
        rxnorm_cache_path : Path to rxnorm_cache.csv for entity verification.
        run_ragas         : Whether to run the RAGAS module (requires LLM backend).
        weights           : Override default aggregation weights (optional).

    Returns:
        EvalResult for the "aggregator" module containing:
            .score          β†’ composite score in [0, 1]
            .details        β†’ full breakdown per module
            .latency_ms     β†’ total wall-clock time
    """
    t_start = time.perf_counter()
    logger.info("=== MediRAG Evaluation START ===")
    logger.info("Question: %s", question[:120])
    logger.info("Answer  : %s", answer[:120])
    logger.info("Chunks  : %d context documents", len(context_chunks))

    # Extract text and metadata for modules that need it
    context_texts: list[str] = [c.get("text", "") for c in context_chunks]
    chunk_ids: list[str] = [
        c.get("chunk_id") or c.get("metadata", {}).get("chunk_id") or f"chunk_{i}"
        for i, c in enumerate(context_chunks)
    ]

    # -------------------------------------------------------------------------
    # Retrieval Quality Gate
    # If the retriever's absolute RRF score is too low, the chunks are likely
    # unrelated to the question β€” evaluation against them produces false HRS spikes.
    # Threshold: max raw RRF for top-1 in both sources = 2/(60+1) β‰ˆ 0.0328
    # We flag as insufficient if max_rrf < 0.008 (only very weak BM25 or FAISS match)
    # -------------------------------------------------------------------------
    RETRIEVAL_CONFIDENCE_THRESHOLD = 0.008
    retrieval_confidence = context_chunks[0].get("_retrieval_confidence", 1.0) if context_chunks else 0.0

    if context_chunks and retrieval_confidence < RETRIEVAL_CONFIDENCE_THRESHOLD:
        logger.warning(
            "Retrieval confidence %.6f below threshold %.3f β€” context likely irrelevant to question.",
            retrieval_confidence, RETRIEVAL_CONFIDENCE_THRESHOLD,
        )
        total_ms = int((time.perf_counter() - t_start) * 1000)
        return EvalResult(
            module_name="aggregator",
            score=0.5,
            details={
                "retrieval_insufficient": True,
                "retrieval_confidence": retrieval_confidence,
                "hrs": 50,
                "risk_band": "MODERATE",
                "confidence_level": "LOW",
                "total_pipeline_ms": total_ms,
                "module_results": {},
                "warning": (
                    "Retrieved context has very low relevance to the question "
                    f"(retrieval_confidence={retrieval_confidence:.4f}). "
                    "Evaluation scores would be meaningless. "
                    "Consider rephrasing the question or expanding the index."
                ),
            },
            latency_ms=total_ms,
        )

    # -------------------------------------------------------------------------
    # Module 1: Faithfulness (DeBERTa NLI)
    # -------------------------------------------------------------------------
    logger.info("--- Module 1: Faithfulness ---")
    faith_result = score_faithfulness(
        answer=answer,
        context_docs=context_texts,
        chunk_ids=chunk_ids,
        config=config,
    )

    # -------------------------------------------------------------------------
    # Module 2: Entity Verification (SciSpaCy + RxNorm)
    # -------------------------------------------------------------------------
    logger.info("--- Module 2: Entity Verification ---")
    entity_result = verify_entities(
        answer=answer,
        question=question,
        context_docs=context_texts,
        rxnorm_cache_path=rxnorm_cache_path,
    )

    # -------------------------------------------------------------------------
    # Module 3: Source Credibility (Evidence Tier)
    # -------------------------------------------------------------------------
    logger.info("--- Module 3: Source Credibility ---")
    source_result = score_source_credibility(retrieved_chunks=context_chunks)

    # -------------------------------------------------------------------------
    # Module 4: Contradiction Detection (DeBERTa NLI cross-check)
    # -------------------------------------------------------------------------
    logger.info("--- Module 4: Contradiction Detection ---")
    contra_result = score_contradiction(
        answer=answer,
        context_docs=context_texts,
    )

    # -------------------------------------------------------------------------
    # RAGAS (optional β€” requires LLM backend)
    # -------------------------------------------------------------------------
    ragas_result: Optional[EvalResult] = None
    if run_ragas:
        logger.info("--- RAGAS Evaluation ---")
        ragas_result = score_ragas(
            question=question,
            answer=answer,
            context_docs=context_texts,
        )

    # -------------------------------------------------------------------------
    # Aggregator: weighted composite
    # -------------------------------------------------------------------------
    logger.info("--- Aggregator ---")
    agg_result = aggregate(
        faithfulness_result=faith_result,
        entity_result=entity_result,
        source_result=source_result,
        contradiction_result=contra_result,
        ragas_result=ragas_result,
        weights=weights,
    )

    total_ms = int((time.perf_counter() - t_start) * 1000)
    agg_result.details["total_pipeline_ms"] = total_ms

    # Attach per-module results for API/dashboard access
    agg_result.details["module_results"] = {
        "faithfulness":       {"score": faith_result.score,  "details": faith_result.details},
        "entity_verifier":    {"score": entity_result.score, "details": entity_result.details},
        "source_credibility": {"score": source_result.score, "details": source_result.details},
        "contradiction":      {"score": contra_result.score, "details": contra_result.details},
        "ragas":              {"score": ragas_result.score,  "details": ragas_result.details} if ragas_result else None,
    }

    logger.info(
        "=== MediRAG Evaluation DONE: score=%.3f (%s) in %d ms ===",
        agg_result.score,
        agg_result.details.get("confidence_level", "?"),
        total_ms,
    )
    return agg_result


# ---------------------------------------------------------------------------
# CLI entry point
# ---------------------------------------------------------------------------

def _build_parser() -> argparse.ArgumentParser:
    p = argparse.ArgumentParser(
        description="MediRAG evaluation pipeline (FR-22)"
    )
    p.add_argument("--question",      required=True, help="User question")
    p.add_argument("--answer",        required=True, help="LLM answer to evaluate")
    p.add_argument("--context-file",  default="data/processed/chunks.jsonl",
                   help="JSONL file of chunks (output of ingest.py)")
    p.add_argument("--top-k",         type=int, default=5,
                   help="Number of context chunks to use")
    p.add_argument("--rxnorm-cache",  default="data/rxnorm_cache.csv",
                   help="Path to rxnorm_cache.csv")
    p.add_argument("--no-ragas",      action="store_true",
                   help="Skip RAGAS evaluation (no LLM backend needed)")
    p.add_argument("--json",          action="store_true",
                   help="Output result as JSON")
    return p


def _load_context_from_file(path: str, top_k: int) -> list[dict]:
    """Load top-k chunks from a JSONL file as simple dicts."""
    chunks = []
    try:
        with open(path, "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if line:
                    chunks.append(json.loads(line))
                if len(chunks) >= top_k:
                    break
    except FileNotFoundError:
        logger.error("Context file not found: %s", path)
        sys.exit(1)
    return chunks


if __name__ == "__main__":
    import yaml

    # Load config.yaml for logging setup
    try:
        cfg = yaml.safe_load(Path("config.yaml").read_text())
        log_level = cfg.get("logging", {}).get("level", "INFO")
    except Exception:
        log_level = "INFO"

    logging.basicConfig(
        level=getattr(logging, log_level, logging.INFO),
        format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
    )

    args = _build_parser().parse_args()
    chunks = _load_context_from_file(args.context_file, args.top_k)

    result = run_evaluation(
        question=args.question,
        answer=args.answer,
        context_chunks=chunks,
        rxnorm_cache_path=args.rxnorm_cache,
        run_ragas=not args.no_ragas,
    )

    if args.json:
        import dataclasses
        print(json.dumps(dataclasses.asdict(result), indent=2))
    else:
        print(f"\n{'='*60}")
        print(f"  MediRAG Evaluation Result")
        print(f"{'='*60}")
        print(f"  Score          : {result.score:.3f}")
        print(f"  Confidence     : {result.details.get('confidence_level', 'N/A')}")
        print(f"  Pipeline time  : {result.details.get('total_pipeline_ms', 0)} ms")
        print(f"\n  Module Breakdown:")
        for mod, res in (result.details.get("module_results") or {}).items():
            if res:
                print(f"    {mod:22s}: {res['score']:.3f}")
        print(f"{'='*60}\n")