# saiakhil066/deepseek-rag-chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/saiakhil066/deepseek-rag-chatbot
- Repository: https://github.com/saiakhil066/deepseek-rag-chatbot

## Overview

##### Main Components
- User Interface (UI): Represented by app.py which launches a Streamlit web app. This is where users interact with the chatbot.
- Document Processing: The module (doc_handler.py inside the utils folder) handles file uploads (PDF, DOCX, TXT) and preliminary document processing.
- Retrieval Pipeline: The retriever_pipeline.py handles the hybrid retrieval process using BM25 and FAISS. This module is responsible for fetching the most relevant chunks from the processed documents.
- Knowledge Graph / GraphRAG Module: The build_graph.py script builds the Knowledge Graph from the documents; this component underpins the GraphRAG functionality.
- Neural Reranking & Query Expansion: Although not a separate file, the README indicates the use of a cross-encoder model for neural reranking and HyDE for query expansion.
- Chat Memory Integration: An essential feature that maintains conversation history, influencing how queries are processed for contextual understanding.
- External Model Service (Ollama): The system interacts with external LLM services (via the Ollama tool) to generate responses from the DeepSeek-7B model and others. This is integrated either as a host service or containerized via docker-compose.
- Deployment & Containerization: Key files like Dockerfile, docker-compose.yml, and environment configurations (.env) indicate container setups, particularly for two-container arrangements (one for the chatbot service and one for Ollama).

##### Relationships and Interactions
- User initiates interactions via the UI (Streamlit app).
- Uploaded documents are handled and processed by doc_handler, then routed through the retriever_pipeline which applies retrieval algorithms (BM25 + FAISS).
- The retrieved chunks are further processed by the GraphRAG module to build a knowledge graph.
- Neural reranking and query expansion modules (invoked within the backend logic) refine the results.
- Chat memory integration ensures previous interactions influence the current query, maintaining context.
- The final answer generation involves communication with the external LLM service (Ollama) for deep model inference.
- In a Docker-based setup, the chatbot service and Ollama run in separate containers and are connected via network links (as shown in the docker-compose file).

##### Architectural Patterns and Design Principles
- Modular Design: The separation of concerns is clear (UI layer, document processing, retrieval, graph building, integration with external LLM), which aligns with microservices or modular monolith patterns.
- Containerization: Use of Docker and docker-compose enforces clearly separated runtime environments.
- Hybrid Retrieval and Neural Reranking: Incorporates multiple advanced techniques for information retrieval and response generation.
- Extensibility: With features like selecting different RAG pipelines and models, the architecture is built to naturally extend to new methods or data sources.

## Diagram (Mermaid)

```mermaid
flowchart TD
    U("User"):::external
    UI("Streamlit UI (app.py)"):::frontend
    DP("Document Processing (doc_handler.py)"):::backend
    RP("Retrieval Pipeline (retriever_pipeline.py)"):::backend
    B25("BM25 Retrieval"):::backend
    FAISS("FAISS Retrieval"):::backend
    KG("Knowledge Graph Module (build_graph.py) - GraphRAG"):::backend
    NR("Neural Reranking&QueryExpansion"):::backend
    CM("Chat Memory Integration"):::backend
    OLL("Ollama LLM Service"):::external

    U -->|"initiates"| UI
    UI -->|"uploads"| DP
    DP -->|"processes"| RP
    RP -->|"applies"| B25
    RP -->|"applies"| FAISS
    B25 -->|"retrieved"| KG
    FAISS -->|"retrieved"| KG
    KG -->|"builds"| NR
    NR -->|"refines"| CM
    CM -->|"inferences"| OLL
    OLL -->|"response"| UI

    subgraph "Deployment & Containerization"
        direction TB
        CHAT("Chatbot Service"):::infra
        OLLC("Ollama Service"):::infra
        DF("Dockerfile"):::infra
        DC("docker-compose.yml"):::infra
        ENV(".env"):::infra
    end

    CHAT --> UI
    OLLC --> OLL

    click UI "https://github.com/saiakhil066/deepseek-rag-chatbot/blob/main/app.py"
    click DP "https://github.com/saiakhil066/deepseek-rag-chatbot/blob/main/utils/doc_handler.py"
    click RP "https://github.com/saiakhil066/deepseek-rag-chatbot/blob/main/utils/retriever_pipeline.py"
    click KG "https://github.com/saiakhil066/deepseek-rag-chatbot/blob/main/utils/build_graph.py"
    click DF "https://github.com/saiakhil066/deepseek-rag-chatbot/tree/main/Dockerfile"
    click DC "https://github.com/saiakhil066/deepseek-rag-chatbot/blob/main/docker-compose.yml"
    click ENV "https://github.com/saiakhil066/deepseek-rag-chatbot/blob/main/.env"

    classDef frontend fill:#f9e79f,stroke:#d35400,stroke-width:2px;
    classDef backend fill:#aed6f1,stroke:#2471a3,stroke-width:2px;
    classDef external fill:#abebc6,stroke:#27ae60,stroke-width:2px;
    classDef infra fill:#f5b7b1,stroke:#c0392b,stroke-width:2px;
```
