How rma-mun/langchain-rag-fastapi-service works
1. System shape
- The repository is a three-part application: a Vue 3 frontend, a FastAPI backend, and a separate Django user service.
- The FastAPI app is the main business API; the Django service is dedicated to user registration/login/profile/avatar handling.
2. Frontend boundary
- front/ is a client-only SPA built with Vue 3, Vite, Pinia, Vue Router, and Vant.
- It hosts the main product screens: note editor/list, daily review, AI chat, knowledge base, session management, login/register, and profile/settings.
- It talks to the backend over HTTP and relies on JWT-based auth state in the browser.
3. FastAPI backend as the core orchestration layer
- backend/main.py starts the API server and wires the routers under backend/app/router/.
- The backend is organized as a classic API/service split:
- routers expose endpoints,
- services implement note/review business logic,
- rag/ contains retrieval and document-processing pipelines,
- agent/ contains LLM tool orchestration,
- models/ and schemas/ define persistence and API payloads.
4. Authentication and user isolation
- Authentication is handled by the Django service, not by the FastAPI app.
- The FastAPI backend includes auth utilities and uses the configured DJANGO_API_URL to integrate with that service.
- The architecture is designed for per-user isolation: user identity is used to scope notes, knowledge documents, chat history, and retrieval results.
5. Django user service
- DjangoUserService/ is a standalone Django app with user, file, and utility modules.
- It owns user lifecycle operations and avatar/file upload.
- It uses MySQL for user data, Redis for cache/session-related use, and Celery for background tasks.
6. Note management subsystem
- Notes are handled in backend/app/router/note_router.py and backend/app/services/note_service.py.
- The note flow is: create/edit note → persist to MySQL → generate tags/classification with LLM assistance → index content for semantic search and related-note recommendations.
- The frontend note editor integrates inline AI completion and related-note panels to support writing while editing.
7. Knowledge base and RAG pipeline
- Uploaded knowledge files are processed by the RAG stack in backend/app/rag/.
- Pipeline stages are:
- file ingestion and format-specific parsing (document_handler/),
- text chunking (text_spliter.py),
- deduplication via MD5 tracking (md5_manager/),
- vector storage in ChromaDB (vector_store.py),
- retrieval through hybrid retrievers and reranking,
- answer generation with LLM prompts and context assembly.
- Supported formats include TXT, PDF, MD, PPTX, and DOCX.
8. Chat and agent subsystem
- backend/app/router/chat.py and backend/app/agent/ implement the AI chat experience.
- The chat flow is not just “prompt and response”; it uses an agent + tools pattern, which can call RAG retrieval and related helpers before producing the final answer.
- Chat sessions and history are persisted in MySQL, enabling multi-turn continuity and session replay.
9. Daily review / spaced repetition
- Review logic is isolated in review_service.py and review_router.py.
- It uses an Ebbinghaus-style spaced repetition schedule to surface notes for review on timed intervals.
- Review records are stored in MySQL, and the frontend shows them as a dedicated daily review workflow.
10. Data stores and infrastructure
- MySQL stores durable relational data: users, notes, review records, and chat history.
- Redis is used for caching and background coordination.
- ChromaDB stores vector embeddings for both knowledge documents and note-related semantic search.
- Local or remote LLMs are supported through configuration: DashScope/Aliyun or Ollama, with a separate reranker model path configured locally.
11. Model and prompt layer
- Prompts are externalized in backend/app/prompt/, which makes the AI behaviors configurable without changing business routes.
- The backend also supports embedding and reranking model configuration, so retrieval quality is a first-class architectural concern rather than an implementation detail.
12. Main end-to-end flows
- Login/register: frontend → Django user service → JWT returned → frontend uses token for backend calls.
- Note writing: frontend editor → FastAPI note API → MySQL + semantic indexing → AI completion / related-note retrieval.
- Document Q&A: frontend knowledge upload → FastAPI RAG ingestion → ChromaDB indexing → chat/query endpoint → LLM response with citations.
- Daily review: scheduled/query-based retrieval of due notes → spaced-repetition logic → review UI.
Main entry points: index.js Client Router · router API Routers · note_service.py Note Service · rag_service.py RAG Pipeline
Components 24
Vue SPA
Client-side product UI and browser state.
- Frontend App · Single-page application shell for the product UI.
front/src/App.vue - Client Router · Route-level navigation for product screens.
front/src/router/index.js - Auth Store · Browser state for user, session, theme, and language.
front/src/store/index.js - API Client · Frontend API configuration and backend request entrypoint.
front/src/config/api.js - Note Editor · Writing, inline completion, and related-note UI.
front/src/views/NoteEditor.vue - Daily Review · Spaced-repetition review workflow and review cards.
front/src/views/DailyReview.vue - AI Chat · Multi-turn assistant chat and knowledge Q&A entrypoint.
front/src/views/AIChat.vue - Knowledge Base · Document upload and knowledge management UI.
front/src/views/KnowledgeBase.vue - Login Profile · Login, register, profile, sessions, and settings screens.
front/src/views/Login.vue
FastAPI Core
Main business API, orchestration, RAG, and note/review services.
- API Server · Application bootstrap and router wiring.
backend/main.py - API Routers · Main endpoint layer for chat, notes, review, knowledge, and health.
backend/app/router - Note Service · Create/edit notes, tagging, indexing, and related-note logic.
backend/app/services/note_service.py - Review Service · Due-note selection and Ebbinghaus-style review scheduling.
backend/app/services/review_service.py - Chat Agent · Agent and tool-based chat execution with RAG helpers.
backend/app/agent/agent.py - RAG Pipeline · Document ingestion, chunking, retrieval, reranking, and answer assembly.
backend/app/rag/rag_service.py - Domain Models · MySQL-backed note, review, and chat history entities.
backend/app/models - Prompt Pack · Externalized prompts for writing, review, chat, and RAG behavior.
backend/app/prompt - Auth Config · Auth helpers, config loading, file/image/pdf utilities, and backend integration glue.
backend/app/utils - Data Stores · MySQL and Redis connections used by the FastAPI backend.
backend/app/db - Vector Store · Embedding index for note search and document retrieval.
backend/app/rag/vector_store.py
Django User Service
Standalone user, file, and auth service.
- Django Service · Standalone user lifecycle and file service.
DjangoUserService/DjangoUserService - User Module · Registration, login, profile, avatar, and auth endpoints.
DjangoUserService/apps/user - File Module · Avatar and file upload handling.
DjangoUserService/apps/file - User Utils · Cache and rate-limit helpers for the user service.
DjangoUserService/apps/utils
Connections 29
- Frontend App → Client Router: routes · The app shell is driven by client routing.
- Frontend App → Auth Store: state · The app reads auth and session state from Pinia.
- Frontend App → API Client: requests · Screens call the backend through the shared API client.
- Client Router → Note Editor: navigates · Routes expose the note editor.
- Client Router → Daily Review: navigates · Routes expose the review workflow.
- Client Router → AI Chat: navigates · Routes expose AI chat.
- Client Router → Knowledge Base: navigates · Routes expose knowledge management.
- Client Router → Login Profile: navigates · Routes expose auth and profile screens.
- API Client → API Server: http · Frontend talks to the FastAPI service over HTTP.
- Login Profile → User Module: auth · Login and registration go through the Django user service.
- Login Profile → File Module: upload · Profile and avatar files use the file service.
- API Server → API Routers: mounts · The server wires the application routers.
- API Routers → Note Service: note api · Note endpoints delegate to the note service.
- API Routers → Review Service: review api · Review endpoints delegate to the review service.
- API Routers → Chat Agent: chat api · Chat endpoints delegate to the agent layer.
- API Routers → RAG Pipeline: knowledge api · Knowledge endpoints feed the RAG pipeline.
- Note Service → Domain Models: persist · Notes and related metadata are stored relationally.
- Note Service → Vector Store: index · Notes are indexed for semantic search and recommendations.
- Note Service → Prompt Pack: llm prompts · Tagging and autocomplete use prompt assets.
- Review Service → Domain Models: persist · Review records and due-state data live in MySQL.
- Chat Agent → RAG Pipeline: tools · The agent can call retrieval and helper tools.
- Chat Agent → Domain Models: persist · Chat sessions and history are stored relationally.
- RAG Pipeline → Vector Store: retrieve · RAG reads and writes embeddings in ChromaDB.
- RAG Pipeline → Prompt Pack: compose · Answer generation is driven by prompt templates.
- Auth Config → User Module: integrates · Backend auth utilities integrate with the Django user service.
- Auth Config → Data Stores: connects · Shared backend utilities configure database and cache access.
- Django Service → User Module: hosts · The Django project hosts the user module.
- Django Service → File Module: hosts · The Django project hosts the file module.
- Django Service → User Utils: supports · The Django project includes cache and rate-limit helpers.