Skip to content

HaseebUllahButt/Query-Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Query-Bot πŸ€–

An intelligent platform for generating and executing database queries through AI, powered by LLMs and natural language processing.

Overview

Query-Bot is a full-stack application that bridges the gap between natural language and database queries. Users can describe what data they need in plain English, and the AI system intelligently generates the corresponding SQL queries, executes them, and returns resultsβ€”all with a user-friendly interface.

Perfect for:

  • Data analysts without SQL expertise
  • Rapid data exploration and reporting
  • Learning SQL through AI-assisted examples
  • Organizations reducing query-writing overhead

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Frontend (Next.js/React)                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚  β”‚   Auth UI    β”‚  β”‚ Query Builderβ”‚  β”‚ Schema Mgmt  β”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚ HTTP/REST
                             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Backend API (Express.js/TypeScript)          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚  β”‚ Auth Routes  β”‚  β”‚ Query Routes β”‚  β”‚ Schema Routesβ”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β”‚                             β”‚                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”             β”‚
β”‚  β”‚        LLM Service (Query Generation)           β”‚             β”‚
β”‚  β”‚  - Claude/OpenAI Integration                    β”‚             β”‚
β”‚  β”‚  - Query validation & optimization              β”‚             β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜             β”‚
β”‚                             β”‚                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”             β”‚
β”‚  β”‚      Driver API (Python Executor)               β”‚             β”‚
β”‚  β”‚  - Query execution                              β”‚             β”‚
β”‚  β”‚  - Result processing                            β”‚             β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚ Database Protocol
                             β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  PostgreSQL/SQL β”‚
                    β”‚    Database     β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Component Breakdown

Layer Technology Responsibility
Frontend Next.js 14, React, TypeScript User interface, auth, query building
Backend API Express.js, TypeScript, Node.js REST API, business logic, authentication
LLM Service Claude/OpenAI API Natural language β†’ SQL translation
Query Driver Python FastAPI Query execution, result formatting
Database PostgreSQL Data storage, query execution
Database MongoDB User sessions, query history

πŸ› οΈ Tech Stack

Backend

  • Runtime: Node.js + TypeScript
  • Framework: Express.js
  • Database: PostgreSQL (data), MongoDB (metadata)
  • Authentication: JWT-based
  • LLM Integration: Claude/OpenAI API

Frontend

  • Framework: Next.js 14 (App Router)
  • UI Library: React 18
  • Language: TypeScript
  • Styling: CSS Modules / Tailwind CSS
  • HTTP Client: Axios

Query Execution

  • Language: Python 3.x
  • Framework: FastAPI
  • Database Drivers: psycopg2 (PostgreSQL)

πŸ“‹ Features

βœ… Natural Language Query Generation

  • Describe your data needs in plain English
  • AI intelligently converts to SQL

βœ… Query Execution & Results

  • Execute generated queries directly
  • View formatted results in the UI

βœ… Schema Management

  • Upload database schemas
  • Schema-aware query generation
  • Query history & analytics

βœ… User Authentication

  • Secure login/signup
  • Session management
  • API key support

βœ… Query History

  • Track all executed queries
  • Re-run previous queries
  • Query performance insights

πŸš€ Quick Start

Prerequisites

  • Node.js v18+ and npm/yarn
  • Python 3.8+
  • PostgreSQL 12+ (or configured remote instance)
  • MongoDB 4.4+ (for session storage)
  • OpenAI/Claude API Key (for LLM service)

Installation

1. Clone Repository

git clone https://github.com/HaseebUllahButt/Query-Bot.git
cd QueryBot

2. Backend Setup

cd Backend-API

# Install dependencies
npm install

# Configure environment
cp .env.example .env
# Edit .env with your database credentials and API keys

# Initialize database
npm run db:setup

# Start backend server
npm start
# Server runs on http://localhost:5000

3. Frontend Setup

cd ../Frontend-Web

# Install dependencies
npm install

# Configure environment
cp .env.example .env.local
# Edit with backend API URL

# Start development server
npm run dev
# Access at http://localhost:3000

4. Query Driver Setup

cd ../Driver-API

# Create virtual environment
python3 -m venv venv
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp .env.example .env

# Start driver server
python main.py
# Runs on http://localhost:8000

πŸ” Environment Variables

Backend API (.env)

# Server
NODE_ENV=development
PORT=5000

# Database - PostgreSQL
DB_HOST=localhost
DB_PORT=5432
DB_NAME=querybot
DB_USER=postgres
DB_PASSWORD=your_password

# Database - MongoDB (Sessions)
MONGODB_URI=mongodb://localhost:27017/querybot

# JWT Authentication
JWT_SECRET=your_jwt_secret_key_here

# LLM Service
OPENAI_API_KEY=sk-...
OPENAI_MODEL=gpt-4
# OR
CLAUDE_API_KEY=sk-ant-...
CLAUDE_MODEL=claude-3-sonnet-20240229

