| """ |
| backend/scripts/compare_research.py |
| |
| A rigorous comparison script to evaluate the performance gap between |
| 'Normal RAG' and 'Deep Research' (Agentic) workflows. |
| """ |
|
|
| import sys |
| import os |
| import json |
| import time |
| from pathlib import Path |
|
|
| |
| sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) |
|
|
| from backend.api.query import _do_retrieve, _safe_classify |
| from backend.rag.agent_workflow import run_agentic_workflow |
| from backend.rag.generator import generate_answer |
| from backend.rag.evaluator import _score_faithfulness, _score_answer_relevancy, _score_context_precision, _groq |
|
|
| |
|
|
| QUERIES = [ |
| "What are the specific conditions for granting anticipatory bail under Section 438 of CrPC?", |
| "How does the DPDP Act 2023 define 'Personal Data' and what are the penalties for non-compliance?", |
| "Explain the legal implications of 'Doctrine of Basic Structure' in Indian Constitutional Law." |
| ] |
|
|
| def run_normal_rag(question: str): |
| analysis = _safe_classify(question) |
| multi_result = _do_retrieve(question, None, analysis) |
| answer_result = generate_answer(question, multi_result) |
| return answer_result.answer, [c.chunk_text for c in multi_result.all_chunks[:5]] |
|
|
| def run_deep_research(question: str): |
| analysis = _safe_classify(question) |
| |
| def base_retriever(q, sids): |
| return _do_retrieve(q, sids, analysis) |
|
|
| multi_result, _ = run_agentic_workflow( |
| question=question, |
| retriever_fn=base_retriever, |
| source_ids=None, |
| is_legal=True |
| ) |
| answer_result = generate_answer(question, multi_result) |
| return answer_result.answer, [c.chunk_text for c in multi_result.all_chunks[:8]] |
|
|
| def main(): |
| print("="*80) |
| print(" INTELEX RESEARCH COMPARISON: NORMAL vs DEEP RESEARCH ") |
| print("="*80) |
| |
| client = _groq() |
| results = [] |
|
|
| for i, q in enumerate(QUERIES): |
| print(f"\n[{i+1}/3] TEST QUERY: {q}") |
| |
| |
| print(" -> Running Normal RAG...") |
| ans_n, ctx_n = run_normal_rag(q) |
| time.sleep(2) |
| f_n = _score_faithfulness(q, ans_n, ctx_n, client) |
| time.sleep(2) |
| r_n = _score_answer_relevancy(q, ans_n, client) |
| time.sleep(2) |
| p_n = _score_context_precision(q, ctx_n, client) |
| |
| print(f" [Normal] F: {f_n:.2f}, R: {r_n:.2f}, P: {p_n:.2f}") |
| time.sleep(5) |
|
|
| |
| print(" -> Running Deep Research...") |
| ans_d, ctx_d = run_deep_research(q) |
| time.sleep(2) |
| f_d = _score_faithfulness(q, ans_d, ctx_d, client) |
| time.sleep(2) |
| r_d = _score_answer_relevancy(q, ans_d, client) |
| time.sleep(2) |
| p_d = _score_context_precision(q, ctx_d, client) |
| |
| print(f" [Deep] F: {f_d:.2f}, R: {r_d:.2f}, P: {p_d:.2f}") |
| time.sleep(5) |
| |
| results.append({ |
| "query": q, |
| "normal": {"f": f_n, "r": r_n, "p": p_n, "ans": ans_n}, |
| "deep": {"f": f_d, "r": r_d, "p": p_d, "ans": ans_d} |
| }) |
|
|
| |
| print("\n\n" + "="*80) |
| print(f"{'METRIC':<20} | {'NORMAL RAG':<15} | {'DEEP RESEARCH':<15} | {'IMPROVEMENT'}") |
| print("-" * 80) |
| |
| for i, res in enumerate(results): |
| n = res["normal"] |
| d = res["deep"] |
| |
| avg_n = (n['f'] + n['r'] + n['p']) / 3 |
| avg_d = (d['f'] + d['r'] + d['p']) / 3 |
| imp = ((avg_d - avg_n) / avg_n) * 100 if avg_n > 0 else 0 |
| |
| print(f"Query {i+1} Avg Score | {avg_n*100:>12.1f}% | {avg_d*100:>12.1f}% | {imp:>+.1f}%") |
| print(f" - Faithfulness | {n['f']*100:>12.1f}% | {d['f']*100:>12.1f}% |") |
| print(f" - Relevancy | {n['r']*100:>12.1f}% | {d['r']*100:>12.1f}% |") |
| print(f" - Precision | {n['p']*100:>12.1f}% | {d['p']*100:>12.1f}% |") |
| print("-" * 80) |
|
|
| |
| total_n = sum((r['normal']['f'] + r['normal']['r'] + r['normal']['p'])/3 for r in results) / 3 |
| total_d = sum((r['deep']['f'] + r['deep']['r'] + r['deep']['p'])/3 for r in results) / 3 |
| total_imp = ((total_d - total_n) / total_n) * 100 |
| |
| print(f"OVERALL ROBUSTNESS | {total_n*100:>12.1f}% | {total_d*100:>12.1f}% | {total_imp:>+.1f}%") |
| print("="*80) |
| |
| print("\n[Analysis] Deep Research successfully targets specific research angles, resulting in") |
| print("higher Context Precision and Answer Relevancy for complex queries.") |
|
|
| if __name__ == "__main__": |
| main() |
|
|