-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfeedback_loop_demo.py
More file actions
382 lines (301 loc) · 12 KB
/
feedback_loop_demo.py
File metadata and controls
382 lines (301 loc) · 12 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
"""Agent-to-LLM Feedback Loop Demo (Pattern 6).
This script demonstrates quality-based learning and adaptive routing:
- Recording quality feedback after LLM responses
- Getting tier recommendations based on historical performance
- Analyzing quality statistics and trends
- Identifying underperforming workflow stages
Requires Redis running locally.
Copyright 2025 Smart-AI-Memory
Licensed under Fair Source License 0.9
"""
import random
import time
from empathy_os.telemetry import FeedbackLoop
from empathy_os.telemetry.feedback_loop import ModelTier
def demo_record_feedback():
"""Demo: Recording quality feedback for workflow stages."""
print("=" * 70)
print("DEMO 1: RECORDING QUALITY FEEDBACK")
print("=" * 70)
print()
feedback = FeedbackLoop()
print("📊 Recording quality feedback for code-review workflow...")
print()
# Simulate quality ratings for "cheap" tier over time
print("🔵 Cheap tier performance (10 samples):")
for i in range(10):
# Cheap tier: variable quality (0.55-0.75)
quality = 0.55 + (random.random() * 0.2)
feedback_id = feedback.record_feedback(
workflow_name="code-review",
stage_name="analysis",
tier=ModelTier.CHEAP,
quality_score=quality,
metadata={"tokens": 100 + i * 10, "latency_ms": 800 + i * 50},
)
if feedback_id:
print(f" Sample {i+1}: Quality {quality:.2f} → Recorded {feedback_id}")
print()
# Simulate quality ratings for "capable" tier
print("🟢 Capable tier performance (10 samples):")
for i in range(10):
# Capable tier: better quality (0.75-0.95)
quality = 0.75 + (random.random() * 0.2)
feedback_id = feedback.record_feedback(
workflow_name="code-review",
stage_name="analysis",
tier=ModelTier.CAPABLE,
quality_score=quality,
metadata={"tokens": 200 + i * 20, "latency_ms": 1200 + i * 100},
)
if feedback_id:
print(f" Sample {i+1}: Quality {quality:.2f} → Recorded {feedback_id}")
print()
def demo_get_quality_stats():
"""Demo: Analyzing quality statistics."""
print("=" * 70)
print("DEMO 2: QUALITY STATISTICS ANALYSIS")
print("=" * 70)
print()
feedback = FeedbackLoop()
# Get stats for cheap tier
cheap_stats = feedback.get_quality_stats("code-review", "analysis", tier="cheap")
if cheap_stats:
print("📉 Cheap Tier Statistics:")
print(f" Average Quality: {cheap_stats.avg_quality:.2f}")
print(f" Quality Range: {cheap_stats.min_quality:.2f} - {cheap_stats.max_quality:.2f}")
print(f" Sample Count: {cheap_stats.sample_count}")
print(f" Recent Trend: {cheap_stats.recent_trend:+.2f} ({'📈 improving' if cheap_stats.recent_trend > 0 else '📉 declining'})")
else:
print("No stats available for cheap tier")
print()
# Get stats for capable tier
capable_stats = feedback.get_quality_stats("code-review", "analysis", tier="capable")
if capable_stats:
print("📈 Capable Tier Statistics:")
print(f" Average Quality: {capable_stats.avg_quality:.2f}")
print(f" Quality Range: {capable_stats.min_quality:.2f} - {capable_stats.max_quality:.2f}")
print(f" Sample Count: {capable_stats.sample_count}")
print(f" Recent Trend: {capable_stats.recent_trend:+.2f} ({'📈 improving' if capable_stats.recent_trend > 0 else '📉 declining'})")
else:
print("No stats available for capable tier")
print()
def demo_tier_recommendation():
"""Demo: Getting tier recommendations based on quality."""
print("=" * 70)
print("DEMO 3: TIER RECOMMENDATIONS")
print("=" * 70)
print()
feedback = FeedbackLoop()
# Get recommendation for cheap tier
print("🤔 Asking: Should we upgrade from CHEAP tier?")
recommendation = feedback.recommend_tier(
workflow_name="code-review", stage_name="analysis", current_tier="cheap"
)
print()
print("💡 Recommendation:")
print(f" Current Tier: {recommendation.current_tier.upper()}")
print(f" Recommended Tier: {recommendation.recommended_tier.upper()}")
print(f" Confidence: {recommendation.confidence:.1%}")
print(f" Reason: {recommendation.reason}")
if recommendation.recommended_tier != recommendation.current_tier:
print()
print(f"✅ Action: Upgrade to {recommendation.recommended_tier.upper()} tier for better quality")
else:
print()
print("✅ Action: Continue using current tier")
print()
# Get recommendation for capable tier
print("🤔 Asking: Is CAPABLE tier performing well?")
recommendation2 = feedback.recommend_tier(
workflow_name="code-review", stage_name="analysis", current_tier="capable"
)
print()
print("💡 Recommendation:")
print(f" Current Tier: {recommendation2.current_tier.upper()}")
print(f" Recommended Tier: {recommendation2.recommended_tier.upper()}")
print(f" Confidence: {recommendation2.confidence:.1%}")
print(f" Reason: {recommendation2.reason}")
print()
def demo_underperforming_stages():
"""Demo: Identifying underperforming workflow stages."""
print("=" * 70)
print("DEMO 4: IDENTIFYING UNDERPERFORMING STAGES")
print("=" * 70)
print()
feedback = FeedbackLoop()
# Create feedback for multiple stages with varying quality
print("📊 Creating feedback for multiple workflow stages...")
# Stage 1: Good performance
for i in range(10):
quality = 0.8 + (random.random() * 0.1) # 0.8-0.9
feedback.record_feedback(
workflow_name="multi-stage-workflow",
stage_name="validation",
tier="cheap",
quality_score=quality,
)
# Stage 2: Poor performance
for i in range(10):
quality = 0.5 + (random.random() * 0.15) # 0.5-0.65
feedback.record_feedback(
workflow_name="multi-stage-workflow",
stage_name="generation",
tier="cheap",
quality_score=quality,
)
# Stage 3: Acceptable performance
for i in range(10):
quality = 0.72 + (random.random() * 0.08) # 0.72-0.80
feedback.record_feedback(
workflow_name="multi-stage-workflow",
stage_name="review",
tier="cheap",
quality_score=quality,
)
print()
# Find underperforming stages
print("🔍 Finding stages with quality < 0.7...")
underperforming = feedback.get_underperforming_stages(
workflow_name="multi-stage-workflow", quality_threshold=0.7
)
print()
if underperforming:
print(f"❌ Found {len(underperforming)} underperforming stage(s):")
print()
for stage_name, stats in underperforming:
print(f" Stage: {stage_name}")
print(f" Average Quality: {stats.avg_quality:.2f} (below 0.7 threshold)")
print(f" Sample Count: {stats.sample_count}")
print(f" Range: {stats.min_quality:.2f} - {stats.max_quality:.2f}")
print()
else:
print("✅ All stages performing above threshold!")
print()
def demo_feedback_history():
"""Demo: Retrieving feedback history."""
print("=" * 70)
print("DEMO 5: FEEDBACK HISTORY")
print("=" * 70)
print()
feedback = FeedbackLoop()
# Get recent feedback for code-review
print("📜 Recent feedback for code-review/analysis (last 5)...")
history = feedback.get_feedback_history("code-review", "analysis", limit=5)
print()
if history:
print(f"Found {len(history)} recent feedback entries:")
print()
for i, entry in enumerate(history, 1):
print(f" {i}. {entry.feedback_id}")
print(f" Tier: {entry.tier.upper()}")
print(f" Quality: {entry.quality_score:.2f}")
print(f" Time: {entry.timestamp.strftime('%H:%M:%S')}")
if entry.metadata:
print(f" Metadata: {entry.metadata}")
print()
else:
print("No feedback history available")
print()
def demo_adaptive_routing():
"""Demo: Using feedback for adaptive routing decisions."""
print("=" * 70)
print("DEMO 6: ADAPTIVE ROUTING IN ACTION")
print("=" * 70)
print()
feedback = FeedbackLoop()
# Simulate a workflow that adapts based on feedback
print("🔄 Simulating adaptive workflow routing...")
print()
workflow_name = "adaptive-workflow"
stage_name = "processing"
# Start with cheap tier
current_tier = "cheap"
print(f"Starting with: {current_tier.upper()} tier")
# Simulate 3 iterations
for iteration in range(1, 4):
print()
print(f"--- Iteration {iteration} ---")
# Simulate LLM response quality (degrading over time for demo)
quality = max(0.5, 0.9 - (iteration * 0.15))
print(f"LLM Response Quality: {quality:.2f}")
# Record feedback
feedback_id = feedback.record_feedback(
workflow_name=workflow_name,
stage_name=stage_name,
tier=current_tier,
quality_score=quality,
)
if feedback_id:
print(f"Recorded: {feedback_id}")
# Get recommendation after sufficient samples
if iteration >= 2:
recommendation = feedback.recommend_tier(
workflow_name=workflow_name, stage_name=stage_name, current_tier=current_tier
)
print()
print(f"Recommendation: {recommendation.recommended_tier.upper()}")
print(f"Confidence: {recommendation.confidence:.1%}")
print(f"Reason: {recommendation.reason}")
# Apply recommendation
if recommendation.recommended_tier != current_tier:
print()
print(f"⬆️ Upgrading: {current_tier.upper()} → {recommendation.recommended_tier.upper()}")
current_tier = recommendation.recommended_tier
time.sleep(0.5)
print()
print(f"Final tier: {current_tier.upper()}")
print()
def main():
"""Run all feedback loop demos."""
print("\n")
print("╔" + "=" * 68 + "╗")
print("║" + " " * 13 + "AGENT-TO-LLM FEEDBACK LOOP (PATTERN 6)" + " " * 16 + "║")
print("╚" + "=" * 68 + "╝")
print()
print("This demo shows quality-based learning and adaptive routing.")
print()
try:
# Demo 1: Record feedback
demo_record_feedback()
# Demo 2: Analyze quality stats
demo_get_quality_stats()
# Demo 3: Get tier recommendations
demo_tier_recommendation()
# Demo 4: Find underperforming stages
demo_underperforming_stages()
# Demo 5: View feedback history
demo_feedback_history()
# Demo 6: Adaptive routing
demo_adaptive_routing()
except Exception as e:
print(f"\n❌ Demo failed: {e}")
print(" Make sure Redis is running: redis-server")
print(" Or run: empathy memory start")
import traceback
traceback.print_exc()
return
print()
print("=" * 70)
print("✅ FEEDBACK LOOP DEMO COMPLETE")
print("=" * 70)
print()
print("💡 Key Takeaways:")
print(" 1. Record quality scores (0.0-1.0) after LLM responses")
print(" 2. System recommends tier upgrades when quality < 0.7")
print(" 3. System recommends downgrades when quality > 0.9 for cost savings")
print(" 4. Quality trends track improvement/decline over time")
print(" 5. Identify underperforming stages for optimization")
print()
print("📖 Next Steps:")
print(" - Integrate feedback recording into workflows")
print(" - Use tier recommendations for adaptive routing")
print(" - Monitor quality trends to detect regressions")
print(" - Optimize underperforming stages")
print()
print("📚 Documentation:")
print(" - docs/AGENT_TRACKING_AND_COORDINATION.md")
print(" - Pattern 6: Agent-to-LLM Feedback Loop")
print()
if __name__ == "__main__":
main()