-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_token_efficiency.py
More file actions
182 lines (145 loc) · 6.01 KB
/
demo_token_efficiency.py
File metadata and controls
182 lines (145 loc) · 6.01 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
"""Comprehensive demo of token efficiency improvements"""
from codeshield.utils.token_optimizer import (
get_token_optimizer, optimize_fix_prompt,
LocalProcessor, ModelTier, get_optimal_max_tokens
)
print("=" * 60)
print("CODESHIELD TOKEN EFFICIENCY DEMO")
print("=" * 60)
# =============================================================================
# 1. LOCAL PROCESSING (0 tokens!)
# =============================================================================
print("\n" + "=" * 60)
print("1. LOCAL PROCESSING - 0 TOKENS USED")
print("=" * 60)
code_simple = '''def parse():
return json.loads(os.getenv("DATA"))'''
issues_simple = ['Missing import: json', 'Missing import: os']
print(f"\nCode with missing imports:")
print(code_simple)
print(f"\nIssues: {issues_simple}")
# Check if can fix locally
can_local = LocalProcessor.can_fix_locally(code_simple, issues_simple)
print(f"\nCan fix locally (no LLM): {can_local}")
if can_local:
fixed = LocalProcessor.fix_locally(code_simple, issues_simple)
print(f"\nFixed code (0 tokens used!):")
print(fixed)
print("\n✅ Tokens saved: ~50-100 tokens (entire LLM call avoided)")
# =============================================================================
# 2. PROMPT COMPRESSION
# =============================================================================
print("\n" + "=" * 60)
print("2. PROMPT COMPRESSION")
print("=" * 60)
# Old verbose prompt
old_prompt = """Fix the following Python code issues:
Issues found:
- Missing import: json
- Missing import: os
Original code:
```python
def parse():
return json.loads(os.getenv("DATA"))
```
Return ONLY the fixed Python code, no explanations."""
# New optimized prompt (for complex issues that need LLM)
new_prompt = "Fix:\nMissing import: json; Missing import: os\n\nCode:\n```\ndef parse():\n return json.loads(os.getenv(\"DATA\"))\n```\nReturn fixed code only."
print(f"\nOld prompt: {len(old_prompt)} chars (~{len(old_prompt)//4} tokens)")
print(f"New prompt: {len(new_prompt)} chars (~{len(new_prompt)//4} tokens)")
print(f"Savings: {100 - (len(new_prompt)/len(old_prompt)*100):.0f}%")
# =============================================================================
# 3. DYNAMIC MAX_TOKENS
# =============================================================================
print("\n" + "=" * 60)
print("3. DYNAMIC MAX_TOKENS (Adaptive Response Limits)")
print("=" * 60)
test_cases = [
("fix", 100, "Short code fix"),
("fix", 500, "Medium code fix"),
("fix", 2000, "Large code fix"),
("briefing", 0, "Context briefing"),
("style", 0, "Style suggestions"),
]
print(f"\n{'Task':<20} {'Code Length':<12} {'max_tokens':<12} {'Old Setting':<12}")
print("-" * 56)
for task, length, desc in test_cases:
optimal = get_optimal_max_tokens(task, length)
old = 2000 if task == "fix" else 200
print(f"{desc:<20} {length:<12} {optimal:<12} {old:<12}")
# =============================================================================
# 4. MODEL TIERING
# =============================================================================
print("\n" + "=" * 60)
print("4. MODEL TIERING (Cheap Models for Simple Tasks)")
print("=" * 60)
# Simple task
simple_code = "x = json.loads(data)"
simple_issues = ["Missing import: json"]
simple_model = ModelTier.select_model(simple_code, simple_issues, "cometapi")
# Complex task
complex_code = '''
class DataProcessor:
def __init__(self, config):
self.config = config
self.cache = {}
async def process(self, items):
results = []
for item in items:
if item.id in self.cache:
results.append(self.cache[item.id])
else:
processed = await self._transform(item)
self.cache[item.id] = processed
results.append(processed)
return results
'''
complex_issues = ["Type hints missing", "Error handling needed", "Async context manager needed"]
complex_model = ModelTier.select_model(complex_code, complex_issues, "cometapi")
print(f"\nSimple task model: {simple_model} (free/fast)")
print(f"Complex task model: {complex_model} (smarter)")
# =============================================================================
# 5. CACHING EFFICIENCY
# =============================================================================
print("\n" + "=" * 60)
print("5. CACHING (Avoid Duplicate API Calls)")
print("=" * 60)
optimizer = get_token_optimizer()
optimizer.reset_session()
# Simulate caching
from dataclasses import dataclass
@dataclass
class MockResponse:
content: str = "import json\nimport os\ndef parse():\n return json.loads(os.getenv('DATA'))"
provider: str = "test"
model: str = "test"
tokens_used: int = 50
test_prompt = "Fix:\nMissing import: json\n\nCode:\nx = json.loads(data)"
# First request - cache miss
c1 = optimizer.get_cached(test_prompt)
print(f"\nRequest 1: Cache {'HIT' if c1 else 'MISS'}")
# Cache it
optimizer.cache_response(test_prompt, MockResponse())
# Subsequent requests - cache hits (0 tokens!)
for i in range(2, 6):
c = optimizer.get_cached(test_prompt)
print(f"Request {i}: Cache {'HIT' if c else 'MISS'} - Tokens saved: {c.tokens_saved if c else 0}")
print(f"\nFinal stats: {optimizer.get_stats()}")
# =============================================================================
# SUMMARY
# =============================================================================
print("\n" + "=" * 60)
print("TOKEN EFFICIENCY SUMMARY")
print("=" * 60)
optimizations = [
("Local Processing", "100%", "Fix imports without LLM calls"),
("Prompt Compression", "40-60%", "Shorter prompts, same results"),
("Dynamic max_tokens", "50-75%", "Adaptive response limits"),
("Model Tiering", "30-50%", "Cheap models for simple tasks"),
("Response Caching", "100%", "0 tokens for repeated requests"),
]
print(f"\n{'Optimization':<22} {'Savings':<10} {'Description'}")
print("-" * 70)
for opt, saving, desc in optimizations:
print(f"{opt:<22} {saving:<10} {desc}")
print("\n🚀 Combined effect: Up to 90%+ token reduction for common tasks!")