File size: 15,237 Bytes
01728c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import pickle
import os
from typing import List, Dict, Tuple
import json
import re
from collections import Counter
import math
import config

# Import torch for device handling
try:
    import torch
    TORCH_AVAILABLE = True
except ImportError:
    TORCH_AVAILABLE = False

# Import Hugging Face client
try:
    from .huggingface_client import HuggingFaceEmbeddingModel
    HUGGINGFACE_AVAILABLE = True
except ImportError:
    HUGGINGFACE_AVAILABLE = False

# Fallback to sentence transformers
try:
    import faiss
    from sentence_transformers import SentenceTransformer
    SENTENCE_TRANSFORMERS_AVAILABLE = True
except ImportError:
    SENTENCE_TRANSFORMERS_AVAILABLE = False
    print("Sentence transformers not available. Using TF-IDF fallback.")

class VectorStore:
    """
    Vector store using Sentence Transformers for embeddings and FAISS for similarity search
    """
    
    def __init__(self, model_name: str = None, index_path: str = "vector_index"):
        self.model_name = model_name or config.EMBEDDING_MODEL
        self.index_path = index_path
        self.embedding_model = None
        self.index = None
        self.documents = []
        self.dimension = None
        self.use_huggingface = HUGGINGFACE_AVAILABLE
        self.use_sentence_transformers = SENTENCE_TRANSFORMERS_AVAILABLE
        
        if self.use_huggingface:
            self._load_huggingface_model()
        elif self.use_sentence_transformers:
            self._load_sentence_transformer_model()
        else:
            self._init_simple_search()
    
    def _load_huggingface_model(self):
        """Load the Hugging Face embedding model"""
        try:
            self.embedding_model = HuggingFaceEmbeddingModel(self.model_name)
            # Get dimension
            self.dimension = self.embedding_model.get_dimension()
            print(f"Loaded HuggingFace embedding model: {self.model_name} (dimension: {self.dimension})")
        except Exception as e:
            print(f"Error loading HuggingFace model: {str(e)}")
            self.use_huggingface = False
            if self.use_sentence_transformers:
                self._load_sentence_transformer_model()
            else:
                self._init_simple_search()
    
    def _load_sentence_transformer_model(self):
        """Load the sentence transformer model for embeddings"""
        try:
            # Load with careful device handling - let the library handle device assignment
            self.embedding_model = SentenceTransformer(
                self.model_name, 
                device=None,  # Let the library choose the best device
                trust_remote_code=True
            )
            
            # Get dimension from a sample embedding
            sample_embedding = self.embedding_model.encode(["sample"])
            self.dimension = sample_embedding.shape[1] if hasattr(sample_embedding, 'shape') else len(sample_embedding)
            print(f"Loaded sentence transformer model: {self.model_name} (dimension: {self.dimension})")
        except Exception as e:
            print(f"Error loading sentence transformer model: {str(e)}")
            self.use_sentence_transformers = False
            self._init_simple_search()
    
    def _preprocess_text(self, text: str) -> List[str]:
        """Simple text preprocessing for TF-IDF"""
        # Convert to lowercase and remove punctuation
        text = re.sub(r'[^\w\s]', ' ', text.lower())
        # Split into words and remove empty strings
        words = [word for word in text.split() if len(word) > 2]
        return words
    
    def _compute_tf(self, words: List[str]) -> Dict[str, float]:
        """Compute term frequency"""
        word_count = len(words)
        tf_dict = {}
        for word in words:
            tf_dict[word] = tf_dict.get(word, 0) + 1
        # Normalize by total word count
        for word in tf_dict:
            tf_dict[word] = tf_dict[word] / word_count
        return tf_dict
    
    def _compute_idf(self):
        """Compute inverse document frequency for all terms"""
        N = len(self.documents)
        all_words = set()
        for doc in self.documents:
            words = self._preprocess_text(doc['text'])
            all_words.update(set(words))
        
        for word in all_words:
            containing_docs = sum(1 for doc in self.documents 
                                if word in self._preprocess_text(doc['text']))
            self.idf_scores[word] = math.log(N / containing_docs) if containing_docs > 0 else 0
    
    def _compute_tfidf_similarity(self, query: str, doc_text: str) -> float:
        """Compute TF-IDF cosine similarity between query and document"""
        query_words = self._preprocess_text(query)
        doc_words = self._preprocess_text(doc_text)
        
        if not query_words or not doc_words:
            return 0.0
        
        query_tf = self._compute_tf(query_words)
        doc_tf = self._compute_tf(doc_words)
        
        # Get all unique words
        all_words = set(query_words + doc_words)
        
        # Compute TF-IDF vectors
        query_vector = []
        doc_vector = []
        
        for word in all_words:
            idf = self.idf_scores.get(word, 0)
            query_tfidf = query_tf.get(word, 0) * idf
            doc_tfidf = doc_tf.get(word, 0) * idf
            query_vector.append(query_tfidf)
            doc_vector.append(doc_tfidf)
        
        # Compute cosine similarity
        if not query_vector or not doc_vector:
            return 0.0
        
        dot_product = sum(a * b for a, b in zip(query_vector, doc_vector))
        query_norm = math.sqrt(sum(a * a for a in query_vector))
        doc_norm = math.sqrt(sum(a * a for a in doc_vector))
        
        if query_norm == 0 or doc_norm == 0:
            return 0.0
        
        return dot_product / (query_norm * doc_norm)
    
    def _init_simple_search(self):
        """Initialize simple TF-IDF search"""
        self.vocabulary = {}
        self.idf_scores = {}
        print("Initialized simple TF-IDF search (advanced embeddings not available)")
    
    def create_embeddings(self, texts: List[str]) -> np.ndarray:
        """Create embeddings for a list of texts"""
        if self.use_huggingface or self.use_sentence_transformers:
            try:
                embeddings = self.embedding_model.encode(texts)
                if hasattr(embeddings, 'numpy'):
                    embeddings = embeddings.numpy()
                return embeddings.astype('float32')
            except Exception as e:
                print(f"Error creating embeddings, falling back to simple search: {str(e)}")
                self.use_huggingface = False
                self.use_sentence_transformers = False
                self._init_simple_search()
        
        # Return dummy embeddings for simple search
        return np.zeros((len(texts), 100), dtype='float32')
    
    def initialize_index(self):
        """Initialize FAISS index"""
        if not (self.use_huggingface or self.use_sentence_transformers):
            return
        
        if self.dimension is None:
            raise Exception("Embedding model not properly loaded")
        
        # Use IndexFlatIP for cosine similarity (Inner Product)
        self.index = faiss.IndexFlatIP(self.dimension)
        print(f"Initialized FAISS index with dimension {self.dimension}")
    
    def add_documents(self, chunks: List[Dict]):
        """Add document chunks to the vector store"""
        if not chunks:
            return
        
        # Store documents with metadata
        for i, chunk in enumerate(chunks):
            self.documents.append({
                'id': len(self.documents),
                'text': chunk['text'],
                'metadata': chunk['metadata'],
                'embedding_id': len(self.documents)
            })
        
        if self.use_huggingface or self.use_sentence_transformers:
            # Initialize index if not done
            if self.index is None:
                self.initialize_index()
            
            # Extract texts for embedding
            texts = [chunk['text'] for chunk in chunks]
            
            # Create embeddings
            embeddings = self.create_embeddings(texts)
            
            # Normalize embeddings for cosine similarity
            faiss.normalize_L2(embeddings)
            
            # Add to FAISS index
            self.index.add(embeddings)
            
            print(f"Added {len(chunks)} document chunks to FAISS vector store")
        else:
            # For simple search, compute IDF scores
            self._compute_idf()
            print(f"Added {len(chunks)} document chunks to simple vector store")
    
    def search(self, query: str, k: int = 5, similarity_threshold: float = 0.0) -> List[Dict]:
        """Search for similar documents using semantic similarity with very low threshold"""
        if len(self.documents) == 0:
            return []

        if (self.use_huggingface or self.use_sentence_transformers) and self.index is not None:
            return self._advanced_search(query, k, similarity_threshold)
        else:
            return self._simple_search(query, k, similarity_threshold)
    
    def _advanced_search(self, query: str, k: int, similarity_threshold: float) -> List[Dict]:
        """Advanced search using FAISS and sentence transformers"""
        # Create query embedding
        query_embedding = self.create_embeddings([query])
        
        # Normalize for cosine similarity
        faiss.normalize_L2(query_embedding)
        
        # Search in FAISS index
        scores, indices = self.index.search(query_embedding, min(k, len(self.documents)))
        
        results = []
        for i, (score, idx) in enumerate(zip(scores[0], indices[0])):
            # Filter by similarity threshold
            if score >= similarity_threshold and idx < len(self.documents):
                result = {
                    'document': self.documents[idx],
                    'score': float(score),
                    'rank': i + 1
                }
                results.append(result)
        
        return results
    
    def _simple_search(self, query: str, k: int, similarity_threshold: float) -> List[Dict]:
        """Simple search using improved TF-IDF similarity with better matching"""
        if not self.documents:
            return []
        
        # Compute similarities
        similarities = []
        for doc in self.documents:
            # Calculate multiple similarity scores for better matching
            tfidf_similarity = self._compute_tfidf_similarity(query, doc['text'])
            keyword_similarity = self._compute_keyword_similarity(query, doc['text'])
            combined_similarity = max(tfidf_similarity, keyword_similarity * 0.7)  # Boost keyword matches
            
            similarities.append({
                'document': doc,
                'score': combined_similarity,
                'rank': 0  # Will be set after sorting
            })
        
        # Sort by similarity score
        similarities.sort(key=lambda x: x['score'], reverse=True)
        
        # Always return results, ignore similarity threshold for TF-IDF fallback
        results = []
        for i, result in enumerate(similarities[:k]):
            result['rank'] = i + 1
            results.append(result)
        
        return results
    
    def _compute_keyword_similarity(self, query: str, text: str) -> float:
        """Compute simple keyword-based similarity"""
        query_words = set(query.lower().split())
        text_words = set(text.lower().split())
        
        if not query_words:
            return 0.0
        
        # Calculate Jaccard similarity
        intersection = query_words.intersection(text_words)
        union = query_words.union(text_words)
        
        if not union:
            return 0.0
        
        return len(intersection) / len(union)
    
    def save_index(self):
        """Save vector store to disk"""
        try:
            if (self.use_huggingface or self.use_sentence_transformers) and self.index is not None:
                # Save FAISS index
                faiss.write_index(self.index, f"{self.index_path}.faiss")
            
            # Save documents and metadata
            with open(f"{self.index_path}_docs.pkl", "wb") as f:
                pickle.dump({
                    'documents': self.documents,
                    'dimension': self.dimension,
                    'model_name': self.model_name,
                    'use_huggingface': self.use_huggingface,
                    'use_sentence_transformers': self.use_sentence_transformers,
                    'vocabulary': getattr(self, 'vocabulary', {}),
                    'idf_scores': getattr(self, 'idf_scores', {})
                }, f)
            
            print(f"Saved vector index to {self.index_path}")
        except Exception as e:
            print(f"Error saving index: {str(e)}")
    
    def load_index(self):
        """Load vector store from disk"""
        try:
            if os.path.exists(f"{self.index_path}_docs.pkl"):
                # Load documents and metadata
                with open(f"{self.index_path}_docs.pkl", "rb") as f:
                    data = pickle.load(f)
                    self.documents = data['documents']
                    self.dimension = data.get('dimension')
                    self.vocabulary = data.get('vocabulary', {})
                    self.idf_scores = data.get('idf_scores', {})
                    stored_use_hf = data.get('use_huggingface', False)
                    stored_use_st = data.get('use_sentence_transformers', data.get('use_advanced', True))
                
                # Load FAISS index if available and we're using embeddings
                if ((self.use_huggingface or self.use_sentence_transformers) and 
                    (stored_use_hf or stored_use_st) and 
                    os.path.exists(f"{self.index_path}.faiss")):
                    self.index = faiss.read_index(f"{self.index_path}.faiss")
                
                print(f"Loaded vector index from {self.index_path}")
                return True
        except Exception as e:
            print(f"Error loading index: {str(e)}")
        
        return False
    
    def clear_index(self):
        """Clear the current index and documents"""
        self.index = None
        self.documents = []
        self.vocabulary = {}
        self.idf_scores = {}
        print("Cleared vector index")
    
    def get_stats(self) -> Dict:
        """Get statistics about the vector store"""
        return {
            'total_documents': len(self.documents),
            'index_size': self.index.ntotal if ((self.use_huggingface or self.use_sentence_transformers) and self.index) else len(self.documents),
            'dimension': self.dimension,
            'model_name': self.model_name,
            'search_type': 'HuggingFace Embeddings + FAISS' if self.use_huggingface else 'Sentence Transformers + FAISS' if self.use_sentence_transformers else 'Simple TF-IDF'
        }