# swipyknows/rag-hallucination-detector architecture

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

- Interactive diagram: https://gitdiagram.com/swipyknows/rag-hallucination-detector
- Repository: https://github.com/swipyknows/rag-hallucination-detector

## Overview

This project builds a medical question-answering RAG system and evaluates how well its answers stay grounded in retrieved PubMedQA context. Its main workflow is ingesting and embedding source text, answering through retrieval and generation, then scoring answer quality. Users can run the system through a Streamlit interface or the API; separate scripts compare a no-retrieval baseline and retrieval, chunk-size, and top-k variants. Some runtime modules are documented but were not sampled, so their internal links are limited to documented behavior.

Main entry points: [`ablation_chunking.py`](https://github.com/swipyknows/rag-hallucination-detector/blob/main/ablation_chunking.py) (Chunk-size study), [`ablation_retrieval.py`](https://github.com/swipyknows/rag-hallucination-detector/blob/main/ablation_retrieval.py) (Retriever study), [`ablation_topk.py`](https://github.com/swipyknows/rag-hallucination-detector/blob/main/ablation_topk.py) (Top-k study), [`qa_pairs.json`](https://github.com/swipyknows/rag-hallucination-detector/blob/main/qa_pairs.json) (Reference QA set)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingest["Data ingestion"]
  node_pubmedqa[("PubMedQA corpus")]
  node_embedder["Chunk and embed<br/>[embedder.py]"]
  node_vectorindex[("FAISS index")]
end

subgraph group_runtime["RAG runtime"]
  node_ragpipeline["RAG pipeline<br/>[rag_pipeline.py]"]
  node_api["Query API<br/>[api.py]"]
  node_generation["Answer generation<br/>[api.py]"]
end

subgraph group_interfaces["User interfaces"]
  node_webapp["Web interface<br/>[app.py]"]
  node_unifiedapp["Unified application<br/>[unified_app.py]"]
end

subgraph group_evaluation["Evaluation and experiments"]
  node_qapairs["Reference QA set<br/>[qa_pairs.json]"]
  node_scorer["Quality scoring"]
  node_baseline["Baseline comparison"]
  node_ablchunk["Chunk-size study"]
  node_ablretrieval["Retriever study"]
  node_abltopk["Top-k study<br/>[ablation_topk.py]"]
  node_results["Experiment outputs"]
end

node_gemini{{"Gemini services"}}
node_ragas{{"Ragas evaluator"}}
node_streamlit(("Streamlit user"))

node_pubmedqa -->|"loads contexts"| node_embedder
node_embedder -->|"requests embeddings"| node_gemini
node_embedder -->|"saves index"| node_vectorindex
node_vectorindex -->|"retrieves documents"| node_ragpipeline
node_ragpipeline -->|"generates answers"| node_gemini
node_streamlit -->|"uses interface"| node_webapp
node_webapp -->|"posts query"| node_api
node_streamlit -->|"uses interface"| node_unifiedapp
node_api -->|"dispatches strategy"| node_generation
node_generation -->|"generates answers"| node_gemini
node_api -->|"scores answers"| node_ragas
node_unifiedapp -->|"generates answers"| node_gemini
node_unifiedapp -->|"scores answers"| node_ragas
node_qapairs -->|"provides references"| node_scorer
node_scorer -->|"evaluates quality"| node_ragas
node_qapairs -->|"provides questions"| node_baseline
node_baseline -->|"requests baseline answers"| node_gemini
node_baseline -->|"compares faithfulness"| node_ragas
node_ablchunk -->|"loads sample data"| node_pubmedqa
node_ablchunk -->|"embeds and generates"| node_gemini
node_ablchunk -->|"scores variants"| node_ragas
node_ablretrieval -->|"loads questions"| node_qapairs
node_ablretrieval -->|"generates answers"| node_gemini
node_ablretrieval -->|"scores variants"| node_ragas
node_abltopk -->|"loads questions"| node_qapairs
node_abltopk -->|"generates answers"| node_gemini
node_abltopk -->|"scores variants"| node_ragas
node_ablchunk -->|"writes results"| node_results
node_ablretrieval -->|"writes results"| node_results
node_abltopk -->|"writes results"| node_results

click node_embedder "https://github.com/swipyknows/rag-hallucination-detector/blob/main/embedder.py"
click node_ragpipeline "https://github.com/swipyknows/rag-hallucination-detector/blob/main/rag_pipeline.py"
click node_api "https://github.com/swipyknows/rag-hallucination-detector/blob/main/api.py"
click node_generation "https://github.com/swipyknows/rag-hallucination-detector/blob/main/api.py"
click node_webapp "https://github.com/swipyknows/rag-hallucination-detector/blob/main/app.py"
click node_unifiedapp "https://github.com/swipyknows/rag-hallucination-detector/blob/main/unified_app.py"
click node_qapairs "https://github.com/swipyknows/rag-hallucination-detector/blob/main/qa_pairs.json"
click node_scorer "https://github.com/swipyknows/rag-hallucination-detector/blob/main/hallucination_scorer.py"
click node_baseline "https://github.com/swipyknows/rag-hallucination-detector/blob/main/hallucination_baseline.py"
click node_ablchunk "https://github.com/swipyknows/rag-hallucination-detector/blob/main/ablation_chunking.py"
click node_ablretrieval "https://github.com/swipyknows/rag-hallucination-detector/blob/main/ablation_retrieval.py"
click node_abltopk "https://github.com/swipyknows/rag-hallucination-detector/blob/main/ablation_topk.py"
click node_results "https://github.com/swipyknows/rag-hallucination-detector/tree/main/results"

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_pubmedqa,node_embedder,node_vectorindex,node_streamlit toneBlue
class node_ragpipeline,node_api,node_generation toneAmber
class node_webapp,node_unifiedapp toneMint
class node_qapairs,node_scorer,node_baseline,node_ablchunk,node_ablretrieval,node_abltopk,node_results toneRose
class node_gemini,node_ragas toneIndigo
```

## Components

### Data ingestion

- **PubMedQA corpus**
- [Chunk and embed](https://github.com/swipyknows/rag-hallucination-detector/blob/main/embedder.py): `embedder.py`
- **FAISS index**

### RAG runtime

- [RAG pipeline](https://github.com/swipyknows/rag-hallucination-detector/blob/main/rag_pipeline.py): `rag_pipeline.py`
- [Query API](https://github.com/swipyknows/rag-hallucination-detector/blob/main/api.py): `api.py`
- [Answer generation](https://github.com/swipyknows/rag-hallucination-detector/blob/main/api.py): `api.py`

### User interfaces

- [Web interface](https://github.com/swipyknows/rag-hallucination-detector/blob/main/app.py): `app.py`
- [Unified application](https://github.com/swipyknows/rag-hallucination-detector/blob/main/unified_app.py): `unified_app.py`

### Evaluation and experiments

- [Reference QA set](https://github.com/swipyknows/rag-hallucination-detector/blob/main/qa_pairs.json): `qa_pairs.json`
- [Quality scoring](https://github.com/swipyknows/rag-hallucination-detector/blob/main/hallucination_scorer.py): `hallucination_scorer.py`
- [Baseline comparison](https://github.com/swipyknows/rag-hallucination-detector/blob/main/hallucination_baseline.py): `hallucination_baseline.py`
- [Chunk-size study](https://github.com/swipyknows/rag-hallucination-detector/blob/main/ablation_chunking.py): `ablation_chunking.py`
- [Retriever study](https://github.com/swipyknows/rag-hallucination-detector/blob/main/ablation_retrieval.py): `ablation_retrieval.py`
- [Top-k study](https://github.com/swipyknows/rag-hallucination-detector/blob/main/ablation_topk.py): `ablation_topk.py`
- [Experiment outputs](https://github.com/swipyknows/rag-hallucination-detector/tree/main/results): `results`

### Other components

- **Gemini services**
- **Ragas evaluator**
- **Streamlit user**

## Connections

- PubMedQA corpus → Chunk and embed: loads contexts (evidence: `README.md`)
- Chunk and embed → Gemini services: requests embeddings (evidence: `embedder.py`)
- Chunk and embed → FAISS index: saves index (evidence: `README.md`)
- FAISS index → RAG pipeline: retrieves documents (evidence: `README.md`)
- RAG pipeline → Gemini services: generates answers (evidence: `README.md`)
- Streamlit user → Web interface: uses interface (evidence: `README.md`)
- Web interface → Query API: posts query (evidence: `app.py`)
- Streamlit user → Unified application: uses interface (evidence: `README.md`)
- Query API → Answer generation: dispatches strategy (evidence: `api.py`)
- Answer generation → Gemini services: generates answers (evidence: `api.py`)
- Query API → Ragas evaluator: scores answers (evidence: `api.py`)
- Unified application → Gemini services: generates answers (evidence: `unified_app.py`)
- Unified application → Ragas evaluator: scores answers (evidence: `README.md`)
- Reference QA set → Quality scoring: provides references (evidence: `hallucination_scorer.py`)
- Quality scoring → Ragas evaluator: evaluates quality (evidence: `hallucination_scorer.py`)
- Reference QA set → Baseline comparison: provides questions (evidence: `hallucination_baseline.py`)
- Baseline comparison → Gemini services: requests baseline answers (evidence: `hallucination_baseline.py`)
- Baseline comparison → Ragas evaluator: compares faithfulness (evidence: `hallucination_baseline.py`)
- Chunk-size study → PubMedQA corpus: loads sample data (evidence: `ablation_chunking.py`)
- Chunk-size study → Gemini services: embeds and generates (evidence: `ablation_chunking.py`)
- Chunk-size study → Ragas evaluator: scores variants (evidence: `ablation_chunking.py`)
- Retriever study → Reference QA set: loads questions (evidence: `ablation_retrieval.py`)
- Retriever study → Gemini services: generates answers (evidence: `ablation_retrieval.py`)
- Retriever study → Ragas evaluator: scores variants (evidence: `ablation_retrieval.py`)
- Top-k study → Reference QA set: loads questions (evidence: `README.md`)
- Top-k study → Gemini services: generates answers (evidence: `README.md`)
- Top-k study → Ragas evaluator: scores variants (evidence: `README.md`)
- Chunk-size study → Experiment outputs: writes results (evidence: `ablation_chunking.py`)
- Retriever study → Experiment outputs: writes results (evidence: `README.md`)
- Top-k study → Experiment outputs: writes results (evidence: `README.md`)
