ayush2917 commited on
Commit
112ce73
·
verified ·
1 Parent(s): 9acee57

Update rag/retriever.py

Browse files
Files changed (1) hide show
  1. 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
- for idx in indices[0]:
16
- if idx < len(documents):
17
- results.append({
18
- "text": documents[idx],
19
- "source": metadata[idx]["source"]
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
+