A sophisticated AI-powered application for querying and visualizing distribution analytics data from the Chinook music store database. This project leverages a graph-based agent architecture to process natural language queries, generate SQL, and create interactive data visualizations.
- Natural Language to SQL: Converts complex user questions into specific SQLite queries.
- Intelligent Query Rewriting: Automatically refines vague queries (e.g., "show sales") into actionable requests.
- Agentic Workflow: Uses LangGraph to orchestrate a team of specialized agents (Router, Table Selector, SQL Generator, Validator, Visualization Planner).
- Interactive Visualizations: Dynamically generates Vega-Lite charts when data is best suited for visual representation.
- Scope Guardrails: A General Agent politely handles out-of-scope queries, guiding users back to the domain.
- Real-time Streaming: Server-Sent Events (SSE) provide a real-time view of the agent's thought process and execution steps.
- Modern UI: A polished, responsive React frontend built with Tailwind CSS.
graph TD
User(User Query) --> Router{Query Router}
Router -- Irrelevant --> General[General Agent]
Router -- Relevant --> Rewriter[Query Rewriter]
General --> End
Rewriter --> TableSelector[Table Selector]
TableSelector --> SQLGen[SQL Generator]
SQLGen --> Validator{SQL Validator}
Validator -- Invalid (Retry ≤3) --> SQLGen
Validator -- Invalid (Max Retries) --> End
Validator -- Valid --> Executor[SQL Executor]
Executor -- Error (Retry ≤3) --> SQLGen
Executor -- Success --> Synthesizer[Response Synthesizer]
Executor -- Error (Max Retries) --> Synthesizer
Synthesizer --> VizPlanner{Visualization Planner}
VizPlanner -- No Viz Needed --> End
VizPlanner -- Needs Viz --> VizGen[Visualization Generator]
VizGen --> End
- Python: 3.10 or higher
- Node.js: 18 or higher
- Backend Manager:
uv(recommended) orpip - Frontend Manager:
npmorbun - OpenAI API Key: You need a valid API key with access to GPT-4o.
Clone the repository:
git clone https://github.com/Thebinary110/Agenctic_sql.gitNavigate to the backend directory:
cd backendCreate a virtual environment and install dependencies.
Using uv (Fast & Recommended):
uv syncUsing pip:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -r requirements.txtSet up your environment variables:
Create a .env file in the backend directory:
touch .envAdd your OpenAI API key to .env:
OPENAI_API_KEY=sk-your-openai-api-key-hereOpen a new terminal and navigate to the frontend directory:
cd frontendInstall dependencies:
npm install
# OR
bun installYou will need to run both the backend and frontend servers simultaneously.
Terminal 1 (Backend):
cd backend
./run_api.shThe backend API will start at http://localhost:8000.
Terminal 2 (Frontend):
cd frontend
npm run devThe React application will start at http://localhost:5173.
sql-agent-app/
├── backend/ # FastAPI application
│ ├── app/
│ │ ├── agents/ # Logic for specific agents (Router, SQL, Viz, etc.)
│ │ ├── graph/ # LangGraph workflow definition
│ │ ├── api/ # REST endpoints and SSE streaming
│ │ ├── tools/ # Utilities for DB access and validation
│ │ └── state/ # Global state definition
│ ├── run_api.sh # Helper script to launch the server
│ └── requirements.txt # Python dependencies
│
└── frontend/ # React application
├── src/
│ ├── components/ # UI components (ChatInterface, TraceMonitor)
│ ├── hooks/ # Custom React hooks (useChat)
│ └── types/ # TypeScript interfaces
├── tailwind.config.js # Styling configuration
└── package.json # Frontend dependencies