-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_evaluation_system.py
More file actions
630 lines (526 loc) · 24.9 KB
/
rag_evaluation_system.py
File metadata and controls
630 lines (526 loc) · 24.9 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
import openai
import subprocess
import tempfile
import os
import time
import shutil
import re
import requests
from bs4 import BeautifulSoup
import urllib.parse
import pickle
import numpy as np
from sentence_transformers import SentenceTransformer
import faiss
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime
import json
from typing import Dict, List, Tuple
import traceback
class RAGEvaluationSystem:
def __init__(self, api_key: str, api_base: str):
"""Initialize the evaluation system with API credentials"""
self.api_key = api_key
self.api_base = api_base
openai.api_key = api_key
openai.api_base = api_base
# Initialize embedder
self.embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
# Load or create contexts
self.tri_docs_chunks, self.tri_docs_embeddings = self.load_context("tri_docs")
self.numpy_stl_chunks, self.numpy_stl_embeddings = self.load_context("numpy_stl")
self.tri_custom_chunks, self.tri_custom_embeddings = self.load_context("tri_custom")
self.best_practices_chunks, self.best_practices_embeddings = self.load_context("best_practices")
# Define test shapes
self.test_shapes = {
'box': "Create a hollow box with base sides 20mm and 30mm, height 40mm",
'cylinder': "Create a hollow cylinder with radius 15mm, height 40mm",
'cone': "Create a hollow cone with base radius 20mm, height 35mm",
'pyramid': "Create a hollow pyramid with base 25mm x 25mm, height 30mm",
'sphere': "Create a sphere with radius 45mm"
}
# Evaluation conditions
self.conditions = {
'baseline': {'use_rag': False, 'use_review': False},
'rag_only': {'use_rag': True, 'use_review': False},
'review_only': {'use_rag': False, 'use_review': True},
'full_system': {'use_rag': True, 'use_review': True}
}
# Results storage
self.results = []
self.timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
self.results_dir = f"evaluation_results_{self.timestamp}"
os.makedirs(self.results_dir, exist_ok=True)
def load_context(self, context_type: str) -> Tuple[List[str], np.ndarray]:
"""Load existing context or return empty if not found"""
emb_file = f"{context_type}_embeddings.npy"
chunks_file = f"{context_type}_chunks.pkl"
if os.path.exists(emb_file) and os.path.exists(chunks_file):
embeddings = np.load(emb_file)
with open(chunks_file, "rb") as f:
chunks = pickle.load(f)
return chunks, embeddings
else:
print(f"Warning: Context files for {context_type} not found")
return [], np.array([])
def retrieve_context(self, query: str, k: int = 3) -> str:
"""Retrieve relevant context using the existing RAG system"""
if len(self.tri_docs_chunks) == 0:
return ""
# Retrieve from each source
tri_docs_context = self._retrieve_from_source(
query, self.tri_docs_chunks, self.tri_docs_embeddings, k
)
numpy_stl_context = self._retrieve_from_source(
query, self.numpy_stl_chunks, self.numpy_stl_embeddings, k
)
tri_custom_context = self._retrieve_from_source(
query, self.tri_custom_chunks, self.tri_custom_embeddings, k
)
best_practices_context = self._retrieve_from_source(
query, self.best_practices_chunks, self.best_practices_embeddings, k
)
# Combine contexts
combined_context = (
"Trimesh Documentation context:\n" + tri_docs_context +
"\n\nNumPy-STL Documentation context:\n" + numpy_stl_context +
"\n\nCustom Trimesh Functions context:\n" + tri_custom_context +
"\n\n3D Printing Best Practices context:\n" + best_practices_context
)
return combined_context
def _retrieve_from_source(self, query: str, chunks: List[str], embeddings: np.ndarray, k: int) -> str:
"""Retrieve from a specific source"""
if len(chunks) == 0:
return ""
index = faiss.IndexFlatL2(embeddings.shape[1])
index.add(embeddings)
query_emb = self.embedder.encode([query])
query_emb = np.array(query_emb, dtype=np.float32)
distances, indices = index.search(query_emb, k)
retrieved = [chunks[idx] for idx in indices[0]]
return "\n\n".join(retrieved)
def generate_code(self, prompt: str, extra_context: str = "") -> str:
"""Generate code using the existing function"""
messages = [
{"role": "system", "content": (
"You are a helpful assistant that writes correct Python code. "
"Your task is to generate code that uses the 'trimesh' and 'numpy-stl' libraries to create hollow 3D shapes "
"and export it as an STL file named 'output.stl'. Ensure the mesh is suitable for FDM printing. "
"All explanations and commentary should be placed before or after the code block, not within it. "
"Always wrap your code in markdown code blocks with ```python at the start and ``` at the end. "
"The code block should only contain valid, executable Python code with no comments that are not meant to be executed. "
"Always include necessary imports like 'import trimesh', 'import numpy as np', and 'from stl import mesh' within the code block. "
"Do not generate harmful code."
)},
{"role": "user", "content": prompt + "\n" + extra_context}
]
response = openai.ChatCompletion.create(
model="deepseek-chat",
messages=messages,
stream=False
)
return response.choices[0].message.content.strip()
def extract_code_from_markdown(self, text: str) -> str:
"""Extract Python code from markdown blocks"""
pattern = r'```(?:python)?\s*([\s\S]*?)```'
matches = re.findall(pattern, text)
if not matches:
return ""
return "\n\n".join(match.strip() for match in matches)
def test_code(self, code: str) -> Tuple[bool, str, str]:
"""Test code execution and return success status, stdout, and stderr"""
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".py", encoding="utf-8") as temp_file:
temp_file.write(code)
temp_file_path = temp_file.name
try:
env = os.environ.copy()
env["PYTHONUTF8"] = "1"
result = subprocess.run(
["python", temp_file_path],
capture_output=True,
text=True,
timeout=20,
env=env
)
stdout = result.stdout.strip()
stderr = result.stderr.strip()
# Check if output.stl was created
success = not stderr and os.path.exists("output.stl")
return success, stdout, stderr
except subprocess.TimeoutExpired:
return False, "", "Execution timed out"
except Exception as e:
return False, "", str(e)
finally:
if os.path.exists(temp_file_path):
os.remove(temp_file_path)
def review_code(self, code: str, context: str) -> str:
"""Review and refine code using the existing review function"""
review_prompt = (
"Please review the following Python code based on the provided documentation and best practices for 3D printing. "
"If improvements are needed, return an updated version of the code; otherwise, return the code as is.\n\n"
"Wrap your final code inside ```python and ``` markdown code blocks. "
"The code block should only contain valid, executable Python code. "
"All explanations should be outside the code block.\n\n"
"Code:\n" + code + "\n\nContext:\n" + context
)
messages = [
{"role": "system", "content": (
"You are a helpful assistant that reviews and refines Python code for creating 3D printing models using 'trimesh' and/or 'numpy-stl'. "
"Ensure the mesh is suitable for FDM printing with emphasis on minimum wall thickness and supports where needed. "
"Take note of contact points between the shape and its support(s). "
"Your review should consider best practices and documentation guidelines, and if the code can be improved, return an updated version. "
"Always wrap your code in markdown code blocks with ```python at the start and ``` at the end. "
"The code block should only contain valid, executable Python code with no comments that are not meant to be executed. "
"Always include necessary imports like 'import trimesh', 'import numpy as np', or 'from stl import mesh' within the code block. "
"Do not generate harmful code."
)},
{"role": "user", "content": review_prompt}
]
response = openai.ChatCompletion.create(
model="deepseek-chat",
messages=messages,
stream=False
)
return response.choices[0].message.content.strip()
def run_single_test(self, shape: str, condition: str, run_number: int) -> Dict:
"""Run a single test with specified parameters"""
start_time = time.time()
iterations = 0
success = False
errors = []
context_used = ""
# Get shape prompt
prompt = self.test_shapes[shape]
# Get condition settings
settings = self.conditions[condition]
use_rag = settings['use_rag']
use_review = settings['use_review']
# Retrieve context if needed
if use_rag:
context_used = self.retrieve_context(prompt)
# Iterative generation process
extra_context = context_used
final_code = None
while iterations < 5 and not success:
iterations += 1
# Generate code
generated_response = self.generate_code(prompt, extra_context)
code = self.extract_code_from_markdown(generated_response)
if not code:
errors.append(f"Iteration {iterations}: No valid code block found")
continue
# Test code
success, stdout, stderr = self.test_code(code)
if success:
final_code = code
# Review if needed
if use_review:
reviewed_response = self.review_code(code, context_used)
reviewed_code = self.extract_code_from_markdown(reviewed_response)
if reviewed_code:
review_success, review_stdout, review_stderr = self.test_code(reviewed_code)
if review_success:
final_code = reviewed_code
else:
errors.append(f"Review failed: {review_stderr}")
break
else:
errors.append(f"Iteration {iterations}: {stderr}")
extra_context = context_used + f"\n\nError in iteration {iterations}: {stderr}"
# Clean up and save results
end_time = time.time()
execution_time = end_time - start_time
# Save generated STL if successful
stl_filename = None
if success and os.path.exists("output.stl"):
stl_filename = f"{shape}_{condition}_run{run_number}.stl"
stl_path = os.path.join(self.results_dir, stl_filename)
shutil.move("output.stl", stl_path)
# Save final code
code_filename = f"{shape}_{condition}_run{run_number}.py"
code_path = os.path.join(self.results_dir, code_filename)
with open(code_path, "w", encoding="utf-8") as f:
f.write(final_code if final_code else "# No successful code generated")
return {
'shape': shape,
'condition': condition,
'run_number': run_number,
'success': success,
'iterations': iterations,
'execution_time': execution_time,
'errors': errors,
'stl_file': stl_filename,
'code_file': code_filename,
'timestamp': datetime.now().isoformat()
}
def run_primary_evaluation(self, runs_per_shape: int = 10):
"""Run primary evaluation focusing on DeepSeek with all conditions"""
print("Starting primary evaluation...")
total_tests = len(self.test_shapes) * len(self.conditions) * runs_per_shape
current_test = 0
for shape in self.test_shapes:
for condition in self.conditions:
for run in range(runs_per_shape):
current_test += 1
print(f"\nTest {current_test}/{total_tests}")
print(f"Shape: {shape}, Condition: {condition}, Run: {run + 1}")
try:
result = self.run_single_test(shape, condition, run + 1)
self.results.append(result)
# Save after each test
self.save_results()
except Exception as e:
print(f"Error in test: {str(e)}")
traceback.print_exc()
# Clean up any leftover files
if os.path.exists("output.stl"):
os.remove("output.stl")
def save_results(self):
"""Save current results to files"""
# Save raw results as JSON
json_path = os.path.join(self.results_dir, "raw_results.json")
with open(json_path, "w", encoding="utf-8") as f:
json.dump(self.results, f, indent=2)
# Save as CSV for analysis
df = pd.DataFrame(self.results)
csv_path = os.path.join(self.results_dir, "results.csv")
df.to_csv(csv_path, index=False)
def analyze_results(self):
"""Analyze results and create visualizations"""
df = pd.DataFrame(self.results)
# 1. Success rate by condition
success_by_condition = df.groupby('condition')['success'].mean() * 100
plt.figure(figsize=(10, 6))
success_by_condition.plot(kind='bar')
plt.title('Success Rate by Condition')
plt.ylabel('Success Rate (%)')
plt.xlabel('Condition')
plt.tight_layout()
plt.savefig(os.path.join(self.results_dir, 'success_by_condition.png'))
plt.close()
# 2. Average iterations by condition
avg_iterations = df[df['success'] == True].groupby('condition')['iterations'].mean()
plt.figure(figsize=(10, 6))
avg_iterations.plot(kind='bar')
plt.title('Average Iterations to Success')
plt.ylabel('Iterations')
plt.xlabel('Condition')
plt.tight_layout()
plt.savefig(os.path.join(self.results_dir, 'iterations_by_condition.png'))
plt.close()
# 3. Success rate by shape
success_by_shape = df.groupby(['shape', 'condition'])['success'].mean() * 100
plt.figure(figsize=(12, 8))
success_by_shape.unstack().plot(kind='bar')
plt.title('Success Rate by Shape and Condition')
plt.ylabel('Success Rate (%)')
plt.xlabel('Shape')
plt.legend(title='Condition')
plt.tight_layout()
plt.savefig(os.path.join(self.results_dir, 'success_by_shape.png'))
plt.close()
# 4. NEW: Execution time by condition
execution_time_by_condition = df.groupby('condition')['execution_time'].mean()
plt.figure(figsize=(10, 6))
execution_time_by_condition.plot(kind='bar')
plt.title('Average Execution Time by Condition')
plt.ylabel('Time (seconds)')
plt.xlabel('Condition')
plt.tight_layout()
plt.savefig(os.path.join(self.results_dir, 'execution_time_by_condition.png'))
plt.close()
# 5. NEW: Execution time boxplot
plt.figure(figsize=(12, 6))
sns.boxplot(x='condition', y='execution_time', data=df)
plt.title('Execution Time Distribution by Condition')
plt.ylabel('Time (seconds)')
plt.xlabel('Condition')
plt.tight_layout()
plt.savefig(os.path.join(self.results_dir, 'execution_time_boxplot.png'))
plt.close()
# 6. NEW: Combined metrics comparison
# Normalize metrics for comparison
success_norm = df.groupby('condition')['success'].mean() / df.groupby('condition')['success'].mean().max()
time_norm = df.groupby('condition')['execution_time'].mean() / df.groupby('condition')[
'execution_time'].mean().max()
iterations_norm = df[df['success'] == True].groupby('condition')['iterations'].mean() / \
df[df['success'] == True].groupby('condition')['iterations'].mean().max()
# Create combined metrics dataframe
combined_metrics = pd.DataFrame({
'Success Rate': success_norm,
'Execution Time (lower is better)': 1 - time_norm, # Invert so lower is better
'Iterations (lower is better)': 1 - iterations_norm # Invert so lower is better
})
plt.figure(figsize=(10, 6))
combined_metrics.plot(kind='bar')
plt.title('Normalized Performance Metrics by Condition')
plt.ylabel('Performance (higher is better)')
plt.xlabel('Condition')
plt.ylim(0, 1.1)
plt.tight_layout()
plt.savefig(os.path.join(self.results_dir, 'combined_metrics.png'))
plt.close()
# 4. RAG impact analysis
rag_conditions = df[df['condition'].isin(['rag_only', 'full_system'])]
no_rag_conditions = df[df['condition'].isin(['baseline', 'review_only'])]
rag_success = rag_conditions['success'].mean() * 100
no_rag_success = no_rag_conditions['success'].mean() * 100
rag_improvement = rag_success - no_rag_success
# Time comparison
rag_time = rag_conditions['execution_time'].mean()
no_rag_time = no_rag_conditions['execution_time'].mean()
time_difference = rag_time - no_rag_time
# 5. Summary statistics
summary_stats = {
'Overall Success Rate': f"{df['success'].mean() * 100:.1f}%",
'RAG Improvement': f"{rag_improvement:.1f}%",
'Best Condition': success_by_condition.idxmax(),
'Best Condition Success Rate': f"{success_by_condition.max():.1f}%",
'Average Iterations (Success)': f"{df[df['success'] == True]['iterations'].mean():.2f}",
'Hardest Shape': df.groupby('shape')['success'].mean().idxmin(),
'Easiest Shape': df.groupby('shape')['success'].mean().idxmax(),
'Average Execution Time': f"{df['execution_time'].mean():.2f} seconds",
'Time Difference (RAG vs No RAG)': f"{time_difference:.2f} seconds ({(time_difference / no_rag_time) * 100:.1f}%)"
}
# Save summary
with open(os.path.join(self.results_dir, 'summary.txt'), 'w') as f:
f.write("EVALUATION SUMMARY\n")
f.write("==================\n\n")
for key, value in summary_stats.items():
f.write(f"{key}: {value}\n")
return summary_stats
def create_dashboard(self):
"""Create an HTML dashboard for results"""
df = pd.DataFrame(self.results)
html = f"""
<html>
<head>
<title>RAG Evaluation Results - {self.timestamp}</title>
<style>
body {{ font-family: Arial, sans-serif; margin: 20px; }}
.container {{ max-width: 1200px; margin: 0 auto; }}
table {{ border-collapse: collapse; width: 100%; margin-bottom: 20px; }}
th, td {{ border: 1px solid #ddd; padding: 8px; text-align: center; }}
th {{ background-color: #f2f2f2; }}
.success {{ background-color: #d4edda; }}
.failure {{ background-color: #f8d7da; }}
img {{ max-width: 100%; height: auto; margin: 20px 0; }}
.summary {{ background-color: #f8f9fa; padding: 20px; border-radius: 5px; }}
.chart-container {{ display: flex; flex-wrap: wrap; justify-content: space-between; }}
.chart {{ width: 48%; margin-bottom: 20px; }}
@media (max-width: 768px) {{ .chart {{ width: 100%; }} }}
</style>
</head>
<body>
<div class="container">
<h1>RAG Evaluation Results</h1>
<p>Evaluation completed on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
<div class="summary">
<h2>Summary Statistics</h2>
<ul>
"""
# Add summary statistics
summary_stats = self.analyze_results()
for key, value in summary_stats.items():
html += f"<li><strong>{key}:</strong> {value}</li>"
html += """
</ul>
</div>
<h2>Success Rate and Performance Charts</h2>
<div class="chart-container">
<div class="chart">
<h3>Success Rate by Condition</h3>
<img src="success_by_condition.png" alt="Success Rate by Condition">
</div>
<div class="chart">
<h3>Average Iterations to Success</h3>
<img src="iterations_by_condition.png" alt="Average Iterations">
</div>
</div>
<div class="chart-container">
<div class="chart">
<h3>Success Rate by Shape</h3>
<img src="success_by_shape.png" alt="Success by Shape">
</div>
<div class="chart">
<h3>Combined Performance Metrics</h3>
<img src="combined_metrics.png" alt="Combined Metrics">
</div>
</div>
<h2>Execution Time Analysis</h2>
<div class="chart-container">
<div class="chart">
<h3>Average Execution Time by Condition</h3>
<img src="execution_time_by_condition.png" alt="Execution Time">
</div>
<div class="chart">
<h3>Execution Time Distribution</h3>
<img src="execution_time_boxplot.png" alt="Time Distribution">
</div>
</div>
<h2>Detailed Results</h2>
<table>
<tr>
<th>Shape</th>
<th>Condition</th>
<th>Success Rate</th>
<th>Avg Iterations</th>
<th>Avg Time (s)</th>
</tr>
"""
# Add detailed results table
grouped_results = df.groupby(['shape', 'condition']).agg({
'success': 'mean',
'iterations': 'mean',
'execution_time': 'mean'
}).reset_index()
for _, row in grouped_results.iterrows():
success_class = 'success' if row['success'] > 0.7 else 'failure' if row['success'] < 0.3 else ''
html += f"""
<tr>
<td>{row['shape']}</td>
<td>{row['condition']}</td>
<td class="{success_class}">{row['success'] * 100:.1f}%</td>
<td>{row['iterations']:.1f}</td>
<td>{row['execution_time']:.1f}</td>
</tr>
"""
html += """
</table>
</div>
</body>
</html>
"""
# Save dashboard
dashboard_path = os.path.join(self.results_dir, 'dashboard.html')
with open(dashboard_path, 'w', encoding='utf-8') as f:
f.write(html)
print(f"Dashboard created at: {dashboard_path}")
def main():
"""Main execution function"""
# Initialize the evaluation system
api_key = "sk-XXXXXXXXXXXXXXX" # Replace with your actual API key
api_base = "https://api.deepseek.com/v1"
evaluator = RAGEvaluationSystem(api_key, api_base)
# Run primary evaluation
print("Starting RAG evaluation...")
evaluator.run_primary_evaluation(runs_per_shape=10)
# Analyze results
print("\nAnalyzing results...")
summary = evaluator.analyze_results()
# Create dashboard
print("\nCreating dashboard...")
evaluator.create_dashboard()
# Print summary
print("\n" + "=" * 50)
print("EVALUATION SUMMARY")
print("=" * 50)
for key, value in summary.items():
print(f"{key}: {value}")
print(f"\nResults saved to: {evaluator.results_dir}")
print(f"Dashboard available at: {os.path.join(evaluator.results_dir, 'dashboard.html')}")
if __name__ == "__main__":
main()