Skip to main content

AI-powered SQL and MongoDB query generator that converts natural language to optimized database queries

Project description

AI SQL Generator

License: MIT Python 3.10+ Code style: black

A powerful AI-powered SQL and MongoDB query generator that converts natural language questions into optimized database queries.

๐ŸŽฌ Demo

    โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
    โ•‘                                                           โ•‘
    โ•‘     โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•—                        โ•‘
    โ•‘    โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘                        โ•‘
    โ•‘    โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘                        โ•‘
    โ•‘    โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ•šโ•โ•โ•โ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ–„โ–„ โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘                        โ•‘
    โ•‘    โ–ˆโ–ˆโ•‘  โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—                   โ•‘
    โ•‘    โ•šโ•โ•  โ•šโ•โ•โ•šโ•โ•โ•šโ•โ•โ•โ•โ•โ•โ• โ•šโ•โ•โ–€โ–€โ•โ• โ•šโ•โ•โ•โ•โ•โ•โ•                   โ•‘
    โ•‘                                                           โ•‘
    โ•‘    Natural Language to SQL Generator                      โ•‘
    โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

Using OPENAI_API_KEY from environment: ************

> Show me all customers who ordered in the last 30 days

Generating SQL...

============================================================
SQL Query:
------------------------------------------------------------
SELECT DISTINCT c.id, c.name, c.email
FROM customers c
JOIN orders o ON c.id = o.customer_id
WHERE o.order_date >= CURRENT_DATE - INTERVAL '30 days';
------------------------------------------------------------

Explanation: This query retrieves unique customer records (id, name, email)
for customers who have placed at least one order in the last 30 days.
Valid: Yes
============================================================

> What are the top 5 products by total sales?

Generating SQL...

============================================================
SQL Query:
------------------------------------------------------------
SELECT p.id, p.name, SUM(oi.quantity * oi.unit_price) AS total_sales
FROM products p
JOIN order_items oi ON p.id = oi.product_id
GROUP BY p.id, p.name
ORDER BY total_sales DESC
LIMIT 5;
------------------------------------------------------------

Explanation: This query calculates total sales for each product and returns
the top 5 products ranked by their total sales amount.
Valid: Yes
============================================================

> /quit
Goodbye!

Run the animated demo yourself:

make demo

๐Ÿš€ Features

  • Natural language to SQL query conversion
  • Natural language to MongoDB query conversion
  • Detailed query explanations
  • Query validation against user intent
  • Interactive CLI with custom schema support
  • RESTful API interface

๐Ÿ“‹ Requirements

  • Python 3.10+
  • uv package manager
  • OpenAI API key

๐Ÿ›  Installation

Install from PyPI

pip install aisql

# Development extras (formatting, linting, tests)
pip install "aisql[dev]"

Install from source

  1. Clone the repository

    git clone https://github.com/aladagemre/aisql.git
    cd aisql
    
  2. Install uv (if not already installed)

    # macOS/Linux
    curl -LsSf https://astral.sh/uv/install.sh | sh
    
    # Or with Homebrew
    brew install uv
    
    # Or with pip
    pip install uv
    
  3. Install dependencies

    uv sync
    
  4. Set up your OpenAI API key

Option A: Set environment variable (recommended)

export OPENAI_API_KEY="your-api-key-here"

Option B: Create a .env file

cp .env.example .env
# Edit .env and add your OpenAI API key

Option C: Enter interactively when running the CLI

Optional: Use deterministic mocks without calling the OpenAI API

export AISQL_USE_MOCK=true

๐Ÿ–ฅ Running the CLI

The interactive CLI is the easiest way to use AISQL:

make cli

Or directly with Python:

uv run python -m aisql.cli

Once installed, you can also run:

aisql-cli

Once installed, you can also run:

aisql-cli

CLI Commands

Command Description
/help Show available commands
/schema View current database schema
/set schema Define a custom schema (multi-line input)
/set key Change OpenAI API key
/clear Clear the screen
/quit Exit the application

Example Session

> /schema

Current Schema:
----------------------------------------
Table: customers
- id (int, primary key)
- name (varchar)
- email (varchar)
- created_at (timestamp)

Table: orders
- id (int, primary key)
- customer_id (int, foreign key -> customers.id)
- order_date (date)
- total (decimal)
- status (varchar)
----------------------------------------

> Find customers who spent more than $1000 but haven't ordered in 90 days

