Spaces:
Sleeping
Sleeping
Update rag/retriever.py
Browse files- rag/retriever.py +22 -6
rag/retriever.py
CHANGED
|
@@ -12,11 +12,27 @@ def retrieve_context(query: str):
|
|
| 12 |
distances, indices = index.search(query_embedding, TOP_K_RETRIEVAL)
|
| 13 |
|
| 14 |
results = []
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
return results
|
|
|
|
|
|
| 12 |
distances, indices = index.search(query_embedding, TOP_K_RETRIEVAL)
|
| 13 |
|
| 14 |
results = []
|
| 15 |
+
|
| 16 |
+
for i in range(len(indices[0])):
|
| 17 |
+
idx = int(indices[0][i]) # 🔥 CRITICAL: convert np.int64 → int
|
| 18 |
+
|
| 19 |
+
# Case 1: metadata is a list
|
| 20 |
+
if isinstance(metadata, list):
|
| 21 |
+
if idx < len(metadata):
|
| 22 |
+
meta = metadata[idx]
|
| 23 |
+
results.append({
|
| 24 |
+
"text": documents[idx],
|
| 25 |
+
"source": meta.get("source", "unknown")
|
| 26 |
+
})
|
| 27 |
+
|
| 28 |
+
# Case 2: metadata is a dict
|
| 29 |
+
elif isinstance(metadata, dict):
|
| 30 |
+
meta = metadata.get(idx)
|
| 31 |
+
if meta:
|
| 32 |
+
results.append({
|
| 33 |
+
"text": documents[idx],
|
| 34 |
+
"source": meta.get("source", "unknown")
|
| 35 |
+
})
|
| 36 |
|
| 37 |
return results
|
| 38 |
+
|