# yuchenxia/llmriskanalyzer architecture

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

- Interactive diagram: https://gitdiagram.com/yuchenxia/llmriskanalyzer
- Repository: https://github.com/yuchenxia/llmriskanalyzer

## Overview

##### Project type and purpose
- This is a full-stack web application (“FMEA Assisted by LLM System”) that automates Failure Mode and Effects Analysis by combining a Python/Flask backend with a browser-based frontend.
- The README clarifies its goal: ingest FMEA datasets (CSV), invoke an LLM to generate failure-mode analyses, and display or export results via a simple web UI.

##### File structure
- Top-level “source_code” directory contains:
- app.py: Flask application entry point (routes, request handling)
- llm.py & agents.py: wrappers and orchestration logic for calling the LLM API
- FEMA_data_model.py & data_model_utilities.py: domain model definitions and helper functions for parsing/serializing FMEA data
- dataset/: raw CSV inputs
- saved_fmea_data.json, output.csv, restored_output.csv, fmea_download.csv: intermediate or final data artifacts
- generation_agent.json: LLM prompt configuration
- static/: client-side JavaScript (UI logic) and CSS
- templates/: Jinja2 HTML templates for the frontend
- requirements.txt lists Python dependencies (Flask, OpenAI client, pandas, etc.)

##### README for additional insights
- Describes two demo use cases (general FMEA and brake-by-wire)

##### Main components and interactions
a. Frontend (Browser)
- Static assets (HTML, CSS, JS) served by Flask
- User uploads/selects an FMEA CSV, triggers analysis via AJAX/fetch
b. Backend (Flask app)
- HTTP endpoints (e.g., “/”, “/analyze”, “/download”) implemented in app.py
- Parses incoming CSV using data_model_utilities, builds domain objects via FEMA_data_model
- Routes requests to agents.py which uses llm.py to call an external LLM endpoint
- Collects LLM responses, post-processes them, and writes results to output.csv / JSON
c. Data storage
- Local file system for datasets and outputs (CSV, JSON)
- Optionally swap to a database if volume grows
d. External services
- OpenAI (or similar) LLM REST API for generation
e. User download
- Processed results offered as CSV/JSON for download

##### Key architectural patterns / design principles
- MVC-style separation:
- Model layer: FEMA_data_model & data_model_utilities
- Controller layer: app.py routes
- View layer: Jinja2 templates + static JS
- Agent pattern: agents.py encapsulates the strategy of prompt prep and response handling
- Single process micro-service: monolithic Flask app, straightforward orchestration
- File-based persistence: simple storage without a full RDBMS

## Diagram (Mermaid)

```mermaid
graph TD
    %% User
    User("User"):::external

    %% Frontend Layer
    subgraph "Browser (Frontend)"
        Templates["HTML Templates"]:::frontend
        JSLogic["JavaScript Logic"]:::frontend
        CSSStyles["CSS Styles"]:::frontend
    end

    %% Backend Layer
    subgraph "Flask Backend"
        Routes["HTTP Routes & Controllers (app.py)"]:::backend
        DepManifest["Dependencies Manifest (requirements.txt)"]:::backend
        subgraph "FMEA Domain"
            Domain["FMEA Data Definitions"]:::backend
            Utils["Data Utilities"]:::backend
        end
        subgraph "LLM Agent"
            Agent["Agent Orchestrator"]:::backend
            LLMComp["LLM API Wrapper"]:::backend
            Config["LLM Prompt Config"]:::backend
        end
    end

    %% Storage
    Dataset("Raw CSV Inputs"):::storage
    Outputs("Analysis Outputs & Intermediates"):::storage

    %% External Service
    LLMService(("External LLM Service\n(OpenAI API)")):::external

    %% Flows
    User -->|"views UI"| Templates
    Templates -->|"serves HTML"| JSLogic
    JSLogic -->|"CSV upload (HTTP POST)"| Routes
    Routes -->|"parse CSV"| Utils
    Utils -->|"create domain objects"| Domain
    Domain -->|"provide to agent"| Agent
    Agent -->|"prepare prompt"| LLMComp
    Config -->|"prompt templates"| LLMComp
    LLMComp -->|"LLM prompt"| LLMService
    LLMService -->|"LLM response JSON"| LLMComp
    LLMComp -->|"return analysis"| Agent
    Agent -->|"post-process JSON"| Utils
    Utils -->|"write CSV/JSON"| Outputs
    Routes -->|"HTTP response (JSON)"| JSLogic
    JSLogic -->|"render results"| Templates
    JSLogic -->|"download request"| Routes
    Routes -->|"read files"| Outputs
    Routes -->|"serve file"| JSLogic
    JSLogic -->|"file download"| User

    %% Storage initial load
    Routes -->|"load raw CSV"| Dataset

    %% Click Events
    click Templates "https://github.com/yuchenxia/llmriskanalyzer/blob/main/source_code/templates/index.html"
    click JSLogic "https://github.com/yuchenxia/llmriskanalyzer/blob/main/source_code/static/script.js"
    click CSSStyles "https://github.com/yuchenxia/llmriskanalyzer/blob/main/source_code/static/styles.css"
    click Routes "https://github.com/yuchenxia/llmriskanalyzer/blob/main/source_code/app.py"
    click Domain "https://github.com/yuchenxia/llmriskanalyzer/blob/main/source_code/FEMA_data_model.py"
    click Utils "https://github.com/yuchenxia/llmriskanalyzer/blob/main/source_code/data_model_utilities.py"
    click Agent "https://github.com/yuchenxia/llmriskanalyzer/blob/main/source_code/agents.py"
    click LLMComp "https://github.com/yuchenxia/llmriskanalyzer/blob/main/source_code/llm.py"
    click Config "https://github.com/yuchenxia/llmriskanalyzer/blob/main/source_code/generation_agent.json"
    click Dataset "https://github.com/yuchenxia/llmriskanalyzer/tree/main/source_code/dataset/"
    click DepManifest "https://github.com/yuchenxia/llmriskanalyzer/blob/main/source_code/requirements.txt"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef backend fill:#98FB98,stroke:#333,stroke-width:1px
    classDef external fill:#FFA500,stroke:#333,stroke-width:1px
    classDef storage fill:#D3D3D3,stroke:#333,stroke-width:1px
```
