-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathknowledge_base.py
More file actions
53 lines (42 loc) 路 1.53 KB
/
Copy pathknowledge_base.py
File metadata and controls
53 lines (42 loc) 路 1.53 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
52
53
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
# Load documents from a directory
FILE_PATH = "books/"
def load_documents(data):
loader = DirectoryLoader(
path=FILE_PATH,
glob="**/*.pdf",
show_progress=True,
loader_cls=PyPDFLoader,
)
documents = loader.load()
return documents
documents = load_documents(data=FILE_PATH)
# print(f"Loaded {len(documents)} documents from {FILE_PATH}")
# Split documents into chunks
def create_chunks(extracted_data):
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
)
text_chunks = text_splitter.split_documents(extracted_data)
return text_chunks
text_chunks=create_chunks(extracted_data=documents)
# print(f"Created {len(text_chunks)} chunks from {len(documents)} documents")
# Create embeddings for the text chunks
def get_embedding_model():
embedding_model = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2",
)
return embedding_model
embeddings = get_embedding_model()
# Store embeddings in FAISS
DB_PATH = "vectorstore/faiss_index"
vectorstore = FAISS.from_documents(
documents=text_chunks,
embedding=embeddings,
)
vectorstore.save_local(DB_PATH)
print(f"Saved FAISS index to {DB_PATH}")