Skip to main content
Glama

RAG · Expert chatbot on NTC CDMX (2004 / 2017 / 2023)

Retrieval-Augmented Generation system over the Complementary Technical Standards of the Mexico City Building Regulations, with hybrid search (BM25 + multilingual embeddings + RRF) and citations by edition and clause.

Structure

RAG/
├── src/
│   ├── config.py        # rutas y mapeo PDF → (edición, norma)
│   ├── extract.py       # PDF → páginas de texto por norma (temp/extracted_text/)
│   ├── structure.py     # páginas → secciones X.Y.Z (data/corpus/*.json)
│   ├── index_build.py   # secciones → catálogo + BM25 + embeddings (data/index/)
│   ├── retrieve.py      # retriever híbrido (BM25 + embeddings + RRF + numeral)
│   ├── answer.py        # generador de respuestas con LLM (DeepSeek V4 Flash)
│   ├── calc.py          # cálculos validados (viento, sismo, combinaciones)
│   ├── evaluate.py      # evaluación recall@k con el dataset de 21k Q&A
│   └── finetune_gen.py  # genera dataset RAG-formateado para fine-tune del generador
├── app/app.py           # interfaz web (Streamlit)
└── scripts/run_all.py   # orquesta el pipeline completo

Related MCP server: RAG Anything MCP Server

MCP Server (for opencode, codex, Claude Desktop, etc.)

The project is exposed as an MCP server with three tools:

Tool

What it does

answer_ntc(query)

Answers with RAG + LLM (DeepSeek V4 Flash) citing edition, standard, and clause; also resolves validated calculations

search_ntc(query, edition, norm, top_k)

Returns the relevant raw sections

get_section(edition, norm, numeral)

Returns the full text of a specific clause

Automatic installation (registers the server in opencode and/or codex):

.venv\Scripts\python.exe scripts\install_mcp.py            # opencode + codex
.venv\Scripts\python.exe scripts\install_mcp.py --opencode # solo opencode
.venv\Scripts\python.exe scripts\install_mcp.py --codex    # solo codex

Restart opencode/codex and the RAG will be available as tools (answer_ntc, etc.). The server reads the provider API key from RAG/.env, the corresponding environment variable, or ~/.config/ntc-cdmx/.env.

One-command installation (GitHub + uv)

uvx --from git+https://github.com/Sobrio25/ntc-cdmx-mcp ntc-cdmx-install

That command installs and registers the MCP in opencode, Codex, Command Code, and Kilo Code. Restart the clients and answer_ntc, search_ntc and get_section will be available. The index (BM25 + embeddings) travels inside the package; the provider API key is configured in ~/.config/ntc-cdmx/.env.

To install only the executable:

uv tool install git+https://github.com/Sobrio25/ntc-cdmx-mcp

Test the server manually:

ntc-cdmx                                     # stdio (modo instalado)
.venv\Scripts\python.exe src\mcp_server.py   # stdio (modo desarrollo)

Pipeline

# 1) Extraer y estructurar e indexar
.venv/Scripts/python.exe scripts/run_all.py --steps extract structure index

# 2) Evaluar recall del retriever (muestra 400 preguntas del dataset de 21k)
.venv/Scripts/python.exe scripts/run_all.py --steps eval

# 3) Interfaz web
.venv/Scripts/python.exe -m streamlit run app/app.py

Configure the LLM

Responses use DeepSeek V4 Flash. Configure the provider/API key in src/answer.py (LLM_MODEL, LLM_BASE_URL). Without a key, the chatbot responds with the retrieved sections (no LLM), useful for debugging.

Validated calculations (src/calc.py)

If the question asks for a calculation (e.g., "calculate the wind pressure for Vz=35 m/s"), the engine detects it and uses a formula verified against the text of the standard, without going through the LLM. Included calculators:

Calculation

Formula

Source

Dynamic wind pressure

qz = 0.52·Vz² (m/s → Pa)

NTC-Viento 2023, §5.1.3

Design wind pressure

pz = 0.47·Cp·VD²

NTC-Viento 2017/2004, §3.2

Wind drag force

F = 0.47·CD·VD²·A

NTC-Viento 2017/2004, §3.3

Minimum seismic base shear

Vo,min = amin·Wo

NTC-Sismo 2023, §7.5

Load combination

Grupo B: 1.3·CM+1.5·CV · Grupo A: 1.5·CM+1.7·CV

NTC-Criterios 2023, §3.4.1

If data is missing, the chatbot explicitly asks for it.

Generator fine-tuning (src/finetune_gen.py)

Generates a dataset in chat format where each example includes the retrieved context (so that the generator learns to answer from the context, rather than memorizing the standards):

.venv/Scripts/python.exe src/finetune_gen.py --max 2000 --top_k 8 --require_all

Automatically filters out examples whose "gold" answer is NOT supported by the retrieved context (missing cited clauses → discarded).

Evaluation

The evaluate.py module uses your dataset from Documents\Fine_Tunning\NTC_CDMX\dataset.jsonl: for each question with clauses cited in the "gold" answer, it checks that the clause appears among the retrieved sections.

Reference result (sample of 164 questions with citations, top-6): recall@q ≈ 0.58. Approximately 18% of the clauses cited by the dataset do not exist in the corpus of their edition (possible erroneous citations in the dataset or extraction gaps).

Technical notes

  • The 2004 and 2017 editions come in official gazettes (several documents per PDF); the boundaries of each standard are mapped in src/config.py.

  • The chunking is by numbered section (never by paragraph), preserving formulas/tables.

  • Each section carries metadata {edición, norma, numeral, página} for precise citation.

  • The 2023 PDFs have names with corrupt characters on disk; the extractor resolves them by numeric prefix.

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    AI-powered MCP server that enables Claude and other LLMs to interact directly with construction documents, drawings, and specifications through advanced RAG and hybrid search capabilities.
    9
    MIT
  • A
    license
    Not graded
    quality
    F
    maintenance
    An MCP server that provides comprehensive multimodal Retrieval-Augmented Generation (RAG) capabilities for processing and querying document directories, supporting text, images, tables, and equations.
    37
    MIT
  • F
    license
    Not graded
    quality
    C
    maintenance
    MCP server for a modular RAG system that enables natural language question answering over enterprise documents with intent-aware routing, adaptive retrieval, and citation-backed responses.
    -
  • A
    license
    Not graded
    quality
    A
    maintenance
    MCP server for local RAG over personal notes, PDFs, and documents, enabling plain-English querying and hybrid search with multi-hop context expansion.
    MIT