-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_upload.py
More file actions
133 lines (113 loc) Β· 5.05 KB
/
Copy pathtest_upload.py
File metadata and controls
133 lines (113 loc) Β· 5.05 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
"""
Simple test to upload a new document and verify text extraction works.
"""
import asyncio
import aiohttp
import json
# Configuration
BASE_URL = "http://localhost:8080"
JWT_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3NTU2Njk2NDYsImV4cCI6MTc1NTY3MzI0Niwic3ViIjoiYWU1NDJkMDctZTliMi00ZjMxLWIxMTYtZWE1MjA4MWVlNmI2IiwidGVuYW50IjoibWFjYm9vayIsImVtYWlsIjoic2F1cmFiaGtqaGE5ODExQGdtYWlsLmNvbSIsInJvbGUiOiJST0xFX0FETUlOIn0.SGg-MPhpRw2w8ozxAJT04brBQWKke2oA0BGpINrwolI"
async def test_upload():
"""Test uploading a new document."""
headers = {
"Authorization": f"Bearer {JWT_TOKEN}"
}
print("π Testing Document Upload with Fixed Text Extraction")
print("=" * 60)
# First, get categories
print("\n1οΈβ£ Getting categories...")
try:
async with aiohttp.ClientSession() as session:
async with session.get(f"{BASE_URL}/rag/categories", headers=headers) as response:
if response.status == 200:
categories = await response.json()
category_id = categories[0]['id'] if categories else None
print(f"β
Using category: {categories[0]['name']}")
else:
print(f"β Failed to get categories: {response.status}")
return
except Exception as e:
print(f"β Error: {e}")
return
# Create a simple text file for testing
test_content = """
This is a test document about Autonomy in foreign/second language learning.
Autonomy in language learning refers to the learner's ability to take charge of their own learning process.
It involves making decisions about what to learn, how to learn it, and when to learn it.
Key aspects of autonomy include:
1. Self-direction in learning
2. Motivation and goal-setting
3. Self-assessment and reflection
4. Independent learning strategies
Research shows that autonomous learners are more successful in acquiring foreign languages.
"""
# Create a simple text file
with open("test_document.txt", "w") as f:
f.write(test_content)
print("\n2οΈβ£ Uploading test document...")
try:
data = aiohttp.FormData()
data.add_field('file',
open('test_document.txt', 'rb'),
filename='test_document.txt',
content_type='text/plain')
data.add_field('category_id', category_id)
async with aiohttp.ClientSession() as session:
async with session.post(f"{BASE_URL}/rag/upload", headers=headers, data=data) as response:
if response.status == 200:
result = await response.json()
print(f"β
Upload successful: {result['message']}")
document_id = result['id']
else:
print(f"β Upload failed: {response.status}")
error_text = await response.text()
print(f" Error: {error_text}")
return
except Exception as e:
print(f"β Error uploading: {e}")
return
# Wait a bit for processing
print("\n3οΈβ£ Waiting for document processing...")
await asyncio.sleep(5)
# Check document status
print("\n4οΈβ£ Checking document status...")
try:
async with aiohttp.ClientSession() as session:
async with session.get(f"{BASE_URL}/rag/documents/{document_id}/status", headers=headers) as response:
if response.status == 200:
status = await response.json()
print(f"β
Document status: {status['status']}")
else:
print(f"β Failed to get status: {response.status}")
except Exception as e:
print(f"β Error checking status: {e}")
# Test the chat with the new document
print("\n5οΈβ£ Testing chat with new document...")
try:
chat_data = {
"query": "What is autonomy in language learning?",
"top_k": 5,
"model": "google"
}
async with aiohttp.ClientSession() as session:
async with session.post(f"{BASE_URL}/rag/chat", headers=headers, json=chat_data) as response:
if response.status == 200:
result = await response.json()
print(f"β
Chat successful!")
print(f" Response: {result['response'][:200]}...")
print(f" Sources: {result['total_sources']}")
else:
print(f"β Chat failed: {response.status}")
error_text = await response.text()
print(f" Error: {error_text}")
except Exception as e:
print(f"β Error in chat: {e}")
# Clean up
import os
if os.path.exists("test_document.txt"):
os.remove("test_document.txt")
print("\n" + "=" * 60)
print("π Upload Test Complete!")
if __name__ == "__main__":
asyncio.run(test_upload())