# hardikpatelse/aichatbot architecture

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

- Interactive diagram: https://gitdiagram.com/hardikpatelse/aichatbot
- Repository: https://github.com/hardikpatelse/aichatbot

## Overview

##### Project Type and Purpose
- AIChatBot is a full-stack AI chatbot application consisting of:
- A .NET 8 Web API backend (AIChatBot.API)
- An Angular 20 frontend (AIChatBot.UI)
- Integration with local LLMs (via Ollama) and cloud LLMs (via OpenRouter)
- A Retrieval-Augmented Generation (RAG) pipeline for knowledge-based responses
- Real-time chat via SignalR

##### High-Level File Structure Analysis
- Top-level directories:
- AIChatBot.API (backend)
- AIChatBot.UI (frontend)
- Backend substructure:
- Controllers → HTTP endpoints (ChatController, DocumentsController, etc.)
- Hubs → SignalR ChatHub for real-time messaging
- Services → Business logic (ChatService, RagChatService, AgentService, InMemoryRagStore, MailService, etc.)
- DataContext + Interfaces/DataContext → Abstractions & implementations for persisting Users, Sessions, History, AgentFiles
- Models → Request/Response DTOs, EF Entities, Tool definitions
- Factory → ChatModelServiceFactory selects OllamaChatService or OpenRouterChatService
- Migrations → EF Core schema for persistent storage
- Configuration → appsettings.json, Dockerfile, fly.toml
- Frontend substructure:
- src/app/components → UI components (chat, chat-history, document-upload for RAG, model-selector, etc.)
- src/app/services → Angular services for HTTP, SignalR
- src/app/entities → TypeScript interfaces modeling backend entities
- Routing & assets → app.routes.ts, images, styling

##### README Insights
- Supports two AI model providers:
- Local LLMs served by Ollama
- Online LLMs via OpenRouter API
- RAG pipeline steps: upload → text extraction → chunking → in-memory indexing → semantic search → context-aware prompt assembly
- Multi-mode operation: Chat-Only, AI+Tools (function calling), Knowledge-Based (RAG), Agent Mode
- Database with EF Core for users, sessions, history, models, tools, agent files

##### Key Components
- Frontend (Angular UI)
- HTTP REST client
- SignalR client
- UI components: ChatView, SessionList, ModelSelector, DocumentUpload
- Backend (.NET 8 API)
- Controllers (REST API)
- SignalR Hub (ChatHub)
- Services layer
– ChatService (standard chat)
– RagChatService + InMemoryRagStore (RAG pipeline)
– AgentService + ToolsRegistryService (function-calling)
– ModelService via ChatModelServiceFactory
- Data storage
– EF Core (SQL database): Users, Sessions, Messages, Models, ChatModes, AgentFiles
– In-memory JSON seed files for initial data (models.json, chatHistory.json)
- External LLM Providers
– Ollama (local HTTP endpoint)
– OpenRouter (cloud REST API, secured by API key)
- Database (SQL via EF Core)
- InMemory RAG Store (in-memory index for document chunks)
- External Services
- Ollama daemon
- OpenRouter.ai

##### Relationships & Data Flow
1. User interacts with Angular UI → HTTP POST/GET to Backend Controllers
2. For real-time chat: UI ↔ SignalR Hub ↔ Services
3. ChatService / RagChatService calls ChatModelServiceFactory
- Factory selects OllamaChatService or OpenRouterChatService
- Sends prompt + context → external LLM service → returns AI response
4. RagChatService also queries InMemoryRagStore for top-K chunks before calling LLM
5. Responses routed back to UI via HTTP or SignalR
6. Controllers/Services persist sessions/messages to SQL via EF Core DataContext

