Almas-123 commited on
Commit
d8cd316
·
verified ·
1 Parent(s): 97783ce

Create rag_helper.py

Browse files
Files changed (1) hide show
  1. rag_helper.py +20 -0
rag_helper.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sentence_transformers import SentenceTransformer
2
+ import faiss
3
+ import os
4
+
5
+ model = SentenceTransformer("all-MiniLM-L6-v2")
6
+
7
+ def build_knowledge_base(path="data/medical_guides"):
8
+ texts = []
9
+ for fname in os.listdir(path):
10
+ with open(os.path.join(path, fname), "r") as f:
11
+ texts.append(f.read())
12
+ embeddings = model.encode(texts)
13
+ index = faiss.IndexFlatL2(embeddings.shape[1])
14
+ index.add(embeddings)
15
+ return index, texts, os.listdir(path)
16
+
17
+ def get_medical_guidance(query, index, texts):
18
+ query_vec = model.encode([query])
19
+ D, I = index.search(query_vec, k=1)
20
+ return texts[I[0][0]]