-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrag-document-loader.py
More file actions
60 lines (49 loc) · 2.22 KB
/
rag-document-loader.py
File metadata and controls
60 lines (49 loc) · 2.22 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
54
55
56
57
58
59
60
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
from langchain_community.document_loaders import DirectoryLoader
from langchain_text_splitters import CharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_chroma import Chroma
from dotenv import load_dotenv
import os
import logging
from langchain_core.documents import Document
# Load environment variables
load_dotenv()
# Initialize logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
rag_directory = os.getenv('DIRECTORY', 'meeting_notes')
def load_documents(directory) -> list[Document]:
"""
Loads documents from the specified directory, splits them into chunks, and returns the chunks.
Args:
directory (str): The directory to load documents from.
Returns:
list[Document]: A list of Document objects, each representing a chunk of text.
"""
try:
loader = DirectoryLoader(directory) # Load documents from the directory
documents = loader.load() # Load all documents
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) # Split documents into chunks
return text_splitter.split_documents(documents) # Return the split documents
except Exception as e:
logger.error(f"Error loading documents from directory {directory}: {e}")
return [] # Return an empty list if there's an error
def main():
try:
# Get the documents split into chunks
docs = load_documents(rag_directory)
if not docs:
logger.error("No documents loaded. Exiting.")
return
# Create the open-source embedding function
embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
# Load the documents into Chroma and save it to the disk using the new method
Chroma.from_documents(
documents=docs, embedding=embedding_function, persist_directory="./chroma_db"
)
logger.info("Documents successfully loaded into Chroma.")
except Exception as e:
logger.error(f"Error in main: {e}")
if __name__ == "__main__":
main()