##### Architectural Patterns & Principles
- Layered architecture (Controllers → Services → DataContext → Database)
- Dependency Injection (Interfaces for services, data contexts)
- Factory pattern (ChatModelServiceFactory)
- Real-time messaging (SignalR Hub)
- Modular UI architecture (Angular feature modules per component)
- RAG pipeline as a separate service using in-memory indexing
- Environment-based configuration (appsettings.json, environment.ts)

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend (Angular 20)"
        direction TB
        FE["AIChatBot.UI\nAngular 20, TypeScript"]:::frontend
        RESTClient["HTTP REST Client"]:::frontend
        SIGNALRClient["SignalR Client"]:::frontend
        ChatView["Chat View"]:::frontend
        ChatHistory["Chat History List"]:::frontend
        SessionList["Chat Session List"]:::frontend
        ModelSelector["Model Selector"]:::frontend
        DocumentUpload["Document Upload (RAG)"]:::frontend
        NewSession["New Chat Session Form"]:::frontend
        Entities["Angular Entities"]:::frontend
        Routing["Routing Config"]:::frontend

        FE --> RESTClient
        FE --> SIGNALRClient
        FE --> ChatView
        FE --> ChatHistory
        FE --> SessionList
        FE --> ModelSelector
        FE --> DocumentUpload
        FE --> NewSession
        FE --> Entities
        FE --> Routing
    end

    subgraph "Backend (.NET 8 API)"
        direction TB
        BE["AIChatBot.API\n.NET 8, C#, EF Core, SignalR"]:::backend
        Controllers["Controllers\n(Chat, Documents, Sessions, Models, Tools, Users, Files)"]:::backend
        Hub["SignalR Hub\nChatHub"]:::backend
        Services["Services\nChatService, RagChatService, AgentService, ModelService, ToolsRegistry, MailService, FileService"]:::backend
        Factory["ChatModelServiceFactory"]:::backend
        AIProviders["AI Model Providers\nOllamaChatService, OpenRouterChatService"]:::external
        DataContext["DbContext & DataContexts"]:::backend
        Interfaces["Interfaces\nDataContext & Services"]:::backend
        Models["Domain Models\nDTOs, Entities, Tools"]:::backend
        Migrations["EF Core Migrations"]:::backend
        Config["Config & Deployment\nappsettings, Dockerfile, fly.toml"]:::backend

        BE --> Controllers
        BE --> Hub
        BE --> Services
        BE --> Factory
        Factory --> AIProviders
        Services --> DataContext
        Services --> Interfaces
        Services --> Models
        DataContext --> Migrations
        BE --> Config
    end

    subgraph "Persistence Layer"
        direction TB
        SQLDB["SQL Database\nEF Core"]:::db
        InMemoryRAG["InMemory RAG Store"]:::rag

        DataContext --> SQLDB
        Services --> InMemoryRAG
    end

    subgraph "External LLM Providers"
        direction TB
        Ollama["Ollama (local HTTP)"]:::external
        OpenRouter["OpenRouter API"]:::external

        AIProviders --> Ollama
        AIProviders --> OpenRouter
    end

    ChatView -->|HTTP| Controllers
    SessionList -->|HTTP| Controllers
    DocumentUpload -->|HTTP| Controllers
    RESTClient -->|REST| Controllers
    SIGNALRClient -->|WebSocket| Hub
    Hub -->|WebSocket| SIGNALRClient
    Controllers -->|Invoke| Services
    Hub -->|Invoke| Services
    Services -->|SQL CRUD| SQLDB
    Services -->|In-memory API| InMemoryRAG
    RagChatServiceCall((RagChatService)) -->|retrieve chunks| InMemoryRAG
    RagChatServiceCall -->|call LLM| AIProviders
    ChatServiceCall((ChatService)) -->|call LLM| AIProviders
    AIProviders -->|response| Services
    Controllers -->|HTTP Response| FE
    Hub -->|WebSocket Message| FE

    %% Click Events
    click FE "https://github.com/hardikpatelse/aichatbot/tree/master/AIChatBot.UI/src/app"
    click RESTClient "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.UI/src/app/services/chat.service.ts"
    click SIGNALRClient "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.UI/src/app/services/signalr.service.ts"
    click ChatView "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.UI/src/app/components/chat/chat.component.ts"
    click ChatHistory "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.UI/src/app/components/chat-history/chat-history.component.ts"
    click SessionList "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.UI/src/app/components/chat-session-list/chat-session-list.component.ts"
    click ModelSelector "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.UI/src/app/components/model-selector/model-selector.component.ts"
    click DocumentUpload "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.UI/src/app/components/document-upload/document-upload.component.ts"
    click NewSession "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.UI/src/app/components/new-chat-session/new-chat-session.component.ts"
    click Entities "https://github.com/hardikpatelse/aichatbot/tree/master/AIChatBot.UI/src/app/entities"
    click Routing "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.UI/src/app/app.routes.ts"
    click BE "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.API"
    click Controllers "https://github.com/hardikpatelse/aichatbot/tree/master/AIChatBot.API/Controllers"
    click Hub "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.API/Hubs/ChatHub.cs"
    click Services "https://github.com/hardikpatelse/aichatbot/tree/master/AIChatBot.API/Services"
    click Factory "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.API/Factory/ChatModelServiceFactory.cs"
    click AIProviders "https://github.com/hardikpatelse/aichatbot/tree/master/AIChatBot.API/AIServices"
    click DataContext "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.API/Data/ChatBotDbContext.cs"
    click Interfaces "https://github.com/hardikpatelse/aichatbot/tree/master/AIChatBot.API/Interfaces"
    click Models "https://github.com/hardikpatelse/aichatbot/tree/master/AIChatBot.API/Models"
    click Migrations "https://github.com/hardikpatelse/aichatbot/tree/master/AIChatBot.API/Migrations"
    click Config "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.API/appsettings.json"
    click SQLDB "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.API/Data/ChatBotDbContext.cs"
    click InMemoryRAG "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.API/Services/InMemoryRagStore.cs"
    click Ollama "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.API/AIServices/OllamaChatService.cs"
    click OpenRouter "https://github.com/hardikpatelse/aichatbot/blob/master/AIChatBot.API/AIServices/OpenRouterChatService.cs"

    classDef frontend fill:#e0ffe0,stroke:#333,stroke-width:1px
    classDef backend fill:#e0f0ff,stroke:#333,stroke-width:1px
    classDef db fill:#ffe0e0,stroke:#333,stroke-width:1px
    classDef external fill:#f0f0f0,stroke:#333,stroke-width:1px
    classDef rag fill:#e0e0ff,stroke:#333,stroke-width:1px
    classDef agent fill:#fff0e0,stroke:#333,stroke-width:1px
```
