# halivekp/khula-gyan architecture

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

- Interactive diagram: https://gitdiagram.com/halivekp/khula-gyan
- Repository: https://github.com/halivekp/khula-gyan

## Overview

Khula Gyan is a bilingual Nepali municipal-services guide. Its main workflow takes a question and municipality from the user, retrieves matching charter passages, generates a response, and returns either grounded citations or a refusal. The graph also shows charter ingestion and the documented evaluation harness. Relationships among unsampled ingestion stages follow the README’s workflow; only documented or indexed relationships are shown.

Main entry points: [`src/generation/answer.py`](https://github.com/halivekp/khula-gyan/blob/main/src/generation/answer.py) (Answer generation), [`eval/run_eval.py`](https://github.com/halivekp/khula-gyan/blob/main/eval/run_eval.py) (Evaluation runner), [`scripts/build_index.py`](https://github.com/halivekp/khula-gyan/blob/main/scripts/build_index.py) (Index builder), [`scripts/process_document.py`](https://github.com/halivekp/khula-gyan/blob/main/scripts/process_document.py) (Document processing)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingestion["Charter ingestion"]
  node_indexer["Index builder<br/>[build_index.py]"]
  node_document_processing["Document processing"]
  node_extraction["Page extraction<br/>[extract.py]"]
  node_cleaning["Text repair<br/>[clean.py]"]
  node_chunking["Chunk creation<br/>[chunk.py]"]
  node_embedding_store["Embedding store<br/>[embed_store.py]"]
end

subgraph group_retrieval["Passage retrieval"]
  node_search["Filtered dense search<br/>[search.py]"]
end

subgraph group_generation["Answer generation"]
  node_answer["Answer generation<br/>[answer.py]"]
  node_prompt["Prompt construction<br/>[prompt.py]"]
  node_llm["LLM client<br/>[llm.py]"]
  node_spend["Spend reservation<br/>[spend.py]"]
  node_fake["Fake generator<br/>[fake_llm.py]"]
  node_guard["Evidence guard<br/>[guard.py]"]
  node_schema["Response validation<br/>[schema.py]"]
  node_language["Language detection<br/>[language.py]"]
end

subgraph group_interface["User interface"]
  node_frontend["Streamlit interface<br/>[app.py]"]
  node_pipeline["Ask pipeline<br/>[pipeline.py]"]
end

subgraph group_evaluation["Evaluation"]
  node_eval_runner["Evaluation runner<br/>[run_eval.py]"]
  node_judge["Answer judge<br/>[judge.py]"]
  node_results["Evaluation results log<br/>[results_log.py]"]
end

node_user(("Questioner"))
node_charters["Charter files"]
node_chroma[("Chroma index")]
node_provider{{"Chat API"}}
node_ocr{{"Tesseract OCR"}}

node_user -->|"asks"| node_frontend
node_charters -->|"indexes"| node_indexer
node_indexer -->|"processes"| node_document_processing
node_document_processing -->|"extracts pages"| node_extraction
node_document_processing -->|"repairs text"| node_cleaning
node_indexer -->|"creates chunks"| node_chunking
node_indexer -->|"stores embeddings"| node_embedding_store
node_embedding_store -->|"writes index"| node_chroma
node_frontend -->|"calls ask"| node_pipeline
node_pipeline -->|"retrieves passages"| node_search
node_chroma -->|"supplies passages"| node_search
node_pipeline -->|"generates response"| node_answer
node_answer -->|"detects language"| node_language
node_answer -->|"builds messages"| node_prompt
node_answer -->|"requests completion"| node_llm
node_llm -->|"calls API"| node_provider
node_llm -->|"reserves cost"| node_spend
node_llm -.->|"dispatches offline"| node_fake
node_answer -->|"validates response"| node_schema
node_answer -->|"checks evidence"| node_guard
node_document_processing -.->|"uses for scans"| node_ocr
node_eval_runner -->|"retrieves passages"| node_search
node_eval_runner -->|"evaluates answers"| node_answer
node_eval_runner -->|"optionally judges"| node_judge
node_eval_runner -->|"logs runs"| node_results

click node_charters "https://github.com/halivekp/khula-gyan/tree/main/data"
click node_indexer "https://github.com/halivekp/khula-gyan/blob/main/scripts/build_index.py"
click node_document_processing "https://github.com/halivekp/khula-gyan/blob/main/scripts/process_document.py"
click node_extraction "https://github.com/halivekp/khula-gyan/blob/main/src/ingest/extract.py"
click node_cleaning "https://github.com/halivekp/khula-gyan/blob/main/src/ingest/clean.py"
click node_chunking "https://github.com/halivekp/khula-gyan/blob/main/src/ingest/chunk.py"
click node_embedding_store "https://github.com/halivekp/khula-gyan/blob/main/src/ingest/embed_store.py"
click node_frontend "https://github.com/halivekp/khula-gyan/blob/main/frontend/app.py"
click node_pipeline "https://github.com/halivekp/khula-gyan/blob/main/src/pipeline.py"
click node_search "https://github.com/halivekp/khula-gyan/blob/main/src/retrieval/search.py"
click node_answer "https://github.com/halivekp/khula-gyan/blob/main/src/generation/answer.py"
click node_prompt "https://github.com/halivekp/khula-gyan/blob/main/src/generation/prompt.py"
click node_llm "https://github.com/halivekp/khula-gyan/blob/main/src/generation/llm.py"
click node_spend "https://github.com/halivekp/khula-gyan/blob/main/src/generation/spend.py"
click node_fake "https://github.com/halivekp/khula-gyan/blob/main/src/generation/fake_llm.py"
click node_guard "https://github.com/halivekp/khula-gyan/blob/main/src/generation/guard.py"
click node_schema "https://github.com/halivekp/khula-gyan/blob/main/src/generation/schema.py"
click node_language "https://github.com/halivekp/khula-gyan/blob/main/src/generation/language.py"
click node_eval_runner "https://github.com/halivekp/khula-gyan/blob/main/eval/run_eval.py"
click node_judge "https://github.com/halivekp/khula-gyan/blob/main/eval/judge.py"
click node_results "https://github.com/halivekp/khula-gyan/blob/main/eval/results_log.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_indexer,node_document_processing,node_extraction,node_cleaning,node_chunking,node_embedding_store toneBlue
class node_search,node_chroma toneAmber
class node_answer,node_prompt,node_llm,node_spend,node_fake,node_guard,node_schema,node_language,node_provider toneMint
class node_frontend,node_pipeline toneRose
class node_eval_runner,node_judge,node_results,node_user,node_ocr toneIndigo
class node_charters toneTeal
```

## Components

### Charter ingestion

- [Index builder](https://github.com/halivekp/khula-gyan/blob/main/scripts/build_index.py): `scripts/build_index.py`
- [Document processing](https://github.com/halivekp/khula-gyan/blob/main/scripts/process_document.py): `scripts/process_document.py`
- [Page extraction](https://github.com/halivekp/khula-gyan/blob/main/src/ingest/extract.py): `src/ingest/extract.py`
- [Text repair](https://github.com/halivekp/khula-gyan/blob/main/src/ingest/clean.py): `src/ingest/clean.py`
- [Chunk creation](https://github.com/halivekp/khula-gyan/blob/main/src/ingest/chunk.py): `src/ingest/chunk.py`
- [Embedding store](https://github.com/halivekp/khula-gyan/blob/main/src/ingest/embed_store.py): `src/ingest/embed_store.py`

### Passage retrieval

- [Filtered dense search](https://github.com/halivekp/khula-gyan/blob/main/src/retrieval/search.py): `src/retrieval/search.py`

### Answer generation

- [Answer generation](https://github.com/halivekp/khula-gyan/blob/main/src/generation/answer.py): `src/generation/answer.py`
- [Prompt construction](https://github.com/halivekp/khula-gyan/blob/main/src/generation/prompt.py): `src/generation/prompt.py`
- [LLM client](https://github.com/halivekp/khula-gyan/blob/main/src/generation/llm.py): `src/generation/llm.py`
- [Spend reservation](https://github.com/halivekp/khula-gyan/blob/main/src/generation/spend.py): `src/generation/spend.py`
- [Fake generator](https://github.com/halivekp/khula-gyan/blob/main/src/generation/fake_llm.py): `src/generation/fake_llm.py`
- [Evidence guard](https://github.com/halivekp/khula-gyan/blob/main/src/generation/guard.py): `src/generation/guard.py`
- [Response validation](https://github.com/halivekp/khula-gyan/blob/main/src/generation/schema.py): `src/generation/schema.py`
- [Language detection](https://github.com/halivekp/khula-gyan/blob/main/src/generation/language.py): `src/generation/language.py`

### User interface

- [Streamlit interface](https://github.com/halivekp/khula-gyan/blob/main/frontend/app.py): `frontend/app.py`
- [Ask pipeline](https://github.com/halivekp/khula-gyan/blob/main/src/pipeline.py): `src/pipeline.py`

### Evaluation

- [Evaluation runner](https://github.com/halivekp/khula-gyan/blob/main/eval/run_eval.py): `eval/run_eval.py`
- [Answer judge](https://github.com/halivekp/khula-gyan/blob/main/eval/judge.py): `eval/judge.py`
- [Evaluation results log](https://github.com/halivekp/khula-gyan/blob/main/eval/results_log.py): `eval/results_log.py`

### Other components

- **Questioner**
- [Charter files](https://github.com/halivekp/khula-gyan/tree/main/data): `data`
- **Chroma index**
- **Chat API**
- **Tesseract OCR**

## Connections

- Questioner → Streamlit interface: asks (evidence: `README.md`)
- Charter files → Index builder: indexes (evidence: `README.md`)
- Index builder → Document processing: processes (evidence: `scripts/build_index.py`)
- Document processing → Page extraction: extracts pages (evidence: `scripts/process_document.py`)
- Document processing → Text repair: repairs text (evidence: `scripts/process_document.py`)
- Index builder → Chunk creation: creates chunks (evidence: `scripts/build_index.py`)
- Index builder → Embedding store: stores embeddings (evidence: `scripts/build_index.py`)
- Embedding store → Chroma index: writes index (evidence: `README.md`)
- Streamlit interface → Ask pipeline: calls ask (evidence: `frontend/app.py`)
- Ask pipeline → Filtered dense search: retrieves passages (evidence: `README.md`)
- Chroma index → Filtered dense search: supplies passages (evidence: `README.md`)
- Ask pipeline → Answer generation: generates response (evidence: `README.md`)
- Answer generation → Language detection: detects language (evidence: `src/generation/answer.py`)
- Answer generation → Prompt construction: builds messages (evidence: `src/generation/answer.py`)
- Answer generation → LLM client: requests completion (evidence: `src/generation/answer.py`)
- LLM client → Chat API: calls API (evidence: `src/generation/llm.py`)
- LLM client → Spend reservation: reserves cost (evidence: `src/generation/llm.py`)
- LLM client → Fake generator: dispatches offline (evidence: `src/generation/fake_llm.py`)
- Answer generation → Response validation: validates response (evidence: `src/generation/answer.py`)
- Answer generation → Evidence guard: checks evidence (evidence: `src/generation/answer.py`)
- Document processing → Tesseract OCR: uses for scans (evidence: `README.md`)
- Evaluation runner → Filtered dense search: retrieves passages (evidence: `eval/run_eval.py`)
- Evaluation runner → Answer generation: evaluates answers (evidence: `eval/run_eval.py`)
- Evaluation runner → Answer judge: optionally judges (evidence: `eval/run_eval.py`)
- Evaluation runner → Evaluation results log: logs runs (evidence: `eval/run_eval.py`)
