Spaces:
Sleeping
Sleeping
File size: 15,780 Bytes
ff040f1 | 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | """
Test script for FastAPI endpoints in the RAG Chat API.
This script tests all available endpoints with various scenarios.
"""
import requests
import json
import os
import sys
import tempfile
from typing import Dict, Any
import time
# Add backend to path for imports
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'backend'))
class RAGAPITester:
def __init__(self, base_url: str = "http://localhost:7860"):
self.base_url = base_url
self.session = requests.Session()
self.api_key = None
def test_connection(self) -> bool:
"""Test if the API server is running."""
try:
response = self.session.get(f"{self.base_url}/")
return response.status_code in [200, 404] # 404 is OK if no frontend file
except requests.ConnectionError:
return False
def set_api_key(self, api_key: str) -> Dict[str, Any]:
"""Test setting the GROQ API key."""
print("Testing API key setting...")
url = f"{self.base_url}/set-api-key"
payload = {"api_key": api_key}
response = self.session.post(url, json=payload)
result = {
"endpoint": "/set-api-key",
"status_code": response.status_code,
"response": response.json() if response.status_code != 500 else response.text,
"success": response.status_code == 200
}
if result["success"]:
self.api_key = api_key
print("β API key set successfully")
else:
print(f"β Failed to set API key: {result['response']}")
return result
def test_get_stats(self) -> Dict[str, Any]:
"""Test getting processing statistics."""
print("Testing stats endpoint...")
url = f"{self.base_url}/stats"
response = self.session.get(url)
result = {
"endpoint": "/stats",
"status_code": response.status_code,
"response": response.json() if response.status_code == 200 else response.text,
"success": response.status_code == 200
}
if result["success"]:
print("β Stats retrieved successfully")
else:
print(f"β Failed to get stats: {result['response']}")
return result
def test_get_chat_history(self) -> Dict[str, Any]:
"""Test getting chat history."""
print("Testing chat history endpoint...")
url = f"{self.base_url}/chat-history"
response = self.session.get(url)
result = {
"endpoint": "/chat-history",
"status_code": response.status_code,
"response": response.json() if response.status_code == 200 else response.text,
"success": response.status_code == 200
}
if result["success"]:
print("β Chat history retrieved successfully")
else:
print(f"β Failed to get chat history: {result['response']}")
return result
def test_upload_files(self, file_paths: list = None) -> Dict[str, Any]:
"""Test file upload endpoint."""
print("Testing file upload endpoint...")
url = f"{self.base_url}/upload-files"
# Create test files if none provided
if not file_paths:
test_files = self._create_test_files()
file_paths = test_files
files = []
try:
for file_path in file_paths:
files.append(('files', (os.path.basename(file_path), open(file_path, 'rb'))))
response = self.session.post(url, files=files)
result = {
"endpoint": "/upload-files",
"status_code": response.status_code,
"response": response.json() if response.status_code in [200, 400] else response.text,
"success": response.status_code == 200
}
if result["success"]:
print("β Files uploaded and processed successfully")
else:
print(f"β Failed to upload files: {result['response']}")
finally:
# Close file handles
for _, (_, file_handle) in files:
file_handle.close()
# Clean up test files if we created them
if not file_paths or file_paths == getattr(self, '_test_files', []):
self._cleanup_test_files()
return result
def test_process_directory(self, directory_path: str = None) -> Dict[str, Any]:
"""Test directory processing endpoint."""
print("Testing directory processing endpoint...")
url = f"{self.base_url}/process-directory"
# Use a test directory if none provided
if not directory_path:
directory_path = self._create_test_directory()
payload = {"directory_path": directory_path}
response = self.session.post(url, data=payload)
result = {
"endpoint": "/process-directory",
"status_code": response.status_code,
"response": response.json() if response.status_code in [200, 400] else response.text,
"success": response.status_code == 200
}
if result["success"]:
print("β Directory processed successfully")
else:
print(f"β Failed to process directory: {result['response']}")
return result
def test_chat(self, message: str = "What is the main topic of the documents?") -> Dict[str, Any]:
"""Test chat endpoint."""
print("Testing chat endpoint...")
url = f"{self.base_url}/chat"
payload = {"message": message}
response = self.session.post(url, json=payload)
result = {
"endpoint": "/chat",
"status_code": response.status_code,
"response": response.json() if response.status_code in [200, 400] else response.text,
"success": response.status_code == 200
}
if result["success"]:
print("β Chat response received successfully")
else:
print(f"β Chat failed: {result['response']}")
return result
def test_save_vector_store(self) -> Dict[str, Any]:
"""Test saving vector store."""
print("Testing save vector store endpoint...")
url = f"{self.base_url}/save-vector-store"
response = self.session.post(url)
result = {
"endpoint": "/save-vector-store",
"status_code": response.status_code,
"response": response.json() if response.status_code in [200, 400] else response.text,
"success": response.status_code == 200
}
if result["success"]:
print("β Vector store saved successfully")
else:
print(f"β Failed to save vector store: {result['response']}")
return result
def test_load_vector_store(self) -> Dict[str, Any]:
"""Test loading vector store."""
print("Testing load vector store endpoint...")
url = f"{self.base_url}/load-vector-store"
response = self.session.post(url)
result = {
"endpoint": "/load-vector-store",
"status_code": response.status_code,
"response": response.json() if response.status_code in [200, 400] else response.text,
"success": response.status_code == 200
}
if result["success"]:
print("β Vector store loaded successfully")
else:
print(f"β Failed to load vector store: {result['response']}")
return result
def test_clear_chat(self) -> Dict[str, Any]:
"""Test clearing chat history."""
print("Testing clear chat endpoint...")
url = f"{self.base_url}/clear-chat"
response = self.session.delete(url)
result = {
"endpoint": "/clear-chat",
"status_code": response.status_code,
"response": response.json() if response.status_code == 200 else response.text,
"success": response.status_code == 200
}
if result["success"]:
print("β Chat history cleared successfully")
else:
print(f"β Failed to clear chat: {result['response']}")
return result
def _create_test_files(self) -> list:
"""Create temporary test files for upload testing."""
temp_dir = tempfile.mkdtemp()
self._temp_dir = temp_dir
test_files = []
# Create a text file
txt_file = os.path.join(temp_dir, "test_document.txt")
with open(txt_file, 'w') as f:
f.write("""
This is a test document for the RAG system.
It contains information about artificial intelligence and machine learning.
Machine learning is a subset of artificial intelligence that focuses on
algorithms that can learn and improve from experience without being
explicitly programmed.
Key concepts in machine learning include:
- Supervised learning
- Unsupervised learning
- Reinforcement learning
- Neural networks
- Deep learning
""")
test_files.append(txt_file)
# Create a markdown file
md_file = os.path.join(temp_dir, "test_readme.md")
with open(md_file, 'w') as f:
f.write("""
# Test README
This is a test markdown document for testing the RAG system.
## About AI
Artificial Intelligence (AI) is the simulation of human intelligence
processes by machines, especially computer systems.
## Applications
- Natural Language Processing
- Computer Vision
- Robotics
- Expert Systems
""")
test_files.append(md_file)
self._test_files = test_files
return test_files
def _create_test_directory(self) -> str:
"""Create a test directory with files."""
if not hasattr(self, '_temp_dir'):
self._create_test_files()
return self._temp_dir
def _cleanup_test_files(self):
"""Clean up temporary test files."""
if hasattr(self, '_temp_dir') and os.path.exists(self._temp_dir):
import shutil
shutil.rmtree(self._temp_dir)
def run_all_tests(self, api_key: str = None) -> Dict[str, Any]:
"""Run all available tests."""
print("="*60)
print("Starting RAG API Endpoint Tests")
print("="*60)
results = {}
# Test connection
if not self.test_connection():
print("β Server is not running! Please start the FastAPI server first.")
return {"error": "Server not available"}
print("β Server is running")
print("-"*40)
# Set API key if provided
if api_key:
results["set_api_key"] = self.set_api_key(api_key)
print("-"*40)
# Test basic endpoints
results["stats"] = self.test_get_stats()
print("-"*40)
results["chat_history"] = self.test_get_chat_history()
print("-"*40)
# Test file upload (this will also process documents)
results["upload_files"] = self.test_upload_files()
print("-"*40)
# Test chat (only if upload was successful)
if results["upload_files"]["success"]:
results["chat"] = self.test_chat()
print("-"*40)
# Test vector store operations
results["save_vector_store"] = self.test_save_vector_store()
print("-"*40)
# Test loading vector store
results["load_vector_store"] = self.test_load_vector_store()
print("-"*40)
# Test clearing chat
results["clear_chat"] = self.test_clear_chat()
print("-"*40)
# Test directory processing
results["process_directory"] = self.test_process_directory()
print("-"*40)
# Summary
print("="*60)
print("Test Summary:")
print("="*60)
total_tests = 0
passed_tests = 0
for test_name, result in results.items():
if isinstance(result, dict) and "success" in result:
total_tests += 1
if result["success"]:
passed_tests += 1
print(f"β {test_name}: PASSED")
else:
print(f"β {test_name}: FAILED")
print(f"\nTotal: {passed_tests}/{total_tests} tests passed")
return results
def main():
"""Main function to run the tests."""
import argparse
parser = argparse.ArgumentParser(description="Test RAG API endpoints")
parser.add_argument("--url", default="http://localhost:7860",
help="Base URL of the API server")
parser.add_argument("--api-key",
help="GROQ API key for testing")
parser.add_argument("--test", choices=[
"all", "connection", "api-key", "stats", "chat-history",
"upload", "chat", "save-store", "load-store", "clear-chat", "directory"
], default="all", help="Specific test to run")
args = parser.parse_args()
tester = RAGAPITester(args.url)
if args.test == "all":
results = tester.run_all_tests(args.api_key)
elif args.test == "connection":
success = tester.test_connection()
print(f"Connection test: {'β PASSED' if success else 'β FAILED'}")
elif args.test == "api-key":
if not args.api_key:
print("Please provide --api-key for this test")
return
result = tester.set_api_key(args.api_key)
print(f"API key test: {'β PASSED' if result['success'] else 'β FAILED'}")
elif args.test == "stats":
result = tester.test_get_stats()
print(f"Stats test: {'β PASSED' if result['success'] else 'β FAILED'}")
elif args.test == "chat-history":
result = tester.test_get_chat_history()
print(f"Chat history test: {'β PASSED' if result['success'] else 'β FAILED'}")
elif args.test == "upload":
result = tester.test_upload_files()
print(f"Upload test: {'β PASSED' if result['success'] else 'β FAILED'}")
elif args.test == "chat":
result = tester.test_chat()
print(f"Chat test: {'β PASSED' if result['success'] else 'β FAILED'}")
elif args.test == "save-store":
result = tester.test_save_vector_store()
print(f"Save store test: {'β PASSED' if result['success'] else 'β FAILED'}")
elif args.test == "load-store":
result = tester.test_load_vector_store()
print(f"Load store test: {'β PASSED' if result['success'] else 'β FAILED'}")
elif args.test == "clear-chat":
result = tester.test_clear_chat()
print(f"Clear chat test: {'β PASSED' if result['success'] else 'β FAILED'}")
elif args.test == "directory":
result = tester.test_process_directory()
print(f"Directory test: {'β PASSED' if result['success'] else 'β FAILED'}")
if __name__ == "__main__":
main()
|