forked from lemony-ai/cascadeflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_cascade.py
More file actions
509 lines (397 loc) · 18.5 KB
/
custom_cascade.py
File metadata and controls
509 lines (397 loc) · 18.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
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
"""
Custom Cascade Example
=======================
Build custom cascade strategies beyond the built-in patterns.
What it demonstrates:
- Custom routing logic based on query features
- Domain-specific cascades (code, writing, data)
- Time-of-day routing (peak vs off-peak pricing)
- Budget-aware cascades
- Custom quality thresholds per domain
- A/B testing different cascade strategies
Requirements:
- cascadeflow[all]
- OpenAI API key (or other providers)
Setup:
pip install cascadeflow[all]
export OPENAI_API_KEY="sk-..."
python examples/custom_cascade.py
Use Cases:
1. Domain-specific routing (code → code specialists)
2. Time-based routing (cheap at night, premium during day)
3. Budget constraints (never exceed $X per query)
4. Custom quality rules (higher bar for medical/legal)
5. A/B testing cascade strategies
Documentation:
📖 Custom Cascade Guide: docs/guides/custom_cascade.md
📖 Quick Start: docs/guides/quickstart.md
📚 Examples README: examples/README.md
"""
import asyncio
import os
from datetime import datetime
from typing import Any, Optional
from cascadeflow import CascadeAgent, ModelConfig
# ═══════════════════════════════════════════════════════════════════════════
# PATTERN 1: Domain-Specific Cascades
# ═══════════════════════════════════════════════════════════════════════════
class DomainRouter:
"""Route queries based on detected domain."""
DOMAIN_KEYWORDS = {
"code": ["python", "javascript", "code", "function", "class", "bug", "error", "api", "git"],
"writing": ["write", "essay", "article", "blog", "email", "letter", "draft"],
"data": ["dataframe", "csv", "sql", "database", "analyze", "chart", "visualization"],
"math": ["calculate", "equation", "derivative", "probability", "statistics"],
"general": [], # Fallback
}
@staticmethod
def detect_domain(query: str) -> str:
"""Detect query domain from keywords."""
query_lower = query.lower()
for domain, keywords in DomainRouter.DOMAIN_KEYWORDS.items():
if any(keyword in query_lower for keyword in keywords):
return domain
return "general"
@staticmethod
def get_models_for_domain(domain: str) -> list[ModelConfig]:
"""Get optimal models for domain."""
# Code: Best for technical tasks
if domain == "code":
return [
ModelConfig("gpt-4o-mini", provider="openai", cost=0.00015),
ModelConfig("gpt-4o", provider="openai", cost=0.00625),
]
# Writing: Anthropic excels here
elif domain == "writing":
if os.getenv("ANTHROPIC_API_KEY"):
return [
ModelConfig("claude-haiku-4-5-20251001", provider="anthropic", cost=0.001),
ModelConfig("claude-sonnet-4-5-20250929", provider="anthropic", cost=0.003),
]
else:
return [
ModelConfig("gpt-4o-mini", provider="openai", cost=0.00015),
ModelConfig("gpt-4o", provider="openai", cost=0.00625),
]
# Data: Fast models work well
elif domain == "data":
models = []
if os.getenv("GROQ_API_KEY"):
models.append(ModelConfig("llama-3.1-8b-instant", provider="groq", cost=0.0))
models.extend(
[
ModelConfig("gpt-4o-mini", provider="openai", cost=0.00015),
ModelConfig("gpt-4o", provider="openai", cost=0.00625),
]
)
return models
# Math: Premium models
elif domain == "math":
return [
ModelConfig("gpt-4o-mini", provider="openai", cost=0.00015),
ModelConfig("gpt-4o", provider="openai", cost=0.00625),
]
# General: Free-first cascade
else:
models = []
if os.getenv("GROQ_API_KEY"):
models.append(ModelConfig("llama-3.1-8b-instant", provider="groq", cost=0.0))
models.extend(
[
ModelConfig("gpt-4o-mini", provider="openai", cost=0.00015),
ModelConfig("gpt-4o", provider="openai", cost=0.00625),
]
)
return models
async def pattern_1_domain_routing():
"""Pattern 1: Domain-specific cascade routing."""
print("\n" + "=" * 70)
print("PATTERN 1: Domain-Specific Routing")
print("=" * 70)
print("\nStrategy: Route queries to domain-optimized model cascades")
print("Best for: Diverse workloads with specialized content\n")
# Test queries from different domains
test_queries = [
("Fix this Python bug: list index out of range", "code"),
("Write a professional email requesting time off", "writing"),
("Analyze this CSV and create a bar chart", "data"),
("What is the derivative of x^2?", "math"),
("What is the capital of France?", "general"),
]
total_cost = 0.0
for query, expected_domain in test_queries:
print(f"\n{'─'*70}")
print(f"Query: {query}")
# Detect domain
domain = DomainRouter.detect_domain(query)
print(f"Detected Domain: {domain} (expected: {expected_domain})")
# Get optimal models for domain
models = DomainRouter.get_models_for_domain(domain)
print(f"Selected Models: {[m.name for m in models]}")
# Create agent with domain-specific cascade
agent = CascadeAgent(models=models)
# Run query
result = await agent.run(query, max_tokens=150, temperature=0.7)
total_cost += result.total_cost
print(f"\n💡 Answer: {result.content[:100]}...")
print(f"📊 Model Used: {result.model_used}")
print(f"💰 Cost: ${result.total_cost:.6f}")
print(f"⏱️ Latency: {result.latency_ms:.0f}ms")
print(f"\n{'='*70}")
print(f"💰 Total Cost: ${total_cost:.6f}")
print(f"{'='*70}")
# ═══════════════════════════════════════════════════════════════════════════
# PATTERN 2: Time-Based Routing (Peak vs Off-Peak)
# ═══════════════════════════════════════════════════════════════════════════
class TimeBasedRouter:
"""Route based on time of day for cost optimization."""
@staticmethod
def is_peak_hours() -> bool:
"""Check if current time is peak hours (9am-5pm local time)."""
hour = datetime.now().hour
return 9 <= hour < 17
@staticmethod
def get_models_for_time() -> list[ModelConfig]:
"""Get models based on time of day."""
if TimeBasedRouter.is_peak_hours():
# Peak hours: Use faster, potentially more expensive models
print("⏰ Peak hours: Using premium models for speed")
return [
ModelConfig("gpt-4o-mini", provider="openai", cost=0.00015),
ModelConfig("gpt-4o", provider="openai", cost=0.00625),
]
else:
# Off-peak: Use free/cheap models
print("🌙 Off-peak hours: Using free models for cost savings")
models = []
if os.getenv("GROQ_API_KEY"):
models.append(ModelConfig("llama-3.1-8b-instant", provider="groq", cost=0.0))
models.extend(
[
ModelConfig("gpt-4o-mini", provider="openai", cost=0.00015),
ModelConfig("gpt-4o", provider="openai", cost=0.00625),
]
)
return models
async def pattern_2_time_based():
"""Pattern 2: Time-based routing for cost optimization."""
print("\n\n" + "=" * 70)
print("PATTERN 2: Time-Based Routing")
print("=" * 70)
print("\nStrategy: Use cheaper models during off-peak hours")
print("Best for: Applications with time-flexible workloads\n")
datetime.now().hour
is_peak = TimeBasedRouter.is_peak_hours()
print(f"Current time: {datetime.now().strftime('%I:%M %p')}")
print(f"Peak hours (9am-5pm): {'Yes' if is_peak else 'No'}")
# Get time-appropriate models
models = TimeBasedRouter.get_models_for_time()
print(f"\nSelected cascade: {' → '.join([m.name for m in models])}")
# Create agent
agent = CascadeAgent(models=models)
# Test queries
queries = [
"Explain machine learning in 50 words",
"Write a haiku about coding",
"What is 15% of 847?",
]
total_cost = 0.0
for query in queries:
print(f"\n{'─'*70}")
print(f"Query: {query}")
result = await agent.run(query, max_tokens=100, temperature=0.7)
total_cost += result.total_cost
print(f"💡 Answer: {result.content[:80]}...")
print(f"📊 Model: {result.model_used}")
print(f"💰 Cost: ${result.total_cost:.6f}")
print(f"\n{'='*70}")
print(f"💰 Total Cost: ${total_cost:.6f}")
# Show potential savings
if not is_peak:
print("💡 Off-peak savings: Using free/cheap models saves ~70% vs peak hours")
else:
print("💡 Peak hours: Prioritizing speed over cost")
print(f"{'='*70}")
# ═══════════════════════════════════════════════════════════════════════════
# PATTERN 3: Budget-Aware Cascades
# ═══════════════════════════════════════════════════════════════════════════
class BudgetAwareCascade:
"""Enforce budget constraints on cascades."""
def __init__(self, max_cost_per_query: float = 0.01):
self.max_cost = max_cost_per_query
self.queries_processed = 0
self.total_spent = 0.0
self.queries_blocked = 0
def can_afford(self, estimated_cost: float) -> bool:
"""Check if we can afford this query."""
return estimated_cost <= self.max_cost
def get_models_within_budget(self, query_complexity: str) -> list[ModelConfig]:
"""Get models that fit within budget for complexity level."""
all_models = []
# Free models (always within budget)
if os.getenv("GROQ_API_KEY"):
all_models.append(ModelConfig("llama-3.1-8b-instant", provider="groq", cost=0.0))
# Cheap models
if self.max_cost >= 0.0002:
all_models.append(ModelConfig("gpt-4o-mini", provider="openai", cost=0.00015))
# Premium models (only if budget allows)
if self.max_cost >= 0.006:
all_models.append(ModelConfig("gpt-4o", provider="openai", cost=0.00625))
if not all_models:
raise ValueError(f"Budget ${self.max_cost:.6f} too low for any models")
return all_models
async def process_with_budget(self, query: str, complexity: str = "moderate") -> Optional[Any]:
"""Process query within budget constraints."""
models = self.get_models_within_budget(complexity)
if not models:
self.queries_blocked += 1
print(f"❌ Query blocked: Budget ${self.max_cost:.6f} insufficient")
return None
agent = CascadeAgent(models=models)
result = await agent.run(query, max_tokens=100, temperature=0.7)
self.queries_processed += 1
self.total_spent += result.total_cost
return result
async def pattern_3_budget_aware():
"""Pattern 3: Budget-constrained cascades."""
print("\n\n" + "=" * 70)
print("PATTERN 3: Budget-Aware Cascades")
print("=" * 70)
print("\nStrategy: Enforce per-query budget constraints")
print("Best for: Cost-sensitive applications, free tiers\n")
# Test with different budgets
budgets = [
(0.0001, "Micro budget (free models only)"),
(0.001, "Small budget (includes cheap models)"),
(0.01, "Medium budget (includes premium)"),
]
for max_cost, description in budgets:
print(f"\n{'─'*70}")
print(f"Budget: ${max_cost:.4f} - {description}")
print(f"{'─'*70}")
cascade = BudgetAwareCascade(max_cost_per_query=max_cost)
# Get models within budget
try:
models = cascade.get_models_within_budget("moderate")
print(f"Available models: {[m.name for m in models]}")
# Test query
result = await cascade.process_with_budget("What is Python?", "simple")
if result:
print("\n✓ Query processed:")
print(f" Model: {result.model_used}")
print(f" Cost: ${result.total_cost:.6f}")
print(f" Within budget: {'Yes' if result.total_cost <= max_cost else 'No'}")
except ValueError as e:
print(f"❌ {e}")
print(f"\n{'='*70}")
# ═══════════════════════════════════════════════════════════════════════════
# PATTERN 4: Custom Quality Thresholds per Domain
# ═══════════════════════════════════════════════════════════════════════════
async def pattern_4_custom_thresholds():
"""Pattern 4: Domain-specific quality thresholds."""
print("\n\n" + "=" * 70)
print("PATTERN 4: Custom Quality Thresholds")
print("=" * 70)
print("\nStrategy: Different quality bars for different content types")
print("Best for: Mixed-criticality applications\n")
# Domain-specific thresholds
domain_thresholds = {
"medical": 0.95, # Very high bar
"legal": 0.92, # High bar
"financial": 0.90, # High bar
"general": 0.75, # Standard bar
"casual": 0.60, # Low bar
}
print("Quality thresholds by domain:")
for domain, threshold in domain_thresholds.items():
print(
f" {domain.capitalize():12} {threshold:.2f} {'(strict)' if threshold >= 0.90 else ''}"
)
# Example: Create agents with different thresholds
print(f"\n{'─'*70}")
print("Example: Medical query (strict validation)")
print(f"{'─'*70}")
# High threshold for medical
models = [
ModelConfig("gpt-4o-mini", provider="openai", cost=0.00015),
ModelConfig("gpt-4o", provider="openai", cost=0.00625),
]
# Note: CascadeAgent doesn't directly expose quality_threshold parameter
# In production, you'd implement custom quality validation
agent = CascadeAgent(models=models)
result = await agent.run(
"What are the symptoms of flu?",
max_tokens=150,
temperature=0.3, # Lower temp for factual accuracy
)
print(f"\nModel used: {result.model_used}")
print(f"Cost: ${result.total_cost:.6f}")
print(f"Draft accepted: {result.draft_accepted}")
print("\n💡 High-stakes domains should:")
print(" • Use lower temperature (0.2-0.4)")
print(" • Prefer premium models")
print(" • Enable strict quality validation")
print(" • Log all decisions for audit")
print(f"\n{'='*70}")
# ═══════════════════════════════════════════════════════════════════════════
# MAIN EXAMPLE
# ═══════════════════════════════════════════════════════════════════════════
async def main():
"""Run all custom cascade patterns."""
print("🌊 cascadeflow Custom Cascade Examples")
print("=" * 70)
print("\nDemonstrating 4 custom cascade patterns:")
print(" 1. Domain-Specific Routing")
print(" 2. Time-Based Routing")
print(" 3. Budget-Aware Cascades")
print(" 4. Custom Quality Thresholds")
# Check API keys
if not os.getenv("OPENAI_API_KEY"):
print("\n❌ OPENAI_API_KEY required")
print(" Set with: export OPENAI_API_KEY='sk-...'")
return
print("\n✓ OpenAI API key found")
# Run patterns
await pattern_1_domain_routing()
await pattern_2_time_based()
await pattern_3_budget_aware()
await pattern_4_custom_thresholds()
# Summary
print("\n\n" + "=" * 70)
print("🎓 KEY TAKEAWAYS")
print("=" * 70)
print("\n1. Domain Routing:")
print(" ├─ Detect query domain from keywords")
print(" ├─ Route to domain-optimized models")
print(" └─ 20-40% better quality + 30% cost savings")
print("\n2. Time-Based Routing:")
print(" ├─ Peak hours: Fast premium models")
print(" ├─ Off-peak: Free/cheap models")
print(" └─ 60-80% cost savings during off-peak")
print("\n3. Budget Constraints:")
print(" ├─ Enforce per-query cost limits")
print(" ├─ Block queries that exceed budget")
print(" └─ Prevent runaway costs")
print("\n4. Custom Quality:")
print(" ├─ Higher thresholds for critical domains")
print(" ├─ Lower thresholds for casual content")
print(" └─ Balance quality vs cost by use case")
print("\n5. Implementation Tips:")
print(" ├─ Start with domain detection")
print(" ├─ Add budget constraints early")
print(" ├─ Monitor cascade decisions")
print(" ├─ A/B test strategies")
print(" └─ Iterate based on metrics")
print("\n📚 Learn more:")
print(" • docs/guides/custom_cascade.md - Complete guide")
print(" • examples/production_patterns.py - Production patterns")
print(" • examples/custom_validation.py - Custom validators\n")
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\n\n⚠️ Interrupted by user")
except Exception as e:
print(f"\n\n❌ Error: {e}")
import traceback
traceback.print_exc()