-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_database.py
More file actions
49 lines (36 loc) · 1.49 KB
/
vector_database.py
File metadata and controls
49 lines (36 loc) · 1.49 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
from langchain_community.document_loaders import PDFPlumberLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.embeddings import OllamaEmbeddings
from langchain_community.vectorstores import FAISS
#Step 1: Upload & Load raw PDF(s)
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 = 'universal_declaration_of_human_rights.pdf'
documents = load_pdf(file_path)
#print("PDF pages: ",len(documents))
#Step 2: 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))
#Step 3: Setup Embeddings Model (Use DeepSeek R1 with Ollama)
ollama_model_name="deepseek-r1:1.5b"
def get_embedding_model(ollama_model_name):
embeddings = OllamaEmbeddings(model=ollama_model_name)
return embeddings
#Step 4: Index Documents **Store embeddings in FAISS (vector store)
FAISS_DB_PATH="vectorstore/db_faiss"
faiss_db=FAISS.from_documents(text_chunks, get_embedding_model(ollama_model_name))
faiss_db.save_local(FAISS_DB_PATH)