============================================================
SQL Query:
------------------------------------------------------------
SELECT c.id, c.name, c.email, SUM(o.total) AS total_spent,
       MAX(o.order_date) AS last_order_date
FROM customers c
JOIN orders o ON c.id = o.customer_id
GROUP BY c.id, c.name, c.email
HAVING SUM(o.total) > 1000
   AND MAX(o.order_date) < CURRENT_DATE - INTERVAL '90 days';
------------------------------------------------------------

Explanation: This query identifies high-value customers (>$1000 total spend)
who have become inactive (no orders in the last 90 days).
Valid: Yes
============================================================

๐ŸŽฌ Running the Demo

Watch an animated demonstration of the CLI:

make demo

Or directly:

uv run python demo_cast.py

๐ŸŒ Running the REST API

Start the API server:

make run

Or directly:

uv run uvicorn aisql.main:app --reload

Once installed, you can also run:

aisql-api

Once installed, you can also run:

aisql-api

The API will be available at http://localhost:8000

API Endpoints

Method Endpoint Description
GET / API info
GET /demo Interactive web demo
GET /docs Swagger documentation
POST /generate-postgresql Generate SQL query
POST /generate-mongodb Generate MongoDB query

API Example

curl -X POST "http://localhost:8000/generate-postgresql" \
     -H "Content-Type: application/json" \
     -d '{"question": "Show me all customers who ordered in the last 30 days"}'

Response:

{
    "query": "SELECT DISTINCT c.id, c.name, c.email FROM customers c JOIN orders o ON c.id = o.customer_id WHERE o.order_date >= CURRENT_DATE - INTERVAL '30 days';",
    "explanation": "This query retrieves unique customers who have placed orders in the last 30 days.",
    "is_valid": true,
    "error": null
}

๐Ÿงฑ SDK Usage

Import the generators directly in your Python project:

from aisql.lib import SQLGenerator
from aisql.schemas import POSTGRESQL_SCHEMA

generator = SQLGenerator(schema=POSTGRESQL_SCHEMA)
result = generator.generate("Show me all customers who ordered in the last 30 days")
print(result.query)
print(result.explanation)

Set OPENAI_API_KEY before instantiating the generators, or export AISQL_USE_MOCK=true to run with the deterministic offline mocks used in the test suite.

๐Ÿงช Testing

Run the test suite:

make test

Or directly:

uv run pytest -v

Tests use deterministic mock query generators so they can run without an OPENAI_API_KEY. Integration testing with the real OpenAI models still works by running the application normally.

๐Ÿ›  Development

Format code:

make format

Run linter:

make lint

๐Ÿ“ Project Structure

aisql/
โ”œโ”€โ”€ aisql/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ cli.py           # Interactive CLI
โ”‚   โ”œโ”€โ”€ lib.py           # Query generation logic
โ”‚   โ”œโ”€โ”€ main.py          # FastAPI application
โ”‚   โ”œโ”€โ”€ models.py        # Pydantic models
โ”‚   โ”œโ”€โ”€ schemas/         # Database schema definitions
โ”‚   โ””โ”€โ”€ templates/       # HTML templates
โ”œโ”€โ”€ tests/               # Test suite
โ”œโ”€โ”€ demo_cast.py         # ASCII cinema demo
โ”œโ”€โ”€ Makefile             # Build commands
โ””โ”€โ”€ pyproject.toml       # Project configuration

๐Ÿค Contributing

Contributions are welcome! Please read our Contributing Guide for details.

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

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ”’ Security

For security concerns, please see our Security Policy.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aisql-0.1.0.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aisql-0.1.0-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file aisql-0.1.0.tar.gz.

File metadata

  • Download URL: aisql-0.1.0.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aisql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c5dc20eded4993c6aeb590a6d1400a1de35540eee69e123616096f0558350591
MD5 8b2c6b9943ea14d22d26808689b91652
BLAKE2b-256 9ddd3385ef200aa484bc25caa951325b8daae8b37e03fc0aabc98e147d20caa7

See more details on using hashes here.

File details

Details for the file aisql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: aisql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aisql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 22f8502f50732617d6eebb2b7fe1adcb2cdd48dd93776edc1a2658ccb637fa56
MD5 59f979928b4de981a6bc9c272b33fb39
BLAKE2b-256 282176006dbab261b97949bd713dd39b2afdb56bde646ff5105772318b0c909c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page