Skip to content

Commit 6dcdd8a

Browse files
Replace pickle with JSON for safer document serialization
Signed-off-by: gopal-raj-suresh <gopal.raj.dummugudupu@cloud2labs.com>
1 parent 4bb0fe3 commit 6dcdd8a

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

sample_solutions/MultiAgentQnA/api/services/rag_service.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,44 +157,44 @@ def _save_index(self):
157157
try:
158158
# Save FAISS index
159159
faiss.write_index(self.index, os.path.join(self.vector_store_path, "index.faiss"))
160-
161-
# Save documents as a simple format (JSON would be better but keeping it simple)
162-
import pickle
163-
with open(os.path.join(self.vector_store_path, "documents.pkl"), "wb") as f:
164-
pickle.dump(self.documents, f)
165-
160+
161+
# Save documents as JSON (safer than pickle)
162+
import json
163+
with open(os.path.join(self.vector_store_path, "documents.json"), "w", encoding="utf-8") as f:
164+
json.dump(self.documents, f, ensure_ascii=False, indent=2)
165+
166166
logger.info(f"Index saved to {self.vector_store_path}")
167-
167+
168168
except Exception as e:
169169
logger.error(f"Error saving index: {str(e)}")
170170
raise
171171

172172
def load_index(self) -> bool:
173173
"""
174174
Load FAISS index from disk
175-
175+
176176
Returns:
177177
True if loaded successfully, False otherwise
178178
"""
179179
try:
180180
faiss_path = os.path.join(self.vector_store_path, "index.faiss")
181-
docs_path = os.path.join(self.vector_store_path, "documents.pkl")
182-
181+
docs_path = os.path.join(self.vector_store_path, "documents.json")
182+
183183
if not os.path.exists(faiss_path) or not os.path.exists(docs_path):
184184
logger.warning("No existing index found")
185185
return False
186-
186+
187187
# Load FAISS index
188188
self.index = faiss.read_index(faiss_path)
189-
190-
# Load documents
191-
import pickle
192-
with open(docs_path, "rb") as f:
193-
self.documents = pickle.load(f)
194-
189+
190+
# Load documents from JSON (safer than pickle)
191+
import json
192+
with open(docs_path, "r", encoding="utf-8") as f:
193+
self.documents = json.load(f)
194+
195195
logger.info(f"Loaded index with {self.index.ntotal} vectors")
196196
return True
197-
197+
198198
except Exception as e:
199199
logger.error(f"Error loading index: {str(e)}")
200200
return False

0 commit comments

Comments
 (0)