Document upload
Adding a document is two writes: save the raw text under data/<role>/ and upsert an embedding into the matching ChromaDB collection. Both happen in one function so an admin never has to think about them separately.
def add_document(self, role: str, title: str, content: str) -> bool:
safe_title = title.replace(" ", "_").replace("/", "_").replace("\\", "_")
doc_path = self.docs_dir / role / f"{safe_title}.txt"
with open(doc_path, "w") as f:
f.write(content)
doc_id = f"{role}_{safe_title}_{int(time.time())}"
existing = self.collections[role].get(where={"title": title})
if existing and existing.get("ids"):
self.collections[role].delete(ids=existing["ids"])
self.collections[role].add(
documents=[content],
metadatas=[{"title": title, "path": str(doc_path)}],
ids=[doc_id],
)
return True
Re-uploading a title replaces the previous version in the collection. That gives admins a "paste over" workflow without a separate update button.
Validation checklist: Verify document upload
Loading practice…
Checkpoint: Admin panel checkpoint
Loading practice…