Spaces:
Sleeping
Sleeping
Add: implement quick test for embedding pipeline and document cleaner
Browse files- quick_test.py +86 -0
quick_test.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Quick test to verify the updated embedding pipeline works correctly."""
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
sys.path.insert(0, os.getcwd())
|
| 5 |
+
|
| 6 |
+
from langchain_core.documents import Document
|
| 7 |
+
from src.embedding import EmbeddingPipeline
|
| 8 |
+
from src.vectorstore import FaissVectorStore
|
| 9 |
+
from src.cleaner import DocumentCleaner
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
def test_pipeline():
|
| 13 |
+
print("=" * 60)
|
| 14 |
+
print("QUICK PIPELINE VERIFICATION TEST")
|
| 15 |
+
print("=" * 60)
|
| 16 |
+
|
| 17 |
+
# 1. Test DocumentCleaner
|
| 18 |
+
print("\n--- Test 1: DocumentCleaner ---")
|
| 19 |
+
cleaner = DocumentCleaner(min_length=10)
|
| 20 |
+
|
| 21 |
+
test_docs = [
|
| 22 |
+
Document(page_content="This is a valid document with real content about databases."),
|
| 23 |
+
Document(page_content="Short"), # too short
|
| 24 |
+
Document(page_content=""), # empty
|
| 25 |
+
Document(page_content=None), # None - this would crash old code
|
| 26 |
+
Document(page_content="Another valid document explaining SQL queries and joins."),
|
| 27 |
+
Document(page_content=" \n\n\t "), # whitespace only
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
cleaned = cleaner.clean_documents(test_docs)
|
| 31 |
+
print(f" Input: {len(test_docs)} docs -> Cleaned: {len(cleaned)} docs")
|
| 32 |
+
assert len(cleaned) == 2, f"Expected 2 valid docs, got {len(cleaned)}"
|
| 33 |
+
print(" โ
DocumentCleaner works correctly!")
|
| 34 |
+
|
| 35 |
+
# 2. Test EmbeddingPipeline with cleaning + batching
|
| 36 |
+
print("\n--- Test 2: EmbeddingPipeline (clean + chunk + embed) ---")
|
| 37 |
+
|
| 38 |
+
# Create realistic documents like what PyPDFLoader returns
|
| 39 |
+
docs = [
|
| 40 |
+
Document(page_content="Database Management Systems provide an organized way to store and manage data. " * 5),
|
| 41 |
+
Document(page_content="SQL is a standard language for accessing and manipulating databases. " * 5),
|
| 42 |
+
Document(page_content=None), # Simulates a bad PDF page
|
| 43 |
+
Document(page_content="Machine Learning is a branch of artificial intelligence focused on algorithms. " * 5),
|
| 44 |
+
Document(page_content=""), # Empty page
|
| 45 |
+
Document(page_content="x"), # Too short - should be cleaned
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
pipe = EmbeddingPipeline(chunk_size=200, chunk_overlap=50)
|
| 49 |
+
chunks = pipe.chunk_documents(docs)
|
| 50 |
+
print(f" Chunks created: {len(chunks)}")
|
| 51 |
+
|
| 52 |
+
embeddings, valid_chunks = pipe.embed_chunks(chunks, batch_size=2)
|
| 53 |
+
print(f" Embeddings shape: {embeddings.shape}")
|
| 54 |
+
print(f" Valid chunks: {len(valid_chunks)}")
|
| 55 |
+
assert embeddings.shape[0] == len(valid_chunks), "Embeddings and chunks count must match!"
|
| 56 |
+
assert embeddings.shape[0] > 0, "Should have some embeddings!"
|
| 57 |
+
print(" โ
EmbeddingPipeline works correctly!")
|
| 58 |
+
|
| 59 |
+
# 3. Test FaissVectorStore integration
|
| 60 |
+
print("\n--- Test 3: FaissVectorStore build + query ---")
|
| 61 |
+
test_store_dir = "test_faiss_store"
|
| 62 |
+
store = FaissVectorStore(persist_dir=test_store_dir, chunk_size=200, chunk_overlap=50)
|
| 63 |
+
store.build_from_documents(docs)
|
| 64 |
+
|
| 65 |
+
if store.index is not None:
|
| 66 |
+
results = store.query("What is a database?", top_k=2)
|
| 67 |
+
print(f" Query returned {len(results)} results")
|
| 68 |
+
for r in results:
|
| 69 |
+
snippet = r['metadata']['texts'][:80] if r.get('metadata') and r['metadata'].get('texts') else "None"
|
| 70 |
+
print(f" Distance: {r['distance']:.4f} | {snippet}...")
|
| 71 |
+
print(" โ
FaissVectorStore works correctly!")
|
| 72 |
+
else:
|
| 73 |
+
print(" โ ๏ธ Vector store index is empty")
|
| 74 |
+
|
| 75 |
+
# Cleanup
|
| 76 |
+
import shutil
|
| 77 |
+
if os.path.exists(test_store_dir):
|
| 78 |
+
shutil.rmtree(test_store_dir)
|
| 79 |
+
|
| 80 |
+
print("\n" + "=" * 60)
|
| 81 |
+
print("๐ ALL TESTS PASSED! Pipeline is working correctly.")
|
| 82 |
+
print("=" * 60)
|
| 83 |
+
print("\nYou can now run: python main.py")
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
test_pipeline()
|