-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtest_setup.py
More file actions
144 lines (113 loc) · 5.15 KB
/
test_setup.py
File metadata and controls
144 lines (113 loc) · 5.15 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
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
# Copyright 2024
# Directory: yt-rag/test_setup.py
"""
Test script to verify RAG backend setup is working correctly.
Run this after completing the setup steps.
"""
import asyncio
import sys
import os
from pathlib import Path
# Add the app directory to Python path
sys.path.insert(0, str(Path(__file__).parent))
async def test_setup():
"""Test the complete RAG setup."""
print("🧪 Testing RAG Backend Setup...")
print("=" * 50)
try:
# Test 1: Import modules
print("1️⃣ Testing imports...")
from app.core.config import get_settings
from app.core.database import db
from app.services.rag import rag_service
print(" ✅ All modules imported successfully")
# Test 2: Check configuration
print("\n2️⃣ Testing configuration...")
settings = get_settings()
# Check required environment variables
required_vars = [
'SUPABASE_URL', 'SUPABASE_ANON_KEY', 'SUPABASE_SERVICE_ROLE_KEY',
'OPENAI_API_KEY'
]
missing_vars = []
for var in required_vars:
if not os.getenv(var) or os.getenv(var) == f"your_{var.lower()}_here":
missing_vars.append(var)
if missing_vars:
print(f" ❌ Missing environment variables: {', '.join(missing_vars)}")
print(" 💡 Please update your .env file with real API keys")
return False
print(" ✅ Environment variables configured")
print(f" 📊 Using embedding model: {settings.openai_embed_model}")
print(f" 🤖 Using chat model: {settings.openai_chat_model}")
print(f" 🔗 Using AI provider: {settings.ai_provider}")
# Test 3: Database connection
print("\n3️⃣ Testing database connection...")
await db.connect()
health = await db.health_check()
if not health:
print(" ❌ Database connection failed")
print(" 💡 Check your Supabase credentials and ensure the project is active")
return False
print(" ✅ Database connection successful")
# Test 4: Schema validation
print("\n4️⃣ Testing database schema...")
await db.initialize_schema()
print(" ✅ Database schema validated")
# Test 5: Seeding documents
print("\n5️⃣ Testing document seeding...")
inserted_count = await rag_service.seed_documents()
if inserted_count == 0:
print(" ⚠️ No new documents inserted (may already exist)")
else:
print(f" ✅ Successfully seeded {inserted_count} document chunks")
# Test 6: RAG query
print("\n6️⃣ Testing RAG query...")
test_query = "Can I return shoes after 30 days?"
result = await rag_service.answer_query(test_query)
if not result['text'] or "error" in result['text'].lower():
print(" ❌ RAG query failed")
print(f" 🔍 Response: {result['text'][:100]}...")
return False
print(" ✅ RAG query successful!")
print(f" 📝 Answer: {result['text'][:100]}...")
print(f" 📚 Citations: {result['citations']}")
print(f" ⏱️ Latency: {result['debug']['latency_ms']}ms")
# Cleanup
await db.disconnect()
print("\n🎉 ALL TESTS PASSED!")
print("=" * 50)
print("✅ Your RAG backend is fully functional!")
print("🚀 You can now start the server with: uvicorn main:app --reload --port 8000")
print("📚 Visit http://localhost:8000/docs for interactive API documentation")
return True
except ImportError as e:
print(f" ❌ Import error: {e}")
print(" 💡 Make sure you've installed dependencies: pip install -r requirements.txt")
return False
except Exception as e:
print(f" ❌ Setup test failed: {e}")
print(" 💡 Check the error message above and refer to QUICKSTART.md")
return False
async def main():
"""Main test function."""
print("🔧 RAG Backend Setup Verification")
print("This will test your complete setup without starting the server.\n")
# Load environment variables
from dotenv import load_dotenv
load_dotenv()
success = await test_setup()
if success:
print("\n🎯 Next Steps:")
print("1. Start the server: uvicorn main:app --reload --port 8000")
print("2. Test the health endpoint: curl http://localhost:8000/healthz")
print("3. Ask a question: curl -X POST http://localhost:8000/answer -H 'Content-Type: application/json' -d '{\"query\":\"What is your return policy?\"}'")
print("4. Visit the interactive docs: http://localhost:8000/docs")
sys.exit(0)
else:
print("\n❌ Setup incomplete. Please fix the issues above and try again.")
print("📖 Refer to QUICKSTART.md for detailed setup instructions.")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())