#!/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()