# trongdung143/langchain architecture

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

- Interactive diagram: https://gitdiagram.com/trongdung143/langchain
- Repository: https://github.com/trongdung143/langchain

## Overview

##### Project Type and Purpose
- This is a containerized, FastAPI-based, full‐stack question-answering and emotional-response service. It uses LangChain for document processing and vector embeddings (Faiss) to power two API endpoints: one for factual Q&A and one for styled emotional responses.

##### File Structure Analysis
Top-level:
- Dockerfile, docker-compose.yml, nginx.conf – container orchestration and reverse proxy
- requirements.txt – Python dependencies
- app/ – main application
– api/ (ask.py, rewrite.py, home.py) – HTTP endpoints
– services/ – business logic (prompt creation, document loading, RAG chain orchestration, vector store access, templates)
– models/ – ML model loading (response model, emotional style model)
– config.py, main.py, setup.py – app configuration, entry point, packaging
- data/ – persistence
– courses/ – raw PDFs
– vectorstores/ – Faiss indices + metadata
- scripts/ – utility scripts for conversion and vector DB creation
- tests/ – endpoint test coverage

##### README Insights
- Project description, features, architecture overview
- Env-setup (.env variables for external API keys and file paths)
- Docker & manual run instructions
- Two API “type” modes (1=Q&A, 2=emotional rewrite)

##### Main Components
a. Clients
– User via HTTP (curl, UI, other services)
b. Edge / Ingress
– Nginx reverse proxy (docker-compose)
c. Application Server
– Uvicorn ASGI server running FastAPI (app.main:app)
d. API Layer
– Routers in app/api:
- /ask → services.rag_chain & models.load_response_model
- /rewrite → services.rag_chain & models.load_emotional_model
- /home → health check/static
e. Service Layer
– create_prompt, document_loader (PDF parsing), read_vector_db
– rag_chain orchestrates retrieval + LLM calls
– templates fill in prompts
f. Model Layer
– Response model (LLM)
– Emotion model (LLM)
g. Data Layer
– PDF file storage (data/courses)
– Vector DB (Faiss index + pickle metadata in data/vectorstores)
h. External Services
– Together API (LLM inference)
– Google API (if used for embeddings)

##### Relationships and Data Flow
- Client → Nginx → Uvicorn/FastAPI router
- Router → Service (document loader → vector DB retrieval → chain)
- Chain → External LLM API → process output with templates
- Service → Model layer → construct response
- Response → Router → Client
- Scripts (convert.py, create_vector_db.py) are offline processes updating data/courses & data/vectorstores

