File size: 3,236 Bytes
4420cb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/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()