Update app.py
Browse files
app.py
CHANGED
|
@@ -88,7 +88,7 @@ def initialize_embeddings():
|
|
| 88 |
return embeddings
|
| 89 |
|
| 90 |
def load_vector_store(embeddings):
|
| 91 |
-
"""Load FAISS vector store"""
|
| 92 |
logger.info("π Loading FAISS vector store...")
|
| 93 |
|
| 94 |
vector_store_path = CONFIG["vector_store_path"]
|
|
@@ -108,14 +108,53 @@ def load_vector_store(embeddings):
|
|
| 108 |
logger.info(f"β
Found index.faiss ({os.path.getsize(index_file)/1024/1024:.2f} MB)")
|
| 109 |
logger.info(f"β
Found index.pkl ({os.path.getsize(pkl_file)/1024:.2f} KB)")
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# ============================================================================
|
| 121 |
# RAG PIPELINE FUNCTIONS
|
|
|
|
| 88 |
return embeddings
|
| 89 |
|
| 90 |
def load_vector_store(embeddings):
|
| 91 |
+
"""Load FAISS vector store with compatibility handling"""
|
| 92 |
logger.info("π Loading FAISS vector store...")
|
| 93 |
|
| 94 |
vector_store_path = CONFIG["vector_store_path"]
|
|
|
|
| 108 |
logger.info(f"β
Found index.faiss ({os.path.getsize(index_file)/1024/1024:.2f} MB)")
|
| 109 |
logger.info(f"β
Found index.pkl ({os.path.getsize(pkl_file)/1024:.2f} KB)")
|
| 110 |
|
| 111 |
+
try:
|
| 112 |
+
# Try standard loading first
|
| 113 |
+
vectorstore = FAISS.load_local(
|
| 114 |
+
vector_store_path,
|
| 115 |
+
embeddings,
|
| 116 |
+
allow_dangerous_deserialization=True
|
| 117 |
+
)
|
| 118 |
+
logger.info(f"β
FAISS vector store loaded successfully")
|
| 119 |
+
return vectorstore
|
| 120 |
+
|
| 121 |
+
except (KeyError, AttributeError) as e:
|
| 122 |
+
logger.warning(f"β οΈ Pydantic version mismatch detected: {e}")
|
| 123 |
+
logger.info("π Attempting compatibility fix...")
|
| 124 |
+
|
| 125 |
+
# Monkey-patch for Pydantic v1/v2 compatibility
|
| 126 |
+
import pickle
|
| 127 |
+
import faiss
|
| 128 |
+
|
| 129 |
+
# Load FAISS index directly
|
| 130 |
+
index = faiss.read_index(index_file)
|
| 131 |
+
|
| 132 |
+
# Load pickle with custom unpickler
|
| 133 |
+
with open(pkl_file, "rb") as f:
|
| 134 |
+
try:
|
| 135 |
+
data = pickle.load(f)
|
| 136 |
+
docstore = data[0]
|
| 137 |
+
index_to_docstore_id = data[1]
|
| 138 |
+
except Exception as e2:
|
| 139 |
+
logger.error(f"β Failed to load pickle data: {e2}")
|
| 140 |
+
raise
|
| 141 |
+
|
| 142 |
+
# Create FAISS vectorstore manually
|
| 143 |
+
from langchain_community.docstore.in_memory import InMemoryDocstore
|
| 144 |
+
|
| 145 |
+
# Ensure docstore is proper type
|
| 146 |
+
if not isinstance(docstore, InMemoryDocstore):
|
| 147 |
+
docstore = InMemoryDocstore(docstore._dict if hasattr(docstore, '_dict') else {})
|
| 148 |
+
|
| 149 |
+
vectorstore = FAISS(
|
| 150 |
+
embedding_function=embeddings,
|
| 151 |
+
index=index,
|
| 152 |
+
docstore=docstore,
|
| 153 |
+
index_to_docstore_id=index_to_docstore_id
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
logger.info(f"β
FAISS vector store loaded with compatibility fix")
|
| 157 |
+
return vectorstore
|
| 158 |
|
| 159 |
# ============================================================================
|
| 160 |
# RAG PIPELINE FUNCTIONS
|