# amirabaskohi/mcompassrag architecture

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

- Interactive diagram: https://gitdiagram.com/amirabaskohi/mcompassrag
- Repository: https://github.com/amirabaskohi/mcompassrag

## Overview

MCompassRAG is a metadata-guided RAG system whose offline workflow trains a pluggable topic model, generates teacher-labeled distillation data, builds a chunk metadata bank, and trains a lightweight retriever; serving embeds queries and ranks indexed chunks without LLM calls. The graph keeps those domain stages distinct and includes the shared encoder and retriever internals. README statements establish the overall workflow and backend capabilities; source excerpts and the SOURCE INDEX support the implementation links. Some stage internals were not sampled, so documented links are cited to the README.

Main entry points: [`topic_models/base.py`](https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/base.py) (Topic interface), [`src/index/metadata_bank.py`](https://github.com/amirabaskohi/mcompassrag/blob/main/src/index/metadata_bank.py) (Metadata bank), [`src/inference/retrieve.py`](https://github.com/amirabaskohi/mcompassrag/blob/main/src/inference/retrieve.py) (Retrieval orchestrator), [`data_gen/build_training_data.py`](https://github.com/amirabaskohi/mcompassrag/blob/main/data_gen/build_training_data.py) (Distillation builder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_topics["Topic modeling"]
  node_corpus["WikiWeb2M corpus<br/>[wikiweb2m.py]"]
  node_topic_train["Topic training"]
  node_topic_registry["Backend registry<br/>[registry.py]"]
  node_topic_contract["Topic interface<br/>[base.py]"]
  node_cemtm["CEMTM backend<br/>[cemtm_adapter.py]"]
  node_etm["ETM backend<br/>[etm_adapter.py]"]
  node_cwtm["CWTM backend<br/>[cwtm_adapter.py]"]
  node_softltm["SoftLTM backend<br/>[softltm_adapter.py]"]
end

subgraph group_distill["Teacher distillation"]
  node_data_builder["Distillation builder"]
  node_query_gen["Query generation<br/>[query_gen.py]"]
  node_teacher["Relevance teacher<br/>[teacher.py]"]
  node_negatives["Negative mining<br/>[negatives.py]"]
  node_chat_client["OpenRouter client"]
end

subgraph group_index["Index and embeddings"]
  node_chunks["Chunking<br/>[chunking.py]"]
  node_pipeline["RAG pipeline<br/>[pipeline.py]"]
  node_metadata_bank[("Metadata bank<br/>[metadata_bank.py]")]
  node_backbone["Qwen backbone<br/>[backbone.py]"]
  node_encoder["Retriever encoder"]
end

subgraph group_retrieve["Train and serve"]
  node_train_entry["Retriever training entry<br/>[train.py]"]
  node_train_data["Training dataset<br/>[dataset.py]"]
  node_trainer["Retriever trainer<br/>[trainer.py]"]
  node_retriever["Compass retriever"]
  node_inference["Retrieval orchestrator<br/>[retrieve.py]"]
end

node_openrouter(("OpenRouter"))
node_user(("Query user"))

node_corpus -->|"trains on"| node_topic_train
node_topic_train -->|"selects backend"| node_topic_registry
node_topic_registry -->|"constructs"| node_topic_contract
node_cemtm -->|"implements"| node_topic_contract
node_etm -->|"implements"| node_topic_contract
node_cwtm -->|"implements"| node_topic_contract
node_softltm -->|"implements"| node_topic_contract
node_data_builder -->|"generates queries"| node_query_gen
node_data_builder -->|"requests labels"| node_teacher
node_data_builder -->|"mines negatives"| node_negatives
node_query_gen -->|"calls"| node_chat_client
node_teacher -->|"calls"| node_chat_client
node_chat_client -->|"sends requests"| node_openrouter
node_chunks -.->|"provides chunks"| node_pipeline
node_pipeline -->|"builds topic model"| node_topic_registry
node_pipeline -->|"builds and saves"| node_metadata_bank
node_metadata_bank -->|"embeds chunks"| node_encoder
node_metadata_bank -->|"encodes topics"| node_topic_contract
node_encoder -->|"wraps"| node_backbone
node_train_entry -->|"loads index"| node_metadata_bank
node_train_entry -->|"builds dataset"| node_train_data
node_train_entry -->|"starts training"| node_trainer
node_trainer -->|"optimizes heads"| node_retriever
node_user -->|"submits query"| node_inference
node_inference -->|"embeds query"| node_encoder
node_inference -->|"reads index"| node_metadata_bank
node_inference -->|"scores candidates"| node_retriever

click node_corpus "https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/wikiweb2m.py"
click node_topic_train "https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/train_topic_model.py"
click node_topic_registry "https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/registry.py"
click node_topic_contract "https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/base.py"
click node_cemtm "https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/cemtm_adapter.py"
click node_etm "https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/etm_adapter.py"
click node_cwtm "https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/cwtm_adapter.py"
click node_softltm "https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/softltm_adapter.py"
click node_data_builder "https://github.com/amirabaskohi/mcompassrag/blob/main/data_gen/build_training_data.py"
click node_query_gen "https://github.com/amirabaskohi/mcompassrag/blob/main/data_gen/query_gen.py"
click node_teacher "https://github.com/amirabaskohi/mcompassrag/blob/main/data_gen/teacher.py"
click node_negatives "https://github.com/amirabaskohi/mcompassrag/blob/main/data_gen/negatives.py"
click node_chat_client "https://github.com/amirabaskohi/mcompassrag/blob/main/data_gen/openrouter_client.py"
click node_chunks "https://github.com/amirabaskohi/mcompassrag/blob/main/src/index/chunking.py"
click node_pipeline "https://github.com/amirabaskohi/mcompassrag/blob/main/src/pipeline.py"
click node_metadata_bank "https://github.com/amirabaskohi/mcompassrag/blob/main/src/index/metadata_bank.py"
click node_backbone "https://github.com/amirabaskohi/mcompassrag/blob/main/src/encoders/backbone.py"
click node_encoder "https://github.com/amirabaskohi/mcompassrag/blob/main/src/encoders/retriever_encoder.py"
click node_train_entry "https://github.com/amirabaskohi/mcompassrag/blob/main/src/training/train.py"
click node_train_data "https://github.com/amirabaskohi/mcompassrag/blob/main/src/training/dataset.py"
click node_trainer "https://github.com/amirabaskohi/mcompassrag/blob/main/src/training/trainer.py"
click node_retriever "https://github.com/amirabaskohi/mcompassrag/blob/main/src/models/compass_retriever.py"
click node_inference "https://github.com/amirabaskohi/mcompassrag/blob/main/src/inference/retrieve.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_corpus,node_topic_train,node_topic_registry,node_topic_contract,node_cemtm,node_etm,node_cwtm,node_softltm,node_user toneBlue
class node_data_builder,node_query_gen,node_teacher,node_negatives,node_chat_client toneAmber
class node_chunks,node_pipeline,node_metadata_bank,node_backbone,node_encoder,node_openrouter toneMint
class node_train_entry,node_train_data,node_trainer,node_retriever,node_inference toneRose
```

## Components

### Topic modeling

- [WikiWeb2M corpus](https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/wikiweb2m.py): `topic_models/wikiweb2m.py`
- [Topic training](https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/train_topic_model.py): `topic_models/train_topic_model.py`
- [Backend registry](https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/registry.py): `topic_models/registry.py`
- [Topic interface](https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/base.py): `topic_models/base.py`
- [CEMTM backend](https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/cemtm_adapter.py): `topic_models/cemtm_adapter.py`
- [ETM backend](https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/etm_adapter.py): `topic_models/etm_adapter.py`
- [CWTM backend](https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/cwtm_adapter.py): `topic_models/cwtm_adapter.py`
- [SoftLTM backend](https://github.com/amirabaskohi/mcompassrag/blob/main/topic_models/softltm_adapter.py): `topic_models/softltm_adapter.py`

### Teacher distillation

- [Distillation builder](https://github.com/amirabaskohi/mcompassrag/blob/main/data_gen/build_training_data.py): `data_gen/build_training_data.py`
- [Query generation](https://github.com/amirabaskohi/mcompassrag/blob/main/data_gen/query_gen.py): `data_gen/query_gen.py`
- [Relevance teacher](https://github.com/amirabaskohi/mcompassrag/blob/main/data_gen/teacher.py): `data_gen/teacher.py`
- [Negative mining](https://github.com/amirabaskohi/mcompassrag/blob/main/data_gen/negatives.py): `data_gen/negatives.py`
- [OpenRouter client](https://github.com/amirabaskohi/mcompassrag/blob/main/data_gen/openrouter_client.py): `data_gen/openrouter_client.py`

### Index and embeddings

- [Chunking](https://github.com/amirabaskohi/mcompassrag/blob/main/src/index/chunking.py): `src/index/chunking.py`
- [RAG pipeline](https://github.com/amirabaskohi/mcompassrag/blob/main/src/pipeline.py): `src/pipeline.py`
- [Metadata bank](https://github.com/amirabaskohi/mcompassrag/blob/main/src/index/metadata_bank.py): `src/index/metadata_bank.py`
- [Qwen backbone](https://github.com/amirabaskohi/mcompassrag/blob/main/src/encoders/backbone.py): `src/encoders/backbone.py`
- [Retriever encoder](https://github.com/amirabaskohi/mcompassrag/blob/main/src/encoders/retriever_encoder.py): `src/encoders/retriever_encoder.py`

### Train and serve

- [Retriever training entry](https://github.com/amirabaskohi/mcompassrag/blob/main/src/training/train.py): `src/training/train.py`
- [Training dataset](https://github.com/amirabaskohi/mcompassrag/blob/main/src/training/dataset.py): `src/training/dataset.py`
- [Retriever trainer](https://github.com/amirabaskohi/mcompassrag/blob/main/src/training/trainer.py): `src/training/trainer.py`
- [Compass retriever](https://github.com/amirabaskohi/mcompassrag/blob/main/src/models/compass_retriever.py): `src/models/compass_retriever.py`
- [Retrieval orchestrator](https://github.com/amirabaskohi/mcompassrag/blob/main/src/inference/retrieve.py): `src/inference/retrieve.py`

### Other components

- **OpenRouter**
- **Query user**

## Connections

- WikiWeb2M corpus → Topic training: trains on (evidence: `README.md`)
- Topic training → Backend registry: selects backend (evidence: `topic_models/train_topic_model.py`)
- Backend registry → Topic interface: constructs (evidence: `topic_models/registry.py`)
- CEMTM backend → Topic interface: implements (evidence: `topic_models/cemtm_adapter.py`)
- ETM backend → Topic interface: implements (evidence: `topic_models/etm_adapter.py`)
- CWTM backend → Topic interface: implements (evidence: `topic_models/cwtm_adapter.py`)
- SoftLTM backend → Topic interface: implements (evidence: `topic_models/softltm_adapter.py`)
- Distillation builder → Query generation: generates queries (evidence: `data_gen/build_training_data.py`)
- Distillation builder → Relevance teacher: requests labels (evidence: `data_gen/build_training_data.py`)
- Distillation builder → Negative mining: mines negatives (evidence: `data_gen/build_training_data.py`)
- Query generation → OpenRouter client: calls (evidence: `data_gen/query_gen.py`)
- Relevance teacher → OpenRouter client: calls (evidence: `data_gen/teacher.py`)
- OpenRouter client → OpenRouter: sends requests (evidence: `data_gen/openrouter_client.py`)
- Chunking → RAG pipeline: provides chunks (evidence: `README.md`)
- RAG pipeline → Backend registry: builds topic model (evidence: `src/pipeline.py`)
- RAG pipeline → Metadata bank: builds and saves (evidence: `src/pipeline.py`)
- Metadata bank → Retriever encoder: embeds chunks (evidence: `src/index/metadata_bank.py`)
- Metadata bank → Topic interface: encodes topics (evidence: `src/index/metadata_bank.py`)
- Retriever encoder → Qwen backbone: wraps (evidence: `src/encoders/retriever_encoder.py`)
- Retriever training entry → Metadata bank: loads index (evidence: `src/training/train.py`)
- Retriever training entry → Training dataset: builds dataset (evidence: `src/training/train.py`)
- Retriever training entry → Retriever trainer: starts training (evidence: `src/training/train.py`)
- Retriever trainer → Compass retriever: optimizes heads (evidence: `src/training/trainer.py`)
- Query user → Retrieval orchestrator: submits query (evidence: `README.md`)
- Retrieval orchestrator → Retriever encoder: embeds query (evidence: `src/inference/retrieve.py`)
- Retrieval orchestrator → Metadata bank: reads index (evidence: `src/inference/retrieve.py`)
- Retrieval orchestrator → Compass retriever: scores candidates (evidence: `src/inference/retrieve.py`)
