#!/usr/bin/env python3 """ Script to inspect the contents of the ChromaDB vector store. """ import sys import chromadb from pathlib import Path import logging # Add the app directory to the path to import settings sys.path.append(str(Path(__file__).parent.parent)) from app.core.config import settings # Configure logging to silence the specific ChromaDB telemetry error logging.basicConfig(level=logging.WARNING) logging.getLogger("chromadb.telemetry.product.posthog").setLevel(logging.CRITICAL) def inspect_database(): # Resolve path relative to project root to avoid "not found" errors db_path = Path(settings.chroma_persist_directory) if not db_path.is_absolute(): project_root = Path(__file__).resolve().parent.parent db_path = project_root / db_path print(f"šŸ” Connecting to database at: {db_path}") print(f"šŸ“‚ Target Collection name: {settings.collection_name}") print(f"šŸ“ Configured Chunk Size: {settings.chunk_size}") try: # Connect to Chroma client = chromadb.PersistentClient(path=str(db_path)) # List all collections to help debug mismatches existing_collections = client.list_collections() collection_names = [c.name for c in existing_collections] print(f"šŸ“š Found collections in DB: {collection_names}") if settings.collection_name not in collection_names: print(f"āŒ Error: Collection '{settings.collection_name}' not found in database.") if collection_names: print(f"šŸ’” Hint: Update 'collection_name' in app/core/config.py to '{collection_names[0]}'") return # Get the collection collection = client.get_collection(name=settings.collection_name) # Get data (limit to 20 chunks) data = collection.get(limit=20) count = collection.count() print(f"\nšŸ“Š Total chunks in database: {count}") print("=" * 60) if count == 0: print("āš ļø Database is empty.") return # Count chunks per source file (simple stats) all_metadatas = collection.get()['metadatas'] from collections import Counter sources = [m.get('filename', 'Unknown') for m in all_metadatas] print("šŸ“„ Documents breakdown:") for doc, num in Counter(sources).items(): print(f" - {doc}: {num} chunks") print("=" * 60) # Loop through the results for i in range(len(data['ids'])): print(f"šŸ†” ID: {data['ids'][i]}") source = data['metadatas'][i].get('filename', 'Unknown') print(f"šŸ“„ File: {source}") content_len = len(data['documents'][i]) print(f"šŸ“ Chunk Length: {content_len} characters") # Show more characters and replace newlines for cleaner view content_preview = data['documents'][i][:500].replace('\n', ' ') print(f"šŸ“ Content Preview: \"{content_preview}...\"") print("-" * 60) except Exception as e: print(f"āŒ Error inspecting database: {e}") if __name__ == "__main__": inspect_database()