Spaces:
Sleeping
Sleeping
Update app/policy_vector_db.py
Browse files- app/policy_vector_db.py +7 -8
app/policy_vector_db.py
CHANGED
|
@@ -134,7 +134,6 @@ class PolicyVectorDB:
|
|
| 134 |
|
| 135 |
logger.info(f"Finished adding {len(new_chunks)} chunks.")
|
| 136 |
|
| 137 |
-
# ✅ FIXED: Corrected indexing for distances
|
| 138 |
def search(self, query_text: str, top_k: int = None, filters: Dict = None) -> List[Dict]:
|
| 139 |
"""
|
| 140 |
Enhanced search with metadata filtering and entity extraction.
|
|
@@ -177,21 +176,21 @@ class PolicyVectorDB:
|
|
| 177 |
|
| 178 |
search_results = []
|
| 179 |
if results and results.get('documents') and results['documents'][0]:
|
| 180 |
-
for i, doc in enumerate(results['documents']):
|
| 181 |
-
#
|
| 182 |
-
relevance_score = 1 - results['distances'][i]
|
| 183 |
|
| 184 |
if relevance_score >= self.relevance_threshold:
|
| 185 |
result = {
|
| 186 |
'text': doc,
|
| 187 |
-
'metadata': results['metadatas'][i],
|
| 188 |
'relevance_score': relevance_score
|
| 189 |
}
|
| 190 |
|
| 191 |
# Add monetary filtering if amounts mentioned in query
|
| 192 |
-
if entities['monetary_values'] and 'limit_normalized' in results['metadatas'][i]:
|
| 193 |
try:
|
| 194 |
-
chunk_limit = float(results['metadatas'][i]['limit_normalized'])
|
| 195 |
query_amount = max(entities['monetary_values'])
|
| 196 |
|
| 197 |
# Boost relevance if the limit is appropriate for the query amount
|
|
@@ -303,4 +302,4 @@ def ensure_db_populated(db_instance: PolicyVectorDB, chunks_file_path: str) -> b
|
|
| 303 |
return True
|
| 304 |
except Exception as e:
|
| 305 |
logger.error(f"An error occurred during DB population check: {e}", exc_info=True)
|
| 306 |
-
return False
|
|
|
|
| 134 |
|
| 135 |
logger.info(f"Finished adding {len(new_chunks)} chunks.")
|
| 136 |
|
|
|
|
| 137 |
def search(self, query_text: str, top_k: int = None, filters: Dict = None) -> List[Dict]:
|
| 138 |
"""
|
| 139 |
Enhanced search with metadata filtering and entity extraction.
|
|
|
|
| 176 |
|
| 177 |
search_results = []
|
| 178 |
if results and results.get('documents') and results['documents'][0]:
|
| 179 |
+
for i, doc in enumerate(results['documents'][0]): # Fixed: iterate over results['documents'][0]
|
| 180 |
+
# Fixed: Access distances correctly as results['distances'][0][i]
|
| 181 |
+
relevance_score = 1 - results['distances'][0][i]
|
| 182 |
|
| 183 |
if relevance_score >= self.relevance_threshold:
|
| 184 |
result = {
|
| 185 |
'text': doc,
|
| 186 |
+
'metadata': results['metadatas'][0][i], # Fixed: Access metadata correctly
|
| 187 |
'relevance_score': relevance_score
|
| 188 |
}
|
| 189 |
|
| 190 |
# Add monetary filtering if amounts mentioned in query
|
| 191 |
+
if entities['monetary_values'] and 'limit_normalized' in results['metadatas'][0][i]:
|
| 192 |
try:
|
| 193 |
+
chunk_limit = float(results['metadatas'][0][i]['limit_normalized'])
|
| 194 |
query_amount = max(entities['monetary_values'])
|
| 195 |
|
| 196 |
# Boost relevance if the limit is appropriate for the query amount
|
|
|
|
| 302 |
return True
|
| 303 |
except Exception as e:
|
| 304 |
logger.error(f"An error occurred during DB population check: {e}", exc_info=True)
|
| 305 |
+
return False
|