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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""ChromaDB integration for vector storage and retrieval."""
from typing import List, Dict, Optional, Tuple
import chromadb
from chromadb.config import Settings
import uuid
import os
from embedding_models import EmbeddingFactory, EmbeddingModel
from chunking_strategies import ChunkingFactory
import json


class ChromaDBManager:
    """Manager for ChromaDB operations."""
    
    def __init__(self, persist_directory: str = "./chroma_db"):
        """Initialize ChromaDB manager.
        
        Args:
            persist_directory: Directory to persist ChromaDB data
        """
        self.persist_directory = persist_directory
        os.makedirs(persist_directory, exist_ok=True)
        
        # Initialize ChromaDB client with is_persistent=True to use persistent storage
        try:
            self.client = chromadb.PersistentClient(
                path=persist_directory,
                settings=Settings(
                    anonymized_telemetry=False,
                    allow_reset=True  # Allow reset if needed
                )
            )
        except Exception as e:
            print(f"Warning: Could not create persistent client: {e}")
            print("Falling back to regular client...")
            self.client = chromadb.Client(Settings(
                persist_directory=persist_directory,
                anonymized_telemetry=False,
                allow_reset=True
            ))
        
        self.embedding_model = None
        self.current_collection = None
    
    def reconnect(self):
        """Reconnect to ChromaDB in case of connection loss."""
        try:
            self.client = chromadb.PersistentClient(
                path=self.persist_directory,
                settings=Settings(
                    anonymized_telemetry=False,
                    allow_reset=True
                )
            )
            print("✅ Reconnected to ChromaDB")
        except Exception as e:
            print(f"Error reconnecting: {e}")
    
    def create_collection(
        self,
        collection_name: str,
        embedding_model_name: str,
        metadata: Optional[Dict] = None
    ) -> chromadb.Collection:
        """Create a new collection.
        
        Args:
            collection_name: Name of the collection
            embedding_model_name: Name of the embedding model
            metadata: Additional metadata for the collection
            
        Returns:
            ChromaDB collection
        """
        # Delete if exists
        try:
            self.client.delete_collection(collection_name)
        except:
            pass
        
        # Create embedding model
        self.embedding_model = EmbeddingFactory.create_embedding_model(embedding_model_name)
        self.embedding_model.load_model()
        
        # Create collection with metadata
        collection_metadata = {
            "embedding_model": embedding_model_name,
            "hnsw:space": "cosine"
        }
        if metadata:
            collection_metadata.update(metadata)
        
        self.current_collection = self.client.create_collection(
            name=collection_name,
            metadata=collection_metadata
        )
        
        print(f"Created collection: {collection_name}")
        return self.current_collection
    
    def get_collection(self, collection_name: str) -> chromadb.Collection:
        """Get an existing collection.
        
        Args:
            collection_name: Name of the collection
            
        Returns:
            ChromaDB collection
        """
        self.current_collection = self.client.get_collection(collection_name)
        
        # Load embedding model from metadata
        metadata = self.current_collection.metadata
        if "embedding_model" in metadata:
            self.embedding_model = EmbeddingFactory.create_embedding_model(
                metadata["embedding_model"]
            )
            self.embedding_model.load_model()
        
        return self.current_collection
    
    def list_collections(self) -> List[str]:
        """List all collections.
        
        Returns:
            List of collection names
        """
        collections = self.client.list_collections()
        return [col.name for col in collections]
    
    def clear_all_collections(self) -> int:
        """Delete all collections from the database.
        
        Returns:
            Number of collections deleted
        """
        collections = self.list_collections()
        count = 0
        
        for collection_name in collections:
            try:
                self.client.delete_collection(collection_name)
                print(f"Deleted collection: {collection_name}")
                count += 1
            except Exception as e:
                print(f"Error deleting collection {collection_name}: {e}")
        
        self.current_collection = None
        self.embedding_model = None
        print(f"✅ Cleared {count} collections")
        return count
    
    def delete_collection(self, collection_name: str) -> bool:
        """Delete a specific collection.
        
        Args:
            collection_name: Name of the collection to delete
            
        Returns:
            True if deleted successfully, False otherwise
        """
        try:
            self.client.delete_collection(collection_name)
            if self.current_collection and self.current_collection.name == collection_name:
                self.current_collection = None
                self.embedding_model = None
            print(f"✅ Deleted collection: {collection_name}")
            return True
        except Exception as e:
            print(f"❌ Error deleting collection: {e}")
            return False
    
    def add_documents(
        self,
        documents: List[str],
        metadatas: Optional[List[Dict]] = None,
        ids: Optional[List[str]] = None,
        batch_size: int = 100
    ):
        """Add documents to the current collection.
        
        Args:
            documents: List of document texts
            metadatas: List of metadata dictionaries
            ids: List of document IDs
            batch_size: Batch size for processing
        """
        if not self.current_collection:
            raise ValueError("No collection selected. Create or get a collection first.")
        
        if not self.embedding_model:
            raise ValueError("No embedding model loaded.")
        
        # Generate IDs if not provided
        if ids is None:
            ids = [str(uuid.uuid4()) for _ in documents]
        
        # Generate default metadata if not provided
        if metadatas is None:
            metadatas = [{"index": i} for i in range(len(documents))]
        
        # Process in batches
        total_docs = len(documents)
        print(f"Adding {total_docs} documents to collection...")
        
        for i in range(0, total_docs, batch_size):
            batch_docs = documents[i:i + batch_size]
            batch_ids = ids[i:i + batch_size]
            batch_metadatas = metadatas[i:i + batch_size]
            
            # Generate embeddings
            embeddings = self.embedding_model.embed_documents(batch_docs)
            
            # Add to collection
            self.current_collection.add(
                documents=batch_docs,
                embeddings=embeddings.tolist(),
                metadatas=batch_metadatas,
                ids=batch_ids
            )
            
            print(f"Added batch {i//batch_size + 1}/{(total_docs-1)//batch_size + 1}")
        
        print(f"Successfully added {total_docs} documents")
    
    def load_dataset_into_collection(
        self,
        collection_name: str,
        embedding_model_name: str,
        chunking_strategy: str,
        dataset_data: List[Dict],
        chunk_size: int = 512,
        overlap: int = 50
    ):
        """Load a dataset into a new collection with chunking.
        
        Args:
            collection_name: Name for the new collection
            embedding_model_name: Embedding model to use
            chunking_strategy: Chunking strategy to use
            dataset_data: List of dataset samples
            chunk_size: Size of chunks
            overlap: Overlap between chunks
        """
        # Create collection
        self.create_collection(
            collection_name,
            embedding_model_name,
            metadata={
                "chunking_strategy": chunking_strategy,
                "chunk_size": chunk_size,
                "overlap": overlap
            }
        )
        
        # Get chunker
        chunker = ChunkingFactory.create_chunker(chunking_strategy)
        
        # Process documents
        all_chunks = []
        all_metadatas = []
        
        print(f"Processing {len(dataset_data)} documents with {chunking_strategy} chunking...")
        
        for idx, sample in enumerate(dataset_data):
            # Use 'documents' list if available, otherwise fall back to 'context'
            documents = sample.get("documents", [])
            
            # If documents is empty, use context as fallback
            if not documents:
                context = sample.get("context", "")
                if context:
                    documents = [context]
            
            if not documents:
                continue
            
            # Process each document separately for better granularity
            for doc_idx, document in enumerate(documents):
                if not document or not str(document).strip():
                    continue
                
                # Chunk each document
                chunks = chunker.chunk_text(str(document), chunk_size, overlap)
                
                # Create metadata for each chunk
                for chunk_idx, chunk in enumerate(chunks):
                    all_chunks.append(chunk)
                    all_metadatas.append({
                        "doc_id": idx,
                        "doc_idx": doc_idx,  # Track which document within the sample
                        "chunk_id": chunk_idx,
                        "question": sample.get("question", ""),
                        "answer": sample.get("answer", ""),
                        "dataset": sample.get("dataset", ""),
                        "total_docs": len(documents)
                    })
        
        # Add all chunks to collection
        self.add_documents(all_chunks, all_metadatas)
        
        print(f"Loaded {len(all_chunks)} chunks from {len(dataset_data)} samples")
    
    def query(
        self,
        query_text: str,
        n_results: int = 5,
        filter_metadata: Optional[Dict] = None
    ) -> Dict:
        """Query the collection.
        
        Args:
            query_text: Query text
            n_results: Number of results to return
            filter_metadata: Metadata filter
            
        Returns:
            Query results
        """
        if not self.current_collection:
            raise ValueError("No collection selected.")
        
        if not self.embedding_model:
            raise ValueError("No embedding model loaded.")
        
        # Generate query embedding
        query_embedding = self.embedding_model.embed_query(query_text)
        
        # Query collection with retry logic
        try:
            results = self.current_collection.query(
                query_embeddings=[query_embedding.tolist()],
                n_results=n_results,
                where=filter_metadata
            )
        except Exception as e:
            if "default_tenant" in str(e):
                print("Warning: Lost connection to ChromaDB, reconnecting...")
                self.reconnect()
                # Try again after reconnecting
                results = self.current_collection.query(
                    query_embeddings=[query_embedding.tolist()],
                    n_results=n_results,
                    where=filter_metadata
                )
            else:
                raise
        
        return results
    
    def get_retrieved_documents(
        self,
        query_text: str,
        n_results: int = 5
    ) -> List[Dict]:
        """Get retrieved documents with metadata.
        
        Args:
            query_text: Query text
            n_results: Number of results
            
        Returns:
            List of retrieved documents with metadata
        """
        results = self.query(query_text, n_results)
        
        retrieved_docs = []
        for i in range(len(results['documents'][0])):
            retrieved_docs.append({
                "document": results['documents'][0][i],
                "metadata": results['metadatas'][0][i],
                "distance": results['distances'][0][i] if 'distances' in results else None
            })
        
        return retrieved_docs
    
    def delete_collection(self, collection_name: str):
        """Delete a collection.
        
        Args:
            collection_name: Name of collection to delete
        """
        try:
            self.client.delete_collection(collection_name)
            print(f"Deleted collection: {collection_name}")
        except Exception as e:
            print(f"Error deleting collection: {str(e)}")
    
    def get_collection_stats(self, collection_name: Optional[str] = None) -> Dict:
        """Get statistics for a collection.
        
        Args:
            collection_name: Name of collection (uses current if None)
            
        Returns:
            Dictionary with collection statistics
        """
        if collection_name:
            collection = self.client.get_collection(collection_name)
        elif self.current_collection:
            collection = self.current_collection
        else:
            raise ValueError("No collection specified or selected")
        
        count = collection.count()
        metadata = collection.metadata
        
        return {
            "name": collection.name,
            "count": count,
            "metadata": metadata
        }