dutch_test_assistant / scripts /setup_vector_db.py
bmayfield's picture
Preparing for Hugging Face deployment
c40af6d
Raw History Blame Contribute Delete
3.08 kB
"""
One-time setup script to process PDF documents and initialize vector database.
Run this after adding your PDF documents to knowledge_base/pdfs/
"""
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from app.dutch_chatbot.document_processor import DocumentProcessor
from app.dutch_chatbot.vector_store import VectorStore
from app.dutch_chatbot.config import Config
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def main():
print("🧬 DUTCH Test Assistant Vector Database Setup")
print("=" * 50)
try:
config = Config()
config.validate()
except ValueError as e:
print(f"❌ Configuration error: {e}")
print("Please check your .env file and ensure ANTHROPIC_API_KEY is set.")
return
# Check if PDF directory exists
if not os.path.exists(config.PDF_DATA_PATH):
print(f"❌ PDF directory not found: {config.PDF_DATA_PATH}")
print("Please create the directory and add your medical test PDF documents.")
return
# Check for PDF files
pdf_files = [f for f in os.listdir(config.PDF_DATA_PATH) if f.lower().endswith('.pdf')]
if not pdf_files:
print(f"❌ No PDF files found in {config.PDF_DATA_PATH}")
print("Please add your medical test PDF documents to the directory.")
print("Or run: uv run python scripts/add_sample_data.py to create sample documents")
return
print(f"πŸ“„ Found {len(pdf_files)} PDF files:")
for pdf in pdf_files:
print(f" - {pdf}")
# Initialize components
print("\nπŸ”„ Processing documents...")
processor = DocumentProcessor()
# Process all PDFs
chunks = processor.process_all_pdfs()
if not chunks:
print("❌ No text chunks created. Please check your PDF files.")
return
# Save processed chunks
processor.save_processed_chunks(chunks)
# Initialize vector store
print("\nπŸ”„ Setting up vector database...")
vector_store = VectorStore()
vector_store.create_collection()
# Add documents to vector database
vector_store.add_documents(chunks)
# Verify setup
info = vector_store.get_collection_info()
print(f"\nβœ… Setup complete!")
print(f"πŸ“Š Vector database info:")
print(f" - Documents: {info.get('document_count', 'Unknown')}")
print(f" - Collection: {info.get('collection_name', 'Unknown')}")
# Test search
print("\nπŸ§ͺ Testing search functionality...")
test_query = "blood test"
results = vector_store.search(test_query, top_k=2)
print(f"Test query: '{test_query}'")
print(f"Found {len(results)} relevant documents")
if results:
print("Sample result:")
print(f" Source: {results[0]['metadata'].get('source', 'Unknown')}")
print(f" Text preview: {results[0]['text'][:100]}...")
print("\nπŸŽ‰ Ready to run the DUTCH Test Assistant!")
print("Run: uv run python app/interfaces/gradio_app.py")
if __name__ == "__main__":
main()