# hueiboi/rag architecture

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

- Interactive diagram: https://gitdiagram.com/hueiboi/rag
- Repository: https://github.com/hueiboi/rag

## Overview

This is a personalized document-question-answering RAG chatbot: users authenticate, upload documents, and ask questions through a Next.js UI; FastAPI indexes files and retrieves user-scoped context before generating an answer with Groq. The map preserves upload/indexing, chat/retrieval, history, auth, and the hybrid retrieval internals. Edges reflect sampled calls or README-described integrations; several frontend and retrieval adapter details are unsampled, so only documented or directly supported links are shown.

Main entry points: [`src/api/routes/chat.py`](https://github.com/hueiboi/rag/blob/main/src/api/routes/chat.py) (Chat and document API), [`src/retrieval_factory.py`](https://github.com/hueiboi/rag/blob/main/src/retrieval_factory.py) (Retrieval assembly), [`src/retrieval/engine.py`](https://github.com/hueiboi/rag/blob/main/src/retrieval/engine.py) (Hybrid retrieval engine), [`src/pipeline.py`](https://github.com/hueiboi/rag/blob/main/src/pipeline.py) (RAG orchestration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Web application"]
  node_chat_ui["Chat interface<br/>[page.tsx]"]
  node_auth_ui["Auth screens<br/>[AuthPage.tsx]"]
  node_auth_callback["OAuth callback<br/>[route.ts]"]
  node_auth_proxy["Session route guard<br/>[proxy.ts]"]
  node_api_client["API client<br/>[api.ts]"]
end

subgraph group_backend["API and domain"]
  node_chat_api["Chat and document API<br/>[chat.py]"]
  node_auth_backend["Request authentication<br/>[auth.py]"]
  node_dependencies["Request dependencies<br/>[dependencies.py]"]
  node_ingestion["Document ingestion<br/>[ingestion.py]"]
  node_storage["File storage adapter<br/>[storage.py]"]
  node_history["Chat history<br/>[history.py]"]
  node_rag_pipeline["RAG orchestration<br/>[pipeline.py]"]
  node_prompt_context["Context formatting<br/>[prompt_context.py]"]
  node_llm_service["Prompt and LLM setup<br/>[llm_service.py]"]
end

subgraph group_retrieval["Retrieval system"]
  node_retrieval_factory["Retrieval assembly"]
  node_retrieval_engine["Hybrid retrieval engine<br/>[engine.py]"]
  node_lexical["Lexical ranking<br/>[lexical.py]"]
  node_corpus_stats["Corpus statistics<br/>[pg_corpus_stats.py]"]
  node_vector_adapter["Vector store adapter<br/>[pgvector_store.py]"]
  node_embedder["Embedding adapter<br/>[hf_embedder.py]"]
end

subgraph group_integrations["External services"]
  node_supabase_auth["Supabase Auth"]
  node_supabase_storage["Supabase Storage"]
  node_pgvector_db[("PostgreSQL pgvector")]
  node_groq["Groq LLM"]
end

node_user(("User"))

node_user -->|"asks or uploads"| node_chat_ui
node_user -->|"signs in"| node_auth_ui
node_chat_ui -->|"uses"| node_api_client
node_api_client -->|"sends requests"| node_chat_api
node_auth_ui -->|"authenticates"| node_supabase_auth
node_auth_callback -.->|"completes OAuth"| node_supabase_auth
node_auth_proxy -->|"checks session"| node_supabase_auth
node_chat_api -->|"depends on"| node_dependencies
node_dependencies -->|"uses"| node_auth_backend
node_chat_api -->|"invokes"| node_rag_pipeline
node_chat_api -->|"indexes upload"| node_ingestion
node_chat_api -->|"manages files"| node_storage
node_chat_api -->|"reads and saves"| node_history
node_ingestion -->|"downloads file"| node_storage
node_storage -->|"reads and writes"| node_supabase_storage
node_ingestion -->|"stores indexed chunks"| node_pgvector_db
node_chat_api -->|"deletes file vectors"| node_pgvector_db
node_rag_pipeline -->|"retrieves context"| node_retrieval_factory
node_rag_pipeline -->|"formats chunks"| node_prompt_context
node_rag_pipeline -->|"builds chain with"| node_llm_service
node_llm_service -->|"configures model"| node_groq
node_retrieval_factory -->|"opens vector store"| node_ingestion
node_retrieval_factory -->|"constructs"| node_retrieval_engine
node_retrieval_factory -->|"configures"| node_embedder
node_retrieval_factory -->|"configures"| node_vector_adapter
node_retrieval_factory -->|"configures"| node_corpus_stats
node_retrieval_engine -->|"reranks candidates"| node_lexical
node_retrieval_engine -.->|"embeds query"| node_embedder
node_retrieval_engine -.->|"searches chunks"| node_vector_adapter
node_retrieval_engine -.->|"gets token stats"| node_corpus_stats
node_corpus_stats -.->|"queries chunk text"| node_pgvector_db
node_vector_adapter -.->|"accesses vectors"| node_pgvector_db

click node_chat_ui "https://github.com/hueiboi/rag/blob/main/rag-ui/app/chat/page.tsx"
click node_auth_ui "https://github.com/hueiboi/rag/blob/main/rag-ui/components/auth-page/AuthPage.tsx"
click node_auth_callback "https://github.com/hueiboi/rag/blob/main/rag-ui/app/auth/callback/route.ts"
click node_auth_proxy "https://github.com/hueiboi/rag/blob/main/rag-ui/proxy.ts"
click node_api_client "https://github.com/hueiboi/rag/blob/main/rag-ui/lib/api.ts"
click node_chat_api "https://github.com/hueiboi/rag/blob/main/src/api/routes/chat.py"
click node_auth_backend "https://github.com/hueiboi/rag/blob/main/src/auth.py"
click node_dependencies "https://github.com/hueiboi/rag/blob/main/src/dependencies.py"
click node_ingestion "https://github.com/hueiboi/rag/blob/main/src/ingestion.py"
click node_storage "https://github.com/hueiboi/rag/blob/main/src/storage.py"
click node_history "https://github.com/hueiboi/rag/blob/main/src/history.py"
click node_rag_pipeline "https://github.com/hueiboi/rag/blob/main/src/pipeline.py"
click node_prompt_context "https://github.com/hueiboi/rag/blob/main/src/prompt_context.py"
click node_llm_service "https://github.com/hueiboi/rag/blob/main/src/llm_service.py"
click node_retrieval_factory "https://github.com/hueiboi/rag/blob/main/src/retrieval_factory.py"
click node_retrieval_engine "https://github.com/hueiboi/rag/blob/main/src/retrieval/engine.py"
click node_lexical "https://github.com/hueiboi/rag/blob/main/src/retrieval/lexical.py"
click node_corpus_stats "https://github.com/hueiboi/rag/blob/main/src/retrieval/adapters/pg_corpus_stats.py"
click node_vector_adapter "https://github.com/hueiboi/rag/blob/main/src/retrieval/adapters/pgvector_store.py"
click node_embedder "https://github.com/hueiboi/rag/blob/main/src/retrieval/adapters/hf_embedder.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_chat_ui,node_auth_ui,node_auth_callback,node_auth_proxy,node_api_client,node_user toneBlue
class node_chat_api,node_auth_backend,node_dependencies,node_ingestion,node_storage,node_history,node_rag_pipeline,node_prompt_context,node_llm_service toneAmber
class node_retrieval_factory,node_retrieval_engine,node_lexical,node_corpus_stats,node_vector_adapter,node_embedder toneMint
class node_supabase_auth,node_supabase_storage,node_pgvector_db,node_groq toneRose
```

## Components

### Web application

- [Chat interface](https://github.com/hueiboi/rag/blob/main/rag-ui/app/chat/page.tsx): `rag-ui/app/chat/page.tsx`
- [Auth screens](https://github.com/hueiboi/rag/blob/main/rag-ui/components/auth-page/AuthPage.tsx): `rag-ui/components/auth-page/AuthPage.tsx`
- [OAuth callback](https://github.com/hueiboi/rag/blob/main/rag-ui/app/auth/callback/route.ts): `rag-ui/app/auth/callback/route.ts`
- [Session route guard](https://github.com/hueiboi/rag/blob/main/rag-ui/proxy.ts): `rag-ui/proxy.ts`
- [API client](https://github.com/hueiboi/rag/blob/main/rag-ui/lib/api.ts): `rag-ui/lib/api.ts`

### API and domain

- [Chat and document API](https://github.com/hueiboi/rag/blob/main/src/api/routes/chat.py): `src/api/routes/chat.py`
- [Request authentication](https://github.com/hueiboi/rag/blob/main/src/auth.py): `src/auth.py`
- [Request dependencies](https://github.com/hueiboi/rag/blob/main/src/dependencies.py): `src/dependencies.py`
- [Document ingestion](https://github.com/hueiboi/rag/blob/main/src/ingestion.py): `src/ingestion.py`
- [File storage adapter](https://github.com/hueiboi/rag/blob/main/src/storage.py): `src/storage.py`
- [Chat history](https://github.com/hueiboi/rag/blob/main/src/history.py): `src/history.py`
- [RAG orchestration](https://github.com/hueiboi/rag/blob/main/src/pipeline.py): `src/pipeline.py`
- [Context formatting](https://github.com/hueiboi/rag/blob/main/src/prompt_context.py): `src/prompt_context.py`
- [Prompt and LLM setup](https://github.com/hueiboi/rag/blob/main/src/llm_service.py): `src/llm_service.py`

### Retrieval system

- [Retrieval assembly](https://github.com/hueiboi/rag/blob/main/src/retrieval_factory.py): `src/retrieval_factory.py`
- [Hybrid retrieval engine](https://github.com/hueiboi/rag/blob/main/src/retrieval/engine.py): `src/retrieval/engine.py`
- [Lexical ranking](https://github.com/hueiboi/rag/blob/main/src/retrieval/lexical.py): `src/retrieval/lexical.py`
- [Corpus statistics](https://github.com/hueiboi/rag/blob/main/src/retrieval/adapters/pg_corpus_stats.py): `src/retrieval/adapters/pg_corpus_stats.py`
- [Vector store adapter](https://github.com/hueiboi/rag/blob/main/src/retrieval/adapters/pgvector_store.py): `src/retrieval/adapters/pgvector_store.py`
- [Embedding adapter](https://github.com/hueiboi/rag/blob/main/src/retrieval/adapters/hf_embedder.py): `src/retrieval/adapters/hf_embedder.py`

### External services

- **Supabase Auth**
- **Supabase Storage**
- **PostgreSQL pgvector**
- **Groq LLM**

### Other components

- **User**

## Connections

- User → Chat interface: asks or uploads (evidence: `README.md`)
- User → Auth screens: signs in (evidence: `README.md`)
- Chat interface → API client: uses (evidence: `rag-ui/app/chat/page.tsx`)
- API client → Chat and document API: sends requests (evidence: `README.md`)
- Auth screens → Supabase Auth: authenticates (evidence: `README.md`)
- OAuth callback → Supabase Auth: completes OAuth
- Session route guard → Supabase Auth: checks session (evidence: `README.md`)
- Chat and document API → Request dependencies: depends on (evidence: `src/api/routes/chat.py`)
- Request dependencies → Request authentication: uses (evidence: `src/dependencies.py`)
- Chat and document API → RAG orchestration: invokes (evidence: `src/api/routes/chat.py`)
- Chat and document API → Document ingestion: indexes upload (evidence: `src/api/routes/chat.py`)
- Chat and document API → File storage adapter: manages files (evidence: `src/api/routes/chat.py`)
- Chat and document API → Chat history: reads and saves (evidence: `src/api/routes/chat.py`)
- Document ingestion → File storage adapter: downloads file (evidence: `src/ingestion.py`)
- File storage adapter → Supabase Storage: reads and writes (evidence: `src/storage.py`)
- Document ingestion → PostgreSQL pgvector: stores indexed chunks (evidence: `README.md`)
- Chat and document API → PostgreSQL pgvector: deletes file vectors (evidence: `src/api/routes/chat.py`)
- RAG orchestration → Retrieval assembly: retrieves context (evidence: `src/pipeline.py`)
- RAG orchestration → Context formatting: formats chunks (evidence: `src/pipeline.py`)
- RAG orchestration → Prompt and LLM setup: builds chain with (evidence: `src/pipeline.py`)
- Prompt and LLM setup → Groq LLM: configures model (evidence: `src/llm_service.py`)
- Retrieval assembly → Document ingestion: opens vector store (evidence: `src/retrieval_factory.py`)
- Retrieval assembly → Hybrid retrieval engine: constructs (evidence: `src/retrieval_factory.py`)
- Retrieval assembly → Embedding adapter: configures (evidence: `src/retrieval_factory.py`)
- Retrieval assembly → Vector store adapter: configures (evidence: `src/retrieval_factory.py`)
- Retrieval assembly → Corpus statistics: configures (evidence: `src/retrieval_factory.py`)
- Hybrid retrieval engine → Lexical ranking: reranks candidates (evidence: `src/retrieval/engine.py`)
- Hybrid retrieval engine → Embedding adapter: embeds query
- Hybrid retrieval engine → Vector store adapter: searches chunks
- Hybrid retrieval engine → Corpus statistics: gets token stats
- Corpus statistics → PostgreSQL pgvector: queries chunk text
- Vector store adapter → PostgreSQL pgvector: accesses vectors
