Spaces:
Runtime error
Runtime error
update and add logger
Browse files
core/collection_router_retriever.py
CHANGED
|
@@ -86,6 +86,12 @@ class CollectionRouterRetriever:
|
|
| 86 |
logger.warning("No documents found in collection=%s for BM25 indexing", collection_name)
|
| 87 |
return None
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
# Extract documents and tokenize for BM25
|
| 90 |
docs_for_bm25 = []
|
| 91 |
for point in points_list:
|
|
@@ -169,7 +175,11 @@ class CollectionRouterRetriever:
|
|
| 169 |
bm25_ranked = {} # {doc_key -> rank}
|
| 170 |
if all_docs_dict:
|
| 171 |
try:
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
# For each collection, use cached BM25 index
|
| 175 |
for collection_name in collections:
|
|
@@ -195,15 +205,21 @@ class CollectionRouterRetriever:
|
|
| 195 |
tokenized_subset = [content.lower().split() for content in content_for_bm25]
|
| 196 |
bm25_subset = BM25Okapi(tokenized_subset, k1=1.5, b=0.5)
|
| 197 |
bm25_results = bm25_subset.get_top_n(tokenized_query, content_for_bm25, n=len(content_for_bm25))
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
except Exception:
|
| 209 |
logger.exception("BM25 search failed, falling back to vector-only")
|
|
@@ -250,6 +266,10 @@ class CollectionRouterRetriever:
|
|
| 250 |
limit=candidate_k,
|
| 251 |
alpha=alpha,
|
| 252 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
if cohort_scoped:
|
| 255 |
deduplicated = []
|
|
@@ -284,8 +304,11 @@ class CollectionRouterRetriever:
|
|
| 284 |
|
| 285 |
deduplicated = []
|
| 286 |
seen = set()
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
-
for doc in routed_docs +
|
| 289 |
key = self._doc_key(doc)
|
| 290 |
if key in seen:
|
| 291 |
continue
|
|
|
|
| 86 |
logger.warning("No documents found in collection=%s for BM25 indexing", collection_name)
|
| 87 |
return None
|
| 88 |
|
| 89 |
+
# Filter out None values
|
| 90 |
+
points_list = [p for p in points_list if p is not None]
|
| 91 |
+
if not points_list:
|
| 92 |
+
logger.warning("No valid points found in collection=%s after filtering", collection_name)
|
| 93 |
+
return None
|
| 94 |
+
|
| 95 |
# Extract documents and tokenize for BM25
|
| 96 |
docs_for_bm25 = []
|
| 97 |
for point in points_list:
|
|
|
|
| 175 |
bm25_ranked = {} # {doc_key -> rank}
|
| 176 |
if all_docs_dict:
|
| 177 |
try:
|
| 178 |
+
# Validate query is not empty
|
| 179 |
+
if not query.strip():
|
| 180 |
+
logger.warning("Query is empty, skipping BM25 search")
|
| 181 |
+
else:
|
| 182 |
+
tokenized_query = query.lower().split()
|
| 183 |
|
| 184 |
# For each collection, use cached BM25 index
|
| 185 |
for collection_name in collections:
|
|
|
|
| 205 |
tokenized_subset = [content.lower().split() for content in content_for_bm25]
|
| 206 |
bm25_subset = BM25Okapi(tokenized_subset, k1=1.5, b=0.5)
|
| 207 |
bm25_results = bm25_subset.get_top_n(tokenized_query, content_for_bm25, n=len(content_for_bm25))
|
| 208 |
+
|
| 209 |
+
bm25_rank = 0
|
| 210 |
+
for content in bm25_results: # bm25_results contains strings
|
| 211 |
+
# Find matching doc by content (handles duplicates)
|
| 212 |
+
matched_doc = None
|
| 213 |
+
for doc in docs_from_collection:
|
| 214 |
+
if doc.page_content == content:
|
| 215 |
+
matched_doc = doc
|
| 216 |
+
break
|
| 217 |
+
|
| 218 |
+
if matched_doc:
|
| 219 |
+
doc_key = self._doc_key(matched_doc)
|
| 220 |
+
if doc_key not in bm25_ranked:
|
| 221 |
+
bm25_rank += 1
|
| 222 |
+
bm25_ranked[doc_key] = bm25_rank
|
| 223 |
|
| 224 |
except Exception:
|
| 225 |
logger.exception("BM25 search failed, falling back to vector-only")
|
|
|
|
| 266 |
limit=candidate_k,
|
| 267 |
alpha=alpha,
|
| 268 |
)
|
| 269 |
+
|
| 270 |
+
# Log warning if no documents found
|
| 271 |
+
if not routed_docs:
|
| 272 |
+
logger.warning("No documents found for query=%s, cohort=%s", query[:50], cohort_key)
|
| 273 |
|
| 274 |
if cohort_scoped:
|
| 275 |
deduplicated = []
|
|
|
|
| 304 |
|
| 305 |
deduplicated = []
|
| 306 |
seen = set()
|
| 307 |
+
|
| 308 |
+
# Safe handling of fallback_docs which might be None
|
| 309 |
+
fallback_docs_list = list(fallback_docs) if fallback_docs else []
|
| 310 |
|
| 311 |
+
for doc in routed_docs + fallback_docs_list:
|
| 312 |
key = self._doc_key(doc)
|
| 313 |
if key in seen:
|
| 314 |
continue
|