File size: 6,893 Bytes
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script for OpenRouter free model integration.
Run this to verify OpenRouter is working with your project.
"""

import os
import sys
from pathlib import Path

# Add project root to path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

def test_openrouter_config():
    """Test 1: Verify OpenRouter configuration is loaded"""
    print("=" * 60)
    print("Test 1: Verifying OpenRouter Configuration")
    print("=" * 60)
    
    from src.utils.config import OPENROUTER_API_KEY, OPENROUTER_FREE_MODEL
    
    print(f"βœ… OPENROUTER_API_KEY: {OPENROUTER_API_KEY[:20]}..." if OPENROUTER_API_KEY else "❌ OPENROUTER_API_KEY not set")
    print(f"βœ… OPENROUTER_FREE_MODEL: {OPENROUTER_FREE_MODEL}")
    
    return True

def test_openrouter_provider():
    """Test 2: Initialize ModelOrchestrator with OpenRouter provider"""
    print("\n" + "=" * 60)
    print("Test 2: Initializing OpenRouter Provider")
    print("=" * 60)
    
    try:
        from src.ml.model_orchestrator import ModelOrchestrator
        
        print("Creating ModelOrchestrator with OpenRouter provider...")
        orchestrator = ModelOrchestrator(provider='openrouter')
        
        print(f"βœ… Provider: {orchestrator.provider}")
        print(f"βœ… API Key loaded: {len(orchestrator.api_key) > 0}")
        print(f"βœ… ModelOrchestrator initialized successfully")
        
        return orchestrator
    
    except Exception as e:
        print(f"❌ Error: {str(e)}")
        return None

def test_openrouter_language_model(orchestrator):
    """Test 3: Initialize language model with OpenRouter"""
    print("\n" + "=" * 60)
    print("Test 3: Initializing Language Model (OpenRouter)")
    print("=" * 60)
    
    if not orchestrator:
        print("❌ Skipping: No orchestrator available")
        return False
    
    try:
        print("Initializing language model with free Mistral 7B...")
        orchestrator.init_language_model()
        
        print(f"βœ… Language model initialized")
        print(f"βœ… Model name: {orchestrator.model_name}")
        print(f"βœ… Provider: {orchestrator.provider}")
        
        return True
    
    except Exception as e:
        print(f"❌ Error: {str(e)}")
        import traceback
        traceback.print_exc()
        return False

def test_openrouter_response(orchestrator):
    """Test 4: Generate a response using OpenRouter"""
    print("\n" + "=" * 60)
    print("Test 4: Generating Response (OpenRouter Free Model)")
    print("=" * 60)
    
    if not orchestrator:
        print("❌ Skipping: No orchestrator available")
        return False
    
    try:
        print("Generating response using OpenRouter free model...")
        prompt = "What is the best way to learn programming as a beginner?"
        
        print(f"Prompt: {prompt}\n")
        
        response = orchestrator.generate_response(
            prompt,
            use_cache=False
        )
        
        print(f"βœ… Response received ({len(response)} characters):")
        print("-" * 60)
        print(response[:200] + "..." if len(response) > 200 else response)
        print("-" * 60)
        
        return True
    
    except Exception as e:
        print(f"⚠️  Note: Response generation requires OpenRouter API")
        print(f"Error details: {str(e)}")
        return False

def test_learning_path_generation():
    """Test 5: Generate learning path with OpenRouter"""
    print("\n" + "=" * 60)
    print("Test 5: Learning Path Generation (OpenRouter Free Model)")
    print("=" * 60)
    
    try:
        from src.learning_path import LearningPathGenerator
        
        print("Creating LearningPathGenerator...")
        path_gen = LearningPathGenerator(api_key=None)
        
        print("βœ… LearningPathGenerator created")
        print("Generating learning path with OpenRouter provider...")
        
        path = path_gen.generate_path(
            topic="Python Programming",
            expertise_level="Beginner",
            learning_style="Visual",
            time_commitment="5-7 hours/week",
            duration_weeks=4,
            ai_provider="openrouter",
            ai_model=None  # Will use free model
        )
        
        print(f"βœ… Learning path generated successfully")
        print(f"βœ… Path ID: {path.id}")
        print(f"βœ… Title: {path.title}")
        print(f"βœ… Duration: {path.duration_weeks} weeks")
        print(f"βœ… Total hours: {path.total_hours}")
        
        return True
    
    except Exception as e:
        print(f"⚠️  Note: Full learning path generation may require more setup")
        print(f"Error details: {str(e)}")
        return False

def print_summary(results):
    """Print test summary"""
    print("\n" + "=" * 60)
    print("TEST SUMMARY")
    print("=" * 60)
    
    tests = [
        ("Configuration", results.get('config', False)),
        ("Provider Init", results.get('provider', False)),
        ("Language Model Init", results.get('language_model', False)),
        ("Response Generation", results.get('response', False)),
        ("Learning Path Gen", results.get('learning_path', False)),
    ]
    
    passed = sum(1 for _, result in tests if result)
    total = len(tests)
    
    for test_name, result in tests:
        status = "βœ… PASS" if result else "❌ FAIL"
        print(f"{test_name:.<40} {status}")
    
    print("-" * 60)
    print(f"Results: {passed}/{total} tests passed")
    
    if passed == total:
        print("\nπŸŽ‰ All tests passed! OpenRouter is ready to use!")
    elif passed >= 3:
        print("\nβœ… Core functionality working! Some optional tests may have failed.")
    else:
        print("\n⚠️  Some tests failed. Check configuration and logs above.")

def main():
    """Run all tests"""
    print("\n" + "=" * 60)
    print("OpenRouter Free Model Integration - Test Suite")
    print("=" * 60)
    
    results = {}
    
    # Test 1: Configuration
    try:
        results['config'] = test_openrouter_config()
    except Exception as e:
        print(f"❌ Config test failed: {e}")
        results['config'] = False
    
    # Test 2: Provider initialization
    orchestrator = test_openrouter_provider()
    results['provider'] = orchestrator is not None
    
    # Test 3: Language model initialization
    if orchestrator:
        results['language_model'] = test_openrouter_language_model(orchestrator)
    else:
        results['language_model'] = False
    
    # Test 4: Response generation
    if orchestrator:
        results['response'] = test_openrouter_response(orchestrator)
    else:
        results['response'] = False
    
    # Test 5: Learning path generation (optional)
    results['learning_path'] = test_learning_path_generation()
    
    # Print summary
    print_summary(results)

if __name__ == "__main__":
    main()