# nithish-yenaganti/rag_system architecture

> Architecture diagram of the GitHub repository nithish-yenaganti/rag_system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nithish-yenaganti/rag_system
- Repository: https://github.com/nithish-yenaganti/rag_system

## Overview

1. **Project shape**
   - This is a small, single-process RAG application centered on `Rag_Web.py`.
   - There is no separate backend service or database layer; the app combines UI, document processing, retrieval, and generation in one Python runtime.

2. **Primary runtime boundary**
   - The user interacts through a **Streamlit** web UI.
   - Streamlit is the presentation layer and triggers the RAG pipeline when a question is submitted.

3. **Source data boundary**
   - The knowledge source is a single local document: `Statefarm.pdf`.
   - That PDF is the only domain corpus described in the repo, so the app is specialized to answering questions about a car insurance policy.

4. **Document ingestion pipeline**
   - `PyPDFLoader` reads the PDF into text.
   - `RecursiveCharacterTextSplitter` breaks the document into overlapping chunks.
   - This chunking step is important because retrieval operates on smaller passages rather than whole pages or the full document.

5. **Indexing / retrieval layer**
   - Each chunk is embedded with **OpenAIEmbeddings**.
   - The embeddings are stored in a **FAISS** vector index, which acts as the local retrieval store.
   - This is the main retrieval boundary: raw document text becomes vectorized chunks that can be searched by similarity.

6. **Question answering flow**
   - A user question enters from Streamlit.
   - The retriever fetches the most relevant chunks from FAISS.
   - Those chunks, along with the question, are sent to the LLM for answer generation.

7. **Generation layer**
   - **ChatOpenAI** is used for the answer synthesis step.
   - The prompt is constrained to answer strictly from retrieved context, which is the key anti-hallucination control in the architecture.

8. **External dependencies**
   - The application depends on the **OpenAI API** for both embeddings and chat generation.
   - `OPENAI_API_KEY` is therefore an external runtime requirement and a major infrastructure dependency.

9. **Repository contents and role**
   - `*.ipynb` notebooks are experimentation and development artifacts for loading docs, splitting, embeddings, retrievers, and model testing.
   - They are not the production path, but they show the pipeline was developed step-by-step before being consolidated into the Streamlit script.

10. **Operational model**
   - The app is best understood as a **local RAG pipeline with remote model calls**:
     - local PDF source
     - local chunking and FAISS retrieval
     - remote OpenAI embedding and chat services
     - Streamlit UI as the entry point
   - The architecture is simple and linear, with the main data flow running from document → chunks → vectors → retrieved context → LLM response → UI.

11. **Key diagram boundaries to show**
   - User/browser boundary at Streamlit
   - Local application boundary around `Rag_Web.py`
   - External API boundary around OpenAI
   - Knowledge boundary around `Statefarm.pdf` and its derived FAISS index

