Update search_engine.py
Browse files- search_engine.py +82 -7
search_engine.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# search_engine.py
|
| 2 |
import faiss
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
from utils import setup_logger
|
| 6 |
from config import Config
|
|
@@ -10,21 +11,95 @@ logger = setup_logger('search_engine')
|
|
| 10 |
class SearchEngine:
|
| 11 |
def __init__(self, documents, embeddings):
|
| 12 |
self.documents = documents
|
|
|
|
| 13 |
self.index = self._build_faiss_index(embeddings)
|
| 14 |
self.model = SentenceTransformer(Config.EMBEDDING_MODEL)
|
| 15 |
-
|
| 16 |
def _build_faiss_index(self, embeddings):
|
| 17 |
dimension = embeddings.shape[1]
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return index
|
| 21 |
-
|
| 22 |
def search(self, query):
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
logger.error(f"Error searching documents: {e}")
|
| 29 |
return pd.DataFrame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# search_engine.py
|
| 2 |
import faiss
|
| 3 |
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from utils import setup_logger
|
| 7 |
from config import Config
|
|
|
|
| 11 |
class SearchEngine:
|
| 12 |
def __init__(self, documents, embeddings):
|
| 13 |
self.documents = documents
|
| 14 |
+
self.embeddings = embeddings # NEW: Store embeddings for reference
|
| 15 |
self.index = self._build_faiss_index(embeddings)
|
| 16 |
self.model = SentenceTransformer(Config.EMBEDDING_MODEL)
|
| 17 |
+
|
| 18 |
def _build_faiss_index(self, embeddings):
|
| 19 |
dimension = embeddings.shape[1]
|
| 20 |
+
|
| 21 |
+
# NEW: Use IndexFlatIP for cosine similarity (better than L2)
|
| 22 |
+
index = faiss.IndexFlatIP(dimension)
|
| 23 |
+
|
| 24 |
+
# NEW: Normalize embeddings for cosine similarity
|
| 25 |
+
embeddings_normalized = embeddings.astype('float32').copy()
|
| 26 |
+
faiss.normalize_L2(embeddings_normalized)
|
| 27 |
+
index.add(embeddings_normalized)
|
| 28 |
+
|
| 29 |
+
logger.info(f"FAISS index built with {embeddings.shape[0]} vectors (cosine similarity)")
|
| 30 |
return index
|
| 31 |
+
|
| 32 |
def search(self, query):
|
| 33 |
try:
|
| 34 |
+
# Encode query
|
| 35 |
+
query_embedding = self.model.encode([query]).astype('float32')
|
| 36 |
+
|
| 37 |
+
# NEW: Normalize query for cosine similarity
|
| 38 |
+
faiss.normalize_L2(query_embedding)
|
| 39 |
+
|
| 40 |
+
# NEW: Get more results to filter
|
| 41 |
+
search_k = min(Config.TOP_K * 2, len(self.documents))
|
| 42 |
+
scores, indices = self.index.search(query_embedding, search_k)
|
| 43 |
+
|
| 44 |
+
# NEW: Filter by similarity threshold
|
| 45 |
+
valid_mask = scores[0] >= Config.MIN_SIMILARITY_SCORE
|
| 46 |
+
filtered_indices = indices[0][valid_mask]
|
| 47 |
+
filtered_scores = scores[0][valid_mask]
|
| 48 |
+
|
| 49 |
+
# NEW: Limit to TOP_K after filtering
|
| 50 |
+
if len(filtered_indices) > Config.TOP_K:
|
| 51 |
+
filtered_indices = filtered_indices[:Config.TOP_K]
|
| 52 |
+
filtered_scores = filtered_scores[:Config.TOP_K]
|
| 53 |
+
|
| 54 |
+
# NEW: Handle no results case
|
| 55 |
+
if len(filtered_indices) == 0:
|
| 56 |
+
logger.warning(f"No results above similarity threshold {Config.MIN_SIMILARITY_SCORE}")
|
| 57 |
+
return pd.DataFrame()
|
| 58 |
+
|
| 59 |
+
# NEW: Add similarity scores to results
|
| 60 |
+
results = self.documents.iloc[filtered_indices].copy()
|
| 61 |
+
results['similarity_score'] = filtered_scores
|
| 62 |
+
|
| 63 |
+
# NEW: Sort by similarity score (best first)
|
| 64 |
+
results = results.sort_values('similarity_score', ascending=False)
|
| 65 |
+
|
| 66 |
+
# NEW: Better logging
|
| 67 |
+
logger.info(f"Found {len(results)} chunks (scores: {filtered_scores.min():.2f} - {filtered_scores.max():.2f})")
|
| 68 |
+
|
| 69 |
+
return results
|
| 70 |
+
|
| 71 |
except Exception as e:
|
| 72 |
logger.error(f"Error searching documents: {e}")
|
| 73 |
return pd.DataFrame()
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
---
|
| 77 |
+
|
| 78 |
+
## What Was Added (All marked with "# NEW"):
|
| 79 |
+
|
| 80 |
+
1. ✅ **Store embeddings** - Keep reference for future use
|
| 81 |
+
2. ✅ **IndexFlatIP** - Changed from `IndexFlatL2` to `IndexFlatIP` for cosine similarity
|
| 82 |
+
3. ✅ **Normalize embeddings** - Required for cosine similarity to work
|
| 83 |
+
4. ✅ **Normalize query** - Query must also be normalized
|
| 84 |
+
5. ✅ **Search more results** - Get 2x TOP_K to filter from
|
| 85 |
+
6. ✅ **Filter by threshold** - Only keep results ≥ MIN_SIMILARITY_SCORE
|
| 86 |
+
7. ✅ **Limit to TOP_K** - After filtering, keep only top K
|
| 87 |
+
8. ✅ **Handle no results** - Return empty if nothing matches
|
| 88 |
+
9. ✅ **Add scores to results** - Include similarity scores in dataframe
|
| 89 |
+
10. ✅ **Sort by score** - Best matches first
|
| 90 |
+
11. ✅ **Better logging** - Show score range
|
| 91 |
+
|
| 92 |
+
---
|
| 93 |
+
|
| 94 |
+
## Impact on Accuracy:
|
| 95 |
+
|
| 96 |
+
**Before:**
|
| 97 |
+
```
|
| 98 |
+
Query: "نسبة الحضور"
|
| 99 |
+
Results: 5 chunks (some irrelevant, scores unknown)
|
| 100 |
+
```
|
| 101 |
|
| 102 |
+
**After:**
|
| 103 |
+
```
|
| 104 |
+
Query: "نسبة الحضور"
|
| 105 |
+
Results: 3 chunks (all relevant, scores: 0.72 - 0.85)
|