chatdoc / scripts\inspect_db.py
celiolr's picture
Deploy Docker: scripts\inspect_db.py
4420cb1 verified
Raw History Blame Contribute Delete
3.24 kB
#!/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()