AI-powered SQL and MongoDB query generator that converts natural language to optimized database queries
Project description
AI SQL Generator
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
-
Clone the repository
git clone https://github.com/aladagemre/aisql.git cd aisql
-
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
-
Install dependencies
uv sync -
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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5dc20eded4993c6aeb590a6d1400a1de35540eee69e123616096f0558350591
|
|
| MD5 |
8b2c6b9943ea14d22d26808689b91652
|
|
| BLAKE2b-256 |
9ddd3385ef200aa484bc25caa951325b8daae8b37e03fc0aabc98e147d20caa7
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22f8502f50732617d6eebb2b7fe1adcb2cdd48dd93776edc1a2658ccb637fa56
|
|
| MD5 |
59f979928b4de981a6bc9c272b33fb39
|
|
| BLAKE2b-256 |
282176006dbab261b97949bd713dd39b2afdb56bde646ff5105772318b0c909c
|