# straycat405/personal-llm architecture

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

- Interactive diagram: https://gitdiagram.com/straycat405/personal-llm
- Repository: https://github.com/straycat405/personal-llm

## Overview

#### Purpose and Entry Points
BTLLM is a local-first document-assistant chatbot: React UI plus Spring Boot API, local Ollama inference, PostgreSQL/pgVector retrieval, and optional cloud model providers. Backend starts at `backend/src/main/java/com/bigteam/btllm/BtllmApplication.java`; browser bootstrap is `frontend/src/main.tsx` and `frontend/src/App.tsx`. Docker deployment is orchestrated by `docker-compose.yml`.

#### Client Application
The React 18/TypeScript SPA provides authentication, chat, model selection, document ingestion, and knowledge-base management. `frontend/src/pages/ChatPage.tsx` is the primary user workflow; API clients separate REST concerns under `frontend/src/api/chatRoom.ts` and `frontend/src/api/etl.ts`. Client-side state is partitioned among auth, chat, and knowledge stores, notably `frontend/src/stores/chatStore.ts`.

#### Identity and API Boundary
Spring Security protects HTTP APIs with JWT authentication. Signup/login flow enters `backend/src/main/java/com/bigteam/btllm/user/controller/AuthController.java`, is handled by `backend/src/main/java/com/bigteam/btllm/user/service/UserService.java`, and tokens are verified by `backend/src/main/java/com/bigteam/btllm/common/jwt/JwtAuthFilter.java`. `backend/src/main/java/com/bigteam/btllm/config/SecurityConfig.java` defines the security boundary; ETL progress SSE is intentionally public and controlled through opaque job IDs.

#### Conversational Execution
REST chat-room management is split from streaming generation: `backend/src/main/java/com/bigteam/btllm/chat/controller/ChatRoomController.java` manages persisted rooms/history, while `backend/src/main/java/com/bigteam/btllm/chat/controller/ChatWebSocketHandler.java` receives and streams chat events over WebSocket. `backend/src/main/java/com/bigteam/btllm/config/ChatClientFactory.java` assembles Spring AI clients/providers and advisor behavior. `backend/src/main/java/com/bigteam/btllm/chat/advisor/TokenTrackingAdvisor.java` records actual model usage. `backend/src/main/java/com/bigteam/btllm/chat/service/OllamaGenerationQueue.java` serializes/bounds local GPU generation to protect the 8GB-VRAM Ollama runtime.

#### Agent Tools, Memory, and Persistence
The model chooses tools rather than executing RAG on every prompt. `backend/src/main/java/com/bigteam/btllm/chat/tools/LlmTools.java` exposes web crawling, prior-history search, knowledge-base search, and token usage to Spring AI tool calling. `backend/src/main/java/com/bigteam/btllm/chat/service/ThinkingRouter.java` controls thinking behavior. Durable conversation state is represented by `backend/src/main/java/com/bigteam/btllm/chat/entity/ChatRoom.java` and `backend/src/main/java/com/bigteam/btllm/chat/entity/ChatHistory.java`, backed by PostgreSQL/JPA.

#### RAG Ingestion and Retrieval
ETL requests enter `backend/src/main/java/com/bigteam/btllm/rag/controller/EtlController.java`; `backend/src/main/java/com/bigteam/btllm/rag/service/EtlPipelineService.java` asynchronously fetches/parses URLs and supported files, chunks text, adds a document summary, and writes embeddings to pgVector. `backend/src/main/java/com/bigteam/btllm/rag/service/EtlProgressTracker.java` holds job status and drives SSE progress. Query retrieval can be reranked through `backend/src/main/java/com/bigteam/btllm/rag/service/HybridReranker.java`; settings live in `backend/src/main/java/com/bigteam/btllm/rag/config/RagSearchSettings.java`.

