How hardikpatelse/aichatbot works
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)