Spaces:
Sleeping
Sleeping
Update services/kb_creation.py
Browse files- services/kb_creation.py +13 -3
services/kb_creation.py
CHANGED
|
@@ -254,17 +254,27 @@ def search_knowledge_base(query: str, top_k: int = 10) -> dict:
|
|
| 254 |
res = collection.query(
|
| 255 |
query_embeddings=[query_embedding],
|
| 256 |
n_results=top_k,
|
| 257 |
-
include=['documents', 'metadatas', 'distances'
|
| 258 |
)
|
| 259 |
documents = (res.get("documents", [[]]) or [[]])[0]
|
| 260 |
metadatas = (res.get("metadatas", [[]]) or [[]])[0]
|
| 261 |
distances = (res.get("distances", [[]]) or [[]])[0]
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
return {
|
| 264 |
"documents": documents,
|
| 265 |
"metadatas": metadatas,
|
| 266 |
"distances": distances,
|
| 267 |
-
"ids":
|
| 268 |
}
|
| 269 |
|
| 270 |
# ---------------------- Semantic intent + Hybrid ranking --------------
|
|
|
|
| 254 |
res = collection.query(
|
| 255 |
query_embeddings=[query_embedding],
|
| 256 |
n_results=top_k,
|
| 257 |
+
include=['documents', 'metadatas', 'distances']
|
| 258 |
)
|
| 259 |
documents = (res.get("documents", [[]]) or [[]])[0]
|
| 260 |
metadatas = (res.get("metadatas", [[]]) or [[]])[0]
|
| 261 |
distances = (res.get("distances", [[]]) or [[]])[0]
|
| 262 |
+
|
| 263 |
+
# Synthesize IDs if needed (filename:section:chunk_index)
|
| 264 |
+
synthesized_ids = []
|
| 265 |
+
|
| 266 |
+
for i, m in enumerate(metadatas):
|
| 267 |
+
fn = (m or {}).get("filename", "unknown")
|
| 268 |
+
sec = (m or {}).get("section", "section")
|
| 269 |
+
idx = (m or {}).get("chunk_index", i)
|
| 270 |
+
synthesized_ids.append(f"{fn}:{sec}:{idx}")
|
| 271 |
+
|
| 272 |
+
|
| 273 |
return {
|
| 274 |
"documents": documents,
|
| 275 |
"metadatas": metadatas,
|
| 276 |
"distances": distances,
|
| 277 |
+
"ids": synthesized_ids,
|
| 278 |
}
|
| 279 |
|
| 280 |
# ---------------------- Semantic intent + Hybrid ranking --------------
|