# lalanikarim/ai-chatbot-rag architecture

> Architecture diagram of the GitHub repository lalanikarim/ai-chatbot-rag, 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/lalanikarim/ai-chatbot-rag
- Repository: https://github.com/lalanikarim/ai-chatbot-rag

## Overview

##### Main components
- User Interface (UI): Represented by the Streamlit frontend. This is the entry point through which users interact with the chatbot.
- Web Scraper: A component that handles fetching and parsing website content and PDFs using tools like BeautifulSoup and PyMuPDF.
- Data Processing & Embedding Module: The part that processes the scraped text. It leverages Sentence-Transformers and possibly DocArray for converting text into embeddings that can be used for retrieval.
- Conversational Memory Handler: This component maintains session context and follow-up conversation state.
- Retrieval Module (RAG pipeline): This integrates Langchain with the Llama model (via llama-cpp-python and the Mistral model). The retrieval layer fetches the relevant pieces of text based on the embeddings and user queries.
- Model Inference Engine: The backend process that handles natural language generation using the downloaded Mistral model.

##### Relationships and interactions
- The UI (Streamlit) receives user input and sends queries to the backend engine.
- The Web Scraper feeds content into the Data Processing module to create a knowledge base (embeddings).
- The Embedding Module prepares the content for retrieval and stores it in the appropriate format (possibly within DocArray structures).
- When a user query is received, the Retrieval Module matches it against the pre-processed knowledge base. Results are then passed on to the Model Inference Engine.
- The Model Inference Engine (utilizing Llama-cpp-python with Mistral) processes the query along with context (via conversational memory) to generate responses.
- The conversational memory handler ensures context flow across session turns.
- Finally, the response is returned back to the UI for display.

##### Architectural patterns & design principles
- Modular design: Though your codebase is small, separations in concerns (scraping, processing, retrieval, model inference, and presentation) are conceptually distinct.
- Data flow pattern: Clearly illustrate the pipeline’s sequential stages—from content ingestion and embedding to retrieval augmented generation.

##### Relevant technologies and libraries
- Streamlit (for the UI layer).
- BeautifulSoup and PyMuPDF (for web scraping and PDF processing).
- Sentence-Transformers and DocArray (for creating and managing embeddings).
- Langchain (to orchestrate the retrieval augmented generation pipeline).
- llama-cpp-python with the Mistral model (for model inference and natural language generation).
- Python runtime and dependencies managed via requirements.txt.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Data Ingestion"
        B["Web Scraper<br>(BeautifulSoup/PyMuPDF)"]:::backend
        C["Data Processing & Embedding Module<br>(Sentence-Transformers/DocArray)"]:::backend
        B -->|"feeds"| C
    end

    subgraph "Query Processing"
        A["Streamlit UI"]:::frontend
        D["Retrieval Module (RAG pipeline)<br>(Langchain)"]:::backend
        E["Model Inference Engine<br>(llama-cpp-python/Mistral)"]:::backend
        F["Conversational Memory Handler"]:::backend
        A -->|"sends query"| D
        C -->|"provides knowledge"| D
        D -->|"invokes"| E
        E -->|"updates context"| F
        F -->|"returns response"| A
    end

    G["Project Dependencies"]:::dependency

    %% Dependencies support all modules
    G -.-> A
    G -.-> B
    G -.-> C
    G -.-> D
    G -.-> E
    G -.-> F

    click A "https://github.com/lalanikarim/ai-chatbot-rag/blob/main/main.py"
    click B "https://github.com/lalanikarim/ai-chatbot-rag/blob/main/main.py"
    click C "https://github.com/lalanikarim/ai-chatbot-rag/blob/main/main.py"
    click D "https://github.com/lalanikarim/ai-chatbot-rag/blob/main/main.py"
    click E "https://github.com/lalanikarim/ai-chatbot-rag/blob/main/main.py"
    click F "https://github.com/lalanikarim/ai-chatbot-rag/blob/main/main.py"
    click G "https://github.com/lalanikarim/ai-chatbot-rag/blob/main/requirements.txt"

    classDef frontend fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef backend fill:#A3E4D7,stroke:#1D8348,stroke-width:2px;
    classDef dependency fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;
```
