File size: 1,193 Bytes
02c0be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Bootstrap ingestion: index any documents found on disk that aren't already in ChromaDB.

Runs at container startup. Safe to run repeatedly — `ingest_file` upserts by chunk hash.
"""
from __future__ import annotations

from loguru import logger

from app.core.config import settings
from app.ingestion.pipeline import SUPPORTED_EXTS, ingest_file
from app.rag.retriever import list_sources


def main() -> None:
    docs_dir = settings.documents_dir
    docs_dir.mkdir(parents=True, exist_ok=True)

    files = sorted([p for p in docs_dir.iterdir() if p.suffix.lower() in SUPPORTED_EXTS])
    if not files:
        logger.info("[bootstrap] no documents in {}, skipping", docs_dir)
        return

    indexed = {s["source_file"] for s in list_sources()}
    todo = [p for p in files if p.name not in indexed]

    if not todo:
        logger.info("[bootstrap] all {} files already indexed", len(files))
        return

    logger.info("[bootstrap] indexing {} new files", len(todo))
    for path in todo:
        try:
            ingest_file(path)
        except Exception as e:
            logger.error("[bootstrap] failed on {}: {}", path.name, e)


if __name__ == "__main__":
    main()