# darshankarna/agentic-rag-with-self-correction-and-citation-highlighting architecture

> Architecture diagram of the GitHub repository darshankarna/agentic-rag-with-self-correction-and-citation-highlighting, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting
- Repository: https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting

## Overview

दफा AI is a Nepali legal question-answering product with a self-correcting RAG workflow and citation-backed answers. The principal path runs from the web interface through the API into translation, retrieval, relevance grading, generation, NLI verification and correction, then returns an answer with citations for display. The repository also documents corpus ingestion and domain fine-tuning as supporting product capabilities. Several internal stages and the local API are supported by excerpts; some README capabilities are documented but their implementation was not sampled.

Main entry points: [`backend/self_correcting_rag.py`](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py) (Self-correcting RAG), [`frontend/src/App.jsx`](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/frontend/src/App.jsx) (Web application), [`frontend/lib/rag.js`](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/frontend/lib/rag.js) (Serverless RAG demo), [`prepare_training_data.py`](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/prepare_training_data.py) (Training data preparation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experience["User experience"]
  node_web["Web application<br/>[App.jsx]"]
  node_local_api["FastAPI service<br/>[api.py]"]
  node_chat_api["Web demo chat route<br/>[chat.js]"]
end

subgraph group_rag["Answer pipeline"]
  node_pipeline["Self-correcting RAG"]
  node_translation["Query translation"]
  node_retrieval["Legal retrieval"]
  node_relevance["Document grading"]
  node_generation["Legal answer generation"]
  node_critic["NLI critic"]
  node_correction["Answer correction"]
  node_citation["Verified citations"]
  node_web_pipeline["Serverless RAG demo<br/>[rag.js]"]
end

subgraph group_corpus["Legal corpus"]
  node_vector_store[("ChromaDB corpus index<br/>[ingest.py]")]
  node_okf["OKF document ingestion<br/>[ingest.py]"]
end

subgraph group_models["Model development"]
  node_training_data["Training data preparation"]
  node_fine_tuning["Qwen LoRA fine-tuning<br/>[train_qwen_lora.py]"]
end

node_user(("Legal questioner"))
node_gemini["Gemini API"]
node_ollama["Ollama model service"]

node_user -->|"asks"| node_web
node_web -->|"submits query"| node_local_api
node_local_api -->|"invokes"| node_pipeline
node_pipeline -->|"translates"| node_translation
node_pipeline -->|"dispatches"| node_retrieval
node_retrieval -.->|"retrieves passages"| node_vector_store
node_pipeline -.->|"grades documents"| node_relevance
node_pipeline -.->|"generates draft"| node_generation
node_generation -.->|"calls model"| node_ollama
node_pipeline -.->|"checks entailment"| node_critic
node_critic -.->|"flags claims"| node_correction
node_pipeline -->|"returns verified"| node_citation
node_pipeline -.->|"returns answer"| node_web
node_user -->|"uploads PDF"| node_web
node_web -.->|"posts query"| node_chat_api
node_chat_api -->|"runs pipeline"| node_web_pipeline
node_web_pipeline -->|"requests inference"| node_gemini
node_web_pipeline -->|"returns citations"| node_citation
node_okf -->|"writes embeddings"| node_vector_store
node_training_data -->|"generates examples"| node_ollama
node_training_data -.->|"prepares data"| node_fine_tuning

click node_web "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/frontend/src/App.jsx"
click node_local_api "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/api.py"
click node_chat_api "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/frontend/api/chat.js"
click node_pipeline "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py"
click node_translation "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py"
click node_retrieval "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py"
click node_vector_store "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/scripts/okf_conversion/ingest.py"
click node_relevance "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py"
click node_generation "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py"
click node_critic "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py"
click node_correction "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py"
click node_citation "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py"
click node_okf "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/scripts/okf_conversion/ingest.py"
click node_training_data "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/prepare_training_data.py"
click node_fine_tuning "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/scripts/training/qwen/train_qwen_lora.py"
click node_web_pipeline "https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/frontend/lib/rag.js"

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_web,node_local_api,node_chat_api toneBlue
class node_pipeline,node_translation,node_retrieval,node_relevance,node_generation,node_critic,node_correction,node_citation,node_web_pipeline toneAmber
class node_vector_store,node_okf,node_gemini toneMint
class node_training_data,node_fine_tuning toneRose
class node_user,node_ollama toneIndigo
```

## Components

### User experience

- [Web application](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/frontend/src/App.jsx): `frontend/src/App.jsx`
- [FastAPI service](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/api.py): `backend/api.py`
- [Web demo chat route](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/frontend/api/chat.js): `frontend/api/chat.js`

### Answer pipeline

- [Self-correcting RAG](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py): `backend/self_correcting_rag.py`
- [Query translation](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py): `backend/self_correcting_rag.py`
- [Legal retrieval](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py): `backend/self_correcting_rag.py`
- [Document grading](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py): `backend/self_correcting_rag.py`
- [Legal answer generation](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py): `backend/self_correcting_rag.py`
- [NLI critic](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py): `backend/self_correcting_rag.py`
- [Answer correction](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py): `backend/self_correcting_rag.py`
- [Verified citations](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/backend/self_correcting_rag.py): `backend/self_correcting_rag.py`
- [Serverless RAG demo](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/frontend/lib/rag.js): `frontend/lib/rag.js`

### Legal corpus

- [ChromaDB corpus index](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/scripts/okf_conversion/ingest.py): `scripts/okf_conversion/ingest.py`
- [OKF document ingestion](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/scripts/okf_conversion/ingest.py): `scripts/okf_conversion/ingest.py`

### Model development

- [Training data preparation](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/prepare_training_data.py): `prepare_training_data.py`
- [Qwen LoRA fine-tuning](https://github.com/darshankarna/agentic-rag-with-self-correction-and-citation-highlighting/blob/main/scripts/training/qwen/train_qwen_lora.py): `scripts/training/qwen/train_qwen_lora.py`

### Other components

- **Legal questioner**
- **Gemini API**
- **Ollama model service**

## Connections

- Legal questioner → Web application: asks (evidence: `frontend/src/App.jsx`)
- Web application → FastAPI service: submits query (evidence: `frontend/src/App.jsx`)
- FastAPI service → Self-correcting RAG: invokes (evidence: `backend/api.py`)
- Self-correcting RAG → Query translation: translates (evidence: `backend/self_correcting_rag.py`)
- Self-correcting RAG → Legal retrieval: dispatches (evidence: `backend/self_correcting_rag.py`)
- Legal retrieval → ChromaDB corpus index: retrieves passages
- Self-correcting RAG → Document grading: grades documents (evidence: `backend/self_correcting_rag.py`)
- Self-correcting RAG → Legal answer generation: generates draft (evidence: `backend/self_correcting_rag.py`)
- Legal answer generation → Ollama model service: calls model
- Self-correcting RAG → NLI critic: checks entailment (evidence: `backend/self_correcting_rag.py`)
- NLI critic → Answer correction: flags claims (evidence: `backend/self_correcting_rag.py`)
- Self-correcting RAG → Verified citations: returns verified (evidence: `backend/self_correcting_rag.py`)
- Self-correcting RAG → Web application: returns answer
- Legal questioner → Web application: uploads PDF (evidence: `frontend/src/App.jsx`)
- Web application → Web demo chat route: posts query
- Web demo chat route → Serverless RAG demo: runs pipeline (evidence: `frontend/api/chat.js`)
- Serverless RAG demo → Gemini API: requests inference (evidence: `frontend/lib/rag.js`)
- Serverless RAG demo → Verified citations: returns citations (evidence: `frontend/lib/rag.js`)
- OKF document ingestion → ChromaDB corpus index: writes embeddings (evidence: `scripts/okf_conversion/ingest.py`)
- Training data preparation → Ollama model service: generates examples (evidence: `prepare_training_data.py`)
- Training data preparation → Qwen LoRA fine-tuning: prepares data
