A powerful, modular, and interpretable system that converts natural language queries into SQL, executes them on a PostgreSQL database, and optionally performs Python-based post-processing and graph visualizationβall orchestrated using LangGraph agents and exposed via a FastAPI backend.
Below is the Data Flow Diagram (DFD) that illustrates the multi-agent architecture of this system:
- β Natural Language β SQL Translation
- β Metadata-aware query understanding (tables, columns, system info)
- β SQL Query Generation & Validation
- β Intelligent decision-making on when to post-process data
- β Python code generation for complex tasks
- β Graph generation from query results
- β Chat history persistence
- β Modular, agent-based architecture using LangGraph
- β Fully REST API-based (FastAPI)
- β
Frontend ready (CORS enabled for
http://localhost:4200)
.
βββ main.py # FastAPI + LangGraph orchestration
βββ graph_agent.py # GraphPlottingAgent for visualization
βββ db_metadata.json # Table/column metadata
βββ static/images/ # Auto-generated graph images
βββ requirements.txt # Python dependencies
git clone https://github.com/your-username/text2sql-agents.git
cd text2sql-agentspython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtCreate a .env file:
OPENAI_API_KEY=your_openai_api_keyMake sure the following DB is available:
host=localhost
port=5432
dbname=your db name
user=postgres
password=adminAlso ensure there's a Chat_History table with appropriate columns.
uvicorn main:app --reloadSubmit a natural language query.
{
"user_question": "How many machines were active last month?",
"username": "john_doe"
}Returns recent query history.
Runs predefined metadata queries.
Directly test the GraphPlottingAgent.
Generated plots are stored under:
/static/images/graph_<username>_<timestamp>.png
And served at:
http://localhost:8000/static/images/<filename>
- "What is this system?"
- "Show me power consumption for February"
- "How many machines are in each plant this year?"
- "Compare spindle speed across shifts"
- "What columns are in EMS_Daily?"
FastAPILangChain,LangGraphpsycopg2pandas,numpy,matplotlibpython-dotenvuuid,base64,re,json, etc.
- LangGraph β Multi-agent workflow orchestration
- OpenAI GPT-4o-mini β Prompted agents
- PostgreSQL β Query backend
- FastAPI β RESTful API interface
- Matplotlib β Graph rendering
MIT License Β© Priyanshu Singh




