-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
40 lines (36 loc) · 1.29 KB
/
utils.py
File metadata and controls
40 lines (36 loc) · 1.29 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
import os
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_google_genai import GoogleGenerativeAIEmbeddings
VECTORSTORE_PATH = "vectorstore"
def load_documents(file_paths):
"""Load PDFs and return a list of documents with metadata."""
all_docs = []
for path in file_paths:
loader = PyPDFLoader(path)
docs = loader.load()
# Add page/file metadata
for i, doc in enumerate(docs):
doc.metadata["source_page"] = i + 1
doc.metadata["source_file"] = os.path.basename(path)
all_docs.extend(docs)
return all_docs
def build_vectorstore(docs, persist_path=VECTORSTORE_PATH):
"""Build FAISS vectorstore from documents."""
embeddings = GoogleGenerativeAIEmbeddings(
model="models/embedding-001"
)
db = FAISS.from_documents(docs, embeddings)
os.makedirs(persist_path, exist_ok=True)
db.save_local(persist_path)
return db
def load_vectorstore(persist_path=VECTORSTORE_PATH):
"""Load existing FAISS vectorstore safely."""
embeddings = GoogleGenerativeAIEmbeddings(
model="models/embedding-001"
)
return FAISS.load_local(
persist_path,
embeddings,
allow_dangerous_deserialization=True
)