prazy1208/text2sql
๐ Privacy-Preserving Natural Language Querying via Schema-Driven Agent Pipelines
๐ Overview
This project enables non-technical users to explore data using natural language without ever exposing sensitive data to external systems.
Traditional natural language querying systems often rely on direct access to datasets, which introduces privacy risks. This project takes a fundamentally different approach:
๐ All query generation is performed using schema metadata only (table names, column descriptions, relationships, and business context). ๐ No raw data is ever shared with Large Language Models (LLMs).
By treating privacy as a first-class constraint, the system ensures safe and controlled query generation while maintaining usability and flexibility.
๐ง Key Idea: Schema-Only Reasoning
Instead of sending actual data to the model, the system uses:
- Table schemas
- Column descriptions
- Relationships between tables
- Business context metadata
This allows the LLM to:
- Understand user intent
- Generate accurate structured queries
- Maintain strict data privacy
โ๏ธ Architecture
The system is built as an LLM-powered, agent-driven pipeline that decomposes the problem into modular stages:
๐ Pipeline Stages
- Intent Interpretation
- Understands the user's natural language query
- Schema-Aware Table Selection
- Identifies relevant tables using metadata
- Column Selection & Filtering
- Narrows down to required fields
- Contextual Grounding
- Uses validated examples and business logic
- Structured Query Generation
- Produces SQL (or equivalent structured query)
๐๏ธ Design Principles
๐ Privacy First
- No raw data exposure at any stage
- Schema-only interaction with LLMs
- Safe for sensitive enterprise environments
๐ Execution-Agnostic
- Query generation is decoupled from execution
๐งฉ Modular Agent Design
- Each stage is handled by a dedicated agent
- Improves interpretability and debugging
- Enables independent optimization of components
โฑ๏ธ Performance
- Handles moderately complex analytical queries
- End-to-end pipeline latency: ~30โ50 seconds
๐งช Data Usage
- Uses synthetic or publicly available datasets only
- No real or sensitive data is included in this project
Documentation
Repository layout (summary)
- `backend/` โ FastAPI entrypoint `backend/api/main.py`, agents under
backend/agents/, shared services underbackend/services/, HTTP routes underbackend/api/routes/(including `query.py`:/query,/session,/sessions, session messages). - `frontend/` โ Static chat UI (
index.html,app.js,styles.css) served at/. - `scripts/` โ Database DDL such as `scripts/create_app_schema.sql`.
- `docs/` โ Setup guides and architecture notes.
Running locally
From the project root (requires .env with DATABASE_URL and an LLM API key):
uvicorn backend.api.main:app --reloadThen open http://127.0.0.1:8000/ for the chat UI. Apply scripts/create_app_schema.sql (or your full setup script) so app_schema.sessions and chat_messages exist.
๐ Future Scope
- Introduce caching mechanisms to optimize repeated query performance
- Extend conversational memory and structured persistence for richer reload fidelity
- Refine back-and-forth querying (e.g. intent flows across session switches)
