Spaces:
Build error
Build error
Create rag.py
Browse files
rag.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer
|
| 2 |
+
import faiss
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
class RAG:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 8 |
+
self.index = None
|
| 9 |
+
self.text_chunks = []
|
| 10 |
+
|
| 11 |
+
def chunk_text(self, text, chunk_size=500, overlap=50):
|
| 12 |
+
chunks = []
|
| 13 |
+
start = 0
|
| 14 |
+
while start < len(text):
|
| 15 |
+
end = start + chunk_size
|
| 16 |
+
chunks.append(text[start:end])
|
| 17 |
+
start += chunk_size - overlap
|
| 18 |
+
return chunks
|
| 19 |
+
|
| 20 |
+
def add_document(self, text):
|
| 21 |
+
chunks = self.chunk_text(text)
|
| 22 |
+
embeddings = self.model.encode(chunks)
|
| 23 |
+
|
| 24 |
+
if self.index is None:
|
| 25 |
+
self.index = faiss.IndexFlatL2(len(embeddings[0]))
|
| 26 |
+
|
| 27 |
+
self.index.add(np.array(embeddings))
|
| 28 |
+
self.text_chunks.extend(chunks)
|
| 29 |
+
|
| 30 |
+
def retrieve(self, query, k=3):
|
| 31 |
+
if self.index is None:
|
| 32 |
+
return []
|
| 33 |
+
|
| 34 |
+
query_embedding = self.model.encode([query])
|
| 35 |
+
distances, indices = self.index.search(np.array(query_embedding), k)
|
| 36 |
+
|
| 37 |
+
return [self.text_chunks[i] for i in indices[0]]
|