rebuilt index with new data
Browse files- 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 |
-
|
| 9 |
-
|
| 10 |
-
shutil.rmtree("./chroma_db")
|
| 11 |
-
print("Deleted old chroma_db/")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
splitter = MarkdownHeaderTextSplitter(
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
)
|
| 23 |
-
chunks = splitter.split_text(text)
|
|
|
|
| 24 |
|
| 25 |
-
print(
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
vectorstore = Chroma.from_documents(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
)
|
|
|
|
| 37 |
|
| 38 |
-
print(
|
| 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.")
|
|
|