rebuild chroma on startup
Browse files- Dockerfile +3 -3
- rebuild_index.py +39 -0
Dockerfile
CHANGED
|
@@ -5,10 +5,10 @@ WORKDIR /app
|
|
| 5 |
COPY requirements.txt .
|
| 6 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
|
| 8 |
-
COPY app.py fiscal.py auth.py plaid_client.py ./
|
| 9 |
-
COPY chroma_db/ ./chroma_db/
|
| 10 |
COPY docs/ ./docs/
|
| 11 |
|
| 12 |
EXPOSE 7860
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
| 5 |
COPY requirements.txt .
|
| 6 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
|
| 8 |
+
COPY app.py fiscal.py auth.py plaid_client.py rebuild_index.py ./
|
|
|
|
| 9 |
COPY docs/ ./docs/
|
| 10 |
|
| 11 |
EXPOSE 7860
|
| 12 |
|
| 13 |
+
# Rebuild ChromaDB then start the server
|
| 14 |
+
CMD ["sh", "-c", "python rebuild_index.py && uvicorn app:app --host 0.0.0.0 --port 7860"]
|
rebuild_index.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.document_loaders import TextLoader
|
| 2 |
+
from langchain.text_splitter import MarkdownHeaderTextSplitter
|
| 3 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 4 |
+
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.")
|