-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathingest.py
More file actions
114 lines (90 loc) · 3.63 KB
/
ingest.py
File metadata and controls
114 lines (90 loc) · 3.63 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import sys
import time
from dotenv import load_dotenv
from langchain_community.document_loaders import ObsidianLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_chroma import Chroma
from langchain_google_genai import GoogleGenerativeAIEmbeddings
# Beginning by Loading the .env variables
load_dotenv()
# Hardcoded Database Path
DB_PATH = "./chroma_db"
SKIP_FILES = None
BATCH_SIZE = 8
"""
Creating a Gatekeeper Function, that will essentially not let any non-markdown documents
be converted to embeddings. As the embeddings mdel we are using is only for text embeddings
& the data fed into the AI Studio will be quite a lot & may go over the free-tier limits.
"""
def gatekeeper(raw_docs, skip_list):
valid_docs = []
print(f" Filtering {len(raw_docs)} files... ")
if skip_list is None:
skip_list = []
for doc in raw_docs:
source_path = doc.metadata.get("source", "")
filename = os.path.basename(source_path)
if not source_path.lower().endswith(".md"):
continue
if filename in skip_list:
print(f" 🚫 Skipping ignored files: {filename}")
continue
valid_docs.append(doc)
return valid_docs
def main():
# Safety Checking the API Key
if not os.getenv("GOOGLE_API_KEY"):
print("Error: GOOGLE_API_KEY not found in .env file")
sys.exit(1)
# Getting Obsidian Vault Path
VAULT_PATH = os.getenv("VAULT_PATH")
if not VAULT_PATH:
print("Error: Obsidian Vault Location is not given!")
sys.exit(1)
# Safety Checking the Vault Path
if not os.path.exists(VAULT_PATH):
print(f"Error: Vault Path Not Found: {VAULT_PATH}")
sys.exit(1)
print(f"Loading the Obsidian Notes from: {VAULT_PATH}")
# Loading the Notes! Also the 'ObsidianLoader' Automatically handles the metadata
loader = ObsidianLoader(VAULT_PATH)
raw_docs = loader.load()
# Safety Stopping the Program
if not raw_docs:
print("WARNING!! No .md files found in the given Vault Path")
return
documents = gatekeeper(raw_docs, SKIP_FILES)
# Splitting the Notes!(f" Splitting ({len()})")
print(f" Found {len(documents)} notes")
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
separators=["\n\n", "\n", " ", ""]
)
chunks = text_splitter.split_documents(documents)
# Creating Embeddings & Saving them as Vectors to ChromaDB
print("🧠 Building the Vector Database... ")
embeddings = GoogleGenerativeAIEmbeddings(model="models/gemini-embedding-001")
# Now Embeddings are Created!
# Changed to serve the embeddings at a consistent rate & avoid crossing over rate-limits
vector_db = Chroma(
embedding_function=embeddings,
persist_directory=DB_PATH
)
# Now VectorDB is Created!
print(f"Successfully Saved {len(chunks)} chunks to '{DB_PATH}'.")
total = len(chunks)
for i in range(0, total, BATCH_SIZE):
batch = chunks[i : i + BATCH_SIZE]
try:
vector_db.add_documents(batch)
print(f" Batch{i / (BATCH_SIZE + 1)} ({len(batch)} chunks) done.")
time.sleep(1.5) # To prevent Rate Limit Errors line Error-429
except Exception as e:
print(f" Error on batch size starting at index: {i}: {e}")
print(" Waiting 20 seconds to cooldown...")
time.sleep(20)
print(f"Successfully Knowledge Base Build at path: {DB_PATH}")
if __name__=="__main__":
main()