-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_database.py
More file actions
43 lines (37 loc) · 1.55 KB
/
vector_database.py
File metadata and controls
43 lines (37 loc) · 1.55 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
from langchain_community.document_loaders import PDFPlumberLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_ollama import OllamaEmbeddings
from langchain_community.vectorstores import FAISS
import os
pdfs_directory = 'pdfs/'
def upload_pdf(file):
"""Uploads a PDF file to the pdfs/ directory."""
os.makedirs(pdfs_directory, exist_ok=True) # Ensure directory exists
file_path = os.path.join(pdfs_directory, file.name)
with open(file_path, "wb") as f:
f.write(file.getbuffer())
return file_path # Returning path for further processing
def load_pdf(file_path):
"""Loads a PDF using PDFPlumberLoader."""
loader = PDFPlumberLoader(file_path)
documents = loader.load()
return documents # Return loaded documents
file_path = "ilovepdf_merged.pdf"
documents = load_pdf(file_path)
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)
#step3 : setup embeeding model (use deepseek r1 with ollama)
olama_model_name= "deepseek-r1:1.5b"
def get_embeeding_model(olama_model_name):
embeeding = OllamaEmbeddings(model= olama_model_name)
return embeeding
FAISS_DB_PATH= "vectorstore/db_faiss"
faiss_db =FAISS.from_documents(text_chunks,get_embeeding_model(olama_model_name))
faiss_db.save_local(FAISS_DB_PATH)