An intelligent, multi-agent system built with LangGraph that processes natural language queries by routing them to specialized agents for SQL database analytics .
┌─────────────────────────┐
│ Data Agent (Router) │
└────────────┬────────────┘
│
▼
┌──────────────────┐
│ SQL Analyst │
└─────────┬────────┘
│
├─► Context & Schema Gathering
├─► SQL Generation & Safety Check
└─► Query Execution & Answers
- Intelligent Intent Routing: Automatically classifies natural language user input into SQL.
- SQL Analyst Agent: Converts natural language to safe PostgreSQL queries, performs strict schema validation, and blocks destructive operations (
DROP,DELETE,UPDATE). - Dynamic Model Tiering: Routes queries across fast/cost-effective LLMs or premium models (e.g., Claude 3 Opus) based on task complexity.
- Python: 3.12+
- Database: PostgreSQL instance running locally or remotely.
# Clone repository and navigate inside
cd Data_Agent
# Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .\.venv\Scripts\Activate.ps1
# Install dependencies
pip install -r requirements.txt-
Create a .env file in the root directory:
# API Keys ANTHROPIC_API_KEY=your_claude_api_key OPENAI_API_KEY=your_openai_api_key # Database Connection host=localhost port=5432 user=postgres password=your_password database=data_agent_db # Model Selection LLM_MODEL_LOW=gpt-3.5-turbo LLM_MODEL_MEDIUM=gpt-4-turbo LLM_MODEL_HIGH=claude-3-opus
Run Main Agent
from agents.data_agent import data_agent
from langchain_core.messages import HumanMessage
# Example 1: Database Query (SQL Agent)
sql_response = data_agent.invoke({
"messages": [HumanMessage(content="Show me the top 5 users with the highest ratings")],
"route_response": ""
})Run via Command Line
# Execute full workflow
python main.py
# Run individual sub-agents directly
python agents/sql_analyst.pyData_Agent/
├── agents/ # Sub-agent implementations (router, sql)
├── Models/ # Pydantic schemas and state management
├── utils/ # PostgreSQL drivers, & dynamic LLM selectors
├── data/ # Input/output workspace (extract/ and transform/)
├── feed_db.py # Database initialization script
└── main.py # Main entry pointSQL Guardrails: Queries undergo strict safety parsing prior to database execution to prevent data mutation or deletion.
Sandboxed Code Execution: Standardized input validation and restricted environment limits for runtime Pandas operations.