Skip to content

Architecture

Pagelore is a FastAPI application with three layers, two databases and one pluggable provider for each external system.

Layers

flowchart TB
    subgraph HTTP["routes/"]
        R["Endpoints, request and response schemas,<br/>error mapping to status codes"]
    end
    subgraph UC["controllers/"]
        C["Use cases: ingest, process, retrieve,<br/>rerank, evaluate, reindex"]
    end
    subgraph DATA["models/"]
        M["Data access for projects, documents,<br/>chunks and collections (SQLAlchemy)"]
    end
    subgraph STORES["stores/"]
        L["loaders"]
        CH["chunking"]
        E["embeddings"]
        V["vectordb"]
        RR["rerankers"]
        LLM["llm"]
        S["sources"]
    end
    R --> C
    C --> M
    C --> STORES
    M --> PG[("Postgres")]
    V --> VDB[("Vector DB")]

Each layer only talks to the layer below it:

  • Routes validate requests (Pydantic) and turn errors into status codes: 422 for invalid options, 404 for missing projects, 409 for an index that does not match the configured model, 502 for a failing provider.
  • Controllers hold the logic. The code is synchronous, and the controllers run the work in FastAPI's thread pool (run_in_threadpool), so a slow embedding call never blocks the event loop.
  • Models read and write Postgres with SQLAlchemy.
  • Stores wrap the external systems behind one interface per kind. A new vector DB, embedding provider, reranker, LLM or file format is a new provider class plus a factory entry.

Startup

At startup (FastAPI's lifespan), Pagelore creates one client of each kind and shares it with every request:

  1. The Postgres engine (a connection pool). Startup fails with a readable message if Postgres is unreachable or the tables don't exist yet.
  2. The vector DB client.
  3. The embedding client. It loads the model, or probes the API once, so the vector dimension is known up front.
  4. The reranker, if RERANKER_PROVIDER is set. Local models load here; hosted APIs are first called by a request.
  5. The LLM client, if LLM_PROVIDER is set. The model is first called by a job.

A wrong setting, an unreachable service or a missing model therefore fails at startup, not at the first request. On shutdown, everything is disconnected in reverse order.

Data model

Postgres is the source of truth. The vector DB holds only vectors and a small payload, and chunk text always comes from Postgres, so results match the stored documents even if the vector DB is rebuilt.

erDiagram
    projects ||--o{ documents : has
    projects ||--o{ chunks : has
    documents ||--o{ chunks : "split into"
    documents ||--o{ parent_chunks : "grouped into"
    parent_chunks ||--o{ chunks : contains
    collections ||--o{ documents : "indexed in"

    projects {
        int project_id PK
        string project_name UK
        string collection_name
        jsonb chunking
        string text_search_language
    }
    documents {
        int document_id PK
        string file_name
        string file_hash
        string status
        string source
        string external_id
        int version
        jsonb sections
        jsonb user_metadata
        jsonb document_metadata
        jsonb chunking
        int collection_id FK
    }
    chunks {
        bigint chunk_id PK
        int chunk_index
        text chunk_text
        jsonb chunk_metadata
        text chunk_context
        tsvector search_vector
        bigint parent_id FK
    }
    parent_chunks {
        bigint parent_id PK
        int parent_index
        text parent_text
        jsonb parent_metadata
    }
    collections {
        int id PK
        string vector_db_backend
        string collection_name
        string embedding_provider
        string embedding_model
        int embedding_size
        string distance_method
    }
Table Holds
projects Name, collection name, default chunking options, keyword-search language
documents File name, type, size and SHA-256 hash, status (pending, processing, indexed, failed), error, extracted sections, your metadata, extraction details, source (upload, text, url, s3) and source info (ETag, Last-Modified), external id, version, the chunking it was processed with, and the collection it went into
chunks Text, location metadata, LLM-written context, the full-text tsvector (GIN index), and the parent
parent_chunks The text and combined location of each parent
collections Each vector collection's backend, name, embedding provider, model, dimension and distance

Each vector's payload holds chunk_id, document_id, file_name and chunk_index, plus the location when the format has one: page_start, page_end and page_unit, section titles, start_time / end_time, and row_start / row_end.

Life of a document

sequenceDiagram
    autonumber
    participant App
    participant API as Pagelore
    participant PG as Postgres
    participant VDB as Vector DB
    App->>API: POST /projects/1/documents (file)
    API->>API: hash the file, check external_id
    API->>PG: insert document (pending)
    API->>API: extract sections (loader or Docling)
    API->>API: chunk (strategy) and group parents
    opt llm_context
        API->>API: LLM writes a context per chunk
    end
    API->>API: embed chunks
    API->>VDB: upsert vectors + payload
    API->>PG: insert chunks + tsvector, status indexed
    API->>PG: delete older versions (rows, vectors, files)
    API-->>App: result: indexed

If any step fails, the document is marked failed with the error, and an older version (if there is one) stays searchable.

Retrieval pipeline

flowchart LR
    Q["query"] --> E["embed<br/>(once)"]
    E --> VS["vector search"]
    Q --> KS["BM25 keyword search<br/>(Postgres)"]
    VS --> F["fuse<br/>(RRF / relative score)"]
    KS --> F
    F --> FL["filters"]
    FL --> P["collapse into parents"]
    P --> D["dedupe"]
    D --> RR["rerank"]
    RR --> MMR["diversity (MMR),<br/>max per document"]
    MMR --> K["top_k + neighbors<br/>+ citations"]

The vector mode skips the keyword branch and the keyword mode skips the vector branch. Each later step is optional. See Retrieval.

Files on disk

Path Holds
src/assets/files/<project_id>/ Kept originals (FILE_KEEP_ORIGINALS), so a document can be re-extracted
src/assets/database/<VECTOR_DB_PATH>/ Embedded vector DBs (Qdrant local, Chroma, Milvus Lite, FAISS, LanceDB)

See also Project structure.