bano1 commited on
Commit
c2bd4e0
·
verified ·
1 Parent(s): 1633f3e

Update src/vector_store.py

Browse files
Files changed (1) hide show
  1. src/vector_store.py +60 -16
src/vector_store.py CHANGED
@@ -19,11 +19,15 @@ class VectorStore:
19
  self.docs_path = docs_path
20
 
21
 
22
- # Load existing database if available
23
  self.load()
24
 
25
 
26
 
 
 
 
 
27
  def build(
28
  self,
29
  embeddings,
@@ -31,7 +35,7 @@ class VectorStore:
31
  ):
32
 
33
  """
34
- Build FAISS vector database
35
 
36
  embeddings:
37
  SentenceTransformer embeddings
@@ -41,6 +45,10 @@ class VectorStore:
41
  """
42
 
43
 
 
 
 
 
44
  embeddings = np.array(
45
  embeddings
46
  ).astype("float32")
@@ -55,7 +63,7 @@ class VectorStore:
55
  dimension = embeddings.shape[1]
56
 
57
 
58
- # Cosine similarity index
59
  self.index = faiss.IndexFlatIP(
60
  dimension
61
  )
@@ -69,30 +77,29 @@ class VectorStore:
69
  self.documents = documents
70
 
71
 
72
- # Save database
73
  self.save()
74
 
75
 
76
 
 
 
 
 
77
  def search(
78
  self,
79
  query_embedding,
80
  k=5
81
  ):
82
 
83
-
84
  if self.index is None:
85
-
86
  return []
87
 
88
 
89
-
90
  query_embedding = np.array(
91
  [query_embedding]
92
  ).astype("float32")
93
 
94
 
95
- # Normalize query vector
96
  faiss.normalize_L2(
97
  query_embedding
98
  )
@@ -104,7 +111,7 @@ class VectorStore:
104
  )
105
 
106
 
107
- results=[]
108
 
109
 
110
  for score, idx in zip(
@@ -116,10 +123,7 @@ class VectorStore:
116
  if idx != -1:
117
 
118
  results.append(
119
- {
120
- "text": self.documents[idx],
121
- "score": float(score)
122
- }
123
  )
124
 
125
 
@@ -127,14 +131,48 @@ class VectorStore:
127
 
128
 
129
 
 
 
 
 
130
  def save(self):
131
 
132
  """
133
- Save FAISS index and documents
 
134
  """
135
 
136
 
137
- if self.index:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
  faiss.write_index(
140
  self.index,
@@ -142,6 +180,8 @@ class VectorStore:
142
  )
143
 
144
 
 
 
145
  with open(
146
  self.docs_path,
147
  "wb"
@@ -154,10 +194,14 @@ class VectorStore:
154
 
155
 
156
 
 
 
 
 
157
  def load(self):
158
 
159
  """
160
- Load existing vector database
161
  """
162
 
163
 
 
19
  self.docs_path = docs_path
20
 
21
 
22
+ # Load existing database
23
  self.load()
24
 
25
 
26
 
27
+ # =====================================================
28
+ # BUILD VECTOR DATABASE
29
+ # =====================================================
30
+
31
  def build(
32
  self,
33
  embeddings,
 
35
  ):
36
 
37
  """
38
+ Create FAISS vector database.
39
 
40
  embeddings:
41
  SentenceTransformer embeddings
 
45
  """
46
 
47
 
48
+ if len(embeddings) == 0:
49
+ return
50
+
51
+
52
  embeddings = np.array(
53
  embeddings
54
  ).astype("float32")
 
63
  dimension = embeddings.shape[1]
64
 
65
 
66
+ # Cosine similarity search
67
  self.index = faiss.IndexFlatIP(
68
  dimension
69
  )
 
77
  self.documents = documents
78
 
79
 
 
80
  self.save()
81
 
82
 
83
 
84
+ # =====================================================
85
+ # SEARCH
86
+ # =====================================================
87
+
88
  def search(
89
  self,
90
  query_embedding,
91
  k=5
92
  ):
93
 
 
94
  if self.index is None:
 
95
  return []
96
 
97
 
 
98
  query_embedding = np.array(
99
  [query_embedding]
100
  ).astype("float32")
101
 
102
 
 
103
  faiss.normalize_L2(
104
  query_embedding
105
  )
 
111
  )
112
 
113
 
114
+ results = []
115
 
116
 
117
  for score, idx in zip(
 
123
  if idx != -1:
124
 
125
  results.append(
126
+ self.documents[idx]
 
 
 
127
  )
128
 
129
 
 
131
 
132
 
133
 
134
+ # =====================================================
135
+ # SAVE DATABASE
136
+ # =====================================================
137
+
138
  def save(self):
139
 
140
  """
141
+ Save FAISS index + documents.
142
+ Creates folders automatically.
143
  """
144
 
145
 
146
+ # Create directories if missing
147
+
148
+ index_dir = os.path.dirname(
149
+ self.index_path
150
+ )
151
+
152
+ docs_dir = os.path.dirname(
153
+ self.docs_path
154
+ )
155
+
156
+
157
+ if index_dir:
158
+
159
+ os.makedirs(
160
+ index_dir,
161
+ exist_ok=True
162
+ )
163
+
164
+
165
+ if docs_dir:
166
+
167
+ os.makedirs(
168
+ docs_dir,
169
+ exist_ok=True
170
+ )
171
+
172
+
173
+ # Save FAISS index
174
+
175
+ if self.index is not None:
176
 
177
  faiss.write_index(
178
  self.index,
 
180
  )
181
 
182
 
183
+ # Save documents
184
+
185
  with open(
186
  self.docs_path,
187
  "wb"
 
194
 
195
 
196
 
197
+ # =====================================================
198
+ # LOAD DATABASE
199
+ # =====================================================
200
+
201
  def load(self):
202
 
203
  """
204
+ Load FAISS database if available.
205
  """
206
 
207