test_optimized_prompts.pyโข9.01 kB
#!/usr/bin/env python3
"""
๐งช Test Optimized Prompt Generator
This script demonstrates the dramatic improvement in prompt optimization,
showing before/after comparison with size reduction and quality metrics.
"""
import logging
import sys
from pathlib import Path
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def test_prompt_optimization():
"""Test the optimized prompt generator vs. the original"""
logger.info("๐งช Testing Prompt Optimization System...")
try:
# Test original prompt generator
logger.info("๐ Testing ORIGINAL prompt generator...")
from prompt_generator import PromptGenerator
original_gen = PromptGenerator()
original_prompt = original_gen.generate_enhanced_prompt('test message', 'comprehensive')
original_size = len(original_prompt)
logger.info(f"๐ Original prompt size: {original_size:,} characters ({original_size/1024:.1f} KB)")
# Test optimized prompt generator
logger.info("๐ Testing OPTIMIZED prompt generator...")
from optimized_prompt_generator import OptimizedPromptGenerator
optimized_gen = OptimizedPromptGenerator()
optimized_prompt = optimized_gen.generate_optimized_prompt('test message', 'smart')
optimized_size = len(optimized_prompt)
logger.info(f"๐ Optimized prompt size: {optimized_size:,} characters ({optimized_size/1024:.1f} KB)")
# Calculate improvements
size_reduction = original_size - optimized_size
compression_ratio = (size_reduction / original_size) * 100
logger.info(f"๐ฏ OPTIMIZATION RESULTS:")
logger.info(f" โข Size reduction: {size_reduction:,} characters")
logger.info(f" โข Compression ratio: {compression_ratio:.1f}%")
logger.info(f" โข Efficiency gain: {original_size/optimized_size:.1f}x")
# Quality analysis
logger.info(f"๐ QUALITY ANALYSIS:")
# Check if essential elements are preserved
essential_elements = [
('User preferences', '๐ค PREFERENCES:', '๐ค USER PREFERENCES:'),
('Tech stack', 'โ๏ธ TECH:', 'โ๏ธ TECH STACK:'),
('Agent info', '๐ค AGENT:', '๐ค AGENT METADATA:'),
('Instructions', '๐ฏ RESPOND WITH:', '=== ๐ฏ INSTRUCTIONS ===')
]
for element_name, optimized_marker, original_marker in essential_elements:
has_optimized = optimized_marker in optimized_prompt
has_original = original_marker in original_prompt
status = "โ
" if has_optimized else "โ"
logger.info(f" {status} {element_name}: {'Preserved' if has_optimized else 'Missing'}")
# Check for improvements
improvements = []
if 'โ ๏ธ' not in optimized_prompt:
improvements.append("No warning messages")
if 'not available' not in optimized_prompt.lower():
improvements.append("No fallback text")
if optimized_prompt.count('===') < 5:
improvements.append("Cleaner formatting")
if len(optimized_prompt.split('\n')) < 50:
improvements.append("Better readability")
logger.info(f"๐ IMPROVEMENTS ACHIEVED:")
for improvement in improvements:
logger.info(f" โ
{improvement}")
# Show sample of optimized prompt
logger.info(f"\n๐ SAMPLE OPTIMIZED PROMPT:")
lines = optimized_prompt.split('\n')
for i, line in enumerate(lines[:15]): # Show first 15 lines
logger.info(f" {i+1:2d}: {line}")
if len(lines) > 15:
logger.info(f" ... and {len(lines) - 15} more lines")
return True
except Exception as e:
logger.error(f"โ Test failed: {e}")
return False
def test_intent_analysis():
"""Test the intelligent intent analysis system"""
logger.info("\n๐ง Testing Intelligent Intent Analysis...")
try:
from optimized_prompt_generator import OptimizedPromptGenerator
generator = OptimizedPromptGenerator()
test_messages = [
"How do I fix this bug in the database?",
"Show me the project structure and files",
"Continue from where we left off yesterday",
"What's the weather like today?",
"Implement a new feature for user authentication"
]
for message in test_messages:
# Create a mock context for testing
class MockContext:
pass
context = MockContext()
# Analyze intent
intent = generator._analyze_user_intent(message, context)
logger.info(f"๐ฌ Message: {message}")
logger.info(f" ๐ฏ Intent: {intent['primary_intent']}")
logger.info(f" โ๏ธ Technical: {intent['needs_technical_context']}")
logger.info(f" ๐๏ธ Project: {intent['needs_project_context']}")
logger.info(f" ๐ฌ Conversation: {intent['needs_conversation_context']}")
logger.info(f" ๐ Complexity: {intent['complexity']}")
logger.info("")
return True
except Exception as e:
logger.error(f"โ Intent analysis test failed: {e}")
return False
def test_different_context_types():
"""Test different context types and their optimization"""
logger.info("\n๐ญ Testing Different Context Types...")
try:
from optimized_prompt_generator import OptimizedPromptGenerator
generator = OptimizedPromptGenerator()
context_types = ['smart', 'technical', 'conversation', 'project']
for context_type in context_types:
logger.info(f"๐ฏ Testing context type: {context_type}")
try:
prompt = generator.generate_optimized_prompt('test message', context_type)
size = len(prompt)
logger.info(f" ๐ Size: {size:,} characters ({size/1024:.1f} KB)")
# Check for context-specific content
if context_type == 'technical':
has_tech = 'โ๏ธ TECH:' in prompt
logger.info(f" โ๏ธ Technical context: {'โ
' if has_tech else 'โ'}")
elif context_type == 'conversation':
has_conv = '๐ฌ CONTEXT:' in prompt
logger.info(f" ๐ฌ Conversation context: {'โ
' if has_conv else 'โ'}")
elif context_type == 'project':
has_proj = '๐๏ธ PROJECT:' in prompt
logger.info(f" ๐๏ธ Project context: {'โ
' if has_proj else 'โ'}")
except Exception as e:
logger.error(f" โ Failed: {e}")
return True
except Exception as e:
logger.error(f"โ Context type test failed: {e}")
return False
def main():
"""Run all tests"""
logger.info("๐ Starting Optimized Prompt Generator Tests...")
tests = [
("Prompt Optimization", test_prompt_optimization),
("Intent Analysis", test_intent_analysis),
("Context Types", test_different_context_types)
]
results = []
for test_name, test_func in tests:
logger.info(f"\n{'='*60}")
logger.info(f"๐งช Running: {test_name}")
logger.info(f"{'='*60}")
try:
success = test_func()
results.append((test_name, success))
if success:
logger.info(f"โ
{test_name}: PASSED")
else:
logger.error(f"โ {test_name}: FAILED")
except Exception as e:
logger.error(f"โ {test_name}: ERROR - {e}")
results.append((test_name, False))
# Summary
logger.info(f"\n{'='*60}")
logger.info("๐ TEST SUMMARY")
logger.info(f"{'='*60}")
passed = sum(1 for _, success in results if success)
total = len(results)
for test_name, success in results:
status = "โ
PASS" if success else "โ FAIL"
logger.info(f" {status}: {test_name}")
logger.info(f"\n๐ฏ Overall: {passed}/{total} tests passed ({passed/total*100:.1f}%)")
if passed == total:
logger.info("๐ All tests passed! Prompt optimization system is working correctly.")
return True
else:
logger.error("โ ๏ธ Some tests failed. Check the logs above for details.")
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)