Spaces:
Sleeping
Sleeping
File size: 5,481 Bytes
33f5651 |
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 |
#!/usr/bin/env python3
"""
Test script for Mini RAG system
Run this to verify all components work before deployment
"""
import os
import sys
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def test_imports():
"""Test that all required modules can be imported"""
print("Testing imports...")
try:
from chunker import chunk_text
from llm import LLMProvider
from pinecone_client import PineconeClient
from rag_core import RAGCore
from ingest import load_documents
print("β
All imports successful")
return True
except ImportError as e:
print(f"β Import failed: {e}")
return False
def test_chunking():
"""Test text chunking functionality"""
print("\nTesting chunking...")
try:
from chunker import chunk_text
test_text = "This is a test document. " * 50 # Create long text
chunks = chunk_text(test_text, chunk_size=100, chunk_overlap=20)
if len(chunks) > 1:
print(f"β
Chunking works: {len(chunks)} chunks created")
return True
else:
print("β Chunking failed: expected multiple chunks")
return False
except Exception as e:
print(f"β Chunking test failed: {e}")
return False
def test_environment():
"""Test environment variable configuration"""
print("\nTesting environment variables...")
required_vars = ['PINECONE_API_KEY', 'OPENAI_API_KEY']
optional_vars = ['GROQ_API_KEY', 'COHERE_API_KEY']
missing_required = []
for var in required_vars:
if not os.getenv(var):
missing_required.append(var)
if missing_required:
print(f"β Missing required environment variables: {missing_required}")
print("Please set these in your .env file")
return False
print("β
Required environment variables set")
# Check optional variables
for var in optional_vars:
if os.getenv(var):
print(f"β
{var} is set")
else:
print(f"β οΈ {var} not set (optional)")
return True
def test_document_loading():
"""Test document loading functionality"""
print("\nTesting document loading...")
try:
from ingest import load_documents
# Check if data directory exists
data_dir = "./data"
if not os.path.exists(data_dir):
print(f"β οΈ Data directory {data_dir} not found")
return False
docs = load_documents(data_dir)
if docs:
print(f"β
Document loading works: {len(docs)} documents found")
for doc in docs:
print(f" - {doc['path']} ({len(doc['text'])} characters)")
return True
else:
print("β οΈ No documents found in data directory")
return False
except Exception as e:
print(f"β Document loading test failed: {e}")
return False
def test_llm_provider():
"""Test LLM provider initialization"""
print("\nTesting LLM provider...")
try:
from llm import LLMProvider
llm = LLMProvider()
print(f"β
LLM provider initialized: {llm.provider}")
print(f" - Embedding model: {llm.embedding_model}")
print(f" - LLM model: {llm.llm_model}")
print(f" - Reranker: {llm.rerank_provider}")
return True
except Exception as e:
print(f"β LLM provider test failed: {e}")
return False
def test_pinecone_client():
"""Test Pinecone client initialization"""
print("\nTesting Pinecone client...")
try:
from pinecone_client import PineconeClient
pc = PineconeClient()
print(f"β
Pinecone client initialized")
print(f" - Index: {pc.index_name}")
print(f" - Cloud: {pc.cloud}")
print(f" - Region: {pc.region}")
return True
except Exception as e:
print(f"β Pinecone client test failed: {e}")
return False
def test_rag_core():
"""Test RAG core initialization"""
print("\nTesting RAG core...")
try:
from rag_core import RAGCore
rag = RAGCore()
print("β
RAG core initialized")
return True
except Exception as e:
print(f"β RAG core test failed: {e}")
return False
def main():
"""Run all tests"""
print("π§ͺ Mini RAG System Test Suite")
print("=" * 40)
tests = [
test_imports,
test_environment,
test_chunking,
test_document_loading,
test_llm_provider,
test_pinecone_client,
test_rag_core,
]
passed = 0
total = len(tests)
for test in tests:
if test():
passed += 1
print("\n" + "=" * 40)
print(f"Test Results: {passed}/{total} tests passed")
if passed == total:
print("π All tests passed! System is ready for deployment.")
return True
else:
print("β οΈ Some tests failed. Please fix issues before deployment.")
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)
|