File size: 10,526 Bytes
09da05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""Run retrieval relevance evaluation pipelines."""

from __future__ import annotations

import argparse
import json
from pathlib import Path
from typing import Any, Dict, List, Sequence

from adsp.core.rag.fact_data_index import build_fact_data_index_from_markdown


REPO_ROOT = Path(__file__).resolve().parents[2]


def _default_queries_path() -> Path:
    ground_truth_dir = REPO_ROOT / "data/evaluation/rag_retrieval/ground_truth"
    if ground_truth_dir.exists() and any(ground_truth_dir.glob("*.json")):
        return ground_truth_dir
    return ground_truth_dir / "test_queries.json"


def _default_retrieval_output() -> Path:
    return REPO_ROOT / "data/evaluation/rag_retrieval/system_output/retrieval_results.json"


def _default_evaluation_output() -> Path:
    return REPO_ROOT / "data/evaluation/rag_retrieval/evaluation_results.json"


def _default_fact_data_dir() -> Path:
    return REPO_ROOT / "data/processed/fact_data/pages"


def _normalize_queries_payload(payload: Any) -> List[Dict[str, Any]]:
    if isinstance(payload, dict) and isinstance(payload.get("test_queries"), list):
        return payload["test_queries"]
    if isinstance(payload, list):
        return payload
    return []


def _load_queries(path: Path) -> List[Dict[str, Any]]:
    if path.is_dir():
        entries: List[Dict[str, Any]] = []
        for file_path in sorted(path.glob("*.json")):
            payload = json.loads(file_path.read_text(encoding="utf-8"))
            entries.extend(_normalize_queries_payload(payload))
        return entries

    payload = json.loads(path.read_text(encoding="utf-8"))
    return _normalize_queries_payload(payload)


def _doc_to_payload(doc: Any, *, rank: int, score: float | None) -> Dict[str, Any]:
    return {
        "page_content": doc.page_content,
        "metadata": doc.metadata or {},
        "rank": rank,
        "score": score,
        "relevance": None,
    }

def _build_fact_index(markdown_dir: Path) -> Any:
    index = build_fact_data_index_from_markdown(markdown_dir)
    if index is None:
        raise FileNotFoundError(f"No fact data markdown found in {markdown_dir}")
    return index


def _search_with_scores(index: Any, query: str, *, k: int) -> List[Dict[str, Any]]:
    vectorstore = index.rag.vectorstore
    if hasattr(vectorstore, "similarity_search_with_score"):
        try:
            results = vectorstore.similarity_search_with_score(query, k=k)
            return [
                {"doc": doc, "score": float(score) if score is not None else None}
                for doc, score in results
            ]
        except Exception:
            pass
    docs = index.search(query, k=k)
    return [{"doc": doc, "score": None} for doc in docs]


def _parse_k_values(raw: str) -> List[int]:
    values: List[int] = []
    for item in raw.split(","):
        item = item.strip()
        if not item:
            continue
        value = int(item)
        if value <= 0:
            raise ValueError("k values must be positive integers")
        values.append(value)
    if not values:
        raise ValueError("k values cannot be empty")
    return values


def _is_relevant(doc: Dict[str, Any]) -> bool:
    return doc.get("relevance") == 1 or doc.get("relevance_label") == 1


def _compute_metrics(
    labeled_results: List[Dict[str, Any]],
    k_values: Sequence[int],
) -> Dict[str, Any]:
    totals = {k: {"precision": 0.0, "recall": 0.0} for k in k_values}
    details: List[Dict[str, Any]] = []
    unlabeled_total = 0

    for entry in labeled_results:
        retrieved_docs = entry.get("retrieved_docs") or entry.get("relevant_docs") or []
        if not isinstance(retrieved_docs, list):
            retrieved_docs = []

        relevant_count = sum(1 for doc in retrieved_docs if _is_relevant(doc))
        unlabeled = sum(
            1
            for doc in retrieved_docs
            if doc.get("relevance") is None and doc.get("relevance_label") is None
        )
        unlabeled_total += unlabeled
        detail_entry = {
            "query_id": entry.get("query_id"),
            "query": entry.get("query"),
            "retrieved_total": len(retrieved_docs),
            "relevant_total": relevant_count,
            "unlabeled_total": unlabeled,
        }

        for k in k_values:
            top_k = retrieved_docs[:k]
            hits = sum(1 for doc in top_k if _is_relevant(doc))
            precision = hits / k if k else 0.0
            recall = hits / relevant_count if relevant_count else 0.0
            totals[k]["precision"] += precision
            totals[k]["recall"] += recall
            detail_entry[f"precision_at_{k}"] = precision
            detail_entry[f"recall_at_{k}"] = recall

        details.append(detail_entry)

    query_count = len(labeled_results)
    averages = {
        f"precision_at_{k}": totals[k]["precision"] / query_count if query_count else 0.0
        for k in k_values
    }
    averages.update(
        {
            f"recall_at_{k}": totals[k]["recall"] / query_count if query_count else 0.0
            for k in k_values
        }
    )
    return {
        **averages,
        "total_queries": query_count,
        "unlabeled_total": unlabeled_total,
        "query_details": details,
        "k_values": list(k_values),
    }


def _add_retrieval_subparser(subparsers: argparse._SubParsersAction) -> None:
    parser = subparsers.add_parser("retrieve", help="Run retrieval for all test queries.")
    parser.add_argument(
        "--queries-file",
        type=Path,
        default=_default_queries_path(),
        help="JSON file or directory containing test queries.",
    )
    parser.add_argument(
        "--fact-data-dir",
        "--markdown-dir",
        type=Path,
        dest="fact_data_dir",
        default=_default_fact_data_dir(),
        help="Directory containing fact data markdown pages to index.",
    )
    parser.add_argument(
        "--output",
        type=Path,
        default=_default_retrieval_output(),
        help="Where to write the retrieval results JSON.",
    )
    parser.add_argument(
        "--top-k",
        type=int,
        default=20,
        help="Number of documents to retrieve per query.",
    )


def _add_evaluate_subparser(subparsers: argparse._SubParsersAction) -> None:
    parser = subparsers.add_parser("evaluate", help="Compute metrics from labeled results.")
    parser.add_argument(
        "--labeled-results",
        type=Path,
        default=_default_retrieval_output(),
        help="JSON file containing retrieved docs with manual relevance labels.",
    )
    parser.add_argument(
        "--output",
        type=Path,
        default=_default_evaluation_output(),
        help="Where to write the evaluation results JSON.",
    )
    parser.add_argument(
        "--k-values",
        type=_parse_k_values,
        default=_parse_k_values("3,5,10,20,25"),
        help="Comma-separated list of K values for precision/recall (e.g., 3,5,10,20,25).",
    )


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="RAG retrieval evaluation pipelines.")
    subparsers = parser.add_subparsers(dest="command", required=True)
    _add_retrieval_subparser(subparsers)
    _add_evaluate_subparser(subparsers)
    return parser.parse_args()


def _run_retrieval(args: argparse.Namespace) -> int:
    if not args.queries_file.exists():
        raise FileNotFoundError(f"Queries path not found: {args.queries_file}")
    if args.queries_file.is_dir() and not any(args.queries_file.glob("*.json")):
        raise FileNotFoundError(f"No query JSON files found in: {args.queries_file}")
    if not args.fact_data_dir.exists():
        raise FileNotFoundError(f"Fact data dir not found: {args.fact_data_dir}")

    queries = _load_queries(args.queries_file)
    index = _build_fact_index(args.fact_data_dir)

    results: List[Dict[str, Any]] = []
    for query in queries:
        if not isinstance(query, dict):
            continue
        persona_id = query.get("persona_id") or ""
        query_text = query.get("query") or ""
        if not persona_id or not query_text:
            results.append(
                {
                    "query_id": query.get("query_id"),
                    "persona_id": persona_id,
                    "query": query_text,
                    "relevant_docs": [],
                }
            )
            continue

        retrieved = _search_with_scores(index, query_text, k=args.top_k)
        results.append(
            {
                "query_id": query.get("query_id"),
                "persona_id": persona_id,
                "query": query_text,
                "relevant_docs": [
                    _doc_to_payload(item["doc"], rank=rank, score=item["score"])
                    for rank, item in enumerate(retrieved, start=1)
                ],
            }
        )

    payload = {"top_k": args.top_k, "results": results}
    args.output.parent.mkdir(parents=True, exist_ok=True)
    with args.output.open("w", encoding="utf-8") as handle:
        json.dump(payload, handle, indent=2)
    print(f"Retrieval results saved to {args.output}")
    return 0


def _run_evaluation(args: argparse.Namespace) -> int:
    if not args.labeled_results.exists():
        raise FileNotFoundError(f"Labeled results file not found: {args.labeled_results}")

    payload = json.loads(args.labeled_results.read_text(encoding="utf-8"))
    labeled_results = payload.get("results", payload)
    if not isinstance(labeled_results, list):
        raise ValueError("Labeled results must be a list of query result entries.")

    results = _compute_metrics(labeled_results, args.k_values)

    print("RAG Retrieval Evaluation")
    for key, value in results.items():
        if key.startswith(("precision_at_", "recall_at_")):
            print(f"{key}: {value:.2%}")
    print(f"Total Queries: {results['total_queries']}")
    if results["unlabeled_total"]:
        print(f"Unlabeled Docs: {results['unlabeled_total']}")

    args.output.parent.mkdir(parents=True, exist_ok=True)
    with args.output.open("w", encoding="utf-8") as handle:
        json.dump(results, handle, indent=2)
    print(f"Results saved to {args.output}")
    return 0


def main() -> int:
    args = parse_args()
    if args.command == "retrieve":
        return _run_retrieval(args)
    if args.command == "evaluate":
        return _run_evaluation(args)
    raise ValueError(f"Unknown command: {args.command}")


if __name__ == "__main__":
    raise SystemExit(main())