# shukla-abhinav/fastapi-ollama-qa architecture

> Architecture diagram of the GitHub repository shukla-abhinav/fastapi-ollama-qa, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shukla-abhinav/fastapi-ollama-qa
- Repository: https://github.com/shukla-abhinav/fastapi-ollama-qa

## Overview

##### Project Overview and Main Components
- The system is a backend application built with FastAPI to manage documents and enable Retrieval-Augmented Generation (RAG) based question answering using Llama 2.
- It employs Docker for containerization and uses PostgreSQL as the primary database.
- The architecture is structured into distinct components: API layer, business/service layer, database layer (with SQLAlchemy models and migrations via Alembic), and utility/configuration layers.

##### Identifying Key Components
a. API Layer (app/api/):
- Use clear labels for each endpoint grouping (Documents, QA, Users).

b. Service Layer (app/services/):
- Business logic handling document service and QA service (e.g., document_service.py and qa_service.py).
- These components orchestrate processes like generating document embeddings (with Sentence Transformers) and interfacing with the Llama 2 model for question answering.

c. Database Layer (app/db/ and app/models/):
- PostgreSQL database along with SQLAlchemy models.
- Alembic migrations (alembic/ folder and alembic.ini) managing schema changes.
- Database session management (via app/db/database.py).

d. Containerization and Deployment:
- Dockerfile and docker-compose.yml components. These should encapsulate overall application deployment, indicating containerized microservices or services running in Docker.
- Optionally reference configuration files (setup.py, requirements.txt) that support building the container images.

##### Illustrating Relationships and Data Flow
- From the API layer, show data flowing to the service layer which processes business logic.
- From the service layer, include interactions with the database layer (CRUD operations performed via SQLAlchemy models).
- For document embeddings and QA processes, highlight a flow from the service layer to external machine learning components, such as Sentence Transformers for embeddings and the Llama 2 model for question/answer generation.

##### Architectural Patterns and Design Principles
- Clearly separate concerns using the Clean Architecture pattern. Each layer (API, services, database) should be self-contained.
- Asynchronous API design, especially if there’s any use of async routines in FastAPI endpoints.
- That the application is built as a microservice-like modular structure even within a monolithic deployment facilitated by Docker.

##### Relevant Technologies and Frameworks to be Noted
- FastAPI for routing and API management.
- SQLAlchemy for ORM in the models folder.
- Alembic for database migrations.
- Docker and Docker Compose for container orchestration.
- Sentence Transformers and Llama 2 for document embedding and RAG-based question answering, respectively.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["User/Client"]:::client

    subgraph "API Layer"
        B["Documents Endpoint"]:::api
        C["QA Endpoint"]:::api
        D["Users Endpoint"]:::api
    end

    subgraph "Service Layer"
        E["Document Service"]:::service
        F["QA Service"]:::service
    end

    subgraph "Database Layer"
        G["DB Session"]:::database
        H["SQLAlchemy Models"]:::database
    end

    subgraph "Database Migrations"
        I["Alembic Migrations"]:::migration
        J["Alembic Config"]:::migration
    end

    subgraph "ML Components"
        K["Sentence Transformers"]:::ml
        L["Llama 2 Q&A Model"]:::ml
    end

    subgraph "Containerization & Deployment"
        M["Dockerfile"]:::container
        N["Docker Compose"]:::container
        O["Setup.py"]:::container
        P["Requirements.txt"]:::container
    end

    %% Data Flow Connections
    A -->|"request"| B
    A -->|"request"| C
    A -->|"request"| D

    B -->|"process"| E
    C -->|"process"| F
    D -->|"process"| E

    E -->|"CRUD"| G
    E -->|"CRUD"| H
    F -->|"CRUD"| G
    F -->|"CRUD"| H

    E -->|"embedding"| K
    F -->|"Q&A"| L

    %% Deployment arrows from Containerization to core layers
    N -->|"deploys"| B
    N -->|"deploys"| E
    N -->|"deploys"| G

    %% Click Events for API Layer
    click B "https://github.com/shukla-abhinav/fastapi-ollama-qa/blob/main/app/api/v1/documents.py"
    click C "https://github.com/shukla-abhinav/fastapi-ollama-qa/blob/main/app/api/v1/qa.py"
    click D "https://github.com/shukla-abhinav/fastapi-ollama-qa/blob/main/app/api/v1/users.py"

    %% Click Events for Service Layer
    click E "https://github.com/shukla-abhinav/fastapi-ollama-qa/blob/main/app/services/document_service.py"
    click F "https://github.com/shukla-abhinav/fastapi-ollama-qa/blob/main/app/services/qa_service.py"

    %% Click Events for Database Layer
    click G "https://github.com/shukla-abhinav/fastapi-ollama-qa/blob/main/app/db/database.py"
    click H "https://github.com/shukla-abhinav/fastapi-ollama-qa/blob/main/app/models/models.py"

    %% Click Events for Database Migrations
    click I "https://github.com/shukla-abhinav/fastapi-ollama-qa/tree/main/alembic/"
    click J "https://github.com/shukla-abhinav/fastapi-ollama-qa/blob/main/alembic.ini"

    %% Click Events for Containerization and Deployment
    click M "https://github.com/shukla-abhinav/fastapi-ollama-qa/tree/main/Dockerfile"
    click N "https://github.com/shukla-abhinav/fastapi-ollama-qa/blob/main/docker-compose.yml"
    click O "https://github.com/shukla-abhinav/fastapi-ollama-qa/blob/main/setup.py"
    click P "https://github.com/shukla-abhinav/fastapi-ollama-qa/blob/main/requirements.txt"

    %% Styles
    classDef client fill:#f9d5e5,stroke:#c94040,stroke-width:1px;
    classDef api fill:#cce5ff,stroke:#3399ff,stroke-width:1px;
    classDef service fill:#d4edda,stroke:#28a745,stroke-width:1px;
    classDef database fill:#ffeeba,stroke:#ffc107,stroke-width:1px;
    classDef migration fill:#fff3cd,stroke:#ffc107,stroke-width:1px;
    classDef ml fill:#fddde6,stroke:#ff66c4,stroke-width:1px;
    classDef container fill:#d1ecf1,stroke:#0c5460,stroke-width:1px;
```