#### Runtime and Operations
Core runtime is Java 17/Spring Boot 3.5/Spring AI with PostgreSQL 17 + pgVector and local Ollama (`qwen3:8b`, `bge-m3`); OpenAI, Anthropic, and Google are optional external providers. `backend/src/main/resources/application.yaml` is the backend runtime configuration. Compose also provisions Prometheus, Grafana, Loki, and Promtail; their configurations are under `monitoring/prometheus/prometheus.yml`, `monitoring/grafana/provisioning`, and `monitoring/loki/loki-config.yml`. CI validates backend, frontend build, and Docker image through `.github/workflows/ci.yml`.

Main entry points: [`docker-compose.yml`](https://github.com/straycat405/personal-llm/blob/main/docker-compose.yml) (Compose deployment), [`frontend/src/pages/ChatPage.tsx`](https://github.com/straycat405/personal-llm/blob/main/frontend/src/pages/ChatPage.tsx) (Chat workflow), [`frontend/src/api/chatRoom.ts`](https://github.com/straycat405/personal-llm/blob/main/frontend/src/api/chatRoom.ts) (Chat & ETL clients), [`backend/src/main/java/com/bigteam/btllm/config/SecurityConfig.java`](https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/config/SecurityConfig.java) (JWT security boundary)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client SPA"]
  node_browser["React document assistant<br/>React 18 SPA<br/>[App.tsx]"]
  node_chat_page["Chat workflow<br/>page<br/>[ChatPage.tsx]"]
  node_client_state["Chat state<br/>client store<br/>[chatStore.ts]"]
  node_client_apis["Chat &amp; ETL clients<br/>REST clients<br/>[chatRoom.ts]"]
end

subgraph group_backend["Spring Boot API"]
  node_application["BTLLM application<br/>Spring Boot entrypoint"]
  node_security["JWT security boundary<br/>Spring Security"]
  node_auth["Authentication<br/>controller + service"]
  node_chat_rooms["Chat rooms &amp; history API<br/>REST controller"]
  node_chat_stream["Streaming chat<br/>WebSocket handler"]
  node_chat_orchestration["AI client &amp; agent routing<br/>Spring AI orchestration"]
  node_agent_tools["Agent tools<br/>tool calling<br/>[LlmTools.java]"]
  node_etl_api["Document ingestion API<br/>REST + SSE controller<br/>[EtlController.java]"]
  node_etl_pipeline["Async ETL pipeline<br/>ingestion service"]
end

subgraph group_data_ai["AI &amp; Data Runtime"]
  node_postgres[("PostgreSQL + pgVector<br/>database")]
  node_ollama{{"Local Ollama<br/>inference runtime"}}
  node_generation_queue["GPU generation queue<br/>bounded queue"]
  node_cloud_models{{"Optional cloud models<br/>external providers"}}
end

subgraph group_operations["Deployment &amp; Observability"]
  node_monitoring["Metrics, logs &amp; dashboards<br/>[prometheus.yml]"]
  node_compose["Compose deployment<br/>container orchestration<br/>[docker-compose.yml]"]
  node_ci["CI validation<br/>GitHub Actions<br/>[ci.yml]"]
end

node_browser -->|"renders"| node_chat_page
node_chat_page -->|"reads and updates"| node_client_state
node_chat_page -->|"uses"| node_client_apis
node_client_apis -->|"login / signup"| node_auth
node_client_apis -->|"room REST"| node_chat_rooms
node_chat_page -->|"chat WebSocket"| node_chat_stream
node_client_apis -->|"ingestion + progress"| node_etl_api
node_application -->|"configures"| node_security
node_security -->|"protects"| node_auth
node_security -->|"protects"| node_chat_rooms
node_security -->|"protects"| node_chat_stream
node_auth -->|"users"| node_postgres
node_chat_rooms -->|"rooms and history"| node_postgres
node_chat_stream -->|"streams prompts"| node_chat_orchestration
node_chat_orchestration -->|"invokes selected tools"| node_agent_tools
node_agent_tools -->|"history and knowledge search"| node_postgres
node_chat_orchestration -->|"local generation"| node_generation_queue
node_generation_queue -->|"serialized requests"| node_ollama
node_chat_orchestration -.->|"optional provider calls"| node_cloud_models
node_etl_api -->|"starts async job"| node_etl_pipeline
node_etl_pipeline -->|"creates embeddings"| node_ollama
node_etl_pipeline -->|"writes vectors and documents"| node_postgres
node_compose -->|"runs"| node_application
node_compose -->|"provisions"| node_postgres
node_compose -->|"provisions"| node_ollama
node_compose -->|"provisions"| node_monitoring
node_monitoring -.->|"observes"| node_application
node_ci -.->|"validates image"| node_compose

click node_browser "https://github.com/straycat405/personal-llm/blob/main/frontend/src/App.tsx"
click node_chat_page "https://github.com/straycat405/personal-llm/blob/main/frontend/src/pages/ChatPage.tsx"
click node_client_state "https://github.com/straycat405/personal-llm/blob/main/frontend/src/stores/chatStore.ts"
click node_client_apis "https://github.com/straycat405/personal-llm/blob/main/frontend/src/api/chatRoom.ts"
click node_application "https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/BtllmApplication.java"
click node_security "https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/config/SecurityConfig.java"
click node_auth "https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/user/controller/AuthController.java"
click node_chat_rooms "https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/chat/controller/ChatRoomController.java"
click node_chat_stream "https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/chat/controller/ChatWebSocketHandler.java"
click node_chat_orchestration "https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/config/ChatClientFactory.java"
click node_agent_tools "https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/chat/tools/LlmTools.java"
click node_etl_api "https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/rag/controller/EtlController.java"
click node_etl_pipeline "https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/rag/service/EtlPipelineService.java"
click node_generation_queue "https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/chat/service/OllamaGenerationQueue.java"
click node_monitoring "https://github.com/straycat405/personal-llm/blob/main/monitoring/prometheus/prometheus.yml"
click node_compose "https://github.com/straycat405/personal-llm/blob/main/docker-compose.yml"
click node_ci "https://github.com/straycat405/personal-llm/blob/main/.github/workflows/ci.yml"

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_browser,node_chat_page,node_client_state,node_client_apis toneBlue
class node_application,node_security,node_auth,node_chat_rooms,node_chat_stream,node_chat_orchestration,node_agent_tools,node_etl_api,node_etl_pipeline toneAmber
class node_postgres,node_ollama,node_generation_queue,node_cloud_models toneMint
class node_monitoring,node_compose,node_ci toneRose
```

## Components

### Client SPA

- [React document assistant](https://github.com/straycat405/personal-llm/blob/main/frontend/src/App.tsx): React 18 SPA · Authentication, chat, model, and knowledge-base workflows. · `frontend/src/App.tsx`
- [Chat workflow](https://github.com/straycat405/personal-llm/blob/main/frontend/src/pages/ChatPage.tsx): page · `frontend/src/pages/ChatPage.tsx`
- [Chat state](https://github.com/straycat405/personal-llm/blob/main/frontend/src/stores/chatStore.ts): client store · `frontend/src/stores/chatStore.ts`
- [Chat & ETL clients](https://github.com/straycat405/personal-llm/blob/main/frontend/src/api/chatRoom.ts): REST clients · `frontend/src/api/chatRoom.ts`

### Spring Boot API

- [BTLLM application](https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/BtllmApplication.java): Spring Boot entrypoint · `backend/src/main/java/com/bigteam/btllm/BtllmApplication.java`
- [JWT security boundary](https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/config/SecurityConfig.java): Spring Security · ETL progress SSE is public and protected by opaque job IDs. · `backend/src/main/java/com/bigteam/btllm/config/SecurityConfig.java`
- [Authentication](https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/user/controller/AuthController.java): controller + service · `backend/src/main/java/com/bigteam/btllm/user/controller/AuthController.java`
- [Chat rooms & history API](https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/chat/controller/ChatRoomController.java): REST controller · `backend/src/main/java/com/bigteam/btllm/chat/controller/ChatRoomController.java`
- [Streaming chat](https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/chat/controller/ChatWebSocketHandler.java): WebSocket handler · `backend/src/main/java/com/bigteam/btllm/chat/controller/ChatWebSocketHandler.java`
- [AI client & agent routing](https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/config/ChatClientFactory.java): Spring AI orchestration · Builds providers, advisors, thinking behavior, and model tool calling. · `backend/src/main/java/com/bigteam/btllm/config/ChatClientFactory.java`
- [Agent tools](https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/chat/tools/LlmTools.java): tool calling · Provides web, history, knowledge search, and token-usage tools. · `backend/src/main/java/com/bigteam/btllm/chat/tools/LlmTools.java`
- [Document ingestion API](https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/rag/controller/EtlController.java): REST + SSE controller · `backend/src/main/java/com/bigteam/btllm/rag/controller/EtlController.java`
- [Async ETL pipeline](https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/rag/service/EtlPipelineService.java): ingestion service · Fetches, parses, chunks, summarizes, embeds, and indexes documents. · `backend/src/main/java/com/bigteam/btllm/rag/service/EtlPipelineService.java`

### AI & Data Runtime

- **PostgreSQL + pgVector**: database · Stores users, conversations, knowledge data, and vector embeddings.
- **Local Ollama**: inference runtime · Hosts qwen3:8b generation and bge-m3 embeddings.
- [GPU generation queue](https://github.com/straycat405/personal-llm/blob/main/backend/src/main/java/com/bigteam/btllm/chat/service/OllamaGenerationQueue.java): bounded queue · Serializes local generation for the 8GB-VRAM runtime. · `backend/src/main/java/com/bigteam/btllm/chat/service/OllamaGenerationQueue.java`
- **Optional cloud models**: external providers · OpenAI, Anthropic, and Google providers.

### Deployment & Observability

- [Metrics, logs & dashboards](https://github.com/straycat405/personal-llm/blob/main/monitoring/prometheus/prometheus.yml): Prometheus + Grafana + Loki · `monitoring/prometheus/prometheus.yml`
- [Compose deployment](https://github.com/straycat405/personal-llm/blob/main/docker-compose.yml): container orchestration · Provisions application, data, AI, and observability runtimes. · `docker-compose.yml`
- [CI validation](https://github.com/straycat405/personal-llm/blob/main/.github/workflows/ci.yml): GitHub Actions · Validates backend, frontend build, and Docker image. · `.github/workflows/ci.yml`

## Connections

- React document assistant → Chat workflow: renders
- Chat workflow → Chat state: reads and updates
- Chat workflow → Chat & ETL clients: uses
- Chat & ETL clients → Authentication: login / signup
- Chat & ETL clients → Chat rooms & history API: room REST
- Chat workflow → Streaming chat: chat WebSocket
- Chat & ETL clients → Document ingestion API: ingestion + progress
- BTLLM application → JWT security boundary: configures
- JWT security boundary → Authentication: protects
- JWT security boundary → Chat rooms & history API: protects
- JWT security boundary → Streaming chat: protects
- Authentication → PostgreSQL + pgVector: users
- Chat rooms & history API → PostgreSQL + pgVector: rooms and history
- Streaming chat → AI client & agent routing: streams prompts
- AI client & agent routing → Agent tools: invokes selected tools
- Agent tools → PostgreSQL + pgVector: history and knowledge search
- AI client & agent routing → GPU generation queue: local generation
- GPU generation queue → Local Ollama: serialized requests
- AI client & agent routing → Optional cloud models: optional provider calls
- Document ingestion API → Async ETL pipeline: starts async job
- Async ETL pipeline → Local Ollama: creates embeddings
- Async ETL pipeline → PostgreSQL + pgVector: writes vectors and documents
- Compose deployment → BTLLM application: runs
- Compose deployment → PostgreSQL + pgVector: provisions
- Compose deployment → Local Ollama: provisions
- Compose deployment → Metrics, logs & dashboards: provisions
- Metrics, logs & dashboards → BTLLM application: observes
- CI validation → Compose deployment: validates image
