abdullah-113 commited on
Commit
7bb55ea
·
verified ·
1 Parent(s): bfd16c0

Upload retriever.py

Browse files
Files changed (1) hide show
  1. api/retriever.py +26 -0
api/retriever.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from sentence_transformers import SentenceTransformer
3
+
4
+ TOP_K = 5
5
+
6
+ class ChunkRetriever:
7
+ """Stage 1 Bi-Encoder: quickly narrows down hundreds of chunks
8
+ to the few that are actually semantically relevant to the LLM output."""
9
+
10
+ def __init__(self):
11
+ self.model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
12
+ print("Retriever (MiniLM-L6-v2) loaded.")
13
+
14
+ def get_top_chunks(self, llm_output: str, chunks: list[str], top_k: int = TOP_K) -> list[str]:
15
+ """Embeds everything, ranks by cosine similarity, returns the top_k chunks."""
16
+ if len(chunks) <= top_k:
17
+ return chunks
18
+
19
+ query_embedding = self.model.encode(llm_output, normalize_embeddings=True)
20
+ chunk_embeddings = self.model.encode(chunks, normalize_embeddings=True, batch_size=32)
21
+
22
+ # cosine sim is just dot product when vectors are already L2-normalized
23
+ similarities = np.dot(chunk_embeddings, query_embedding)
24
+
25
+ top_indices = np.argsort(similarities)[::-1][:top_k]
26
+ return [chunks[i] for i in top_indices]