-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector_database.py
More file actions
51 lines (36 loc) · 1.38 KB
/
vector_database.py
File metadata and controls
51 lines (36 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from langchain_community.document_loaders import PDFPlumberLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_ollama import OllamaEmbeddings
from langchain_community.vectorstores import FAISS
#step1: upload and load raw PDF file
pdfs_directory= 'pdfs/'
def upload_pdf(file):
with open(pdfs_directory + file.name, "wb") as f:
f.write(file.getbuffer())
def load_pdf(file_path):
loader = PDFPlumberLoader(file_path)
documents = loader.load()
return documents
file_path="eng.pdf"
documents = load_pdf(file_path)
# print("Pdf pages:",len(documents))
#step2:create chunks
def create_chunks(documents):
text_splitter=RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
add_start_index=True
)
text_chunks= text_splitter.split_documents(documents)
return text_chunks
text_chunks= create_chunks(documents)
# print("chunks count:",len(text_chunks))
#step3:setup embeddings model (using DeepSeek with Ollama)
ollama_model_name="deepseek-r1:8b"
def get_embeddings_model(ollama_model_name):
embeddings = OllamaEmbeddings(model=ollama_model_name)
return embeddings
#step4: Index Documents **Store embeddings in FAISS (vector store)
FAISS_DB_path = "vectorstore/db_faiss"
faiss_db = FAISS.from_documents(text_chunks, get_embeddings_model(ollama_model_name))
faiss_db.save_local(FAISS_DB_path)