prasad799596 commited on
Commit
e4f95be
·
verified ·
1 Parent(s): b802336

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +41 -0
rag.py CHANGED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.document_loaders import PyPDFLoader, TextLoader
2
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
3
+ from langchain_community.vectorstores import FAISS
4
+ from langchain_community.embeddings import HuggingFaceEmbeddings
5
+
6
+
7
+ def create_vectorstore(file_path):
8
+
9
+ # ----------------------------
10
+ # Load Document
11
+ # ----------------------------
12
+ if file_path.endswith(".pdf"):
13
+ loader = PyPDFLoader(file_path)
14
+ else:
15
+ loader = TextLoader(file_path, encoding="utf-8")
16
+
17
+ documents = loader.load()
18
+
19
+ # ----------------------------
20
+ # Split Documents into Chunks
21
+ # ----------------------------
22
+ text_splitter = RecursiveCharacterTextSplitter(
23
+ chunk_size=1000,
24
+ chunk_overlap=200
25
+ )
26
+
27
+ docs = text_splitter.split_documents(documents)
28
+
29
+ # ----------------------------
30
+ # Create Embeddings
31
+ # ----------------------------
32
+ embeddings = HuggingFaceEmbeddings(
33
+ model_name="sentence-transformers/all-MiniLM-L6-v2"
34
+ )
35
+
36
+ # ----------------------------
37
+ # Create FAISS Vector Store
38
+ # ----------------------------
39
+ vectorstore = FAISS.from_documents(docs, embeddings)
40
+
41
+ return vectorstore