Skip to content

Bhavyashah20/query-ease

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

QueryEase ๐Ÿ”

QueryEase Demo Query any SQL database in plain English.

QueryEase converts natural language questions into SQL queries using Groq's LLM, executes them against your database, and returns results โ€” all from your terminal.

$ python3 main.py "Show top 5 customers by total spending"

โ†’ Detected database: ๐Ÿ˜ POSTGRESQL
โ†’ Schema ready โ€” 22 table(s) (from cache)
โ†’ Generating POSTGRESQL query with Groq...

โ•ญโ”€ Generated SQL โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ SELECT c.first_name, c.last_name,                            โ”‚
โ”‚        SUM(p.amount) AS total_spent                          โ”‚
โ”‚ FROM customer c                                              โ”‚
โ”‚ JOIN payment p ON c.customer_id = p.customer_id              โ”‚
โ”‚ GROUP BY c.customer_id                                       โ”‚
โ”‚ ORDER BY total_spent DESC                                    โ”‚
โ”‚ LIMIT 5                                                      โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ first_name     โ”‚ last_name     โ”‚ total_spent โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Eleanor        โ”‚ Hunt          โ”‚ 211.55      โ”‚
โ”‚ Karl           โ”‚ Seal          โ”‚ 208.58      โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
  โฑ  18ms  |  5 rows

Features

  • Natural language to SQL โ€” powered by Groq's llama-3.3-70b-versatile
  • Multi-database support โ€” PostgreSQL, MySQL, SQLite โ€” auto-detected from URL
  • Schema caching โ€” schema fetched once, cached locally, auto-refreshes when DB changes
  • LLM judge โ€” complex queries and write operations are verified before execution
  • Correction loop โ€” if results are wrong, describe what you wanted and QueryEase fixes the SQL
  • Multi-turn chat โ€” follow-up questions with full conversation context (--chat)
  • Query history โ€” every query logged locally, viewable with --history
  • SQL explanation โ€” plain English description of what the generated SQL does
  • Write query support โ€” INSERT, UPDATE, DELETE with confirmation prompt
  • Prompt injection protection โ€” user input sanitized before hitting the LLM
  • Column descriptions โ€” annotate cryptic column names to improve LLM accuracy

Requirements

  • Python 3.9+
  • A Groq API key (free tier available)
  • A running PostgreSQL, MySQL, or SQLite database

Installation

Option A โ€” Run locally (recommended for development)

git clone https://github.com/bhavyashah09/QueryEase.git
cd QueryEase
pip install -r requirements.txt

Option B โ€” Install as a package

pip install -e .
# Then use anywhere:
queryease "Show all customers from Canada"

Setup

1. Create your .env file:

cp .env.example .env

2. Fill in your credentials:

# Groq API key โ€” get one free at console.groq.com
GROQ_API_KEY=gsk_xxxxxxxxxxxxxxxxxxxx

# Your database โ€” pick one:
DATABASE_URL=postgresql://username:password@localhost:5432/mydb
# DATABASE_URL=mysql://username:password@localhost:3306/mydb
# DATABASE_URL=sqlite:///path/to/mydb.sqlite

3. Run your first query:

python3 main.py "Show all tables"

Usage

Basic query

python3 main.py "Show all customers from Canada"
python3 main.py "What are the top 5 products by revenue?"
python3 main.py "How many orders were placed last month?"

Multi-turn chat mode

Have a conversation โ€” QueryEase remembers context across turns:

python3 main.py --chat
[Turn 1] Ask: Show all customers from Canada
[Turn 2] Ask: Now show their rental history
[Turn 3] Ask: Filter to rentals from the last 6 months
[Turn 4] Ask: exit

Write queries

QueryEase handles INSERT, UPDATE, DELETE with an LLM judge + confirmation:

python3 main.py "Add a new customer named Raj with email raj@gmail.com"
python3 main.py "Update the rental rate of all horror films to 3.99"
python3 main.py "Delete all rentals that were never returned"

Other flags

# Force refresh schema cache
python3 main.py --refresh "Show all tables"

# Skip SQL explanation panel
python3 main.py --no-explain "Show top customers"

# View query history
python3 main.py --history
python3 main.py --history 20   # show last 20

How it works

Your question
     โ†“
Schema loaded from cache (or DB on first run)
     โ†“
Groq LLM generates SQL (dialect-aware)
     โ†“
Validator checks safety (no DROP/TRUNCATE, SELECT-only for reads)
     โ†“
LLM Judge verifies complex queries and write operations
     โ†“
Confirmation prompt for write queries
     โ†“
SQL executed against your database
     โ†“
Results displayed + explanation shown
     โ†“
"Were these results correct?" โ†’ correction loop if needed

Column Descriptions (optional)

For databases with cryptic column names, create descriptions.json in the project root:

{
  "orders": {
    "rev_amt": "total revenue amount in dollars",
    "flg_status": "order status: 1=pending, 2=delivered, 3=cancelled"
  },
  "customers": {
    "flg_active": "1 if customer is active, 0 if churned"
  }
}

QueryEase injects these into the LLM prompt so it understands abbreviated column names.


Supported Databases

Database URL Format
PostgreSQL postgresql://user:pass@host:5432/dbname
MySQL mysql://user:pass@host:3306/dbname
SQLite sqlite:///path/to/file.db

Project Structure

QueryEase/
โ”œโ”€โ”€ main.py                    # CLI entry point
โ”œโ”€โ”€ pyproject.toml             # Package config (pip install)
โ”œโ”€โ”€ requirements.txt           # Dependencies
โ”œโ”€โ”€ .env.example               # Environment template
โ”œโ”€โ”€ descriptions.json.example  # Column descriptions template
โ”œโ”€โ”€ sample_data.sql            # Sample ecommerce database
โ””โ”€โ”€ src/queryease/
    โ”œโ”€โ”€ cli.py                 # pip-installed entry point
    โ”œโ”€โ”€ config.py              # Config loader + validation
    โ”œโ”€โ”€ schema.py              # Schema extraction, caching, descriptions
    โ”œโ”€โ”€ generator.py           # SQL generation + correction + multi-turn
    โ”œโ”€โ”€ validator.py           # Safety validation + injection protection
    โ”œโ”€โ”€ judge.py               # LLM judge for complex/write queries
    โ”œโ”€โ”€ executor.py            # Query execution
    โ”œโ”€โ”€ formatter.py           # Terminal output formatting
    โ”œโ”€โ”€ history.py             # Query history tracking
    โ””โ”€โ”€ db/
        โ”œโ”€โ”€ __init__.py        # Auto-detects DB type from URL
        โ”œโ”€โ”€ base.py            # Abstract connector interface
        โ”œโ”€โ”€ postgres.py        # PostgreSQL connector
        โ”œโ”€โ”€ mysql.py           # MySQL connector
        โ””โ”€โ”€ sqlite.py          # SQLite connector

Running Tests

pip install -e ".[dev]"
pytest tests/ -v

Contributing

  1. Fork the repo
  2. Create a feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -m 'Add my feature')
  4. Push to the branch (git push origin feature/my-feature)
  5. Open a Pull Request

License

MIT โ€” see LICENSE for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages