File size: 2,456 Bytes
c6aa66a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Retrieval evaluation for Docent AI.

For each question in eval_questions.json, runs the full retrieval pipeline
(hybrid BM25 + vector search + reranker) and checks whether a chunk from one
of the expected source PDFs appears in the results.

Reports:
  - hit@k   : fraction of questions where an expected file was retrieved
  - MRR     : mean reciprocal rank of the first expected file

Usage (from the project root):
    py evaluate.py
"""
import json
import sys
import time
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent / "backend"))

from ingest import load_vector_store          # noqa: E402
from rag import build_engine, RETRIEVE_K      # noqa: E402

EVAL_FILE = Path(__file__).parent / "eval_questions.json"


def retrieve_with_retry(engine, question: str, attempts: int = 3):
    """The free NVIDIA endpoints occasionally return transient 5xx errors."""
    for i in range(attempts):
        try:
            return engine._retrieve(question)
        except Exception as e:
            if i == attempts - 1:
                raise
            print(f"  (transient {e.__class__.__name__} — retrying in 3s)")
            time.sleep(3)


def main() -> None:
    cases = json.loads(EVAL_FILE.read_text(encoding="utf-8"))

    store = load_vector_store()
    if store is None:
        print("No FAISS index found — ingest some PDFs first.")
        sys.exit(1)

    engine = build_engine(store)

    hits = 0
    reciprocal_ranks = []
    print(f"Evaluating {len(cases)} questions (k={RETRIEVE_K})\n")

    for case in cases:
        question = case["question"]
        expected = set(case["expected_files"])

        docs = retrieve_with_retry(engine, question)
        retrieved = [d.metadata.get("source", "?") for d in docs]

        rank = next(
            (i + 1 for i, f in enumerate(retrieved) if f in expected),
            None,
        )
        if rank is not None:
            hits += 1
            reciprocal_ranks.append(1.0 / rank)
            print(f"  HIT @{rank}  {question}")
        else:
            reciprocal_ranks.append(0.0)
            print(f"  MISS      {question}")
            print(f"            expected : {sorted(expected)}")
            print(f"            retrieved: {retrieved}")

    n = len(cases)
    print("\n" + "=" * 60)
    print(f"hit@{RETRIEVE_K} : {hits}/{n}  ({hits / n:.0%})")
    print(f"MRR    : {sum(reciprocal_ranks) / n:.3f}")


if __name__ == "__main__":
    main()