File size: 1,861 Bytes
85252a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
One-time (or re-runnable) ingestion script.

Loads all PDFs from the documents/ directory, chunks them, embeds the
chunks, and upserts them into Pinecone. Run this once before starting the
API, and re-run whenever documents/ changes.

Usage:
    python -m scripts.ingest
"""

import logging
import sys
from pathlib import Path

from app.core.config import settings
from app.core.logging_config import configure_logging
from app.services import chunker, embedder, pdf_processor, vector_store

configure_logging()
logger = logging.getLogger(__name__)


def main() -> None:
    docs_dir = Path(settings.DOCUMENTS_DIR)

    logger.info(f"Starting ingestion from '{docs_dir}'...")

    try:
        all_pages = pdf_processor.load_all_documents(docs_dir)
    except pdf_processor.PDFProcessingError as e:
        logger.error(f"Ingestion aborted β€” PDF processing failed: {e}")
        sys.exit(1)

    try:
        all_chunks = chunker.chunk_all_documents(
            all_pages,
            chunk_size_words=settings.CHUNK_SIZE_WORDS,
            overlap_words=settings.CHUNK_OVERLAP_WORDS,
        )
    except ValueError as e:
        logger.error(f"Ingestion aborted β€” chunking failed: {e}")
        sys.exit(1)

    try:
        texts = [c.text for c in all_chunks]
        embeddings = embedder.embed_texts(texts)
    except embedder.EmbeddingError as e:
        logger.error(f"Ingestion aborted β€” embedding failed: {e}")
        sys.exit(1)

    try:
        vector_store.ensure_index_exists()
        upserted_count = vector_store.upsert_chunks(all_chunks, embeddings)
    except vector_store.VectorStoreError as e:
        logger.error(f"Ingestion aborted β€” vector store operation failed: {e}")
        sys.exit(1)

    logger.info(f"βœ… Ingestion complete. {upserted_count} chunks indexed into Pinecone.")


if __name__ == "__main__":
    main()