# rafsanoo1/rag-service architecture

> Architecture diagram of the GitHub repository rafsanoo1/rag-service, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rafsanoo1/rag-service
- Repository: https://github.com/rafsanoo1/rag-service

## Overview

DocuSense is a FastAPI document-grounded question-answering service. Users upload or query a document: the API validates uploads, loads and cleans document text, chunks it, and builds an active FAISS index; queries retrieve top matches, apply a similarity guardrail, and invoke Ollama only when context is sufficient. Responses include the answer, source snippets, similarity scores, and token usage. The graph reflects the sampled call paths and documented capabilities; unsampled API internals and configuration wiring are not expanded.

Main entry points: [`app/main.py`](https://github.com/rafsanoo1/rag-service/blob/main/app/main.py) (Query and upload API), [`app/ingestion/document_loader.py`](https://github.com/rafsanoo1/rag-service/blob/main/app/ingestion/document_loader.py) (Document loader), [`app/generation/llm_client.py`](https://github.com/rafsanoo1/rag-service/blob/main/app/generation/llm_client.py) (Grounded answer client), [`app/retrieval/vector_store.py`](https://github.com/rafsanoo1/rag-service/blob/main/app/retrieval/vector_store.py) (FAISS vector index)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API and orchestration"]
  node_api["Query and upload API<br/>[main.py]"]
  node_index_builder["Index builder<br/>[main.py]"]
  node_settings["Runtime settings<br/>[config.py]"]
end

subgraph group_ingestion["Document ingestion"]
  node_document_loader["Document loader<br/>[document_loader.py]"]
  node_pdf_extractor["PDF extraction<br/>[pdf_loader.py]"]
  node_text_cleaner["Text cleaning<br/>[text_cleaner.py]"]
  node_chunker["Overlapping chunker<br/>[chunker.py]"]
end

subgraph group_retrieval["Retrieval and guardrails"]
  node_vector_store[("FAISS vector index<br/>[vector_store.py]")]
  node_embeddings["Sentence embeddings<br/>[embeddings.py]"]
  node_guardrail["Similarity guardrail<br/>[guardrail.py]"]
end

subgraph group_generation["Grounded generation"]
  node_generator["Grounded answer client<br/>[llm_client.py]"]
end

node_user(("API user"))
node_ollama{{"Ollama runtime"}}

node_user -->|"queries or uploads"| node_api
node_api -->|"builds index"| node_index_builder
node_index_builder -->|"loads document"| node_document_loader
node_index_builder -->|"chunks pages"| node_chunker
node_index_builder -->|"builds index"| node_vector_store
node_document_loader -->|"extracts PDF"| node_pdf_extractor
node_document_loader -->|"cleans text"| node_text_cleaner
node_pdf_extractor -->|"cleans pages"| node_text_cleaner
node_chunker -->|"reads chunk settings"| node_settings
node_vector_store -->|"embeds chunks and queries"| node_embeddings
node_api -->|"checks query context"| node_guardrail
node_guardrail -->|"searches top-k"| node_vector_store
node_api -->|"reads query settings"| node_settings
node_api -->|"generates if supported"| node_generator
node_generator -->|"requests completion"| node_ollama
node_generator -->|"reads generation settings"| node_settings
node_api -->|"returns answer and sources"| node_user

click node_api "https://github.com/rafsanoo1/rag-service/blob/main/app/main.py"
click node_index_builder "https://github.com/rafsanoo1/rag-service/blob/main/app/main.py"
click node_settings "https://github.com/rafsanoo1/rag-service/blob/main/app/config.py"
click node_document_loader "https://github.com/rafsanoo1/rag-service/blob/main/app/ingestion/document_loader.py"
click node_pdf_extractor "https://github.com/rafsanoo1/rag-service/blob/main/app/ingestion/pdf_loader.py"
click node_text_cleaner "https://github.com/rafsanoo1/rag-service/blob/main/app/ingestion/text_cleaner.py"
click node_chunker "https://github.com/rafsanoo1/rag-service/blob/main/app/ingestion/chunker.py"
click node_vector_store "https://github.com/rafsanoo1/rag-service/blob/main/app/retrieval/vector_store.py"
click node_embeddings "https://github.com/rafsanoo1/rag-service/blob/main/app/retrieval/embeddings.py"
click node_guardrail "https://github.com/rafsanoo1/rag-service/blob/main/app/retrieval/guardrail.py"
click node_generator "https://github.com/rafsanoo1/rag-service/blob/main/app/generation/llm_client.py"

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_api,node_index_builder,node_settings,node_user toneBlue
class node_document_loader,node_pdf_extractor,node_text_cleaner,node_chunker toneAmber
class node_vector_store,node_embeddings,node_guardrail toneMint
class node_generator toneRose
class node_ollama toneIndigo
```

## Components

### API and orchestration

- [Query and upload API](https://github.com/rafsanoo1/rag-service/blob/main/app/main.py): `app/main.py`
- [Index builder](https://github.com/rafsanoo1/rag-service/blob/main/app/main.py): `app/main.py`
- [Runtime settings](https://github.com/rafsanoo1/rag-service/blob/main/app/config.py): `app/config.py`

### Document ingestion

- [Document loader](https://github.com/rafsanoo1/rag-service/blob/main/app/ingestion/document_loader.py): `app/ingestion/document_loader.py`
- [PDF extraction](https://github.com/rafsanoo1/rag-service/blob/main/app/ingestion/pdf_loader.py): `app/ingestion/pdf_loader.py`
- [Text cleaning](https://github.com/rafsanoo1/rag-service/blob/main/app/ingestion/text_cleaner.py): `app/ingestion/text_cleaner.py`
- [Overlapping chunker](https://github.com/rafsanoo1/rag-service/blob/main/app/ingestion/chunker.py): `app/ingestion/chunker.py`

### Retrieval and guardrails

- [FAISS vector index](https://github.com/rafsanoo1/rag-service/blob/main/app/retrieval/vector_store.py): `app/retrieval/vector_store.py`
- [Sentence embeddings](https://github.com/rafsanoo1/rag-service/blob/main/app/retrieval/embeddings.py): `app/retrieval/embeddings.py`
- [Similarity guardrail](https://github.com/rafsanoo1/rag-service/blob/main/app/retrieval/guardrail.py): `app/retrieval/guardrail.py`

### Grounded generation

- [Grounded answer client](https://github.com/rafsanoo1/rag-service/blob/main/app/generation/llm_client.py): `app/generation/llm_client.py`

### Other components

- **API user**
- **Ollama runtime**

## Connections

- API user → Query and upload API: queries or uploads (evidence: `README.md`)
- Query and upload API → Index builder: builds index (evidence: `app/main.py`)
- Index builder → Document loader: loads document (evidence: `app/main.py`)
- Index builder → Overlapping chunker: chunks pages (evidence: `app/main.py`)
- Index builder → FAISS vector index: builds index (evidence: `app/main.py`)
- Document loader → PDF extraction: extracts PDF (evidence: `app/ingestion/document_loader.py`)
- Document loader → Text cleaning: cleans text (evidence: `app/ingestion/document_loader.py`)
- PDF extraction → Text cleaning: cleans pages (evidence: `app/ingestion/pdf_loader.py`)
- Overlapping chunker → Runtime settings: reads chunk settings (evidence: `app/ingestion/chunker.py`)
- FAISS vector index → Sentence embeddings: embeds chunks and queries (evidence: `app/retrieval/vector_store.py`)
- Query and upload API → Similarity guardrail: checks query context (evidence: `app/main.py`)
- Similarity guardrail → FAISS vector index: searches top-k (evidence: `app/retrieval/guardrail.py`)
- Query and upload API → Runtime settings: reads query settings (evidence: `app/main.py`)
- Query and upload API → Grounded answer client: generates if supported (evidence: `app/main.py`)
- Grounded answer client → Ollama runtime: requests completion (evidence: `app/generation/llm_client.py`)
- Grounded answer client → Runtime settings: reads generation settings (evidence: `app/generation/llm_client.py`)
- Query and upload API → API user: returns answer and sources (evidence: `README.md`)
