File size: 10,576 Bytes
9b457ed | 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | """
ChromaDB vector storage interface.
This module provides a clean interface to ChromaDB for storing and retrieving
document chunks with their embeddings and metadata.
"""
import chromadb
from typing import List, Optional
import numpy as np
import json
from datetime import datetime
from src.config.settings import get_settings, get_collection_name_for_model, EMBEDDING_MODELS
from src.utils.logging import get_logger
from src.ingestion.models import Chunk
logger = get_logger(__name__)
class VectorStore:
"""ChromaDB interface for vector storage."""
def __init__(self, embedding_model: Optional[str] = None):
"""
Initialize vector store with settings from configuration.
Args:
embedding_model: Optional embedding model ID. If provided, uses model-specific collection.
"""
settings = get_settings()
self.persist_dir = settings.chroma_persist_dir
self._base_collection_name = settings.chroma_collection_name
self._embedding_model = embedding_model or settings.embedding_model
# Use model-specific collection name
self.collection_name = get_collection_name_for_model(
self._embedding_model,
self._base_collection_name
)
self._client = None
self._collection = None
@property
def client(self):
"""
Lazy initialize ChromaDB client.
Returns:
chromadb.Client: ChromaDB client instance
"""
if self._client is None:
logger.info(f"Initializing ChromaDB client: {self.persist_dir}")
self._client = chromadb.PersistentClient(path=self.persist_dir)
logger.debug(f"ChromaDB client initialized")
return self._client
def get_collection(self):
"""
Get or create the collection.
Returns:
chromadb.Collection: Collection instance
"""
if self._collection is None:
self._collection = self.client.get_or_create_collection(
name=self.collection_name,
metadata={"description": "Hierarchical PDF chunks with embeddings"}
)
logger.info(f"Collection loaded: {self.collection_name}")
return self._collection
def add_chunks(self, chunks: List[Chunk], embeddings: np.ndarray):
"""
Add chunks with embeddings to ChromaDB.
Args:
chunks: List of chunks to store
embeddings: Numpy array of embeddings (num_chunks x embedding_dim)
"""
if len(chunks) != len(embeddings):
raise ValueError(f"Number of chunks ({len(chunks)}) != number of embeddings ({len(embeddings)})")
collection = self.get_collection()
# Prepare data for ChromaDB
ids = [str(chunk.chunk_id) for chunk in chunks]
documents = [chunk.text for chunk in chunks]
metadatas = [self._prepare_metadata(chunk) for chunk in chunks]
logger.info(f"Adding {len(chunks)} chunks to ChromaDB")
# Add to collection
collection.add(
ids=ids,
embeddings=embeddings.tolist(),
documents=documents,
metadatas=metadatas
)
logger.info(f"Successfully added {len(chunks)} chunks")
def _prepare_metadata(self, chunk: Chunk) -> dict:
"""
Prepare metadata for ChromaDB storage.
ChromaDB metadata can only contain: str, int, float, bool.
Lists must be JSON-encoded.
Args:
chunk: Chunk to extract metadata from
Returns:
dict: Metadata dictionary
"""
return {
"chunk_id": str(chunk.chunk_id),
"document_id": str(chunk.document_id),
"parent_id": str(chunk.parent_id) if chunk.parent_id else "",
"chunk_type": chunk.chunk_type,
"token_count": chunk.token_count,
"chunk_index": chunk.chunk_index,
"page_numbers": json.dumps(chunk.page_numbers),
"start_char": chunk.start_char,
"end_char": chunk.end_char,
"file_hash": chunk.file_hash,
"filename": chunk.filename,
}
def document_exists(self, file_hash: str) -> bool:
"""
Check if document with given hash already exists.
Args:
file_hash: SHA256 hash of document
Returns:
bool: True if document exists
"""
collection = self.get_collection()
try:
# Try to query for any chunk with this file hash
results = collection.get(
where={"file_hash": file_hash},
limit=1
)
exists = len(results['ids']) > 0
if exists:
logger.debug(f"Document with hash {file_hash[:8]}... already exists")
return exists
except Exception as e:
# If metadata field doesn't exist, document doesn't exist
logger.debug(f"Document check failed: {e}")
return False
def get_chunk(self, chunk_id: str) -> Optional[dict]:
"""
Retrieve a specific chunk by ID.
Args:
chunk_id: UUID of chunk to retrieve
Returns:
Optional[dict]: Chunk data or None if not found
"""
collection = self.get_collection()
try:
results = collection.get(
ids=[chunk_id],
include=["documents", "metadatas", "embeddings"]
)
if len(results['ids']) > 0:
return {
"id": results['ids'][0],
"document": results['documents'][0],
"metadata": results['metadatas'][0],
"embedding": results['embeddings'][0] if results['embeddings'] else None
}
return None
except Exception as e:
logger.error(f"Failed to retrieve chunk {chunk_id}: {e}")
return None
def delete_document(self, document_id: str):
"""
Delete all chunks for a document.
Args:
document_id: UUID of document to delete
"""
collection = self.get_collection()
try:
collection.delete(
where={"document_id": document_id}
)
logger.info(f"Deleted all chunks for document: {document_id}")
except Exception as e:
logger.error(f"Failed to delete document {document_id}: {e}")
raise
def get_collection_stats(self) -> dict:
"""
Get statistics about the collection.
Returns:
dict: Collection statistics
"""
collection = self.get_collection()
try:
count = collection.count()
return {
"name": self.collection_name,
"total_chunks": count,
"persist_dir": self.persist_dir,
"embedding_model": self._embedding_model,
}
except Exception as e:
logger.error(f"Failed to get collection stats: {e}")
return {}
def list_all_collections(self) -> List[dict]:
"""
List all available collections with their stats.
Returns:
List[dict]: List of collection info dictionaries
"""
collections = []
settings = get_settings()
for model_id, model_config in EMBEDDING_MODELS.items():
collection_name = get_collection_name_for_model(
model_id,
self._base_collection_name
)
try:
coll = self.client.get_collection(name=collection_name)
count = coll.count()
collections.append({
"collection_name": collection_name,
"embedding_model": model_id,
"model_name": model_config.get("name", model_id),
"dimensions": model_config.get("dimensions"),
"total_chunks": count,
"is_active": model_id == self._embedding_model,
})
except Exception:
# Collection doesn't exist yet
collections.append({
"collection_name": collection_name,
"embedding_model": model_id,
"model_name": model_config.get("name", model_id),
"dimensions": model_config.get("dimensions"),
"total_chunks": 0,
"is_active": model_id == self._embedding_model,
})
return collections
def switch_collection(self, embedding_model: str):
"""
Switch to a different collection based on embedding model.
Args:
embedding_model: Embedding model ID to switch to
"""
self._embedding_model = embedding_model
self.collection_name = get_collection_name_for_model(
embedding_model,
self._base_collection_name
)
self._collection = None # Reset cached collection
logger.info(f"Switched to collection: {self.collection_name}")
def query(
self,
query_embedding: np.ndarray,
top_k: int = 10,
filter_filenames: Optional[List[str]] = None,
) -> dict:
"""
Query the collection with an embedding.
Args:
query_embedding: Query embedding vector
top_k: Number of results to return
filter_filenames: Optional list of filenames to filter results
Returns:
dict: Query results with ids, documents, metadatas, and distances
"""
collection = self.get_collection()
try:
# Build where clause for filtering
where_clause = None
if filter_filenames:
if len(filter_filenames) == 1:
where_clause = {"filename": filter_filenames[0]}
else:
where_clause = {"filename": {"$in": filter_filenames}}
results = collection.query(
query_embeddings=[query_embedding.tolist()],
n_results=top_k,
include=["documents", "metadatas", "distances"],
where=where_clause,
)
return results
except Exception as e:
logger.error(f"Query failed: {e}")
return {"ids": [], "documents": [], "metadatas": [], "distances": []}
|