##### Architectural Patterns & Principles
- Layered architecture (Ingress → API → Services → Models → Data)
- Separation of concerns: routing vs business logic vs ML model loading vs persistence
- Stateless API containers with persistent volumes for data/vectorstores
- 12-factor friendly (env config, logs to stdout, backing services as attached resources)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    Client["Client (HTTP)"]:::external

    %% Docker Host Boundary
    subgraph "Docker Host" 
        style DockerHost fill:#E8E8E8,stroke:#999
        DockerHost(( )) 

        %% Ingress Layer
        subgraph "Ingress / Reverse Proxy"
            Nginx["Nginx Reverse Proxy"]:::compute
            click Nginx "https://github.com/trongdung143/langchain/blob/main/nginx.conf"
        end

        %% Application Layer
        subgraph "Application Server"
            Uvicorn["Uvicorn ASGI Server"]:::compute
            click Uvicorn "https://github.com/trongdung143/langchain/blob/main/app/main.py"

            %% Configuration & Packaging
            Config["Configuration (app/config.py)"]:::compute
            click Config "https://github.com/trongdung143/langchain/blob/main/app/config.py"
            Setup["Setup (app/setup.py)"]:::compute
            click Setup "https://github.com/trongdung143/langchain/blob/main/app/setup.py"
            Requirements["requirements.txt"]:::compute
            click Requirements "https://github.com/trongdung143/langchain/blob/main/requirements.txt"

            %% API Layer
            subgraph "API Layer (FastAPI Routers)"
                Ask["/ask Endpoint"]:::compute
                click Ask "https://github.com/trongdung143/langchain/blob/main/app/api/ask.py"
                Rewrite["/rewrite Endpoint"]:::compute
                click Rewrite "https://github.com/trongdung143/langchain/blob/main/app/api/rewrite.py"
                Home["/home Endpoint"]:::compute
                click Home "https://github.com/trongdung143/langchain/blob/main/app/api/home.py"
            end

            %% Service Layer
            subgraph "Service Layer"
                Prompt["Prompt Creation"]:::compute
                click Prompt "https://github.com/trongdung143/langchain/blob/main/app/services/create_prompt.py"
                DocLoader["Document Loader"]:::compute
                click DocLoader "https://github.com/trongdung143/langchain/blob/main/app/services/document_loader.py"
                VectorAccess["Vector DB Access"]:::compute
                click VectorAccess "https://github.com/trongdung143/langchain/blob/main/app/services/read_vertor_db.py"
                RAG["RAG Chain Orchestration"]:::compute
                click RAG "https://github.com/trongdung143/langchain/blob/main/app/services/rag_chain.py"
                Templates["Prompt Templates"]:::compute
                click Templates "https://github.com/trongdung143/langchain/blob/main/app/services/templates.py"
            end

            %% Model Layer
            subgraph "Model Layer"
                RespModel["Response Model Loader"]:::compute
                click RespModel "https://github.com/trongdung143/langchain/blob/main/app/models/load_response_model.py"
                EmoModel["Emotional Model Loader"]:::compute
                click EmoModel "https://github.com/trongdung143/langchain/blob/main/app/models/load_emotional_model.py"
            end
        end

        %% Data Layer
        subgraph "Data Layer"
            PDFStore["PDF Storage"]:::persistence
            click PDFStore "https://github.com/trongdung143/langchain/tree/main/data/courses/"
            VStore["Faiss Vectorstores"]:::persistence
            click VStore "https://github.com/trongdung143/langchain/tree/main/data/vectorstores/"
        end

        %% Offline Scripts
        subgraph "Offline Utility Scripts"
            Convert["PDF Conversion"]:::compute
            click Convert "https://github.com/trongdung143/langchain/blob/main/scripts/convert.py"
            CreateDB["Vector DB Creation"]:::compute
            click CreateDB "https://github.com/trongdung143/langchain/blob/main/scripts/create_vector_db.py"
        end

        %% Tests
        subgraph "Tests"
            QA_Test["Q&A Tests"]:::compute
            click QA_Test "https://github.com/trongdung143/langchain/blob/main/tests/test_api_question.py"
            Emotion_Test["Emotion Rewrite Tests"]:::compute
            click Emotion_Test "https://github.com/trongdung143/langchain/blob/main/tests/test_api_emotion.py"
        end

        %% Orchestration Files
        DockerCompose["docker-compose.yml"]:::compute
        click DockerCompose "https://github.com/trongdung143/langchain/blob/main/docker-compose.yml"
        Dockerfile["Dockerfile"]:::compute
        click Dockerfile "https://github.com/trongdung143/langchain/tree/main/Dockerfile"
        NginxConf["nginx.conf"]:::compute
        click NginxConf "https://github.com/trongdung143/langchain/blob/main/nginx.conf"
    end

    %% External Services
    subgraph "External Services"
        TogetherAPI["Together API (LLM Inference)"]:::external
        GoogleAPI["Google Embedding API"]:::external
    end

    %% Connections
    Client --> Nginx
    Nginx --> Uvicorn
    Uvicorn --> Ask
    Uvicorn --> Rewrite
    Uvicorn --> Home

    Ask --> Prompt
    Ask --> DocLoader
    Ask --> VectorAccess
    Ask --> RAG

    Rewrite --> Prompt
    Rewrite --> DocLoader
    Rewrite --> VectorAccess
    Rewrite --> RAG

    RAG --> RespModel
    RAG --> EmoModel
    RAG --> TogetherAPI
    RAG --> GoogleAPI

    DocLoader --> PDFStore
    VectorAccess --> VStore

    %% Offline script flows
    Convert --> PDFStore
    CreateDB --> VStore

    %% Styles
    classDef compute fill:#D6EAF8,stroke:#333
    classDef persistence fill:#D5F5E3,stroke:#333
    classDef external fill:#FDEBD0,stroke:#333
    classDef boundary fill:#E8E8E8,stroke:#999
    class DockerHost boundary
```
