# fedeflowers/rag_bgg architecture

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

- Interactive diagram: https://gitdiagram.com/fedeflowers/rag_bgg
- Repository: https://github.com/fedeflowers/rag_bgg

## Overview

1. **High-level shape**
   This repo is a single Python application with a Streamlit-style UI in `bot/` that implements a RAG workflow for board-game rulebooks. The app is split across three main boundaries: interactive UI, ingestion/retrieval logic, and persistence/search backends.

2. **User-facing application layer**
   `bot/app.py` is the entry point that likely wires together the pages and session state. The UI is broken into `utils/login_page.py` for authentication and `utils/main_page.py` for the main experience after login. `bot/css.py`, `icons/`, and `readme_images/` support presentation only.

3. **Authentication and session continuity**
   `utils/cookie_manager.py` indicates browser-side session persistence, so users stay logged in across reruns. `init-mongo.js` suggests MongoDB is initialized with app-specific collections/users. Authentication is a distinct subsystem from document retrieval, which is important for the architecture diagram.

4. **PDF ingestion pipeline**
   Uploaded rulebooks flow through `utils/ingestion_manager.py` and `utils/ingestion_module.py`. These modules are the bridge from raw PDF files into indexable knowledge. The README and filenames imply the pipeline extracts content from PDFs, chunks it, and prepares it for vector search.

5. **LLM-assisted extraction**
   `utils/openai_extractor.py` shows that OpenAI is not just used for chat answers but also for document processing/extraction. The repo requires an OpenAI API key, so the LLM is an external dependency central to both ingestion and question answering.

6. **Retrieval layer**
   `bot/evaluation/vector_db.py` and the README’s Qdrant requirement point to Qdrant as the vector store. This is the core retrieval boundary: embeddings or text chunks are indexed there, then queried when the user asks a rule question. Qdrant is separate from the app process and from MongoDB.

7. **Chat/Q&A flow**
   The main interaction is: user asks a question in the UI, the app retrieves relevant chunks from Qdrant, then sends context plus the user prompt to OpenAI for the final answer. `README.md` mentions “adjust your bot and temperature,” so model selection and generation settings are user-configurable inputs into this flow.

8. **Saved chats and reference management**
   The app supports viewing, retrieving, and deleting prior chats tied to ingested files. That implies a persistent chat/history store, most likely in MongoDB, with file-name or library-level grouping. This is separate from the vector index, which stores searchable content rather than chat history.

9. **Empty-state handling**
   The “No Libraries” feature means the app explicitly handles the case where nothing has been ingested yet. That is an important control-flow branch in the UI: the app must distinguish between authenticated-but-empty and authenticated-with-available-rulebooks.

10. **Evaluation/benchmarking subsystem**
   `bot/evaluation/` is an isolated analysis harness, not part of the runtime UI. It contains templates, test notebooks, `results.csv`, and saved query/answer artifacts for measuring retrieval/answer quality against the vector DB. This should be treated as an offline validation subsystem.

11. **Deployment and infrastructure**
   `dockerfile` and `docker-compose.yml` package the app with MongoDB and Qdrant. The README says the app is exposed on port 8501 and uses an external Docker network, so the architecture is containerized and service-oriented rather than monolithic. Running locally without Docker requires separate MongoDB and Qdrant setup and OpenAI credentials.

12. **What to draw in a diagram**
   A clear diagram should show: browser/user -> Streamlit app -> authentication/session store in MongoDB; upload path -> ingestion/extraction -> Qdrant; question path -> Qdrant retrieval -> OpenAI answer generation -> UI response; and a parallel persistence path for saved chats/history in MongoDB. The evaluation folder should be shown as an offline, non-production sidecar.

