File size: 2,886 Bytes
476e500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Simple test script for the FastAPI TextGen API.
Run this script to test the API endpoints locally.
"""

import requests
import json
import sys

def test_health_endpoint(base_url):
    """Test the health check endpoint."""
    print("🔍 Testing health endpoint...")
    try:
        response = requests.get(f"{base_url}/health")
        if response.status_code == 200:
            data = response.json()
            print(f"✅ Health check passed: {data['message']}")
            print(f"🔑 OpenAI configured: {data['openai_configured']}")
            return True
        else:
            print(f"❌ Health check failed with status: {response.status_code}")
            return False
    except requests.exceptions.RequestException as e:
        print(f"❌ Health check failed: {e}")
        return False

def test_generate_endpoint(base_url, question):
    """Test the generate endpoint with a question."""
    print(f"\n💭 Testing question: '{question}'")
    try:
        response = requests.post(
            f"{base_url}/api/generate",
            json={"question": question},
            headers={"Content-Type": "application/json"}
        )
        
        if response.status_code == 200:
            data = response.json()
            print("✅ Response received:")
            print(f"📝 Answer: {data['text'][:200]}{'...' if len(data['text']) > 200 else ''}")
            return True
        else:
            print(f"❌ Request failed with status: {response.status_code}")
            print(f"📄 Response: {response.text}")
            return False
    except requests.exceptions.RequestException as e:
        print(f"❌ Request failed: {e}")
        return False

def main():
    """Main test function."""
    base_url = "http://localhost:8000"
    
    print("🚀 FastAPI TextGen API Test Suite")
    print("=" * 50)
    
    # Test health endpoint
    if not test_health_endpoint(base_url):
        print("\n❌ Health check failed. Make sure the server is running.")
        print("💡 Start the server with: python dev_run.py")
        sys.exit(1)
    
    # Test questions
    test_questions = [
        "What is artificial intelligence?",
        "Explain Python programming in one sentence",
        "What are the benefits of renewable energy?",
    ]
    
    print(f"\n🧪 Testing {len(test_questions)} questions...")
    
    success_count = 0
    for question in test_questions:
        if test_generate_endpoint(base_url, question):
            success_count += 1
    
    print(f"\n📊 Test Results: {success_count}/{len(test_questions)} tests passed")
    
    if success_count == len(test_questions):
        print("🎉 All tests passed! Your API is working correctly.")
    else:
        print("⚠️  Some tests failed. Check your OpenAI API key and configuration.")

if __name__ == "__main__":
    main()