# smil7/ai-teaching-assistant architecture

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

- Interactive diagram: https://gitdiagram.com/smil7/ai-teaching-assistant
- Repository: https://github.com/smil7/ai-teaching-assistant

## Overview

##### Project Type and Purpose
- This is a full-stack application with two main parts: a backend that implements a Retrieval-Augmented Generation (RAG) pipeline to serve context-aware AI responses, and a frontend web interface (built with React) for interactive user access.
- The application is designed to assist students by answering course-related questions. It ingests course documents (PDFs, CSVs, etc.) into a vector database (Chroma) and uses lightweight embedding models (all-MiniLM-L6-v2) along with a quantized Llama-2-7b-Chat-GGUF model to generate answers grounded in course material.

##### Analyzing the File Structure
- There are two main directories:
a. "Backend-RAG" – Contains backend modules, scripts, and utility functions. Key files include:
- ingest.py – For processing and ingesting course material into Chroma.
- load_models.py, prompt_template_utils.py, crawl.py, constants.py – Supporting modules for model loading and query handling.
- run_localGPT.py, run_localGPT_API.py, localGPT_UI.py – Entry points for running the system as a command-line tool, API service, or local UI.
- Dockerfile and related configuration files – For containerizing the application.
- SOURCE_DOCUMENTS – Where raw course materials and PDF assignments are stored.
b. "ai-teaching-assistant" – Contains the frontend code (React project) as well as a backend folder mirroring some functionalities (likely to support the API endpoints and serve the front-end).
- In the src/backend folder, you have server.py (likely the API server linking frontend to backend services), ingestion-related modules, and utility scripts.
- In the src/frontend folder, you have components (ChatbotInterface, WelcomePage, FormPage) and styles for rendering the UI.

