import requests import os BASE_URL = "http://127.0.0.1:7860" def test_api(): print("--- Starting API Test ---") # 1. Create a dummy file test_file = "api_test_doc.txt" with open(test_file, "w") as f: f.write("The API secret is 99999. Do not share this.") conversation_id = "api_chat_1" try: # 2. Upload File print(f"\n1. Uploading to {conversation_id}...") with open(test_file, "rb") as f: files = {"file": f} data = {"conversation_id": conversation_id} response = requests.post(f"{BASE_URL}/api/upload", files=files, data=data) print(f"Upload Status: {response.status_code}") print(f"Upload Response: {response.json()}") if response.status_code != 200: print("❌ Upload Failed") return # 3. Chat (Ask about the file) print(f"\n2. Asking about the file in {conversation_id}...") chat_data = { "message": "What is the API secret?", "history": [], "conversation_id": conversation_id, "user_id": "test_user" } response = requests.post(f"{BASE_URL}/api/chat", json=chat_data) print(f"Chat Status: {response.status_code}") result = response.json() print(f"Chat Response: {result.get('response')}") if "99999" in result.get('response', ''): print("✅ Success: AI found the secret!") else: print("❌ Failure: AI did not find the secret.") except Exception as e: print(f"Test Failed: {e}") finally: if os.path.exists(test_file): os.remove(test_file) if __name__ == "__main__": test_api()