File size: 4,562 Bytes
4ecf54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script for the Mistral Nemo Backend Service
"""

import requests
import json
import time

# Service configuration
BASE_URL = "http://localhost:8001"

def test_health():
    """Test the health endpoint"""
    print("πŸ₯ Testing health endpoint...")
    try:
        response = requests.get(f"{BASE_URL}/health", timeout=5)
        if response.status_code == 200:
            print(f"βœ… Health check passed: {response.json()}")
            return True
        else:
            print(f"❌ Health check failed: {response.status_code}")
            return False
    except Exception as e:
        print(f"❌ Health check error: {e}")
        return False

def test_root():
    """Test the root endpoint"""
    print("🏠 Testing root endpoint...")
    try:
        response = requests.get(f"{BASE_URL}/", timeout=5)
        if response.status_code == 200:
            data = response.json()
            print(f"βœ… Root endpoint: {data}")
            return True
        else:
            print(f"❌ Root endpoint failed: {response.status_code}")
            return False
    except Exception as e:
        print(f"❌ Root endpoint error: {e}")
        return False

def test_models():
    """Test the models endpoint"""
    print("πŸ“‹ Testing models endpoint...")
    try:
        response = requests.get(f"{BASE_URL}/v1/models", timeout=5)
        if response.status_code == 200:
            data = response.json()
            print(f"βœ… Available models: {[model['id'] for model in data['data']]}")
            return True
        else:
            print(f"❌ Models endpoint failed: {response.status_code}")
            return False
    except Exception as e:
        print(f"❌ Models endpoint error: {e}")
        return False

def test_chat_completion():
    """Test a simple chat completion"""
    print("πŸ’¬ Testing chat completion...")
    try:
        payload = {
            "model": "unsloth/Mistral-Nemo-Instruct-2407",
            "messages": [
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": "Hello! Tell me a fun fact about AI."}
            ],
            "max_tokens": 100,
            "temperature": 0.7
        }
        
        response = requests.post(f"{BASE_URL}/v1/chat/completions", 
                               json=payload, timeout=30)
        
        if response.status_code == 200:
            data = response.json()
            message = data["choices"][0]["message"]["content"]
            print(f"βœ… Chat completion successful!")
            print(f"πŸ€– Assistant: {message}")
            return True
        else:
            print(f"❌ Chat completion failed: {response.status_code}")
            print(f"Response: {response.text}")
            return False
    except Exception as e:
        print(f"❌ Chat completion error: {e}")
        return False

def wait_for_service():
    """Wait for the service to be ready"""
    print("⏳ Waiting for service to be ready...")
    max_attempts = 60  # Wait up to 5 minutes
    
    for attempt in range(max_attempts):
        try:
            response = requests.get(f"{BASE_URL}/health", timeout=5)
            if response.status_code == 200:
                print(f"βœ… Service is ready after {attempt * 5} seconds!")
                return True
        except:
            pass
        
        if attempt < max_attempts - 1:
            print(f"⏳ Attempt {attempt + 1}/{max_attempts} - waiting 5 seconds...")
            time.sleep(5)
    
    print("❌ Service did not become ready within the timeout period")
    return False

def main():
    """Run all tests"""
    print("πŸš€ Testing Mistral Nemo Backend Service")
    print("=" * 50)
    
    # Wait for service to be ready
    if not wait_for_service():
        print("❌ Service is not ready. Exiting.")
        return
    
    # Run tests
    tests = [test_root, test_health, test_models, test_chat_completion]
    passed = 0
    
    for test in tests:
        try:
            if test():
                passed += 1
            print()
        except Exception as e:
            print(f"❌ Test failed with exception: {e}")
            print()
    
    print("=" * 50)
    print(f"πŸ“Š Test Results: {passed}/{len(tests)} tests passed")
    
    if passed == len(tests):
        print("πŸŽ‰ All tests passed! Your Mistral Nemo service is working perfectly!")
    else:
        print("⚠️ Some tests failed. Check the logs above for details.")

if __name__ == "__main__":
    main()