# Driver API
DRIVER_API_URL=http://localhost:8000

# CORS
FRONTEND_URL=http://localhost:3000

Frontend (.env.local)

NEXT_PUBLIC_API_URL=http://localhost:5000
NEXT_PUBLIC_APP_NAME=Query-Bot

Driver API (.env)

DATABASE_URL=postgresql://user:password@localhost:5432/querybot
PORT=8000
LOG_LEVEL=info

πŸ“ Project Structure

QueryBot/
β”œβ”€β”€ Backend-API/                  # Express.js backend
β”‚   β”œβ”€β”€ routes/                   # API endpoints (auth, query, schema)
β”‚   β”œβ”€β”€ services/                 # Business logic (LLM service)
β”‚   β”œβ”€β”€ middleware/               # Authentication, error handling
β”‚   β”œβ”€β”€ database/
β”‚   β”‚   β”œβ”€β”€ models/               # Data models (User, Query, Schema, Session)
β”‚   β”‚   β”œβ”€β”€ connection.ts         # DB initialization
β”‚   β”‚   └── setup.ts              # Database migrations
β”‚   β”œβ”€β”€ types/                    # TypeScript interfaces
β”‚   └── utils/                    # Helpers (schema parser, constants)
β”‚
β”œβ”€β”€ Frontend-Web/                 # Next.js frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ app/                  # App Router pages
β”‚   β”‚   β”œβ”€β”€ components/           # React components
β”‚   β”‚   β”‚   β”œβ”€β”€ auth/             # Login, signup
β”‚   β”‚   β”‚   β”œβ”€β”€ dashboard/        # Main UI
β”‚   β”‚   β”‚   β”œβ”€β”€ query/            # Query builder
β”‚   β”‚   β”‚   └── schemas/          # Schema management
β”‚   β”‚   β”œβ”€β”€ lib/
β”‚   β”‚   β”‚   β”œβ”€β”€ api/              # API client
β”‚   β”‚   β”‚   └── context/          # React context (Auth, Schemas)
β”‚   β”‚   └── types/                # TypeScript definitions
β”‚   └── public/                   # Static assets
β”‚
└── Driver-API/                   # Python query executor
    β”œβ”€β”€ main.py                   # FastAPI server
    └── requirements.txt          # Python dependencies

πŸ“š API Documentation

Authentication Endpoints

POST /api/auth/signup

{
  "email": "user@example.com",
  "password": "secure_password",
  "fullName": "John Doe"
}

POST /api/auth/login

{
  "email": "user@example.com",
  "password": "secure_password"
}

Query Endpoints

POST /api/query/generate

  • Generate SQL from natural language
  • Requires: prompt (string), schemaId (string)
  • Returns: Generated SQL query

POST /api/query/execute

  • Execute a query and get results
  • Requires: query (string), schemaId (string)
  • Returns: Query results

GET /api/query/history

  • Fetch user's query history
  • Returns: Array of previous queries

Schema Endpoints

POST /api/schema/upload

  • Upload database schema
  • Requires: file upload (SQL or JSON)

GET /api/schema/list

  • List all user schemas

GET /api/schema/:id

  • Get specific schema details

πŸ”„ Data Flow Example

  1. User Input β†’ Types "Show me all users created in the last month"
  2. Frontend β†’ Sends to /api/query/generate with schema context
  3. Backend β†’ Passes to LLM Service with schema information
  4. LLM Service β†’ Generates optimized SQL query
  5. Backend β†’ Validates query, sends to Driver API
  6. Driver β†’ Executes query against PostgreSQL
  7. Results β†’ Returned to frontend and displayed to user

πŸ§ͺ Testing

# Backend tests
cd Backend-API
npm run test

# Frontend tests
cd ../Frontend-Web
npm run test

πŸ› Troubleshooting

Issue Solution
Port already in use Change PORT in .env or kill existing process
Database connection failed Verify DB credentials and ensure server is running
LLM API errors Check API key validity and quota limits
CORS errors Verify FRONTEND_URL matches your frontend origin
Schema parsing errors Ensure uploaded schema is valid SQL/JSON

πŸš€ Deployment

Using Docker

# Build images
docker-compose build

# Start services
docker-compose up

Cloud Deployment

Deployable to: Vercel (frontend), Render/Railway (backend), AWS Lambda (driver)


πŸ“ Future Enhancements

  • Support for multiple database types (MySQL, SQLite, MongoDB)
  • Query optimization recommendations
  • Advanced analytics & query performance tracking
  • Team collaboration features
  • Query scheduling & automation
  • Advanced security (encryption, audit logs)

πŸ“„ License

MIT License - See LICENSE file for details


πŸ‘₯ Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Commit changes with clear messages
  4. Submit a pull request

πŸ“§ Support

For issues or questions:

  • Open an GitHub issue

Made with ❀️ for making data accessible to everyone

About

A platform which helps generate Database queries through AI and execute them

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors