# srimathib01/querynest--multidoc-chatbot- architecture

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

- Interactive diagram: https://gitdiagram.com/srimathib01/querynest--multidoc-chatbot-
- Repository: https://github.com/srimathib01/querynest--multidoc-chatbot-

## Overview

##### Project type and purpose
- This is a lightweight, single-page, full-stack “chatbot” application built with Streamlit for the UI and Python (LangChain) for the backend logic.
- Purpose: Let users upload multiple PDF documents, build a FAISS semantic-search index over their embeddings, then ask natural-language questions. Answers are generated by an LLaMA 3.1 model via the OpenRouter API, with source passages surfaced for transparency.

##### File structure
- Top-level files only (no separate “frontend” or “backend” folders).
- app.py – the single entry point implementing both UI and logic.
- requirements.txt – lists dependencies (Streamlit, LangChain, FAISS, OpenRouter client, etc.).
- faiss_index*, faiss_docstore.pkl – local persisted vector index & document store.
- No containerization or deployment scripts, so it runs locally or on Streamlit Cloud.

##### Key insights from README
- Technology stack: Streamlit UI, LangChain for orchestration, FAISS vector store, external LLM via OpenRouter.
- Workflow: upload → embedding & index build → semantic retrieval → LLM call → display responses + sources.

##### Main components
a. Streamlit User Interface
- Accepts PDF uploads, displays chat interface, shows source snippets.
b. Document Processing & Embedding
- PDF loader → text chunker → embedding model (e.g., OpenAI embeddings or local embedding model).
c. FAISS Vector Store + Docstore
- FAISS index holds embeddings; docstore (.pkl) maps index IDs back to original text.
d. Query Pipeline
- Semantic search against FAISS → top-k relevant chunks.
e. LLM Client & External Service
- Wrapper in app.py calling OpenRouter’s API endpoint for LLaMA 3.1.
f. Results Aggregator
- Formats the LLM’s answer and attaches source references for display.

##### Relationships & data flows
- User → Streamlit UI: file upload or question input.
- UI → Document Processor: on upload, text extraction & embedding.
- Processor → FAISS: write vectors, update index & docstore.
- UI (question) → Query Pipeline: embed question → search FAISS → retrieve chunks.
- Pipeline → OpenRouter API: send prompt + context → receive model answer.
- Pipeline → Results Aggregator → UI: show answer + source passages.

##### Architectural patterns & design principles
- Single-page application with model-view-controller semantics within app.py.
- Persistent vector store for session-independent retrieval.
- Clear separation between:
- State & storage (FAISS, .pkl files)
- Business logic (LangChain chains in Python)
- Presentation (Streamlit components)

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Layer 1: UI"
        UI["Streamlit UI<br/>(app.py)"]:::frontend
    end

    subgraph "Layer 2: Application"
        Loader["PDF Loader<br/>(app.py)"]:::processing
        Chunker["Text Chunker<br/>(app.py)"]:::processing
        Embedder["Embedding Model<br/>(app.py)"]:::processing
        Retriever["Query Pipeline<br/>(app.py)"]:::processing
        LLMClient["LLM Client<br/>(app.py)"]:::processing
        Aggregator["Results Aggregator<br/>(app.py)"]:::processing
    end

    subgraph "Layer 3: Data"
        FAISS["FAISS Vector Store<br/>(faiss_index)"]:::datastore
        Docstore["Document Store Mapping<br/>(faiss_docstore.pkl<br/>faiss_index_to_docstore_id.pkl)"]:::datastore
    end

    subgraph "Layer 4: External"
        LLMService["OpenRouter LLM Service<br/>(LLaMA 3.1)"]:::external
    end

    UI -->|"PDF Upload"| Loader
    Loader --> Chunker
    Chunker --> Embedder
    Embedder -->|"vectors"| FAISS
    Embedder -->|"mappings"| Docstore
    UI -->|"User Question"| Retriever
    Retriever -->|"embed question"| Embedder
    Retriever -->|"search index"| FAISS
    FAISS -->|"top-k chunks"| Retriever
    Retriever -->|"context+prompt"| LLMClient
    LLMClient -->|"API call"| LLMService
    LLMService -->|"answer JSON"| LLMClient
    LLMClient -->|"raw answer"| Aggregator
    Aggregator -->|"formatted answer+sources"| UI

    click UI "https://github.com/srimathib01/querynest--multidoc-chatbot-/blob/main/app.py"
    click Loader "https://github.com/srimathib01/querynest--multidoc-chatbot-/blob/main/app.py"
    click Chunker "https://github.com/srimathib01/querynest--multidoc-chatbot-/blob/main/app.py"
    click Embedder "https://github.com/srimathib01/querynest--multidoc-chatbot-/blob/main/app.py"
    click Retriever "https://github.com/srimathib01/querynest--multidoc-chatbot-/blob/main/app.py"
    click LLMClient "https://github.com/srimathib01/querynest--multidoc-chatbot-/blob/main/app.py"
    click Aggregator "https://github.com/srimathib01/querynest--multidoc-chatbot-/blob/main/app.py"
    click FAISS "https://github.com/srimathib01/querynest--multidoc-chatbot-/tree/main/faiss_index"
    click Docstore "https://github.com/srimathib01/querynest--multidoc-chatbot-/blob/main/faiss_docstore.pkl"

    classDef frontend fill:#D0E8FF,stroke:#0366d6,color:#0366d6
    classDef processing fill:#E2F7E2,stroke:#208F22,color:#208F22
    classDef datastore fill:#FFE6CC,stroke:#E65C00,color:#E65C00
    classDef external fill:#F0E0FF,stroke:#8A2BE2,color:#8A2BE2
```
