How awais-nazir/knowledge-assistant works
1. Overall shape
- This is a FastAPI-based backend for a multi-user “smart knowledge assistant.”
- The architecture is layered: API routers → services → repositories → database/storage, with a separate RAG/embedding pipeline and background workers for document processing.
2. External-facing API layer
- app/main.py is the application entrypoint.
- app/api/routers/ splits HTTP concerns into auth, documents, chat, and health endpoints.
- app/api/deps.py centralizes dependency injection, likely for DB sessions, authenticated users, and service wiring.
3. Core platform concerns
- app/core/ holds cross-cutting infrastructure:
- config.py for runtime configuration
- database.py for DB session/engine setup
- security.py for auth-related primitives
- logging.py, middleware.py, exceptions.py for request lifecycle and error handling
- This layer is the boundary between framework/runtime behavior and business logic.
4. Domain models and persistence
- app/models/ contains the persisted entities: user, document, chunk, chat, memory.
- app/repositories/ encapsulates data access per aggregate, keeping SQL/ORM details out of services.
- alembic/ and alembic/versions/ show a migration-driven schema lifecycle; the repository is designed to evolve the database explicitly.
5. Business services
- app/services/ contains the main application workflows:
- auth_service.py for signup/login/user identity flows
- document_service.py for upload and document lifecycle
- chat_service.py for conversational interactions and persistence
- These services coordinate repositories, storage, and RAG components, which is where most business rules live.
6. Document ingestion pipeline
- Uploaded documents are not just stored; they are processed into chunks and embeddings.
- app/storage/local.py indicates a local file storage abstraction, with app/storage/base.py defining the contract.
- app/workers/embedding_tasks.py and app/workers/celery_app.py show that embedding generation is offloaded to Celery background workers rather than handled synchronously in the API path.
7. RAG subsystem
- app/rag/ is the core retrieval-augmented generation subsystem.
- Key pieces:
- chunker.py for text segmentation
- hybrid_retriever.py for retrieval logic
- prompt_builder.py for assembling model prompts
- pipeline.py for end-to-end orchestration
- factory.py and interfaces.py for pluggable component selection
- This is the main boundary where stored knowledge becomes model context.
8. Model/provider abstraction
- app/rag/providers/ is split by capability:
- embedders: openai.py, huggingface.py
- llms: openai.py, ollama.py
- rerankers: flashrank.py
- The repo is intentionally provider-agnostic, so the architecture can swap hosted or local AI backends without changing the rest of the app.
9. Chat and memory flow
- Chat requests likely flow from chat router → chat_service → RAG pipeline → LLM provider.
- Responses and conversation state are persisted via chat_repo.py and memory_repo.py.
- The memory model indicates an explicit personalization layer, so the assistant can use prior interactions in later responses.
10. Multi-tenancy and isolation
- The README’s main promise is that each user manages isolated data.
- That implies every document, chunk, chat, and memory record is scoped to a user, and the repositories/services enforce that boundary.
- This is the key architectural security boundary: user identity from auth must flow through all data access paths.
11. Operational/runtime architecture
- Dockerfile, docker-compose.yaml, docker-compose.prod.yml, and deployment/nginx.conf indicate containerized deployment with an Nginx front layer in production.
- MakeFile and .github/workflows/ci.yml show supporting build/test automation.
- req.txt, pyproject.toml, and uv.lock suggest Python dependency management with reproducible builds.
12. What to draw in an architecture diagram
- Draw three main planes:
1. HTTP/API plane: FastAPI routers, middleware, auth, health.
2. Application/data plane: services, repositories, SQL database, file storage, migrations.
3. AI processing plane: Celery workers, chunking/embedding, retrieval, prompt building, LLM/reranker providers.
- The most important data flows are:
- Register/login → auth service → user store
- Upload document → store file → enqueue embedding task → chunk/embed → persist chunks
- Chat query → retrieve relevant chunks + memory → build prompt → call LLM → persist chat/memory
Main entry points: services Services · pipeline.py RAG pipeline · routers Routers · repositories Repositories
Components 18
HTTP/API
FastAPI entrypoint, routers, and request-scoped dependencies.
Core
Cross-cutting runtime, config, security, and middleware.
- Core runtime · Config, database, security, logging, middleware, and exceptions.
app/core
Application
Business services, schemas, and orchestration.
Data
Domain models, repositories, storage, and migrations.
- Domain models · User, document, chunk, chat, and memory persistence models.
app/models - Repositories · Per-aggregate persistence boundary for users, docs, chats, chunks, and memory.
app/repositories - Migrations · Alembic environment and versioned database migrations.
alembic - File storage · Storage abstraction with local filesystem implementation.
app/storage - Chat & memory · Chat and memory persistence boundary for conversational history and personalization.
app/repositories/chat_repo.py
AI/RAG
Retrieval, prompt construction, providers, and workers.
- RAG pipeline · Chunking, retrieval, prompt building, and orchestration.
app/rag/pipeline.py - Retriever · Combines lexical/vector retrieval for relevant chunks.
app/rag/hybrid_retriever.py - Prompt builder · Builds model context from chat, memory, and retrieved knowledge.
app/rag/prompt_builder.py - Providers · Embedders, LLMs, and rerankers for OpenAI, Hugging Face, Ollama, and Flashrank.
app/rag/providers - RAG factory · Selects concrete AI components behind interfaces.
app/rag/factory.py - Workers · Celery app and embedding tasks for asynchronous document processing.
app/workers
Ops
Containerization and deployment support.
- Deployment · Nginx and container deployment configuration.
deployment/nginx.conf
Connections 19
- App entry → Routers: mounts · Registers HTTP routes.
- App entry → Core runtime: uses · Loads config, middleware, logging, and error handling.
- Routers → Deps: depends on · Resolves request-scoped dependencies.
- Routers → Services: calls · Routes delegate to business workflows.
- Services → Repositories: persists via · Services read and write domain data through repositories.
- Services → File storage: stores files · Document workflows use file storage.
- Services → Schemas: serializes · Uses DTOs for request and response bodies.
- Repositories → Domain models: maps · Repositories operate on ORM entities.
- Repositories → Migrations: evolves with · Schema changes are managed through Alembic versions.
- Services → RAG pipeline: invokes · Chat and document workflows trigger RAG processing.
- RAG pipeline → Retriever: retrieves · Fetches relevant chunks and context.
- RAG pipeline → Prompt builder: builds · Assembles prompts for generation.
- RAG pipeline → Providers: uses · Calls embedders, LLMs, and rerankers through provider adapters.
- RAG pipeline → RAG factory: composes · Selects concrete implementations.
- Workers → RAG pipeline: processes · Background jobs chunk and embed documents.
- Workers → Providers: embeds with · Embedding tasks use provider implementations.
- Services → Chat & memory: updates · Chat workflows persist conversation state and memory.
- Routers → Core runtime: protected by · Auth, security, and middleware apply at request boundaries.
- Deployment → App entry: fronts · Production deployment places Nginx in front of the app.