Main entry points: [`bot/utils/main_page.py`](https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/main_page.py) (Main page), [`bot/utils/utils_funcs.py`](https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/utils_funcs.py) (Q&A flow), [`bot/evaluation/vector_db.py`](https://github.com/fedeflowers/rag_bgg/blob/main/bot/evaluation/vector_db.py) (Vector DB), [`bot/app.py`](https://github.com/fedeflowers/rag_bgg/blob/main/bot/app.py) (App entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_ui["UI"]
  node_bot_app["App entry<br/>streamlit app<br/>[app.py]"]
  node_login_page["Login page<br/>auth ui<br/>[login_page.py]"]
  node_main_page["Main page<br/>chat ui<br/>[main_page.py]"]
  node_avatar_manager["Avatars<br/>ui helper<br/>[avatar_manager.py]"]
  node_css["Styles<br/>presentation<br/>[css.py]"]
end

subgraph group_grp_ingest["Ingestion"]
  node_ingestion_manager["Ingestion mgr<br/>pipeline coordinator"]
  node_ingestion_module["Ingestion module<br/>pdf pipeline"]
  node_openai_extractor["Extractor<br/>llm extraction"]
end

subgraph group_grp_search["Retrieval"]
  node_vector_db[("Vector DB<br/>qdrant access<br/>[vector_db.py]")]
  node_answer_flow["Q&A flow<br/>rag orchestration<br/>[utils_funcs.py]"]
end

subgraph group_grp_state["State"]
  node_cookie_manager["Cookies<br/>session persistence<br/>[cookie_manager.py]"]
  node_mongo_init["Mongo init<br/>db bootstrap<br/>[init-mongo.js]"]
  node_saved_chats[("Chat store<br/>mongo persistence<br/>[utils_funcs.py]")]
end

subgraph group_grp_eval["Evaluation"]
  node_evaluation["Eval runner<br/>offline benchmark<br/>[evaluation.py]"]
  node_templates["Templates<br/>eval prompts<br/>[templates.py]"]
  node_results_csv["Results<br/>eval artifacts<br/>[results.csv]"]
end

subgraph group_grp_deploy["Deployment"]
  node_docker_compose["Compose<br/>service wiring<br/>[docker-compose.yml]"]
  node_dockerfile["Dockerfile<br/>container build"]
end

node_bot_app -->|"renders"| node_login_page
node_bot_app -->|"routes"| node_main_page
node_bot_app -->|"restores session"| node_cookie_manager
node_login_page -->|"auth data"| node_mongo_init
node_main_page -->|"uploads"| node_ingestion_manager
node_ingestion_manager -->|"coordinates"| node_ingestion_module
node_ingestion_module -->|"extracts"| node_openai_extractor
node_ingestion_module -->|"indexes"| node_vector_db
node_main_page -->|"asks"| node_answer_flow
node_answer_flow -->|"retrieves"| node_vector_db
node_answer_flow -->|"generates"| node_openai_extractor
node_answer_flow -->|"stores chat"| node_saved_chats
node_main_page -->|"loads history"| node_saved_chats
node_mongo_init -->|"provisions"| node_saved_chats
node_main_page -->|"decorates"| node_avatar_manager
node_main_page -->|"styles"| node_css
node_evaluation -.->|"benchmarks"| node_vector_db
node_evaluation -.->|"uses"| node_templates
node_evaluation -.->|"writes"| node_results_csv
node_docker_compose -->|"builds"| node_dockerfile
node_docker_compose -->|"starts"| node_mongo_init
node_docker_compose -->|"starts"| node_vector_db

click node_bot_app "https://github.com/fedeflowers/rag_bgg/blob/main/bot/app.py"
click node_login_page "https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/login_page.py"
click node_main_page "https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/main_page.py"
click node_cookie_manager "https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/cookie_manager.py"
click node_ingestion_manager "https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/ingestion_manager.py"
click node_ingestion_module "https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/ingestion_module.py"
click node_openai_extractor "https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/openai_extractor.py"
click node_vector_db "https://github.com/fedeflowers/rag_bgg/blob/main/bot/evaluation/vector_db.py"
click node_answer_flow "https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/utils_funcs.py"
click node_avatar_manager "https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/avatar_manager.py"
click node_css "https://github.com/fedeflowers/rag_bgg/blob/main/bot/css.py"
click node_mongo_init "https://github.com/fedeflowers/rag_bgg/blob/main/bot/init-mongo.js"
click node_saved_chats "https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/utils_funcs.py"
click node_evaluation "https://github.com/fedeflowers/rag_bgg/blob/main/bot/evaluation/evaluation.py"
click node_templates "https://github.com/fedeflowers/rag_bgg/blob/main/bot/evaluation/templates.py"
click node_results_csv "https://github.com/fedeflowers/rag_bgg/blob/main/bot/evaluation/results.csv"
click node_docker_compose "https://github.com/fedeflowers/rag_bgg/blob/main/docker-compose.yml"
click node_dockerfile "https://github.com/fedeflowers/rag_bgg/tree/main/dockerfile"

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_bot_app,node_login_page,node_main_page,node_avatar_manager,node_css toneBlue
class node_ingestion_manager,node_ingestion_module,node_openai_extractor toneAmber
class node_vector_db,node_answer_flow toneMint
class node_cookie_manager,node_mongo_init,node_saved_chats toneRose
class node_evaluation,node_templates,node_results_csv toneIndigo
class node_docker_compose,node_dockerfile toneTeal
```

## Components

### UI

Interactive Streamlit app and presentation layer

- [App entry](https://github.com/fedeflowers/rag_bgg/blob/main/bot/app.py): streamlit app · Main application entry point that wires UI and session flow · `bot/app.py`
- [Login page](https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/login_page.py): auth ui · Login and access control screen · `bot/utils/login_page.py`
- [Main page](https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/main_page.py): chat ui · Primary authenticated experience for libraries and Q&A · `bot/utils/main_page.py`
- [Avatars](https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/avatar_manager.py): ui helper · Presentation helper for chat display · `bot/utils/avatar_manager.py`
- [Styles](https://github.com/fedeflowers/rag_bgg/blob/main/bot/css.py): presentation · UI styling and visual theming · `bot/css.py`

### Ingestion

PDF processing and indexing pipeline

- [Ingestion mgr](https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/ingestion_manager.py): pipeline coordinator · Coordinates upload handling and indexing workflow · `bot/utils/ingestion_manager.py`
- [Ingestion module](https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/ingestion_module.py): pdf pipeline · Processes rulebook content into indexable chunks · `bot/utils/ingestion_module.py`
- [Extractor](https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/openai_extractor.py): llm extraction · Uses OpenAI during document processing and extraction · `bot/utils/openai_extractor.py`

### Retrieval

Vector search and answer generation path

- [Vector DB](https://github.com/fedeflowers/rag_bgg/blob/main/bot/evaluation/vector_db.py): qdrant access · Search layer used for retrieval over indexed chunks · `bot/evaluation/vector_db.py`
- [Q&A flow](https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/utils_funcs.py): rag orchestration · Retrieves context and generates final answers · `bot/utils/utils_funcs.py`

### State

Authentication and persistence backends

- [Cookies](https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/cookie_manager.py): session persistence · Browser-side login continuity across reruns · `bot/utils/cookie_manager.py`
- [Mongo init](https://github.com/fedeflowers/rag_bgg/blob/main/bot/init-mongo.js): db bootstrap · Database bootstrap and collection/user initialization · `bot/init-mongo.js`
- [Chat store](https://github.com/fedeflowers/rag_bgg/blob/main/bot/utils/utils_funcs.py): mongo persistence · Stores prior chats and library-associated history · `bot/utils/utils_funcs.py`

### Evaluation

Offline benchmarking and analysis

- [Eval runner](https://github.com/fedeflowers/rag_bgg/blob/main/bot/evaluation/evaluation.py): offline benchmark · Runs retrieval and answer evaluation experiments · `bot/evaluation/evaluation.py`
- [Templates](https://github.com/fedeflowers/rag_bgg/blob/main/bot/evaluation/templates.py): eval prompts · Prompt and query templates for benchmarking · `bot/evaluation/templates.py`
- [Results](https://github.com/fedeflowers/rag_bgg/blob/main/bot/evaluation/results.csv): eval artifacts · Saved evaluation outputs and metrics · `bot/evaluation/results.csv`

### Deployment

Containerization and runtime wiring

- [Compose](https://github.com/fedeflowers/rag_bgg/blob/main/docker-compose.yml): service wiring · Container orchestration for app and backing services · `docker-compose.yml`
- [Dockerfile](https://github.com/fedeflowers/rag_bgg/tree/main/dockerfile): container build · Image definition for the application runtime · `dockerfile`

## Connections

- App entry → Login page: renders · App selects authentication screen for unauthenticated users
- App entry → Main page: routes · App routes authenticated users into the main experience
- App entry → Cookies: restores session · App uses cookie-backed session continuity
- Login page → Mongo init: auth data · Login flow depends on MongoDB-backed state and initialization
- Main page → Ingestion mgr: uploads · Main UI sends uploaded rulebooks into the ingestion pipeline
- Ingestion mgr → Ingestion module: coordinates · Manager delegates parsing and chunking work
- Ingestion module → Extractor: extracts · Pipeline uses OpenAI-assisted extraction during processing
- Ingestion module → Vector DB: indexes · Processed chunks are stored for retrieval
- Main page → Q&A flow: asks · User questions enter the RAG answer path
- Q&A flow → Vector DB: retrieves · Answer flow fetches relevant chunks from the vector store
- Q&A flow → Extractor: generates · LLM is used to synthesize the final response
- Q&A flow → Chat store: stores chat · Conversation history is persisted separately from search index
- Main page → Chat store: loads history · Main UI reads prior chats and file-grouped libraries
- Mongo init → Chat store: provisions · Mongo initialization prepares persistence storage
- Main page → Avatars: decorates · Chat display uses avatar/presentation helpers
- Main page → Styles: styles · UI styling supports the main experience
- Eval runner → Vector DB: benchmarks · Offline evaluation queries the retrieval layer
- Eval runner → Templates: uses · Benchmark runner consumes canned prompts and queries
- Eval runner → Results: writes · Evaluation outputs are saved as artifacts
- Compose → Dockerfile: builds · Compose uses the application image definition
- Compose → Mongo init: starts · Deployment includes database bootstrap
- Compose → Vector DB: starts · Deployment includes the vector search service
