File size: 1,791 Bytes
5263a14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()