-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrutal_feedback_tests.py
More file actions
257 lines (216 loc) · 7.38 KB
/
Copy pathbrutal_feedback_tests.py
File metadata and controls
257 lines (216 loc) · 7.38 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
"""
Comprehensive test suite for brutal feedback optimization.
Loads pre-trained model from brutal_feedback_llm_judge.json.
Focus on demonstrating DSPy's improvement with extensive test cases.
"""
import dspy
import os
from brutal_feedback import (
BrutalFeedbackModule,
simple_metric
)
# Use single model
model = dspy.LM(
model='ollama/phi3:mini',
api_base='http://localhost:11434',
api_key='',
temperature=0.7
)
dspy.configure(lm=model)
# Extensive test cases
COMPREHENSIVE_TESTS = [
# Code quality
{
"content": "if x == True:\n return True\nelse:\n return False",
"context": "What's wrong here?",
"category": "code_smell"
},
{
"content": "def process(data):\n # TODO: implement\n pass",
"context": "Review this function",
"category": "incomplete"
},
{
"content": "import *\nfrom module import *",
"context": "Imports review",
"category": "bad_practice"
},
{
"content": "x = 1\ny = 2\nz = 3\na = 4\nb = 5",
"context": "Variable naming",
"category": "naming"
},
# Architecture decisions
{
"content": "Let's use MongoDB because it's web scale",
"context": "Database choice",
"category": "architecture"
},
{
"content": "We'll handle 1M users by adding more servers",
"context": "Scalability plan",
"category": "scaling"
},
{
"content": "Let's rewrite in Rust for performance",
"context": "Should we?",
"category": "rewrite"
},
{
"content": "We need Kubernetes for our 3-person startup",
"context": "Infrastructure decision",
"category": "over_engineering"
},
# Business/product ideas
{
"content": "It's like Uber but for dog walking",
"context": "Startup pitch",
"category": "idea"
},
{
"content": "We'll monetize later once we have users",
"context": "Business model",
"category": "business"
},
{
"content": "AI will solve all our problems",
"context": "Product strategy",
"category": "buzzword"
},
# Security
{
"content": "password = request.GET['pwd']",
"context": "Authentication code",
"category": "security"
},
{
"content": "We'll add security later",
"context": "MVP planning",
"category": "security_planning"
},
# Testing
{
"content": "# Tests are for people who don't know how to code",
"context": "Testing philosophy",
"category": "testing"
},
{
"content": "time.sleep(5) # wait for db",
"context": "Test code",
"category": "flaky_tests"
},
# Documentation
{
"content": "# This function does stuff",
"context": "Comment review",
"category": "documentation"
},
{
"content": "README: Coming soon!",
"context": "Project docs",
"category": "documentation"
},
# Performance
{
"content": "for i in range(1000000):\n list.append(expensive_operation())",
"context": "Optimize this",
"category": "performance"
},
{
"content": "We'll cache everything in memory",
"context": "Performance strategy",
"category": "caching"
},
# Code style
{
"content": "def a(b,c,d,e,f,g,h):\n x=b+c\n y=d+e\n return x*y*f*g*h",
"context": "Function review",
"category": "style"
},
# Error handling
{
"content": "result = api_call()\nif result:\n return result",
"context": "Error handling?",
"category": "errors"
},
]
def train_and_test():
"""Load pre-trained model and run comprehensive tests."""
CACHE_FILE = "brutal_feedback_llm_judge.json"
print("="*80)
print("LOADING PRE-TRAINED MODEL")
print("="*80)
unoptimized = BrutalFeedbackModule()
if not os.path.exists(CACHE_FILE):
print(f"\nERROR: Could not find pre-trained model at {CACHE_FILE}")
print("Please run brutal_feedback.py first to train the model.")
return
print(f"\nLoading optimized model from {CACHE_FILE}...")
optimized = BrutalFeedbackModule()
optimized.load(CACHE_FILE)
print("Model loaded successfully!")
print("\n" + "="*80)
print("COMPREHENSIVE TESTING - {} test cases".format(len(COMPREHENSIVE_TESTS)))
print("="*80)
results_by_category = {}
for i, test in enumerate(COMPREHENSIVE_TESTS, 1):
category = test['category']
if category not in results_by_category:
results_by_category[category] = {'unopt': [], 'opt': []}
print(f"\n[{i}/{len(COMPREHENSIVE_TESTS)}] {category.upper()}")
print(f"Content: {test['content'][:70]}...")
print(f"Context: {test['context']}")
# Unoptimized
unopt_result = unoptimized(content=test['content'], context=test['context'])
unopt_score = simple_metric(
dspy.Example(content=test['content'], context=test['context']),
unopt_result
)
# Optimized
opt_result = optimized(content=test['content'], context=test['context'])
opt_score = simple_metric(
dspy.Example(content=test['content'], context=test['context']),
opt_result
)
results_by_category[category]['unopt'].append(unopt_score)
results_by_category[category]['opt'].append(opt_score)
print(f"\n UNOPTIMIZED (score: {unopt_score:.2f}):")
print(f" {unopt_result.feedback}")
print(f"\n OPTIMIZED (score: {opt_score:.2f}):")
print(f" {opt_result.feedback}")
improvement = opt_score - unopt_score
print(f"\n IMPROVEMENT: {improvement:+.2f}")
print("-"*80)
# Summary
print("\n" + "="*80)
print("SUMMARY BY CATEGORY")
print("="*80)
overall_unopt = []
overall_opt = []
for category, scores in sorted(results_by_category.items()):
avg_unopt = sum(scores['unopt']) / len(scores['unopt'])
avg_opt = sum(scores['opt']) / len(scores['opt'])
improvement = avg_opt - avg_unopt
overall_unopt.extend(scores['unopt'])
overall_opt.extend(scores['opt'])
print(f"\n{category}:")
print(f" Unoptimized: {avg_unopt:.3f}")
print(f" Optimized: {avg_opt:.3f}")
print(f" Improvement: {improvement:+.3f}")
# Overall
print("\n" + "="*80)
print("OVERALL RESULTS")
print("="*80)
total_unopt = sum(overall_unopt) / len(overall_unopt)
total_opt = sum(overall_opt) / len(overall_opt)
total_improvement = total_opt - total_unopt
pct_improvement = (total_improvement / total_unopt * 100) if total_unopt > 0 else 0
print(f"\nAverage Score Across {len(COMPREHENSIVE_TESTS)} Tests:")
print(f" Unoptimized: {total_unopt:.3f}")
print(f" Optimized: {total_opt:.3f}")
print(f" Improvement: {total_improvement:+.3f} ({pct_improvement:+.1f}%)")
print(f"\nTests where optimized won: {sum(1 for u, o in zip(overall_unopt, overall_opt) if o > u)}/{len(COMPREHENSIVE_TESTS)}")
print(f"Tests where scores tied: {sum(1 for u, o in zip(overall_unopt, overall_opt) if o == u)}/{len(COMPREHENSIVE_TESTS)}")
print(f"Tests where unoptimized won: {sum(1 for u, o in zip(overall_unopt, overall_opt) if o < u)}/{len(COMPREHENSIVE_TESTS)}")
if __name__ == "__main__":
train_and_test()