##### Main Components and Their Roles
a. Ingestion Module:
- Responsible for processing course documents by splitting them into chunks, embedding these using the all-MiniLM-L6-v2 model, and storing them in the Chroma vector database.
- Files: ingest.py, and similar ingestion code under ai-teaching-assistant/src/backend/ingest_class.py.
b. Retrieval Component:
- Processes user queries by converting them into embeddings and retrieving the most relevant document chunks from the vector database.
- Files: Prompt/template utilities, qa_retrieval_class.py.
c. Generation Component:
- Uses the Llama-2-7b-Chat-GGUF model to generate context-aware responses based on the retrieved document chunks.
- Files: load_models.py, run_localGPT.py, run_localGPT_API.py.
d. API / Command-line Interface:
- Provides endpoints or a terminal-based interface for client interaction.
- File: server.py (in the frontend's backend submodule) and localGPT_UI.py for local interactive sessions.
e. Frontend UI:
- A React-based web application serving as the interactive interface for end-users.
- Files: Located under ai-teaching-assistant/src/frontend, including ChatbotInterface.js, FormPage.js, and WelcomePage.js.
f. Storage and Data:
- Course documents in the "SOURCE_DOCUMENTS" folder (both in the Backend-RAG and the ai-teaching-assistant/src/backend folders).
- A Chroma vector database (conceptually represented) acting as the intermediary data store for embeddings.

##### Relationships and Data Flow
- User initiates a query via the frontend UI.
- The query is sent to the backend server (server.py or one of the run_*.py entry points) through an API call.
- The query is embedded and used to query the Chroma vector database, which holds document embeddings generated from the course materials (using ingest.py).
- The retrieved relevant document chunks are then fed into the generation module (Llama-2-7b-Chat-GGUF) to produce the final response.
- The generated answer is sent back to the frontend and displayed in the chatbot interface.
- Additionally, there is a data ingestion pipeline that is typically run as a preprocessing step to prepare the Chroma DB from SOURCE_DOCUMENTS.

##### Architectural Patterns and Technologies
- MVC/MVVM pattern: The separation of concerns between the frontend (view and controller layers) and the backend (model and business logic).
- Microservice-like separation: Although contained within one repository, there is a clear separation between backend processing (ingest, retrieval, generation) and frontend UI.
- Use of containerization: Dockerfiles are present to build standardized runtime environments.
- Key Libraries & Frameworks:
- langchain for chaining generative processes.
- transformers and llama-cpp-python for model inference.
- pytorch for model execution.
- chromadb as the vector database.
- React for the frontend UI.
- Others: click, nltk, sentence-transformers, etc.

## Diagram (Mermaid)

```mermaid
graph TD
  %% Data Ingestion Pipeline
  subgraph "Data Ingestion Pipeline"
    Docs["Course Documents (SOURCE_DOCUMENTS)"]:::doc
    Ingest_Backend["Ingestion Module (Backend)"]:::backend
    Ingest_Frontend["Ingestion Module (Frontend)"]:::backend
    Ingest_Merge["Combined Ingestion Output"]:::backend
    Chroma["Chroma Vector Database"]:::db

    Docs --> Ingest_Backend
    Docs --> Ingest_Frontend
    Ingest_Backend --> Ingest_Merge
    Ingest_Frontend --> Ingest_Merge
    Ingest_Merge --> Chroma
  end

  %% Query and Generation Pipeline
  subgraph "Query & Generation Pipeline"
    subgraph "React Frontend"
      Chatbot_Interface["Chatbot Interface"]:::frontend
      Form_Page["FormPage"]:::frontend
      Welcome_Page["WelcomePage"]:::frontend
      %% Relationships within React UI (auxiliary components)
      Chatbot_Interface --> Form_Page
      Chatbot_Interface --> Welcome_Page
    end
    %% API and Retrieval
    API_Server["API Server"]:::backend
    Chatbot_Interface --> API_Server

    subgraph "Retrieval Process" [ ]
      Ret_Backend["Retrieval Component (Backend)"]:::backend
      Ret_Frontend["Retrieval Component (Frontend)"]:::backend
      Ret_Merge["Query Embedding & Retrieval"]:::backend
      API_Server --> Ret_Backend
      API_Server --> Ret_Frontend
      Ret_Backend --> Ret_Merge
      Ret_Frontend --> Ret_Merge
      %% Retrieval queries the vector DB
      Chroma --- Ret_Merge
    end

    %% Generation Process
    subgraph "Generation Process" [ ]
      Model_Backend["Model Loader (Backend)"]:::backend
      Model_Frontend["Model Loader (Frontend)"]:::backend
      Generation_API["Generation API (Backend)"]:::backend
      Ret_Merge --> Model_Backend
      Ret_Merge --> Model_Frontend
      Model_Backend --> Generation_API
      Model_Frontend --> Generation_API
      %% Also allow API server to trigger generation directly
      API_Server --> Generation_API
      Generation_API --> Chatbot_Interface
    end

    %% Local CLI/UI Branch
    CLI["Local CLI/UI"]:::backend
    API_Server --> CLI
    CLI --> Chatbot_Interface
  end

  %% Click Events
  click Docs "https://github.com/smil7/ai-teaching-assistant/tree/main/Backend-RAG/SOURCE_DOCUMENTS"
  click Ingest_Backend "https://github.com/smil7/ai-teaching-assistant/blob/main/Backend-RAG/ingest.py"
  click Ingest_Frontend "https://github.com/smil7/ai-teaching-assistant/blob/main/ai-teaching-assistant/src/backend/ingest_class.py"
  click Ret_Backend "https://github.com/smil7/ai-teaching-assistant/blob/main/Backend-RAG/prompt_template_utils.py"
  click Ret_Frontend "https://github.com/smil7/ai-teaching-assistant/blob/main/ai-teaching-assistant/src/backend/qa_retrieval_class.py"
  click Model_Backend "https://github.com/smil7/ai-teaching-assistant/blob/main/Backend-RAG/load_models.py"
  click Model_Frontend "https://github.com/smil7/ai-teaching-assistant/blob/main/ai-teaching-assistant/src/backend/load_models.py"
  click Generation_API "https://github.com/smil7/ai-teaching-assistant/blob/main/Backend-RAG/run_localGPT.py"
  click API_Server "https://github.com/smil7/ai-teaching-assistant/blob/main/ai-teaching-assistant/src/backend/server.py"
  click CLI "https://github.com/smil7/ai-teaching-assistant/blob/main/Backend-RAG/localGPT_UI.py"
  click Chatbot_Interface "https://github.com/smil7/ai-teaching-assistant/blob/main/ai-teaching-assistant/src/frontend/components/ChatbotInterface.js"
  click Form_Page "https://github.com/smil7/ai-teaching-assistant/blob/main/ai-teaching-assistant/src/frontend/components/FormPage.js"
  click Welcome_Page "https://github.com/smil7/ai-teaching-assistant/blob/main/ai-teaching-assistant/src/frontend/components/WelcomePage.js"

  %% Styles
  classDef backend fill:#ADD8E6,stroke:#000,stroke-width:1px;
  classDef frontend fill:#FFB6C1,stroke:#000,stroke-width:1px;
  classDef db fill:#90EE90,stroke:#000,stroke-width:1px,stroke-dasharray: 5 5;
  classDef doc fill:#D3D3D3,stroke:#000,stroke-width:1px;
```
