-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_store.py
More file actions
64 lines (51 loc) · 1.91 KB
/
vector_store.py
File metadata and controls
64 lines (51 loc) · 1.91 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
61
62
63
from dotenv import load_dotenv
import os
from pathlib import Path
# LangChain & LangGraph Imports
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma
from langchain_community.document_loaders import PyMuPDFLoader, Docx2txtLoader, TextLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
# Configuration & Environment
load_dotenv()
# LLM_MODEL = os.getenv("MODEL_LLM")
EMB_MODEL = os.getenv("MODEL_EMBEDDING")
DOCS_DIR = 'exports'
PERSIST_DIR = 'vector-store'
def ingest():
if not os.path.exists(DOCS_DIR):
print(f"Error: {DOCS_DIR} folder not found.")
return
# Document Loading Logic
print("--- Loading Documents ---")
paths = list(Path(DOCS_DIR).rglob("*"))
raw_docs = []
for p in paths:
try:
if p.suffix.lower() == ".pdf":
loader = PyMuPDFLoader(str(p))
raw_docs.extend(loader.load())
elif p.suffix.lower() == ".docx":
raw_docs.extend(Docx2txtLoader(str(p)).load())
elif p.suffix.lower() in {".txt", ".md"}:
raw_docs.extend(TextLoader(str(p), encoding="utf-8").load())
except Exception as e:
print(f"Skipping {p.name} due to error: {e}")
if not raw_docs:
print("No documents found to index.")
return
print(f"Splitting {len(raw_docs)} documents...")
splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=120)
all_splits = splitter.split_documents(raw_docs)
print(f"Generating embeddings and saving to {PERSIST_DIR}...")
embeddings = OpenAIEmbeddings(model=EMB_MODEL)
# Create the vector store on disk
Chroma.from_documents(
documents=all_splits,
embedding=embeddings,
persist_directory=PERSIST_DIR,
collection_name="collection_test"
)
print("Indexing complete!")
if __name__ == "__main__":
ingest()