Spaces:
Build error
Build error
Download scripts/test_e2e.py from AIenthusSP/RAG-Documind-AI: direct link, hf CLI and curl.
- Browser
- Download file 9.2 kB
-
https://huggingface.co/spaces/AIenthusSP/RAG-Documind-AI/resolve/main/scripts/test_e2e.py
- Command line
-
hf download hf://spaces/AIenthusSP/RAG-Documind-AI/scripts/test_e2e.py
-
curl -L -o test_e2e.py https://huggingface.co/spaces/AIenthusSP/RAG-Documind-AI/resolve/main/scripts/test_e2e.py
9.2 kB
| """ | |
| End-to-End Test Script for RAG Pipeline | |
| This script demonstrates and tests the complete RAG pipeline: | |
| 1. Document loading | |
| 2. Chunking | |
| 3. Embedding and storage | |
| 4. Retrieval | |
| 5. Generation with LLM | |
| Run this script to verify the entire pipeline works correctly. | |
| """ | |
| from src.rag.loaders import MultiFormatDocumentLoader | |
| from src.rag.chunking import DocumentChunker | |
| from src.rag.embeddings import get_embeddings, EmbeddingsManager | |
| from src.rag.vectorstore import create_vector_store | |
| from src.rag.retrieval import Retriever | |
| from src.rag.llm import LLMManager | |
| from src.rag.pipeline import create_rag_pipeline | |
| import sys | |
| import logging | |
| from pathlib import Path | |
| # Add project root to path | |
| project_root = Path(__file__).parent.parent | |
| sys.path.insert(0, str(project_root)) | |
| # Configure logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
| ) | |
| logger = logging.getLogger(__name__) | |
| def print_section(title: str): | |
| """Print a section header.""" | |
| print("\n" + "=" * 60) | |
| print(f" {title}") | |
| print("=" * 60) | |
| def test_document_loading(): | |
| """Test document loading functionality.""" | |
| print_section("1. Testing Document Loading") | |
| loader = MultiFormatDocumentLoader() | |
| # Test loading sample documents | |
| sample_dir = project_root / "data" / "sample_docs" | |
| if not sample_dir.exists(): | |
| print("β οΈ Sample documents directory not found. Creating sample docs...") | |
| sample_dir.mkdir(parents=True, exist_ok=True) | |
| # Create a sample document | |
| sample_file = sample_dir / "sample.txt" | |
| sample_file.write_text(""" | |
| RAG (Retrieval-Augmented Generation) Documentation | |
| What is RAG? | |
| RAG is a technique that combines information retrieval with text generation. | |
| It retrieves relevant documents from a knowledge base and uses them as context | |
| for generating accurate, grounded responses. | |
| Key Components: | |
| 1. Document Loader - Ingests documents from various sources | |
| 2. Chunker - Splits documents into smaller pieces | |
| 3. Embeddings - Converts text to vector representations | |
| 4. Vector Store - Stores and indexes embeddings for fast retrieval | |
| 5. Retriever - Finds relevant documents for a query | |
| 6. LLM - Generates responses based on retrieved context | |
| Benefits of RAG: | |
| - Reduces hallucination by grounding responses in real documents | |
| - Enables up-to-date knowledge without retraining | |
| - Provides traceable sources for generated answers | |
| - Cost-effective compared to fine-tuning | |
| Refund Policy: | |
| Our refund policy allows returns within 30 days of purchase. | |
| All refunds are processed within 7 business days. | |
| Items must be in original condition with receipt. | |
| """) | |
| print(f"β Created sample document: {sample_file}") | |
| # Load documents | |
| documents = loader.load_directory(sample_dir) | |
| print(f"β Loaded {len(documents)} document(s)") | |
| for doc in documents: | |
| print(f" - {doc.metadata.get('file_name', 'unknown')}: {len(doc.page_content)} chars") | |
| return documents | |
| def test_chunking(documents): | |
| """Test document chunking.""" | |
| print_section("2. Testing Document Chunking") | |
| chunker = DocumentChunker(chunk_size=500, chunk_overlap=50) | |
| chunks = chunker.chunk_documents(documents) | |
| print(f"β Created {len(chunks)} chunks") | |
| stats = chunker.get_chunk_stats(chunks) | |
| print(f" - Average chunk size: {stats['avg_chunk_size']:.0f} chars") | |
| print(f" - Min chunk size: {stats['min_chunk_size']} chars") | |
| print(f" - Max chunk size: {stats['max_chunk_size']} chars") | |
| return chunks | |
| def test_embeddings(): | |
| """Test embedding functionality.""" | |
| print_section("3. Testing Embeddings") | |
| manager = EmbeddingsManager() | |
| embeddings = manager.get_embeddings() | |
| # Test embedding a sample text | |
| sample_text = "What is RAG?" | |
| vector = manager.embed_text(sample_text) | |
| print(f"β Embedding model loaded: {manager.model_name}") | |
| print(f" - Vector dimensions: {len(vector)}") | |
| print(f" - Sample vector (first 5): {vector[:5]}") | |
| return embeddings | |
| def test_vector_store(chunks, embeddings): | |
| """Test vector store operations.""" | |
| print_section("4. Testing Vector Store") | |
| # Create vector store with test collection | |
| store = create_vector_store( | |
| embedding_function=embeddings, | |
| collection_name="test_collection" | |
| ) | |
| # Add documents | |
| ids = store.add_documents(chunks) | |
| print(f"β Added {len(ids)} chunks to vector store") | |
| # Get stats | |
| stats = store.get_collection_stats() | |
| print(f" - Collection: {stats['collection_name']}") | |
| print(f" - Document count: {stats['document_count']}") | |
| return store | |
| def test_retrieval(store): | |
| """Test retrieval functionality.""" | |
| print_section("5. Testing Retrieval") | |
| retriever = Retriever(vector_store=store, default_k=3) | |
| # Test queries | |
| test_queries = [ | |
| "What is RAG?", | |
| "What is the refund policy?", | |
| "What are the key components?", | |
| ] | |
| for query in test_queries: | |
| result = retriever.retrieve(query) | |
| print(f"\nπ Query: '{query}'") | |
| print(f" Retrieved {result.num_results} documents") | |
| if result.scores: | |
| print(f" Top score: {result.scores[0]:.4f}") | |
| print(f" Context preview: {result.get_context()[:100]}...") | |
| return retriever | |
| def test_llm(): | |
| """Test LLM functionality.""" | |
| print_section("6. Testing LLM") | |
| try: | |
| manager = LLMManager() | |
| info = manager.get_model_info() | |
| if not info.get("api_key_configured"): | |
| print("β οΈ No API key configured. Set GOOGLE_API_KEY in .env") | |
| print(" Skipping LLM generation test...") | |
| return None | |
| print(f"β LLM configured: {info['model_name']}") | |
| print(f" - Temperature: {info['temperature']}") | |
| # Test simple generation | |
| response = manager.generate("Say 'Hello, RAG!' in exactly 3 words.") | |
| print(f" - Test response: {response[:50]}...") | |
| return manager | |
| except Exception as e: | |
| print(f"β οΈ LLM test failed: {e}") | |
| return None | |
| def test_rag_pipeline(llm_available=True): | |
| """Test complete RAG pipeline.""" | |
| print_section("7. Testing Complete RAG Pipeline") | |
| if not llm_available: | |
| print("β οΈ Skipping RAG pipeline test (no LLM available)") | |
| return | |
| try: | |
| # Create pipeline | |
| pipeline = create_rag_pipeline(collection_name="test_collection") | |
| print("β RAG Pipeline created") | |
| info = pipeline.get_pipeline_info() | |
| print(f" - LLM: {info['llm_model']}") | |
| print(f" - Top K: {info['top_k']}") | |
| # Test query | |
| question = "What is RAG and what are its benefits?" | |
| print(f"\nπ Testing query: '{question}'") | |
| result = pipeline.query(question, include_sources=True) | |
| print("\n㪠Answer:") | |
| print(f" {result['answer'][:300]}...") | |
| if result.get("sources"): | |
| print(f"\nπ Sources ({result['num_sources']}):") | |
| for source in result["sources"][:2]: | |
| print(f" - {source['metadata'].get('file_name', 'unknown')}") | |
| return True | |
| except Exception as e: | |
| print(f"β RAG pipeline test failed: {e}") | |
| return False | |
| def cleanup_test_collection(): | |
| """Clean up test collection.""" | |
| print_section("8. Cleanup") | |
| try: | |
| embeddings = get_embeddings() | |
| store = create_vector_store( | |
| embedding_function=embeddings, | |
| collection_name="test_collection" | |
| ) | |
| store.delete_collection() | |
| print("β Test collection cleaned up") | |
| except Exception as e: | |
| print(f"β οΈ Cleanup warning: {e}") | |
| def main(): | |
| """Run all end-to-end tests.""" | |
| print("\n" + "=" * 60) | |
| print(" π RAG Pipeline End-to-End Test Suite") | |
| print("=" * 60) | |
| try: | |
| # Run tests in order | |
| documents = test_document_loading() | |
| chunks = test_chunking(documents) | |
| embeddings = test_embeddings() | |
| store = test_vector_store(chunks, embeddings) | |
| test_retrieval(store) | |
| llm = test_llm() | |
| if llm: | |
| success = test_rag_pipeline(llm_available=True) | |
| else: | |
| test_rag_pipeline(llm_available=False) | |
| success = True # Partial success | |
| # Cleanup | |
| cleanup_test_collection() | |
| # Summary | |
| print_section("Test Summary") | |
| if success: | |
| print("β All tests passed!") | |
| print("\nπ Phase 1 Complete: Core RAG Pipeline is working!") | |
| else: | |
| print("β οΈ Some tests skipped (LLM not configured)") | |
| print(" Configure GOOGLE_API_KEY in .env to enable full testing") | |
| return 0 | |
| except Exception as e: | |
| print(f"\nβ Test failed with error: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |