|
|
|
""" |
|
Test script for the Mistral Nemo Backend Service |
|
""" |
|
|
|
import requests |
|
import json |
|
import time |
|
|
|
|
|
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 |
|
|
|
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) |
|
|
|
|
|
if not wait_for_service(): |
|
print("β Service is not ready. Exiting.") |
|
return |
|
|
|
|
|
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() |
|
|