File size: 13,942 Bytes
e7586f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
"""
Vector store module for Qdrant integration.
Handles embedding generation, storage, and retrieval from Qdrant.
"""

import logging
import os
from typing import List, Optional
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct, HasIdCondition
from sentence_transformers import SentenceTransformer
from metadata_schema import Chunk
from config import QDRANT_CONFIG, LLM_CONFIG

logger = logging.getLogger(__name__)


class VectorStore:
    """
    Manages embeddings and vector storage in Qdrant.
    Handles both in-memory and network-based Qdrant instances.
    """
    
    def __init__(self):
        """Initialize vector store client."""
        self.client = self._init_qdrant_client()
        # Using sentence-transformers for embeddings (all-MiniLM-L6-v2)
        self.embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
        self.vector_size = 384  # all-MiniLM-L6-v2 produces 384-dimensional vectors
    
    def _init_qdrant_client(self) -> QdrantClient:
        """
        Initialize Qdrant client based on configuration.
        
        Returns:
            QdrantClient instance
        """
        mode = QDRANT_CONFIG.get("mode", "memory")
        
        try:
            if mode == "memory":
                # In-memory Qdrant for development
                logger.info("Initializing Qdrant in-memory mode")
                return QdrantClient(":memory:")
            
            elif mode == "local":
                # Local persistent storage
                path = QDRANT_CONFIG.get("path", "qdrant_storage")
                logger.info(f"Initializing Qdrant in local persistent mode at: {path}")
                # Ensure directory exists
                os.makedirs(path, exist_ok=True)
                return QdrantClient(path=path)
            
            elif mode == "url":
                # Network Qdrant
                url = QDRANT_CONFIG.get("url", "localhost:6333")
                api_key = QDRANT_CONFIG.get("api_key")
                logger.info(f"Initializing Qdrant with URL: {url}")
                return QdrantClient(
                    url=url,
                    api_key=api_key,
                    timeout=30,
                )
            
            else:
                logger.warning(f"Unknown Qdrant mode: {mode}, defaulting to memory")
                return QdrantClient(":memory:")
        
        except Exception as e:
            logger.error(f"Failed to initialize Qdrant: {str(e)}")
            # Fallback to memory mode
            return QdrantClient(":memory:")
    
    def create_collection(self, collection_name: str, vector_size: int = None) -> bool:
        """
        Create a collection in Qdrant.
        
        Args:
            collection_name: Name of the collection
            vector_size: Size of vectors (default from config)
            
        Returns:
            True if successful, False otherwise
        """
        if vector_size is None:
            vector_size = self.vector_size
        
        try:
            # Check if collection exists
            collections = self.client.get_collections()
            if any(c.name == collection_name for c in collections.collections):
                logger.info(f"Collection '{collection_name}' already exists")
                return True
            
            # Create new collection
            self.client.create_collection(
                collection_name=collection_name,
                vectors_config=VectorParams(size=vector_size, distance=Distance.COSINE),
            )
            logger.info(f"Created collection: {collection_name}")
            return True
        
        except Exception as e:
            logger.error(f"Error creating collection {collection_name}: {str(e)}")
            return False
    
    def embed_text(self, text: str) -> Optional[List[float]]:
        """
        Generate embedding for text using SentenceTransformer.
        
        Args:
            text: Text to embed
            
        Returns:
            Embedding vector or None if error
        """
        try:
            # Truncate if too long (max ~512 tokens for sentence-transformers)
            if len(text) > 30000:
                text = text[:30000]
            
            embedding = self.embedding_model.encode(text, convert_to_tensor=False)
            return embedding.tolist()
        
        except Exception as e:
            logger.error(f"Error generating embedding with SentenceTransformer: {str(e)}")
            return None
    
    def store_chunks(
        self,
        chunks: List[Chunk],
        collection_name: str,
    ) -> bool:
        """
        Store chunks with embeddings in Qdrant.
        
        Args:
            chunks: List of Chunk objects
            collection_name: Target collection name
            
        Returns:
            True if successful
        """
        try:
            # Ensure collection exists
            if not self.create_collection(collection_name):
                logger.error(f"Failed to create collection {collection_name}")
                return False
            
            # Generate embeddings and prepare points
            points = []
            
            for chunk in chunks:
                # Generate embedding
                embedding = self.embed_text(chunk.text)
                if not embedding:
                    logger.warning(f"Failed to embed chunk {chunk.id}")
                    continue
                
                # Create point with metadata payload
                point = PointStruct(
                    id=self._hash_id(chunk.id),
                    vector=embedding,
                    payload=chunk.to_qdrant_payload(),
                )
                points.append(point)
            
            if not points:
                logger.warning(f"No points to store in {collection_name}")
                return True
            
            # Upload points to Qdrant
            self.client.upsert(
                collection_name=collection_name,
                points=points,
            )
            
            logger.info(f"Stored {len(points)} chunks in collection {collection_name}")
            return True
        
        except Exception as e:
            logger.error(f"Error storing chunks in {collection_name}: {str(e)}")
            return False
    
    def search_with_filter(
        self,
        collection_name: str,
        query_embedding: List[float],
        access_roles: List[str],
        top_k: int = 5,
        score_threshold: float = 0.5,
    ) -> List[dict]:
        """
        Search collection with RBAC filter.
        CRITICAL: This ensures only chunks accessible to the user are returned.
        
        Args:
            collection_name: Collection to search
            query_embedding: Query embedding vector
            access_roles: Roles the user has (determines what they can access)
            top_k: Number of results to return
            score_threshold: Minimum similarity score
            
        Returns:
            List of matching chunks with metadata
        """
        try:
            from qdrant_client.models import Filter, FieldCondition, MatchAny
            
            # Build native Qdrant RBAC filter
            # Checks if chunk's access_roles field contains any of the user's roles
            rbac_filter = Filter(
                must=[
                    FieldCondition(
                        key="access_roles",
                        match=MatchAny(any=access_roles)
                    )
                ]
            )
            
            # qdrant-client >= 1.14 uses query_points; legacy .search() was removed.
            query_response = self.client.query_points(
                collection_name=collection_name,
                query=query_embedding,
                query_filter=rbac_filter,
                limit=top_k,
                score_threshold=score_threshold,
                with_payload=True,
            )
            results = getattr(query_response, "points", None) or []

            filtered_results = []
            for scored_point in results:
                payload = scored_point.payload or {}
                filtered_results.append({
                    "id": scored_point.id,
                    "score": scored_point.score,
                    "source_document": payload.get("source_document", "unknown"),
                    "collection": payload.get("collection", "unknown"),
                    "access_roles": payload.get("access_roles", []),
                    "section_title": payload.get("section_title", ""),
                    "subsection_title": payload.get("subsection_title", ""),
                    "page_number": payload.get("page_number", 0),
                    "chunk_type": payload.get("chunk_type", "text"),
                    "text": payload.get("text", ""),
                    "parent_chunk_id": payload.get("parent_chunk_id", ""),
                    "parent_summary": payload.get("parent_summary", ""),
                })
            
            logger.info(
                f"Retrieved {len(filtered_results)} chunks from {collection_name} "
                f"after RBAC filtering (user roles: {access_roles})"
            )
            
            return filtered_results[:top_k]
        
        except Exception as e:
            logger.error(f"Error searching collection {collection_name}: {str(e)}")
            return []
    
    def search_by_text(
        self,
        collection_name: str,
        query_text: str,
        access_roles: List[str],
        top_k: int = 5,
        score_threshold: float = 0.5,
    ) -> List[dict]:
        """
        Search by text query (convenience wrapper).
        
        Args:
            collection_name: Collection to search
            query_text: Query text
            access_roles: User's accessible roles
            top_k: Number of results
            score_threshold: Minimum score
            
        Returns:
            List of matching chunks
        """
        # Embed query
        query_embedding = self.embed_text(query_text)
        if not query_embedding:
            logger.error("Failed to embed query")
            return []
        
        # Search with RBAC filter
        return self.search_with_filter(
            collection_name=collection_name,
            query_embedding=query_embedding,
            access_roles=access_roles,
            top_k=top_k,
            score_threshold=score_threshold,
        )
    
    def list_collections(self) -> List[str]:
        """
        Get list of all collections in vector store.
        
        Returns:
            List of collection names
        """
        try:
            collections = self.client.get_collections()
            return [c.name for c in collections.collections]
        except Exception as e:
            logger.error(f"Error listing collections: {str(e)}")
            return []
    
    def delete_collection(self, collection_name: str) -> bool:
        """
        Delete a collection.
        
        Args:
            collection_name: Collection to delete
            
        Returns:
            True if successful
        """
        try:
            self.client.delete_collection(collection_name=collection_name)
            logger.info(f"Deleted collection: {collection_name}")
            return True
        except Exception as e:
            logger.error(f"Error deleting collection {collection_name}: {str(e)}")
            return False
    
    def get_collection_stats(self, collection_name: str) -> Optional[dict]:
        """
        Get statistics about a collection.
        
        Args:
            collection_name: Collection name
            
        Returns:
            Dictionary with collection stats, or zeros if the collection does not exist
            in Qdrant yet (e.g. not ingested). None only on unexpected errors.
        """
        try:
            if not self.client.collection_exists(collection_name=collection_name):
                return {
                    "name": collection_name,
                    "points_count": 0,
                    "vectors_count": 0,
                }
            info = self.client.get_collection(collection_name=collection_name)
            # Qdrant REST CollectionInfo has no `name` (we already have it) or top-level
            # `vectors_count`; use points_count and indexed_vectors_count.
            # Use points_count as the definitive total document count
            points = info.points_count if info.points_count is not None else 0
            
            # indexed_vectors_count shows how many have been HNSW-indexed (can be 0 initially)
            indexed = info.indexed_vectors_count
            
            # For the summary 'vectors_count', we prefer the total points if indexing is still 0
            vectors_count = indexed if indexed is not None and indexed > 0 else points
            
            return {
                "name": collection_name,
                "points_count": points,
                "vectors_count": vectors_count,
            }
        except Exception as e:
            logger.error(f"Error getting collection stats: {str(e)}")
            return None
    
    @staticmethod
    def _hash_id(text_id: str) -> int:
        """
        Convert string ID to integer hash for Qdrant.
        
        Args:
            text_id: Text ID
            
        Returns:
            Integer hash
        """
        return abs(hash(text_id)) % (2**63)


# Global vector store instance
_vector_store = None


def get_vector_store() -> VectorStore:
    """
    Get singleton vector store instance.
    
    Returns:
        VectorStore instance
    """
    global _vector_store
    if _vector_store is None:
        _vector_store = VectorStore()
    return _vector_store