shahabkahn's picture
Update db.py
5bbafa5 verified
raw
history blame
964 Bytes
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from dotenv import load_dotenv
import os
import logging
load_dotenv()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def load_embeddings():
return HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": "cpu"})
def load_vector_database(embeddings):
try:
db = FAISS.load_local("vectorstore/db_faiss", embeddings, allow_dangerous_deserialization=True)
logger.info("Vector database loaded successfully!")
return db
except Exception as e:
logger.error(f"Failed to load vector database: {e}")
raise e
embeddings = load_embeddings()
vector_db = load_vector_database(embeddings)