# darkerror64/rag_python architecture

> Architecture diagram of the GitHub repository darkerror64/rag_python, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/darkerror64/rag_python
- Repository: https://github.com/darkerror64/rag_python

## Overview

Agentic RAG Pipeline ingests PDFs into a searchable corpus, then answers questions through a ReAct agent whose retrieval gate requires successful content retrieval and whose citations are checked against stored chunks. The principal workflow runs from upload and asynchronous processing through PDF extraction, hierarchical parsing, semantic chunking, embedding, and database storage; queries retrieve chunks for the agent to use. The README also documents scoped search, CLI operations, provider checks, and retrieval benchmarking. Some API, worker, and database internals were not sampled; their connections below rely on the README’s architecture description.

Main entry points: [`app/controllers/main_controller.py`](https://github.com/darkerror64/rag_python/blob/master/app/controllers/main_controller.py) (Ingestion controller), [`app/db/database_manager.py`](https://github.com/darkerror64/rag_python/blob/master/app/db/database_manager.py) (PostgreSQL and pgvector), [`app/main.py`](https://github.com/darkerror64/rag_python/blob/master/app/main.py) (FastAPI service), [`app/engines/hierarchical_chunker.py`](https://github.com/darkerror64/rag_python/blob/master/app/engines/hierarchical_chunker.py) (Hierarchical chunking)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingestion["PDF Ingestion"]
  node_api["FastAPI service<br/>[main.py]"]
  node_controller["Ingestion controller<br/>[main_controller.py]"]
  node_worker["Async worker<br/>[worker.py]"]
  node_broker["Redis broker"]
  node_pdf["PDF extraction<br/>[pdf_extractor.py]"]
  node_parser["Markdown AST parser<br/>[markdown_parser.py]"]
  node_hierarchical["Hierarchical chunking"]
  node_semantic["Semantic chunking"]
  node_token["Token optimizer<br/>[token_optimizer.py]"]
end

subgraph group_retrieval["Retrieval and Agent"]
  node_retriever["Vector retrieval"]
  node_agent["ReAct agent<br/>[agent_engine.py]"]
  node_llm["LLM service<br/>[llm.py]"]
end

subgraph group_infrastructure["Storage and Providers"]
  node_db[("PostgreSQL and pgvector")]
  node_embeddings["Embedding providers<br/>[embeddings.py]"]
  node_config["Runtime configuration<br/>[config.py]"]
  node_hosted_llm["Hosted or local LLM"]
end

subgraph group_operations["Interfaces and Evaluation"]
  node_cli["CLI<br/>[cli.py]"]
  node_evaluation["Retrieval evaluation<br/>[retrieval_eval.py]"]
end

node_client(("Client"))
node_questioner(("Questioner"))

node_client -->|"uploads PDF"| node_api
node_api -.->|"hands off ingestion"| node_controller
node_api -->|"stores upload bytes"| node_db
node_api -->|"enqueues blob"| node_broker
node_broker -->|"dispatches job"| node_worker
node_worker -->|"fetches PDF bytes"| node_db
node_worker -->|"runs ingestion"| node_controller
node_controller -->|"extracts PDF"| node_pdf
node_controller -->|"saves document"| node_db
node_controller -.->|"segments document"| node_hierarchical
node_pdf -->|"provides markdown"| node_parser
node_hierarchical -->|"parses structure"| node_parser
node_hierarchical -->|"detects boundaries"| node_semantic
node_hierarchical -->|"uses token limits"| node_token
node_hierarchical -->|"embeds chunks"| node_embeddings
node_controller -.->|"stores chunks"| node_db
node_questioner -->|"asks question"| node_api
node_api -.->|"routes query"| node_controller
node_controller -->|"runs scoped search"| node_retriever
node_agent -->|"searches corpus"| node_retriever
node_retriever -->|"embeds query"| node_semantic
node_retriever -->|"queries vectors"| node_db
node_agent -->|"reads chunks and verifies citations"| node_db
node_agent -->|"requests next step"| node_llm
node_llm -->|"calls provider"| node_hosted_llm
node_embeddings -->|"reads provider settings"| node_config
node_llm -->|"reads LLM settings"| node_config
node_cli -->|"runs ingest and query"| node_controller
node_cli -->|"starts agent chat"| node_agent
node_cli -->|"checks provider"| node_embeddings
node_evaluation -->|"benchmarks chunking"| node_hierarchical
node_evaluation -->|"stores and searches benchmark data"| node_db
node_evaluation -->|"builds baseline chunks"| node_token

click node_api "https://github.com/darkerror64/rag_python/blob/master/app/main.py"
click node_controller "https://github.com/darkerror64/rag_python/blob/master/app/controllers/main_controller.py"
click node_worker "https://github.com/darkerror64/rag_python/blob/master/app/worker.py"
click node_pdf "https://github.com/darkerror64/rag_python/blob/master/app/utils/pdf_extractor.py"
click node_parser "https://github.com/darkerror64/rag_python/blob/master/app/engines/markdown_parser.py"
click node_hierarchical "https://github.com/darkerror64/rag_python/blob/master/app/engines/hierarchical_chunker.py"
click node_semantic "https://github.com/darkerror64/rag_python/blob/master/app/engines/semantic_chunker.py"
click node_token "https://github.com/darkerror64/rag_python/blob/master/app/utils/token_optimizer.py"
click node_retriever "https://github.com/darkerror64/rag_python/blob/master/app/db/retrieval_manager.py"
click node_agent "https://github.com/darkerror64/rag_python/blob/master/app/engines/agent_engine.py"
click node_llm "https://github.com/darkerror64/rag_python/blob/master/app/core/llm.py"
click node_db "https://github.com/darkerror64/rag_python/blob/master/app/db/database_manager.py"
click node_embeddings "https://github.com/darkerror64/rag_python/blob/master/app/engines/embeddings.py"
click node_config "https://github.com/darkerror64/rag_python/blob/master/app/core/config.py"
click node_cli "https://github.com/darkerror64/rag_python/blob/master/cli.py"
click node_evaluation "https://github.com/darkerror64/rag_python/blob/master/app/eval/retrieval_eval.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_controller,node_worker,node_broker,node_pdf,node_parser,node_hierarchical,node_semantic,node_token,node_client toneBlue
class node_retriever,node_agent,node_llm toneAmber
class node_db,node_embeddings,node_config,node_hosted_llm toneMint
class node_cli,node_evaluation toneRose
class node_questioner toneIndigo
```

## Components

### PDF Ingestion

- [FastAPI service](https://github.com/darkerror64/rag_python/blob/master/app/main.py): `app/main.py`
- [Ingestion controller](https://github.com/darkerror64/rag_python/blob/master/app/controllers/main_controller.py): `app/controllers/main_controller.py`
- [Async worker](https://github.com/darkerror64/rag_python/blob/master/app/worker.py): `app/worker.py`
- **Redis broker**
- [PDF extraction](https://github.com/darkerror64/rag_python/blob/master/app/utils/pdf_extractor.py): `app/utils/pdf_extractor.py`
- [Markdown AST parser](https://github.com/darkerror64/rag_python/blob/master/app/engines/markdown_parser.py): `app/engines/markdown_parser.py`
- [Hierarchical chunking](https://github.com/darkerror64/rag_python/blob/master/app/engines/hierarchical_chunker.py): `app/engines/hierarchical_chunker.py`
- [Semantic chunking](https://github.com/darkerror64/rag_python/blob/master/app/engines/semantic_chunker.py): `app/engines/semantic_chunker.py`
- [Token optimizer](https://github.com/darkerror64/rag_python/blob/master/app/utils/token_optimizer.py): `app/utils/token_optimizer.py`

### Retrieval and Agent

- [Vector retrieval](https://github.com/darkerror64/rag_python/blob/master/app/db/retrieval_manager.py): `app/db/retrieval_manager.py`
- [ReAct agent](https://github.com/darkerror64/rag_python/blob/master/app/engines/agent_engine.py): `app/engines/agent_engine.py`
- [LLM service](https://github.com/darkerror64/rag_python/blob/master/app/core/llm.py): `app/core/llm.py`

### Storage and Providers

- [PostgreSQL and pgvector](https://github.com/darkerror64/rag_python/blob/master/app/db/database_manager.py): `app/db/database_manager.py`
- [Embedding providers](https://github.com/darkerror64/rag_python/blob/master/app/engines/embeddings.py): `app/engines/embeddings.py`
- [Runtime configuration](https://github.com/darkerror64/rag_python/blob/master/app/core/config.py): `app/core/config.py`
- **Hosted or local LLM**

### Interfaces and Evaluation

- [CLI](https://github.com/darkerror64/rag_python/blob/master/cli.py): `cli.py`
- [Retrieval evaluation](https://github.com/darkerror64/rag_python/blob/master/app/eval/retrieval_eval.py): `app/eval/retrieval_eval.py`

### Other components

- **Client**
- **Questioner**

## Connections

- Client → FastAPI service: uploads PDF (evidence: `README.md`)
- FastAPI service → Ingestion controller: hands off ingestion
- FastAPI service → PostgreSQL and pgvector: stores upload bytes (evidence: `README.md`)
- FastAPI service → Redis broker: enqueues blob (evidence: `README.md`)
- Redis broker → Async worker: dispatches job (evidence: `README.md`)
- Async worker → PostgreSQL and pgvector: fetches PDF bytes (evidence: `README.md`)
- Async worker → Ingestion controller: runs ingestion (evidence: `README.md`)
- Ingestion controller → PDF extraction: extracts PDF (evidence: `app/controllers/main_controller.py`)
- Ingestion controller → PostgreSQL and pgvector: saves document (evidence: `app/controllers/main_controller.py`)
- Ingestion controller → Hierarchical chunking: segments document
- PDF extraction → Markdown AST parser: provides markdown (evidence: `README.md`)
- Hierarchical chunking → Markdown AST parser: parses structure (evidence: `app/engines/hierarchical_chunker.py`)
- Hierarchical chunking → Semantic chunking: detects boundaries (evidence: `app/engines/hierarchical_chunker.py`)
- Hierarchical chunking → Token optimizer: uses token limits (evidence: `app/engines/hierarchical_chunker.py`)
- Hierarchical chunking → Embedding providers: embeds chunks (evidence: `README.md`)
- Ingestion controller → PostgreSQL and pgvector: stores chunks (evidence: `app/controllers/main_controller.py`)
- Questioner → FastAPI service: asks question (evidence: `README.md`)
- FastAPI service → Ingestion controller: routes query
- Ingestion controller → Vector retrieval: runs scoped search (evidence: `app/controllers/main_controller.py`)
- ReAct agent → Vector retrieval: searches corpus (evidence: `app/engines/agent_engine.py`)
- Vector retrieval → Semantic chunking: embeds query (evidence: `app/db/retrieval_manager.py`)
- Vector retrieval → PostgreSQL and pgvector: queries vectors (evidence: `app/db/retrieval_manager.py`)
- ReAct agent → PostgreSQL and pgvector: reads chunks and verifies citations (evidence: `app/engines/agent_engine.py`)
- ReAct agent → LLM service: requests next step (evidence: `app/engines/agent_engine.py`)
- LLM service → Hosted or local LLM: calls provider (evidence: `app/core/llm.py`)
- Embedding providers → Runtime configuration: reads provider settings (evidence: `app/engines/embeddings.py`)
- LLM service → Runtime configuration: reads LLM settings (evidence: `app/core/llm.py`)
- CLI → Ingestion controller: runs ingest and query (evidence: `cli.py`)
- CLI → ReAct agent: starts agent chat (evidence: `README.md`)
- CLI → Embedding providers: checks provider (evidence: `README.md`)
- Retrieval evaluation → Hierarchical chunking: benchmarks chunking (evidence: `app/eval/retrieval_eval.py`)
- Retrieval evaluation → PostgreSQL and pgvector: stores and searches benchmark data (evidence: `app/eval/retrieval_eval.py`)
- Retrieval evaluation → Token optimizer: builds baseline chunks (evidence: `app/eval/retrieval_eval.py`)
