-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_optional_features.py
More file actions
executable file
·185 lines (160 loc) · 5.45 KB
/
Copy patheval_optional_features.py
File metadata and controls
executable file
·185 lines (160 loc) · 5.45 KB
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
#!/usr/bin/env python3
"""Evaluate optional features (Knowledge Graph, MMR) on MIRAGE.
Compares:
1. baseline - Current defaults (no KG, no MMR)
2. with_kg - Baseline + Knowledge Graph
3. with_mmr - Baseline + MMR
4. with_kg_mmr - Baseline + KG + MMR
Goal: Determine if Knowledge Graph and/or MMR should be enabled by default.
Dataset: MIRAGE subset (30 examples)
Expected time: 40-60 min
"""
import os
import sys
import time
from datetime import datetime
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root / "src"))
from dotenv import load_dotenv # noqa: E402
from rag_agent.evaluation import ( # noqa: E402
compare_results,
load_mirage_pool,
print_summary,
print_verdict,
run_single_config,
save_results,
)
# Load environment
env_test_path = project_root / ".env.test"
if env_test_path.exists():
load_dotenv(env_test_path, override=True)
else:
load_dotenv()
# Configuration definitions
CONFIGS = {
"baseline": {
"description": "Current defaults (no KG, no MMR)",
"enable_hyde": True,
"enable_query_decomposition": True,
"enable_hybrid_retrieval": True,
"enable_reranking": True,
"enable_knowledge_graph": False,
"enable_mmr": False,
},
"with_kg": {
"description": "Baseline + Knowledge Graph",
"enable_hyde": True,
"enable_query_decomposition": True,
"enable_hybrid_retrieval": True,
"enable_reranking": True,
"enable_knowledge_graph": True,
"enable_mmr": False,
},
"with_mmr": {
"description": "Baseline + MMR",
"enable_hyde": True,
"enable_query_decomposition": True,
"enable_hybrid_retrieval": True,
"enable_reranking": True,
"enable_knowledge_graph": False,
"enable_mmr": True,
"mmr_lambda": 0.5,
},
"with_kg_mmr": {
"description": "Baseline + KG + MMR",
"enable_hyde": True,
"enable_query_decomposition": True,
"enable_hybrid_retrieval": True,
"enable_reranking": True,
"enable_knowledge_graph": True,
"enable_mmr": True,
"mmr_lambda": 0.5,
},
"with_mmr_07": {
"description": "Baseline + MMR (lambda=0.7, more relevance)",
"enable_hyde": True,
"enable_query_decomposition": True,
"enable_hybrid_retrieval": True,
"enable_reranking": True,
"enable_knowledge_graph": False,
"enable_mmr": True,
"mmr_lambda": 0.7,
},
"with_mmr_08": {
"description": "Baseline + MMR (lambda=0.8, high relevance)",
"enable_hyde": True,
"enable_query_decomposition": True,
"enable_hybrid_retrieval": True,
"enable_reranking": True,
"enable_knowledge_graph": False,
"enable_mmr": True,
"mmr_lambda": 0.8,
},
}
def main():
print("=" * 70)
print("OPTIONAL FEATURES EVALUATION")
print("Comparing: baseline vs KG vs MMR vs KG+MMR")
print("=" * 70)
print(f"Timestamp: {datetime.now().isoformat()}")
# Configuration
provider = os.getenv("LLM_PROVIDER", "openai")
model_name = os.getenv("LLM_MODEL", "gpt-4o-mini")
num_examples = int(os.getenv("EVAL_EXAMPLES", "30"))
noise_docs = int(os.getenv("EVAL_NOISE_DOCS", "500"))
max_workers = int(os.getenv("EVAL_MAX_WORKERS", "4"))
use_async = os.getenv("EVAL_USE_ASYNC", "false").lower() == "true"
# Check which configs to run (can override with env var)
configs_to_run = os.getenv("EVAL_CONFIGS", "baseline,with_kg,with_mmr,with_kg_mmr").split(",")
configs_to_run = [c.strip() for c in configs_to_run if c.strip() in CONFIGS]
print(f"Provider: {provider}")
print(f"Model: {model_name}")
print(f"Examples: {num_examples}")
print(f"Noise docs: {noise_docs}")
print(f"Max workers: {max_workers}")
print(f"Use async: {use_async}")
print(f"Configurations: {', '.join(configs_to_run)}")
print("=" * 70)
# Load MIRAGE dataset and build pool
dataset, pool_docs = load_mirage_pool(num_examples, noise_docs)
results = {}
total_start = time.time()
# Run each configuration
for i, config_name in enumerate(configs_to_run):
print(f"\n{'=' * 70}")
print(f"CONFIGURATION {i + 1}/{len(configs_to_run)}: {config_name.upper()}")
print("=" * 70)
results[config_name] = run_single_config(
config_name,
CONFIGS[config_name],
pool_docs,
dataset,
provider,
model_name,
num_examples,
experiment_prefix="optional_features",
max_workers=max_workers,
use_async=use_async,
)
total_elapsed = time.time() - total_start
# Compare results
comparison = compare_results(results, baseline_key="baseline", test_name="optional_features")
comparison["total_elapsed_time"] = total_elapsed
# Save results
output_path = save_results(
comparison,
project_root / "evaluation_results",
prefix="optional_features_eval",
)
# Print summary
print("\n" + "=" * 70)
print("OPTIONAL FEATURES EVALUATION SUMMARY")
print("=" * 70)
print_summary(comparison, baseline_key="baseline", configs_to_show=configs_to_run)
print(f"\nResults saved to: {output_path}")
# Final verdict
print_verdict(comparison.get("summary", {}), test_name="OPTIONAL FEATURES EVALUATION")
if __name__ == "__main__":
main()