# awais-nazir/knowledge-assistant architecture

> Architecture diagram of the GitHub repository awais-nazir/knowledge-assistant, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/awais-nazir/knowledge-assistant
- Repository: https://github.com/awais-nazir/knowledge-assistant

## Overview

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: [`app/services`](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/services) (Services), [`app/rag/pipeline.py`](https://github.com/awais-nazir/knowledge-assistant/blob/main/app/rag/pipeline.py) (RAG pipeline), [`app/api/routers`](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/api/routers) (Routers), [`app/repositories`](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/repositories) (Repositories)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP/API"]
  node_main["App entry<br/>FastAPI app<br/>[main.py]"]
  node_routers["Routers<br/>HTTP endpoints"]
  node_deps["Deps<br/>DI wiring<br/>[deps.py]"]
end

subgraph group_core["Core"]
  node_core_runtime["Core runtime<br/>infra layer"]
end

subgraph group_app["Application"]
  node_services["Services<br/>use cases"]
  node_schemas["Schemas<br/>DTOs"]
end

subgraph group_data["Data"]
  node_models[("Domain models<br/>ORM entities")]
  node_repos["Repositories<br/>data access"]
  node_migrations["Migrations<br/>schema evolution"]
  node_storage[("File storage<br/>blob storage")]
  node_chat_memory["Chat & memory<br/>conversation state<br/>[chat_repo.py]"]
end

subgraph group_ai["AI/RAG"]
  node_rag_pipeline["RAG pipeline<br/>retrieval flow<br/>[pipeline.py]"]
  node_retriever["Retriever<br/>hybrid search"]
  node_prompt_builder["Prompt builder<br/>prompt assembly<br/>[prompt_builder.py]"]
  node_providers["Providers<br/>AI adapters"]
  node_rag_factory["RAG factory<br/>composition root<br/>[factory.py]"]
  node_workers["Workers<br/>background jobs"]
end

subgraph group_ops["Ops"]
  node_deployment["Deployment<br/>runtime ops<br/>[nginx.conf]"]
end

node_main -->|"mounts"| node_routers
node_main -->|"uses"| node_core_runtime
node_routers -->|"depends on"| node_deps
node_routers -->|"calls"| node_services
node_services -->|"persists via"| node_repos
node_services -->|"stores files"| node_storage
node_services -->|"serializes"| node_schemas
node_repos -->|"maps"| node_models
node_repos -.->|"evolves with"| node_migrations
node_services -->|"invokes"| node_rag_pipeline
node_rag_pipeline -->|"retrieves"| node_retriever
node_rag_pipeline -->|"builds"| node_prompt_builder
node_rag_pipeline -->|"uses"| node_providers
node_rag_pipeline -->|"composes"| node_rag_factory
node_workers -->|"processes"| node_rag_pipeline
node_workers -->|"embeds with"| node_providers
node_services -->|"updates"| node_chat_memory
node_routers -->|"protected by"| node_core_runtime
node_deployment -.->|"fronts"| node_main

click node_main "https://github.com/awais-nazir/knowledge-assistant/blob/main/app/main.py"
click node_routers "https://github.com/awais-nazir/knowledge-assistant/tree/main/app/api/routers"
click node_deps "https://github.com/awais-nazir/knowledge-assistant/blob/main/app/api/deps.py"
click node_core_runtime "https://github.com/awais-nazir/knowledge-assistant/tree/main/app/core"
click node_models "https://github.com/awais-nazir/knowledge-assistant/tree/main/app/models"
click node_repos "https://github.com/awais-nazir/knowledge-assistant/tree/main/app/repositories"
click node_migrations "https://github.com/awais-nazir/knowledge-assistant/tree/main/alembic"
click node_storage "https://github.com/awais-nazir/knowledge-assistant/tree/main/app/storage"
click node_services "https://github.com/awais-nazir/knowledge-assistant/tree/main/app/services"
click node_schemas "https://github.com/awais-nazir/knowledge-assistant/tree/main/app/schemas"
click node_rag_pipeline "https://github.com/awais-nazir/knowledge-assistant/blob/main/app/rag/pipeline.py"
click node_retriever "https://github.com/awais-nazir/knowledge-assistant/blob/main/app/rag/hybrid_retriever.py"
click node_prompt_builder "https://github.com/awais-nazir/knowledge-assistant/blob/main/app/rag/prompt_builder.py"
click node_providers "https://github.com/awais-nazir/knowledge-assistant/tree/main/app/rag/providers"
click node_rag_factory "https://github.com/awais-nazir/knowledge-assistant/blob/main/app/rag/factory.py"
click node_workers "https://github.com/awais-nazir/knowledge-assistant/tree/main/app/workers"
click node_chat_memory "https://github.com/awais-nazir/knowledge-assistant/blob/main/app/repositories/chat_repo.py"
click node_deployment "https://github.com/awais-nazir/knowledge-assistant/blob/main/deployment/nginx.conf"

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_main,node_routers,node_deps toneBlue
class node_core_runtime toneAmber
class node_services,node_schemas toneMint
class node_models,node_repos,node_migrations,node_storage,node_chat_memory toneRose
class node_rag_pipeline,node_retriever,node_prompt_builder,node_providers,node_rag_factory,node_workers toneIndigo
class node_deployment toneTeal
```

## Components

### HTTP/API

FastAPI entrypoint, routers, and request-scoped dependencies.

- [App entry](https://github.com/awais-nazir/knowledge-assistant/blob/main/app/main.py): FastAPI app · Application startup and router wiring. · `app/main.py`
- [Routers](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/api/routers): HTTP endpoints · Auth, documents, chat, and health routes. · `app/api/routers`
- [Deps](https://github.com/awais-nazir/knowledge-assistant/blob/main/app/api/deps.py): DI wiring · Shared dependency injection for sessions, users, and services. · `app/api/deps.py`

### Core

Cross-cutting runtime, config, security, and middleware.

- [Core runtime](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/core): infra layer · Config, database, security, logging, middleware, and exceptions. · `app/core`

### Application

Business services, schemas, and orchestration.

- [Services](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/services): use cases · Auth, document, and chat workflows that coordinate data and AI components. · `app/services`
- [Schemas](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/schemas): DTOs · Request and response shapes for auth, chat, documents, and users. · `app/schemas`

### Data

Domain models, repositories, storage, and migrations.

- [Domain models](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/models): ORM entities · User, document, chunk, chat, and memory persistence models. · `app/models`
- [Repositories](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/repositories): data access · Per-aggregate persistence boundary for users, docs, chats, chunks, and memory. · `app/repositories`
- [Migrations](https://github.com/awais-nazir/knowledge-assistant/tree/main/alembic): schema evolution · Alembic environment and versioned database migrations. · `alembic`
- [File storage](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/storage): blob storage · Storage abstraction with local filesystem implementation. · `app/storage`
- [Chat & memory](https://github.com/awais-nazir/knowledge-assistant/blob/main/app/repositories/chat_repo.py): conversation state · 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](https://github.com/awais-nazir/knowledge-assistant/blob/main/app/rag/pipeline.py): retrieval flow · Chunking, retrieval, prompt building, and orchestration. · `app/rag/pipeline.py`
- [Retriever](https://github.com/awais-nazir/knowledge-assistant/blob/main/app/rag/hybrid_retriever.py): hybrid search · Combines lexical/vector retrieval for relevant chunks. · `app/rag/hybrid_retriever.py`
- [Prompt builder](https://github.com/awais-nazir/knowledge-assistant/blob/main/app/rag/prompt_builder.py): prompt assembly · Builds model context from chat, memory, and retrieved knowledge. · `app/rag/prompt_builder.py`
- [Providers](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/rag/providers): AI adapters · Embedders, LLMs, and rerankers for OpenAI, Hugging Face, Ollama, and Flashrank. · `app/rag/providers`
- [RAG factory](https://github.com/awais-nazir/knowledge-assistant/blob/main/app/rag/factory.py): composition root · Selects concrete AI components behind interfaces. · `app/rag/factory.py`
- [Workers](https://github.com/awais-nazir/knowledge-assistant/tree/main/app/workers): background jobs · Celery app and embedding tasks for asynchronous document processing. · `app/workers`

### Ops

Containerization and deployment support.

- [Deployment](https://github.com/awais-nazir/knowledge-assistant/blob/main/deployment/nginx.conf): runtime ops · Nginx and container deployment configuration. · `deployment/nginx.conf`

## Connections

- 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.
