Download scripts/inspect_db.py from aionaion/chatdoc: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/aionaion/chatdoc/resolve/main/scripts/inspect_db.py
- Command line
-
hf download hf://spaces/aionaion/chatdoc/scripts/inspect_db.py
-
curl -L -o inspect_db.py https://huggingface.co/spaces/aionaion/chatdoc/resolve/main/scripts/inspect_db.py
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() |