How yxh-y/tablerag works
1. What this repo is
TableRAG is a two-stage system for heterogeneous document QA: an offline database construction pipeline and an online iterative reasoning pipeline. Its core idea is to combine SQL execution over structured tables with text retrieval for multi-hop answer generation.
2. Main architectural boundary
The repository is split into two major subsystems:
- offline_data_ingestion_and_query_interface/: builds and serves the structured data layer
- online_inference/: runs the TableRAG reasoning loop against that data layer
This separation is important: the online agent does not ingest raw tables directly; it talks to a query service backed by MySQL.
3. Offline ingestion pipeline
The offline side ingests the Excel-based table dataset from dataset/hybridqa/dev_excel.zip, maps values/types, and persists them into a MySQL 8 database.
Key components:
- data_persistent.py: main ingestion entrypoint
- sql_alchemy_helper.py: database access/ORM-style helper
- dtype_mapping.py: type normalization for table fields
- common_utils.py, log_service.py: shared helpers and logging
Configuration is driven by config/database_config.json.
4. Persistent data store
MySQL is the durable storage layer for TableRAG’s structured knowledge. The dataset is transformed from spreadsheet files into relational tables so downstream reasoning can execute SQL-style operations efficiently. This is the main stateful boundary in the system.
5. Query service layer
offline_data_ingestion_and_query_interface/src/interface.py starts the database query service, and handle_requests.py/service.py implement request handling around it.
This service is the bridge between the online reasoning agent and the database. The README indicates it is configured with an external model/API endpoint, so this layer likely handles request orchestration and query execution rather than direct database access from the online side.
6. Online reasoning loop
online_inference/main.py is the top-level orchestrator for inference. It runs a multi-iteration reasoning process with a configurable max_iter, using the current question, intermediate results, and tool outputs to progressively refine the answer.
7. Tooling used during inference
The online side uses a tool-based architecture:
- tools/retriever.py: textual retrieval over supporting passages or evidence
- tools/sql_tool.py: SQL query tool that talks to the offline query service
- utils/tool_utils.py, utils/utils.py: tool output formatting and shared inference helpers
This is the core TableRAG hybrid design: retrieve text when unstructured evidence is needed, and query SQL when table operations are needed.
8. Prompting and chat orchestration
online_inference/prompt.py and chat_utils.py manage prompt construction and LLM interaction. config.py stores the model endpoint, API key, and query-service URL.
This means the online subsystem depends on two external services:
- an LLM inference endpoint
- the TableRAG SQL query service
9. Evaluation path
online_inference/evaluation/hybrid_eval.py evaluates outputs on the HybridQA/HeteQA-style benchmark data. The online pipeline consumes a JSON data file such as my_dev.json, generates answers, and then the evaluation module scores them.
10. End-to-end data flow
The full architecture is:
Excel tables → offline ingestion → MySQL → query service → online reasoning loop → final answer
In parallel, the online loop can also pull textual evidence via retriever and combine it with SQL results before producing the final response. This hybrid flow is the defining architectural feature of the repo.
Main entry points: main.py Inference loop · data_persistent.py Ingestor · service.py Service core · sql_alchemy_helper.py DB helper
Components 20
Offline data layer
Builds and serves the structured table store.
- Excel tables · HybridQA spreadsheet source data packaged as a zip archive.
offline_data_ingestion_and_query_interface/dataset/hybridqa/dev_excel.zip - DB config · Database connection and persistence settings.
offline_data_ingestion_and_query_interface/config/database_config.json - Ingestor · Main offline ingestion entrypoint that loads tables into relational storage.
offline_data_ingestion_and_query_interface/src/data_persistent.py - Type mapping · Normalizes table field values and data types during ingestion.
offline_data_ingestion_and_query_interface/src/dtype_mapping.py - DB helper · MySQL access and ORM-style helper layer.
offline_data_ingestion_and_query_interface/src/sql_alchemy_helper.py - Common utils · Shared offline-side utility functions.
offline_data_ingestion_and_query_interface/src/common_utils.py - Logging · Logging utilities used by the offline pipeline.
offline_data_ingestion_and_query_interface/src/log_service.py - Query service · Starts the database query service exposed to online inference.
offline_data_ingestion_and_query_interface/src/interface.py - Request handler · Routes and handles incoming query-service requests.
offline_data_ingestion_and_query_interface/src/handle_requests.py - Service core · Core service logic behind the query interface.
offline_data_ingestion_and_query_interface/src/service.py
Online reasoning
Runs hybrid retrieval and SQL-based inference.
- Inference loop · Top-level multi-iteration TableRAG reasoning coordinator.
online_inference/main.py - Prompting · Builds prompts and chat context for the reasoning loop.
online_inference/prompt.py - Chat utils · LLM chat/request interaction helpers.
online_inference/chat_utils.py - Retriever · Textual evidence retrieval tool.
online_inference/tools/retriever.py - SQL tool · Issues SQL queries through the offline query service.
online_inference/tools/sql_tool.py - Tool utils · Formats tool outputs and supports inference-time tool usage.
online_inference/utils/tool_utils.py - Inference utils · Shared inference helpers and support functions.
online_inference/utils/utils.py - Hybrid eval · Benchmark evaluation for generated answers.
online_inference/evaluation/hybrid_eval.py
External systems
Runtime dependencies outside the repository.
- MySQL 8 · Durable relational store for structured table knowledge.
- LLM endpoint · External model or API endpoint used for prompting and generation.
Connections 21
- Excel tables → Ingestor: ingest · Spreadsheet tables are loaded into the offline ingestion pipeline.
- DB config → Ingestor: configure · Ingestion uses the database configuration.
- Ingestor → Type mapping: normalize · Types and values are normalized before persistence.
- Ingestor → DB helper: write · The ingestion pipeline persists records through the DB helper.
- DB helper → MySQL 8: store · Structured table data is stored in MySQL.
- Common utils → Ingestor: support · Shared utilities support the offline pipeline.
- Logging → Ingestor: log · Logging services capture ingestion activity.
- Query service → Request handler: serve · The interface starts the request handling service.
- Request handler → Service core: dispatch · Requests are routed into the service core.
- Service core → DB helper: query db · The service core reaches MySQL through the DB helper.
- Service core → MySQL 8: access · Query execution ultimately reads and writes the relational store.
- Inference loop → Prompting: build · The inference loop builds prompts for each iteration.
- Inference loop → Chat utils: llm call · The loop delegates generation and chat interaction.
- Inference loop → Retriever: retrieve · Text evidence is fetched as one branch of the hybrid reasoning flow.
- Inference loop → SQL tool: sql query · Structured questions are answered through the SQL tool.
- SQL tool → Query service: request · SQL tool calls the offline query service.
- Prompting → LLM endpoint: invoke · Prompted generation is sent to the external LLM endpoint.
- Chat utils → LLM endpoint: chat · Chat utilities communicate with the external model API.
- Tool utils → Inference loop: format · Tool outputs are normalized for iterative reasoning.
- Inference utils → Inference loop: support · Shared utilities support orchestration and inference flow.
- Hybrid eval → Inference loop: evaluate · The evaluation path scores outputs from the inference loop.