Main entry points: [`Rag_Web.py`](https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py) (Streamlit UI), [`Statefarm.pdf`](https://github.com/nithish-yenaganti/rag_system/blob/main/Statefarm.pdf) (Statefarm PDF), [`complete_RAG.ipynb`](https://github.com/nithish-yenaganti/rag_system/blob/main/complete_RAG.ipynb) (Notebooks)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Presentation"]
  node_user(("User<br/>person"))
  node_streamlit_ui["Streamlit UI<br/>presentation<br/>[Rag_Web.py]"]
  node_answer["Answer<br/>response<br/>[Rag_Web.py]"]
end

subgraph group_app["Local RAG App"]
  node_rag_web["RAG app<br/>orchestrator<br/>[Rag_Web.py]"]
  node_pdf_loader["PDF loader<br/>ingestion<br/>[Rag_Web.py]"]
  node_splitter["Text splitter<br/>chunking<br/>[Rag_Web.py]"]
  node_retriever["Retriever<br/>search<br/>[Rag_Web.py]"]
  node_context_builder["Context packer<br/>prompting<br/>[Rag_Web.py]"]
end

subgraph group_data["Knowledge Base"]
  node_faiss_index[("FAISS index<br/>vector store<br/>[Rag_Web.py]")]
  node_statefarm_pdf["Statefarm PDF<br/>source document<br/>[Statefarm.pdf]"]
end

subgraph group_external["External Services"]
  node_embedder["Embeddings<br/>[Rag_Web.py]"]
  node_chat_model["ChatOpenAI<br/>llm<br/>[Rag_Web.py]"]
  node_openai_api{{"OpenAI API<br/>remote api"}}
  node_env_key{{"API key<br/>runtime secret"}}
end

subgraph group_dev["Experiments"]
  node_notebooks["Notebooks<br/>experiments<br/>[complete_RAG.ipynb]"]
end

node_user -->|"asks"| node_streamlit_ui
node_streamlit_ui -->|"triggers"| node_rag_web
node_rag_web -->|"loads"| node_pdf_loader
node_statefarm_pdf -->|"source"| node_pdf_loader
node_pdf_loader -->|"text"| node_splitter
node_splitter -->|"chunks"| node_embedder
node_embedder -->|"embed request"| node_openai_api
node_embedder -->|"stores vectors"| node_faiss_index
node_faiss_index -->|"search space"| node_retriever
node_streamlit_ui -->|"question"| node_retriever
node_retriever -->|"top chunks"| node_context_builder
node_context_builder -->|"prompt"| node_chat_model
node_chat_model -->|"completion request"| node_openai_api
node_env_key -->|"auth"| node_openai_api
node_chat_model -->|"answer text"| node_answer
node_answer -->|"renders"| node_streamlit_ui
node_notebooks -.->|"prototype lineage"| node_rag_web

click node_streamlit_ui "https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py"
click node_rag_web "https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py"
click node_pdf_loader "https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py"
click node_splitter "https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py"
click node_embedder "https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py"
click node_faiss_index "https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py"
click node_retriever "https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py"
click node_context_builder "https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py"
click node_chat_model "https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py"
click node_answer "https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py"
click node_statefarm_pdf "https://github.com/nithish-yenaganti/rag_system/blob/main/Statefarm.pdf"
click node_notebooks "https://github.com/nithish-yenaganti/rag_system/blob/main/complete_RAG.ipynb"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_user,node_streamlit_ui,node_answer toneBlue
class node_rag_web,node_pdf_loader,node_splitter,node_retriever,node_context_builder toneAmber
class node_faiss_index,node_statefarm_pdf toneMint
class node_embedder,node_chat_model,node_openai_api,node_env_key toneRose
class node_notebooks toneIndigo
```

## Components

### Presentation

User-facing Streamlit entry point and response display.

- **User**: person · Question sender at the browser boundary.
- [Streamlit UI](https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py): presentation · Web interface that accepts questions and renders answers. · `Rag_Web.py`
- [Answer](https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py): response · Returned response shown in the UI. · `Rag_Web.py`

### Local RAG App

Single-process Python pipeline in Rag_Web.py.

- [RAG app](https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py): orchestrator · Main script that wires ingestion, retrieval, and generation together. · `Rag_Web.py`
- [PDF loader](https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py): ingestion · Reads the local PDF into text. · `Rag_Web.py`
- [Text splitter](https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py): chunking · Breaks loaded text into overlapping chunks. · `Rag_Web.py`
- [Retriever](https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py): search · Fetches the most relevant chunks for a query. · `Rag_Web.py`
- [Context packer](https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py): prompting · Combines retrieved passages with the user question. · `Rag_Web.py`

### Knowledge Base

Local source document and derived retrieval store.

- [FAISS index](https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py): vector store · Local similarity index over embedded chunks. · `Rag_Web.py`
- [Statefarm PDF](https://github.com/nithish-yenaganti/rag_system/blob/main/Statefarm.pdf): source document · Local policy document that is the corpus for Q&A. · `Statefarm.pdf`

### External Services

Remote OpenAI dependencies for embeddings and generation.

- [Embeddings](https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py): embedding · Turns chunks into vectors via OpenAI embeddings. · `Rag_Web.py`
- [ChatOpenAI](https://github.com/nithish-yenaganti/rag_system/blob/main/Rag_Web.py): llm · Synthesizes the final answer from constrained context. · `Rag_Web.py`
- **OpenAI API**: remote api · External service used for embeddings and chat completion.
- **API key**: runtime secret · OPENAI_API_KEY required at runtime.

### Experiments

Notebook-based exploration and prototyping.

- [Notebooks](https://github.com/nithish-yenaganti/rag_system/blob/main/complete_RAG.ipynb): experiments · Prototype notebooks for loading, splitting, embeddings, retrievers, and model tests. · `complete_RAG.ipynb`

## Connections

- User → Streamlit UI: asks · User submits a question through the web UI.
- Streamlit UI → RAG app: triggers · UI event enters the main RAG workflow.
- RAG app → PDF loader: loads · Initial document ingestion step.
- Statefarm PDF → PDF loader: source · Local PDF is the only knowledge corpus.
- PDF loader → Text splitter: text · Loaded document is segmented into chunks.
- Text splitter → Embeddings: chunks · Chunks are prepared for vectorization.
- Embeddings → OpenAI API: embed request · Embedding calls go to OpenAI.
- Embeddings → FAISS index: stores vectors · Embedded chunks are written to the local vector index.
- FAISS index → Retriever: search space · Retriever queries the FAISS index by similarity.
- Streamlit UI → Retriever: question · The user query is passed into retrieval.
- Retriever → Context packer: top chunks · Most relevant passages are selected for prompting.
- Context packer → ChatOpenAI: prompt · Context and question are sent for answer synthesis.
- ChatOpenAI → OpenAI API: completion request · Chat generation is performed through OpenAI.
- API key → OpenAI API: auth · Runtime secret required for API access.
- ChatOpenAI → Answer: answer text · Generated response is returned to the app.
- Answer → Streamlit UI: renders · Final answer is displayed in the Streamlit page.
- Notebooks → RAG app: prototype lineage · Notebook experiments informed the production script.
