MJ-Prod commited on
Commit
ad86004
·
1 Parent(s): 85ab5fe

rebuilt index with new data

Browse files
Files changed (1) hide show
  1. rebuild_index.py +30 -27
rebuild_index.py CHANGED
@@ -5,35 +5,38 @@ from langchain_community.vectorstores import Chroma
5
  import shutil
6
  import os
7
 
8
- # Wipe old index
9
- if os.path.exists("./chroma_db"):
10
- shutil.rmtree("./chroma_db")
11
- print("Deleted old chroma_db/")
12
 
13
- # Load markdown
14
- loader = TextLoader("./docs/fiscal_knowledge.md", encoding="utf-8")
15
- docs = loader.load()
16
- text = docs[0].page_content
17
- print(f"Loaded {len(text):,} characters")
 
 
 
 
 
18
 
19
- splitter = MarkdownHeaderTextSplitter(
20
- headers_to_split_on=[("##", "question")],
21
- strip_headers=False,
22
- )
23
- chunks = splitter.split_text(text)
 
24
 
25
- print(f"Split into {len(chunks)} chunks")
26
- print("Loading embedding model (first run downloads ~270MB)...")
27
- embeddings = HuggingFaceEmbeddings(
28
- model_name="nomic-ai/nomic-embed-text-v1",
29
- model_kwargs={"trust_remote_code": True},
30
- )
31
 
32
- vectorstore = Chroma.from_documents(
33
- chunks,
34
- embedding=embeddings,
35
- persist_directory="./chroma_db",
36
- )
 
37
 
38
- print(f"Indexed {len(chunks)} chunks into chroma_db/")
39
- print("Done. Ready to deploy.")
 
5
  import shutil
6
  import os
7
 
8
+ CHROMA_DIR = "./chroma_db"
9
+ DOCS_FILE = "./docs/fiscal_knowledge.md"
 
 
10
 
11
+ # Only rebuild if chroma_db doesn't exist or docs changed
12
+ if os.path.exists(CHROMA_DIR):
13
+ print("chroma_db/ exists, skipping rebuild. Delete chroma_db/ to force rebuild.")
14
+ else:
15
+ print("Building new index...")
16
+
17
+ loader = TextLoader(DOCS_FILE, encoding="utf-8")
18
+ docs = loader.load()
19
+ text = docs[0].page_content
20
+ print(f"Loaded {len(text):,} characters")
21
 
22
+ splitter = MarkdownHeaderTextSplitter(
23
+ headers_to_split_on=[("##", "question")],
24
+ strip_headers=False,
25
+ )
26
+ chunks = splitter.split_text(text)
27
+ print(f"Split into {len(chunks)} chunks")
28
 
29
+ print("Loading embedding model...")
30
+ embeddings = HuggingFaceEmbeddings(
31
+ model_name="nomic-ai/nomic-embed-text-v1",
32
+ model_kwargs={"trust_remote_code": True},
33
+ )
 
34
 
35
+ vectorstore = Chroma.from_documents(
36
+ chunks,
37
+ embedding=embeddings,
38
+ persist_directory=CHROMA_DIR,
39
+ )
40
+ print(f"Indexed {len(chunks)} chunks into chroma_db/")
41
 
42
+ print("Ready.")