-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_litellm_integration.py
More file actions
366 lines (292 loc) Β· 12.5 KB
/
test_litellm_integration.py
File metadata and controls
366 lines (292 loc) Β· 12.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
#!/usr/bin/env python3
CONSTANT_3 = 3
CONSTANT_4 = 4
CONSTANT_5 = 5
CONSTANT_40 = 40
CONSTANT_60 = 60
CONSTANT_200 = 200
"""
Direct integration of LiteLLM with planfile strategy generation.
"""
import asyncio
import json
import os
import sys
from pathlib import Path
from typing import Any
import yaml
# Add planfile to path
if __name__ == "__main__":
sys.path.insert(0, str(Path(__file__).parent.parent))
from planfile.llm.client import call_llm
from planfile.models import Strategy
class LiteLLMStrategyTester:
"""Test strategies with different LiteLLM models."""
def __init__(self):
self.models_to_test = [
# OpenAI models
"gpt-4-turbo-preview",
"gpt-3.5-turbo",
# Anthropic models (via OpenRouter/Anthropic API)
"anthropic/claude-3-opus-20240229",
"anthropic/claude-3-sonnet-20240229",
"anthropic/claude-3-haiku-20240307",
# Google models
"gemini-pro",
# Open source models
"replicate/llama-2-70b-chat:02e509c789964a7ea8736978a4382599e9a051f75f23364203f8973a8ee5e718",
"togethercomputer/llama-2-70b-chat"
]
self.test_prompts = [
{
"name": "Microservices Migration",
"focus": "architecture",
"sprints": 4,
"description": "Migrate monolithic application to microservices"
},
{
"name": "AI Platform Development",
"focus": "ml-engineering",
"sprints": 3,
"description": "Build an AI/ML platform with model training and deployment"
},
{
"name": "Security Compliance",
"focus": "security",
"sprints": 5,
"description": "Achieve SOC 2 Type II compliance"
},
{
"name": "Performance Optimization",
"focus": "performance",
"sprints": 2,
"description": "Optimize application performance and scalability"
}
]
def create_test_prompt(self, prompt_config: dict[str, Any]) -> str:
"""Create a test prompt from configuration."""
return f"""
Generate a comprehensive software development strategy for: {prompt_config['name']}
Project Description:
{prompt_config['description']}
Focus Area: {prompt_config['focus']}
Number of Sprints: {prompt_config['sprints']}
Requirements:
1. Create a YAML strategy with proper structure
2. Include detailed sprint objectives
3. Define quality gates with metrics
4. Add task patterns with priorities
5. Include resource estimates
Output only valid YAML wrapped in ```yaml``` blocks.
"""
async def test_model_with_prompt(
self,
model: str,
prompt: str
) -> dict[str, Any]:
"""Test a specific model with a prompt."""
try:
import time
start_time = time.time()
response = call_llm(prompt, model=model, temperature=0.3)
end_time = time.time()
# Extract YAML from response
yaml_content = None
if "```yaml" in response:
yaml_content = response.split("```yaml")[1].split("```")[0]
elif "```" in response:
yaml_content = response.split("```")[1].split("```")[0]
# Validate YAML if present
is_valid_yaml = False
strategy_obj = None
if yaml_content:
try:
data = yaml.safe_load(yaml_content)
is_valid_yaml = True
# Try to validate with Strategy model
try:
strategy_obj = Strategy(**data)
except Exception:
pass
except yaml.YAMLError:
pass
return {
"model": model,
"success": True,
"response_time": end_time - start_time,
"response_length": len(response),
"has_yaml": yaml_content is not None,
"is_valid_yaml": is_valid_yaml,
"is_valid_strategy": strategy_obj is not None,
"response_preview": response[:200] + "..." if len(response) > 200 else response,
"yaml_preview": yaml_content[:200] + "..." if yaml_content and len(yaml_content) > 200 else yaml_content
}
except Exception as e:
return {
"model": model,
"success": False,
"error": str(e),
"response_time": 0,
"response_length": 0,
"has_yaml": False,
"is_valid_yaml": False,
"is_valid_strategy": False
}
async def run_comprehensive_test(self) -> dict[str, Any]:
"""Run comprehensive tests across models and prompts."""
print("=" * 60)
print("LITELLM COMPREHENSIVE STRATEGY TESTS")
print("=" * 60)
# Check API keys
api_keys = {
'OPENAI_API_KEY': os.environ.get('OPENAI_API_KEY'),
'ANTHROPIC_API_KEY': os.environ.get('ANTHROPIC_API_KEY'),
'OPENROUTER_API_KEY': os.environ.get('OPENROUTER_API_KEY'),
'GOOGLE_API_KEY': os.environ.get('GOOGLE_API_KEY'),
'REPLICATE_API_TOKEN': os.environ.get('REPLICATE_API_TOKEN'),
'TOGETHER_AI_API_KEY': os.environ.get('TOGETHER_AI_API_KEY')
}
print("\nπ API Key Status:")
for key, value in api_keys.items():
status = "β
" if value else "β"
print(f" {key}: {status}")
print("\nπ§ͺ Testing models...")
results = {}
for prompt_config in self.test_prompts:
print(f"\nπ Prompt: {prompt_config['name']}")
print("-" * 40)
prompt = self.create_test_prompt(prompt_config)
prompt_results = []
for model in self.models_to_test:
print(f" π Testing {model}...")
result = await self.test_model_with_prompt(model, prompt)
prompt_results.append(result)
status = "β
" if result['success'] else "β"
yaml_status = "π" if result['has_yaml'] else "π"
valid_status = "β¨" if result['is_valid_strategy'] else "β οΈ"
print(f" {status} {yaml_status} {valid_status} {result['response_time']:.2f}s")
if not result['success']:
print(f" Error: {result.get('error', 'Unknown error')}")
results[prompt_config['name']] = prompt_results
# Generate summary
print("\n" + "=" * 60)
print("TEST SUMMARY")
print("=" * 60)
summary = self.generate_summary(results)
print(summary)
# Save detailed results
output_file = Path("litellm-test-results.json")
with open(output_file, 'w') as f:
json.dump({
'timestamp': str(asyncio.get_event_loop().time()),
'api_keys_present': {k: bool(v) for k, v in api_keys.items()},
'results': results,
'summary': summary
}, f, indent=2)
print(f"\nπΎ Detailed results saved to: {output_file}")
return results
def generate_summary(self, results: dict[str, Any]) -> str:
"""Generate a summary of test results."""
summary_lines = []
# Overall stats
all_results = []
for prompt_results in results.values():
all_results.extend(prompt_results)
total = len(all_results)
successful = sum(1 for r in all_results if r['success'])
with_yaml = sum(1 for r in all_results if r['has_yaml'])
with_valid_strategy = sum(1 for r in all_results if r['is_valid_strategy'])
summary_lines.append(f"Total tests: {total}")
summary_lines.append(f"Successful API calls: {successful} ({successful/total*100:.1f}%)")
summary_lines.append(f"Generated YAML: {with_yaml} ({with_yaml/total*100:.1f}%)")
summary_lines.append(f"Valid strategies: {with_valid_strategy} ({with_valid_strategy/total*100:.1f}%)")
# Best performers
successful_results = [r for r in all_results if r['success']]
if successful_results:
fastest = min(successful_results, key=lambda x: x['response_time'])
summary_lines.append(f"\nFastest model: {fastest['model']} ({fastest['response_time']:.2f}s)")
valid_strategies = [r for r in successful_results if r['is_valid_strategy']]
if valid_strategies:
best_strategy = max(valid_strategies, key=lambda x: x['response_length'])
summary_lines.append(f"Most detailed: {best_strategy['model']} ({best_strategy['response_length']} chars)")
# Model performance table
summary_lines.append("\nModel Performance:")
summary_lines.append("-" * 40)
model_stats = {}
for r in all_results:
model = r['model']
if model not in model_stats:
model_stats[model] = {'total': 0, 'success': 0, 'valid': 0}
model_stats[model]['total'] += 1
if r['success']:
model_stats[model]['success'] += 1
if r['is_valid_strategy']:
model_stats[model]['valid'] += 1
for model, stats in sorted(model_stats.items()):
success_rate = stats['success'] / stats['total'] * 100
valid_rate = stats['valid'] / stats['total'] * 100
summary_lines.append(
f" {model}: {stats['success']}/{stats['total']} ({success_rate:.0f}%) "
f"valid: {stats['valid']} ({valid_rate:.0f}%)"
)
return "\n".join(summary_lines)
async def test_specific_strategy():
"""Test generating a specific strategy with different models."""
print("\n" + "=" * 60)
print("SPECIFIC STRATEGY GENERATION TEST")
print("=" * 60)
# Test with the microservices migration strategy
strategy_path = Path(__file__).parent / "strategies" / "microservices-migration.yaml"
if not strategy_path.exists():
print(f"β Strategy file not found: {strategy_path}")
return
# Read the strategy as a reference
with open(strategy_path) as f:
reference_strategy = f.read()
# Create a new prompt based on it
prompt = f"""
Based on this microservices migration strategy, create a new strategy for "Mobile App Development".
Keep the same structure and quality, but adapt for a mobile application project.
Reference strategy structure:
{reference_strategy[:1000]}...
Generate a complete YAML strategy for:
- Project: Food Delivery Mobile App
- Platform: iOS & Android (React Native)
- Focus: User experience, performance, and scalability
- Sprints: 4
"""
models_to_try = [
"gpt-3.5-turbo",
"anthropic/claude-3-haiku-20240307"
]
if os.environ.get('OPENROUTER_API_KEY'):
models_to_try.append("anthropic/claude-3-haiku")
for model in models_to_try:
print(f"\nπ Generating with {model}...")
try:
response = call_llm(prompt, model=model, temperature=0.3)
# Save the response
output_file = Path(f"generated-strategy-{model.replace('/', '-')}.yaml")
# Extract YAML
if "```yaml" in response:
yaml_content = response.split("```yaml")[1].split("```")[0]
with open(output_file, 'w') as f:
f.write(yaml_content)
print(f"β
Strategy saved to: {output_file}")
else:
print("β οΈ No YAML found in response")
with open(output_file.with_suffix('.txt'), 'w') as f:
f.write(response)
print(f"π Response saved to: {output_file.with_suffix('.txt')}")
except Exception as e:
print(f"β Error: {e}")
async def main():
"""Main test function."""
# Run comprehensive tests
tester = LiteLLMStrategyTester()
await tester.run_comprehensive_test()
# Test specific strategy generation
await test_specific_strategy()
print("\nβ
All tests completed!")
if __name__ == "__main__":
asyncio.run(main())