# abhinav281203/llama2-rag architecture

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

- Interactive diagram: https://gitdiagram.com/abhinav281203/llama2-rag
- Repository: https://github.com/abhinav281203/llama2-rag

## Overview

##### Project type and purpose
- This is a small, self-contained “Retrieval-Augmented Generation” (RAG) application built in Python.
- It provides a Streamlit-based UI that lets users query a Llama-2 model enriched by PDF documents via LangChain and LlamaIndex.
- The goal is to run locally (offline/private) and answer questions from your own PDFs.

##### File structure
- Top level:
- Llama2 RAG.py   → the main Streamlit app
- Llama2 RAG.ipynb → exploratory/notebook version
- requirements.txt → Python dependencies (langchain, llama-index, streamlit, transformers, etc.)
- documents/       → folder with source PDFs (e.g., Guntur_Kaaram.pdf)
- README.md       → project description & run instructions
- No separate “frontend” vs. “backend” folders; the Streamlit script handles both UI and orchestration.
- The documents folder is your local knowledge base.

##### README for additional insights
- Confirms the use of Llama-2-7b-chat-hf as the core LLM.
- Mentions typcial RAG pipeline: ingest PDFs, build index, handle user queries, retrieve relevant chunks, and call the LLM.
- Run command: `streamlit run "Llama2 RAG.py"`.

##### Main components
a. User Interface (Streamlit Frontend)
b. RAG Orchestrator (the Python process in Llama2 RAG.py)
c. Document Loader & Index Builder (LlamaIndex component)
d. Vector Store / Retrieval Engine (e.g., FAISS or in-memory index)
e. Language Model API (Llama-2-7b-chat-hf via HuggingFace Transformers)
f. Local File System (documents/*.pdf)

##### Relationships and data flows
1. User enters a query in Streamlit UI →
2. RAG Orchestrator checks the Vector Store →
– If index not built or changed, the Document Loader reads PDFs, chunks them, and populates the Vector Store.
3. RAG Orchestrator sends query embeddings to Retrieval Engine → obtains top-k relevant document chunks →
4. RAG Orchestrator assembles prompt (query + retrieved context) → calls Llama-2 model →
5. Response flows back to Streamlit UI → displayed to user.

##### Architectural patterns and design principles
- Modular RAG pipeline: index build, retrieval, prompt construction, LLM inference.
- Single-process, event-driven: Streamlit reruns on each user action.
- Separation of concerns in code: document ingestion vs. retrieval vs. UI rendering.

##### Key technologies & frameworks
- Streamlit for UI
- langchain & llama-index for RAG abstractions
- HuggingFace Transformers for Llama-2 model
- Local vector store (e.g., FAISS or in-memory)
- Python 3.x (specified in requirements.txt)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interaction
    User["User"] 
    UI["Streamlit UI"]:::ui
    User -->|"submits query"| UI

    %% Backend / Orchestration
    subgraph "Backend / RAG Orchestrator"
        Loader["Document Loader & Index Builder"]:::backend
        Retriever["Retrieval Engine Controller"]:::backend
        PromptBuilder["Prompt Builder"]:::backend
        ModelClient["LLM API Client"]:::backend
    end

    %% Data Services
    subgraph "Data Layer"
        DocFS["Local File System (PDFs)"]:::data
        IndexStore["Vector Store (FAISS/in-memory)"]:::data
        PDF["Guntur_Kaaram.pdf"]:::data
    end

    %% External LLM Service
    LLM["Llama-2-7b-chat-hf (HF Transformers)"]:::external

    %% Project Files
    README["README.md"]:::data
    REQS["requirements.txt"]:::data
    NOTEBOOK["Llama2 RAG.ipynb"]:::data

    %% Flows
    UI -->|"trigger orchestration"| Loader
    UI -->|"query request"| Retriever

    DocFS -->|contains| PDF
    DocFS -->|read PDFs| Loader
    Loader -->|build index| IndexStore

    Retriever -->|check index| IndexStore
    Retriever -->|retrieve top-k chunks| PromptBuilder
    PromptBuilder -->|"assembled prompt"| ModelClient
    ModelClient -->|"API call"| LLM
    LLM -->|"response"| ModelClient
    ModelClient -->|"answer"| UI

    %% Click Events
    click UI "https://github.com/abhinav281203/llama2-rag/blob/main/Llama2 RAG.py"
    click Loader "https://github.com/abhinav281203/llama2-rag/blob/main/Llama2 RAG.py"
    click Retriever "https://github.com/abhinav281203/llama2-rag/blob/main/Llama2 RAG.py"
    click PromptBuilder "https://github.com/abhinav281203/llama2-rag/blob/main/Llama2 RAG.py"
    click ModelClient "https://github.com/abhinav281203/llama2-rag/blob/main/Llama2 RAG.py"
    click LLM "https://github.com/abhinav281203/llama2-rag/blob/main/Llama2 RAG.py"
    click DocFS "https://github.com/abhinav281203/llama2-rag/tree/main/documents/"
    click PDF "https://github.com/abhinav281203/llama2-rag/blob/main/documents/Guntur_Kaaram.pdf"
    click README "https://github.com/abhinav281203/llama2-rag/blob/main/README.md"
    click REQS "https://github.com/abhinav281203/llama2-rag/blob/main/requirements.txt"
    click NOTEBOOK "https://github.com/abhinav281203/llama2-rag/blob/main/Llama2 RAG.ipynb"

    %% Styles
    classDef ui fill:#D0E1F2,stroke:#333,stroke-width:1px;
    classDef backend fill:#F2D0E1,stroke:#333,stroke-width:1px;
    classDef data fill:#D0F2D0,stroke:#333,stroke-width:1px;
    classDef external fill:#F2E1D0,stroke:#333,stroke-width:1px;
```
