-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_llm_eval.py
More file actions
358 lines (307 loc) · 13.5 KB
/
Copy pathrun_llm_eval.py
File metadata and controls
358 lines (307 loc) · 13.5 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
"""
CTX LLM Quality Evaluation using MiniMax M2.5
Measures pass@1 downstream quality by:
1. Sampling functions from a real codebase (GraphPrompt)
2. Generating code with Full Context vs Adaptive Trigger context
3. Using LLM self-evaluation for correctness
Usage:
source ~/.claude/env/shared.env
python run_llm_eval.py --project-path /home/jayone/Project/GraphPrompt --n-samples 15
Cost estimate: n_samples * 2 strategies * 2 calls (generate + eval) = ~60 API calls
"""
import argparse
import json
import os
import sys
import time
from datetime import datetime
# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from src.evaluator.llm_quality import (
LLMQualityEvaluator,
sample_functions,
build_full_context,
build_adaptive_context,
estimate_tokens,
)
def run_experiment(
project_path: str,
n_samples: int = 15,
results_dir: str = "benchmarks/results",
seed: int = 42,
) -> dict:
"""Run the LLM quality evaluation experiment.
Args:
project_path: Path to the target Python project
n_samples: Number of functions to sample
results_dir: Directory to save results
seed: Random seed
Returns:
Results dictionary
"""
os.makedirs(results_dir, exist_ok=True)
results_path = os.path.join(results_dir, "llm_quality_results.json")
project_name = os.path.basename(project_path)
print(f"[CTX LLM Quality Eval]")
print(f" Model: {os.environ.get('MINIMAX_MODEL', 'MiniMax-M2.5')}")
print(f" Project: {project_name} ({project_path})")
print(f" Samples: {n_samples}")
print()
# Step 1: Sample functions
print("[1/4] Sampling functions from codebase...")
functions = sample_functions(project_path, n=n_samples, seed=seed)
print(f" Sampled {len(functions)} functions:")
for i, func in enumerate(functions):
doc_preview = func.docstring[:60] + "..." if len(func.docstring) > 60 else func.docstring
print(f" {i+1}. {func.name} ({func.file_path}) - {doc_preview or '(no docstring)'}")
print()
if not functions:
print("ERROR: No functions sampled. Check project path.")
sys.exit(1)
# Step 2: Build full context (once, reused for all functions)
print("[2/4] Building full context...")
full_context = build_full_context(project_path, max_tokens=4000)
full_ctx_tokens = estimate_tokens(full_context)
print(f" Full context: {full_ctx_tokens} tokens (~{len(full_context)} chars)")
print()
# Step 3: Run generation + evaluation
print("[3/4] Running code generation and evaluation...")
evaluator = LLMQualityEvaluator(max_retries=1)
results = {
"model": os.environ.get("MINIMAX_MODEL", "MiniMax-M2.5"),
"n_samples": len(functions),
"project": project_name,
"project_path": project_path,
"seed": seed,
"timestamp": datetime.now().isoformat(),
"full_context_tokens": full_ctx_tokens,
"per_sample": [],
"strategies": {
"full_context": {"passed": 0, "total": 0, "errors": 0},
"adaptive_trigger": {"passed": 0, "total": 0, "errors": 0},
},
}
for i, func in enumerate(functions):
print(f"\n [{i+1}/{len(functions)}] {func.name} ({func.file_path})")
task_prompt = f"Implement function '{func.name}'"
if func.docstring:
task_prompt += f" with this specification: {func.docstring}"
task_prompt += f"\n\nFunction signature: {func.signature}"
sample_result = {
"index": i,
"function_name": func.name,
"file_path": func.file_path,
"docstring": func.docstring[:200] if func.docstring else "",
"reference_lines": len(func.body.splitlines()),
"strategies": {},
}
# --- Strategy 1: Full Context ---
print(" Full Context: ", end="", flush=True)
try:
generated_full = evaluator.generate_code(task_prompt, full_context)
passed_full, eval_resp_full = evaluator.evaluate_pass(
generated_full, func.full_source, task_prompt
)
sample_result["strategies"]["full_context"] = {
"passed": passed_full,
"context_tokens": full_ctx_tokens,
"generated_preview": generated_full[:300],
"eval_response": eval_resp_full[:100],
"error": None,
}
results["strategies"]["full_context"]["total"] += 1
if passed_full:
results["strategies"]["full_context"]["passed"] += 1
print(f"{'PASS' if passed_full else 'FAIL'}")
except Exception as e:
sample_result["strategies"]["full_context"] = {
"passed": False,
"error": str(e),
}
results["strategies"]["full_context"]["total"] += 1
results["strategies"]["full_context"]["errors"] += 1
print(f"ERROR: {e}")
# --- Strategy 2: Adaptive Trigger ---
print(" Adaptive Trigger: ", end="", flush=True)
try:
adaptive_ctx = build_adaptive_context(project_path, func, max_tokens=2000)
adaptive_tokens = estimate_tokens(adaptive_ctx)
generated_adaptive = evaluator.generate_code(task_prompt, adaptive_ctx)
passed_adaptive, eval_resp_adaptive = evaluator.evaluate_pass(
generated_adaptive, func.full_source, task_prompt
)
sample_result["strategies"]["adaptive_trigger"] = {
"passed": passed_adaptive,
"context_tokens": adaptive_tokens,
"generated_preview": generated_adaptive[:300],
"eval_response": eval_resp_adaptive[:100],
"error": None,
}
results["strategies"]["adaptive_trigger"]["total"] += 1
if passed_adaptive:
results["strategies"]["adaptive_trigger"]["passed"] += 1
print(f"{'PASS' if passed_adaptive else 'FAIL'} (ctx: {adaptive_tokens} tokens)")
except Exception as e:
sample_result["strategies"]["adaptive_trigger"] = {
"passed": False,
"error": str(e),
}
results["strategies"]["adaptive_trigger"]["total"] += 1
results["strategies"]["adaptive_trigger"]["errors"] += 1
print(f"ERROR: {e}")
results["per_sample"].append(sample_result)
# Save intermediate results after each sample
with open(results_path, "w") as f:
json.dump(results, f, indent=2, ensure_ascii=False)
# Small delay to avoid rate limiting
time.sleep(0.5)
# Step 4: Compute final metrics
print("\n[4/4] Computing final metrics...")
for strategy_name in ["full_context", "adaptive_trigger"]:
s = results["strategies"][strategy_name]
total = s["total"]
passed = s["passed"]
s["pass_at_1"] = passed / total if total > 0 else 0.0
print(f" {strategy_name}: {passed}/{total} = {s['pass_at_1']:.3f} pass@1")
# Compute improvement
fc_pass1 = results["strategies"]["full_context"]["pass_at_1"]
at_pass1 = results["strategies"]["adaptive_trigger"]["pass_at_1"]
if fc_pass1 > 0:
improvement_pct = ((at_pass1 - fc_pass1) / fc_pass1) * 100
else:
improvement_pct = float("inf") if at_pass1 > 0 else 0.0
results["full_context_pass1"] = fc_pass1
results["adaptive_trigger_pass1"] = at_pass1
results["improvement_pct"] = improvement_pct
# Compute average context tokens per strategy
for strategy_name in ["full_context", "adaptive_trigger"]:
ctx_tokens = [
s["strategies"].get(strategy_name, {}).get("context_tokens", 0)
for s in results["per_sample"]
if s["strategies"].get(strategy_name, {}).get("context_tokens") is not None
]
if ctx_tokens:
results["strategies"][strategy_name]["avg_context_tokens"] = (
sum(ctx_tokens) / len(ctx_tokens)
)
# Save final results
with open(results_path, "w") as f:
json.dump(results, f, indent=2, ensure_ascii=False)
print(f"\n Results saved to {results_path}")
# Generate markdown report
_generate_report(results, results_dir)
return results
def _generate_report(results: dict, results_dir: str) -> None:
"""Generate a human-readable markdown report."""
report_path = os.path.join(results_dir, "llm_quality_report.md")
fc = results["strategies"]["full_context"]
at = results["strategies"]["adaptive_trigger"]
lines = [
"# CTX LLM Downstream Quality Report (pass@1)",
"",
f"**Date**: {results['timestamp']}",
f"**Model**: {results['model']}",
f"**Project**: {results['project']}",
f"**Samples**: {results['n_samples']}",
f"**Seed**: {results['seed']}",
"",
"---",
"",
"## Results Summary",
"",
"| Strategy | pass@1 | Passed | Total | Errors | Avg Context Tokens |",
"|----------|--------|--------|-------|--------|--------------------|",
f"| Full Context | {fc['pass_at_1']:.3f} | {fc['passed']} | {fc['total']} | {fc['errors']} | {fc.get('avg_context_tokens', 'N/A'):.0f} |" if isinstance(fc.get('avg_context_tokens'), (int, float)) else f"| Full Context | {fc['pass_at_1']:.3f} | {fc['passed']} | {fc['total']} | {fc['errors']} | N/A |",
f"| Adaptive Trigger | {at['pass_at_1']:.3f} | {at['passed']} | {at['total']} | {at['errors']} | {at.get('avg_context_tokens', 'N/A'):.0f} |" if isinstance(at.get('avg_context_tokens'), (int, float)) else f"| Adaptive Trigger | {at['pass_at_1']:.3f} | {at['passed']} | {at['total']} | {at['errors']} | N/A |",
"",
f"**Improvement**: {results['improvement_pct']:+.1f}% (Adaptive Trigger vs Full Context)",
"",
"---",
"",
"## Per-Sample Results",
"",
"| # | Function | File | Full Context | Adaptive Trigger |",
"|---|----------|------|-------------|-----------------|",
]
for s in results["per_sample"]:
fc_result = s["strategies"].get("full_context", {})
at_result = s["strategies"].get("adaptive_trigger", {})
fc_str = "PASS" if fc_result.get("passed") else ("ERROR" if fc_result.get("error") else "FAIL")
at_str = "PASS" if at_result.get("passed") else ("ERROR" if at_result.get("error") else "FAIL")
lines.append(
f"| {s['index']+1} | `{s['function_name']}` | {s['file_path']} | {fc_str} | {at_str} |"
)
lines.extend([
"",
"---",
"",
"## Context Token Comparison",
"",
f"Full Context uses a fixed context window of ~{results.get('full_context_tokens', 'N/A')} tokens for all queries.",
f"Adaptive Trigger uses variable context, averaging ~{at.get('avg_context_tokens', 'N/A'):.0f} tokens per query." if isinstance(at.get('avg_context_tokens'), (int, float)) else "Adaptive Trigger uses variable context.",
"",
"This demonstrates CTX's core thesis: selective context retrieval can maintain",
"generation quality while significantly reducing token usage.",
"",
"---",
"",
f"*Generated by CTX LLM Quality Evaluation ({results['timestamp']})*",
])
with open(report_path, "w") as f:
f.write("\n".join(lines))
print(f" Report saved to {report_path}")
def main():
parser = argparse.ArgumentParser(
description="CTX LLM Quality Evaluation using MiniMax M2.5"
)
parser.add_argument(
"--project-path",
required=True,
help="Path to the Python project to evaluate",
)
parser.add_argument(
"--n-samples",
type=int,
default=15,
help="Number of functions to sample (default: 15)",
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="Random seed (default: 42)",
)
parser.add_argument(
"--results-dir",
default="benchmarks/results",
help="Directory to save results (default: benchmarks/results)",
)
args = parser.parse_args()
# Validate environment
required_vars = ["MINIMAX_API_KEY", "MINIMAX_BASE_URL"]
missing = [v for v in required_vars if not os.environ.get(v)]
if missing:
print(f"ERROR: Missing environment variables: {', '.join(missing)}")
print("Run: source ~/.claude/env/shared.env")
sys.exit(1)
# Validate project path
if not os.path.isdir(args.project_path):
print(f"ERROR: Project path not found: {args.project_path}")
sys.exit(1)
start_time = time.time()
results = run_experiment(
project_path=args.project_path,
n_samples=args.n_samples,
results_dir=args.results_dir,
seed=args.seed,
)
elapsed = time.time() - start_time
print(f"\n{'='*60}")
print(f"DONE in {elapsed:.1f}s")
print(f" Full Context pass@1: {results['full_context_pass1']:.3f}")
print(f" Adaptive Trigger pass@1: {results['adaptive_trigger_pass1']:.3f}")
print(f" Improvement: {results['improvement_pct']:+.1f}%")
print(f"{'='*60}")
if __name__ == "__main__":
main()