Spaces:
Runtime error
Runtime error
Update rag.py
Browse files
rag.py
CHANGED
@@ -1,28 +1,35 @@
|
|
1 |
-
from
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
# load model only once
|
6 |
-
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
7 |
|
8 |
class VectorStore:
|
9 |
def __init__(self):
|
10 |
-
self.
|
11 |
-
self.
|
12 |
-
self.
|
|
|
|
|
|
|
13 |
|
14 |
def add_texts(self, texts):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
self.
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def retrieve(self, query, top_k=3):
|
23 |
-
|
24 |
-
if not self.index:
|
25 |
return []
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
1 |
+
from langchain_community.vectorstores import FAISS
|
2 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
3 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
|
|
|
|
|
4 |
|
5 |
class VectorStore:
|
6 |
def __init__(self):
|
7 |
+
self.vectorstore = None
|
8 |
+
self.embedder = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
9 |
+
self.text_splitter = RecursiveCharacterTextSplitter(
|
10 |
+
chunk_size=500,
|
11 |
+
chunk_overlap=50
|
12 |
+
)
|
13 |
|
14 |
def add_texts(self, texts):
|
15 |
+
if not texts:
|
16 |
+
return
|
17 |
+
|
18 |
+
# Split and add texts
|
19 |
+
if self.vectorstore is None:
|
20 |
+
self.vectorstore = FAISS.from_texts(
|
21 |
+
self.text_splitter.split_text("\n\n".join(texts)),
|
22 |
+
self.embedder
|
23 |
+
)
|
24 |
+
else:
|
25 |
+
self.vectorstore.add_texts(
|
26 |
+
self.text_splitter.split_text("\n\n".join(texts))
|
27 |
+
)
|
28 |
|
29 |
def retrieve(self, query, top_k=3):
|
30 |
+
if self.vectorstore is None:
|
|
|
31 |
return []
|
32 |
+
return [
|
33 |
+
doc.page_content
|
34 |
+
for doc in self.vectorstore.similarity_search(query, k=top_k)
|
35 |
+
]
|