forked from Priyamakeshwari/TeachGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.py
72 lines (60 loc) · 2.08 KB
/
builder.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import logging
from langchain.document_loaders import DirectoryLoader, PDFMinerLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceInstructEmbeddings
from langchain.vectorstores import Chroma
from config import (
PERSIST_DIRECTORY,
MODEL_DIRECTORY,
SOURCE_DIR,
EMBEDDING_MODEL,
DEVICE_TYPE,
CHROMA_SETTINGS,
)
def load_docs(directory: str = SOURCE_DIR):
"""
Loads documents from a specified directory.
Args:
directory (str): The directory path containing PDF documents.
Returns:
list: A list of loaded documents.
"""
loader = DirectoryLoader(directory, glob="**/*.pdf", use_multithreading=True, loader_cls=PDFMinerLoader)
docs = loader.load()
logging.info(f"Loaded {len(docs)} documents from {directory}")
return docs
def split_docs(documents,chunk_size=1000,chunk_overlap=200):
"""
Splits documents into smaller chunks for processing.
Args:
documents (list): List of documents to be split.
chunk_size (int): The size of each chunk.
chunk_overlap (int): The overlap between adjacent chunks.
Returns:
list: List of split documents.
"""
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
docs = text_splitter.split_documents(documents)
logging.info(f"Split {len(docs)} documents into chunks")
return docs
def builder():
"""
Builds the database by loading, splitting, and embedding documents.
"""
logging.info("Building the database")
documents = load_docs()
docs = split_docs(documents)
embeddings = HuggingFaceInstructEmbeddings(
model_name=EMBEDDING_MODEL,
model_kwargs={"device": DEVICE_TYPE},
cache_folder=MODEL_DIRECTORY,
)
db = Chroma.from_documents(
docs,
embeddings,
persist_directory=PERSIST_DIRECTORY,
client_settings=CHROMA_SETTINGS,
)
logging.info(f"Loaded Documents to Chroma DB Successfully")
if __name__ == '__main__':
builder()