File size: 10,483 Bytes
d0f35dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import List, Optional, Dict, Any
from langchain_classic.schema import Document
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_qdrant import QdrantVectorStore
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct
from config import Config
import uuid


class VectorStoreManager:
    """Manages Qdrant vector store operations for insurance documents"""
    
    def __init__(self):
        """Initialize Qdrant client and embeddings"""
        # Validate configuration
        Config.validate_config()
        
        # Get configuration
        self.qdrant_config = Config.get_qdrant_config()
        self.retrieval_config = Config.get_retrieval_config()
        
        # Initialize Qdrant client
        self.client = QdrantClient(
            url=self.qdrant_config["url"],
            api_key=self.qdrant_config["api_key"],
        )
        
        # Initialize embeddings
        self.embeddings = GoogleGenerativeAIEmbeddings(
            model=Config.EMBEDDING_MODEL,
            output_dimensionality=Config.EMBEDDING_DIMENSION,
            google_api_key=Config.GEMINI_API_KEY
        )
        
        self.collection_name = self.qdrant_config["collection_name"]
        
        print("Vector store manager initialized")
    
    def create_collection(self, recreate: bool = False) -> bool:
        """
        Create a new collection in Qdrant
        
        Args:
            recreate: If True, delete existing collection and create new one
            
        Returns:
            Boolean indicating success
        """
        try:
            # Check if collection exists
            collections = self.client.get_collections().collections
            collection_exists = any(c.name == self.collection_name for c in collections)
            
            if collection_exists:
                if recreate:
                    print(f"⚠ Deleting existing collection: {self.collection_name}")
                    self.client.delete_collection(self.collection_name)
                else:
                    print(f" Collection '{self.collection_name}' already exists")
                    return True
            
            # Create new collection
            self.client.create_collection(
                collection_name=self.collection_name,
                vectors_config=VectorParams(
                    size=self.qdrant_config["vector_size"],
                    distance=Distance.COSINE
                )
            )
            
            print(f" Created collection: {self.collection_name}")
            return True
            
        except Exception as e:
            print(f" Error creating collection: {str(e)}")
            raise
    
    def add_documents(self, documents: List[Document], batch_size: int = 100) -> List[str]:
        """
        Add documents to Qdrant vector store
        
        Args:
            documents: List of Document objects to add
            batch_size: Number of documents to process in each batch
            
        Returns:
            List of document IDs
        """
        try:
            print(f"Adding {len(documents)} documents to vector store...")
            
            # Ensure collection exists
            self.create_collection(recreate=False)
            
            # Initialize vector store
            vector_store = QdrantVectorStore(
                client=self.client,
                collection_name=self.collection_name,
                embedding=self.embeddings
            )
            
            # Add documents in batches
            all_ids = []
            for i in range(0, len(documents), batch_size):
                batch = documents[i:i + batch_size]
                
                # Generate unique IDs for this batch
                batch_ids = [str(uuid.uuid4()) for _ in batch]
                
                # Add to vector store
                vector_store.add_documents(documents=batch, ids=batch_ids)
                all_ids.extend(batch_ids)
                
                print(f"   Processed batch {i//batch_size + 1}/{(len(documents)-1)//batch_size + 1}")
            
            print(f" Successfully added {len(documents)} documents")
            return all_ids
            
        except Exception as e:
            print(f" Error adding documents: {str(e)}")
            raise
    
    def similarity_search(
        self, 
        query: str, 
        k: Optional[int] = None,
        filter_dict: Optional[Dict[str, Any]] = None
    ) -> List[Document]:
        """
        Search for similar documents using semantic similarity
        
        Args:
            query: Search query string
            k: Number of results to return (default from config)
            filter_dict: Optional metadata filters (e.g., {"section_type": "exclusions"})
            
        Returns:
            List of most similar Documents
        """
        try:
            if k is None:
                k = self.retrieval_config["top_k"]
            
            # Initialize vector store for querying
            vector_store = QdrantVectorStore(
                client=self.client,
                collection_name=self.collection_name,
                embedding=self.embeddings
            )

            if filter_dict:
                # Get more results than needed
                results = vector_store.similarity_search(query=query, k=k*3)
                
                # Filter by metadata
                filtered_results = []
                for doc in results:
                    match = True
                    for key, value in filter_dict.items():
                        if doc.metadata.get(key) != value:
                            match = False
                            break
                    if match:
                        filtered_results.append(doc)
                    
                    # Stop when we have enough results
                    if len(filtered_results) >= k:
                        break
                
                return filtered_results[:k]
            else:
                results = vector_store.similarity_search(query=query, k=k)
                return results
            
        except Exception as e:
            print(f" Error during similarity search: {str(e)}")
            raise
    
    def similarity_search_with_score(
        self, 
        query: str, 
        k: Optional[int] = None,
        score_threshold: Optional[float] = None
    ) -> List[tuple[Document, float]]:
        """
        Search with similarity scores
        
        Args:
            query: Search query string
            k: Number of results to return
            score_threshold: Minimum similarity score (default from config)
            
        Returns:
            List of (Document, score) tuples
        """
        try:
            if k is None:
                k = self.retrieval_config["top_k"]
            
            if score_threshold is None:
                score_threshold = self.retrieval_config["similarity_threshold"]
            
            # Initialize vector store
            vector_store = QdrantVectorStore(
                client=self.client,
                collection_name=self.collection_name,
                embedding=self.embeddings
            )
            
            # Search with scores
            results = vector_store.similarity_search_with_score(query=query, k=k)
            
            # Filter by score threshold
            filtered_results = [
                (doc, score) for doc, score in results 
                if score >= score_threshold
            ]
            
            print(f" Found {len(filtered_results)} results above threshold {score_threshold}")
            return filtered_results
            
        except Exception as e:
            print(f" Error during similarity search with score: {str(e)}")
            raise
    
    def search_by_section_type(
        self, 
        query: str, 
        section_type: str,
        k: Optional[int] = None
    ) -> List[Document]:
        """
        Search within a specific section type (e.g., 'exclusions', 'addons')
        
        Args:
            query: Search query string
            section_type: Type of section to search in
            k: Number of results to return
            
        Returns:
            List of Documents from specified section type
        """
        filter_dict = {"section_type": section_type}
        return self.similarity_search(query=query, k=k, filter_dict=filter_dict)
    
    def get_collection_info(self) -> Dict:
        """
        Get information about the current collection
        
        Returns:
            Dictionary with collection statistics
        """
        try:
            collection_info = self.client.get_collection(self.collection_name)
            
            return {
                "name": self.collection_name,
                "vectors_count": collection_info.vectors_count,
                "points_count": collection_info.points_count,
                "status": collection_info.status,
            }
            
        except Exception as e:
            print(f" Error getting collection info: {str(e)}")
            return {}
    
    def delete_collection(self) -> bool:
        """
        Delete the current collection
        
        Returns:
            Boolean indicating success
        """
        try:
            self.client.delete_collection(self.collection_name)
            print(f" Deleted collection: {self.collection_name}")
            return True
            
        except Exception as e:
            print(f" Error deleting collection: {str(e)}")
            return False
    
    def get_retriever(self, **kwargs):
        """
        Get a LangChain retriever object for use in chains
        
        Args:
            **kwargs: Additional arguments for retriever configuration
            
        Returns:
            VectorStoreRetriever object
        """
        vector_store = QdrantVectorStore(
            client=self.client,
            collection_name=self.collection_name,
            embedding=self.embeddings
        )
        
        # Set default search kwargs
        search_kwargs = {
            "k": self.retrieval_config["top_k"]
        }
        search_kwargs.update(kwargs)
        
        return vector_store.as_retriever(search_kwargs=search_kwargs)